#!/opt/bin/perl # ######################################################################### # # # Program to list all unused user and group IDs (UIDs and GIDs) # # # ######################################################################### # # # Version 1.0 - Written 26.09.95 by Steffen Beyer # # # ######################################################################### # # # Copyright (C) 1995 by software design & management GmbH & Co. KG # # # ######################################################################### # # Some defaults: # $version = 'version 1.0'; # $self = $0; # if ($self =~ m!/([^/]+)$!) { $self = $1; } # $argc = @ARGV; # # Subroutine definitions: # sub numerically { $a <=> $b } # # Check number of arguments: # if ($argc != 2) { die "$self error: wrong number of arguments ($argc instead of 2)!\n"; } # # Get and check the two filenames: # $file1 = shift; $file2 = shift; # unless (-f $file1) { die "$self error: '$file1' is not a file!\n"; } # unless (-f $file2) { die "$self error: '$file2' is not a file!\n"; } # unless (-r $file1) { die "$self error: '$file1' is not readable!\n"; } # unless (-r $file2) { die "$self error: '$file2' is not readable!\n"; } # unless (open(FILE1, "<$file1")) { die "$self error: can't open '$file1': $!\n"; } # $state = 0; # while () { if ($state == 0) { if (/^UIDs:/) { $state = 1; next; } } elsif ($state == 1) { if (/^GIDs:/) { $state = 2; next; } if (/^(\d+)$/) { $uid_list{$1} = 1; } } else { if (/^(\d+)$/) { $gid_list{$1} = 1; } } } # close(FILE1); # unless (open(FILE2, "<$file2")) { die "$self error: can't open '$file2': $!\n"; } # $state = 0; # while () { if ($state == 0) { if (/^UIDs:/) { $state = 1; next; } } elsif ($state == 1) { if (/^GIDs:/) { $state = 2; next; } if (/^(\d+)$/) { $uid_list{$1} = 1; # if ($uid_list{$1} < 2) { $uid_list{$1} += 2; } } } else { if (/^(\d+)$/) { $gid_list{$1} = 1; # if ($gid_list{$1} < 2) { $gid_list{$1} += 2; } } } } # close(FILE2); # # Header: # print "\n$self $version\n\n"; # # List the unused UIDs: # print "Free UIDs:\n\n"; # $previous = 0; # foreach $uid (sort numerically keys(%uid_list)) { if ($uid >= 0) { $difference = $uid - $previous; if ($difference == 1) { print "$previous "; } elsif ($difference == 2) { printf("%d %d ", $previous, $uid - 1); } elsif ($difference > 2) { printf("%d..%d ", $previous, $uid - 1); } else {} $previous = $uid + 1; } } # print "$previous..*\n\n"; # # List the unused GIDs: # print "Free GIDs:\n\n"; # $previous = 0; # foreach $gid (sort numerically keys(%gid_list)) { if ($gid >= 0) { $difference = $gid - $previous; if ($difference == 1) { print "$previous "; } elsif ($difference == 2) { printf("%d %d ", $previous, $gid - 1); } elsif ($difference > 2) { printf("%d..%d ", $previous, $gid - 1); } else {} $previous = $gid + 1; } } # print "$previous..*\n\n"; # # Done. #