Skip to content

Commit 1ef9954

Browse files
authored
id: exit 1 on failure (briandfoy#846)
* Avoid calling die() for better control of exit code * The pod manual stated the exit code would be 1, but for the NoSuchUser case it wasn't
1 parent d5feefe commit 1ef9954

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

bin/id

+24-10
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@ License:
2020
#
2121

2222
use strict;
23-
use Getopt::Std;
23+
24+
use File::Basename qw(basename);
25+
use Getopt::Std qw(getopts);
26+
27+
use constant EX_SUCCESS => 0;
28+
use constant EX_FAILURE => 1;
29+
2430
use vars qw($opt_G $opt_n $opt_u $opt_g $opt_r $opt_a $opt_p);
2531

32+
my $Program = basename($0);
33+
2634
getopts('Gnugrap') or help();
2735
if ( ($opt_G + $opt_g + $opt_p + $opt_u) > 1 ) {
28-
print STDERR "You may only choose one of -G, -g, -p, or -u. Doh!\n\n";
29-
&help;
36+
warn "$Program: Choose only one of -G, -g, -p, or -u\n";
37+
help();
3038
}
3139

3240
my($user,$pw,$uid,$gid,$tp);
@@ -37,7 +45,10 @@ if ( @ARGV ) { # user specified
3745
if (!defined($uid) && $ARGV[0] =~ m/\A[0-9]+\Z/) {
3846
($user,$pw,$uid,$gid) = getpwuid $ARGV[0];
3947
}
40-
die "id: $ARGV[0]: No such user\n" unless (defined $uid);
48+
unless (defined $uid) {
49+
warn "$Program: $ARGV[0]: No such user\n";
50+
exit EX_FAILURE;
51+
}
4152
}
4253

4354
if ( $opt_u ) { # print uid
@@ -59,8 +70,11 @@ elsif ( $opt_p ) { # human-readable form (names when possible, etc.)
5970
}
6071
}
6172
else {
62-
my($login) = getlogin || die "getlogin failed!";
63-
73+
my $login = getlogin;
74+
unless ($login) {
75+
warn "$Program: getlogin failed\n";
76+
exit EX_FAILURE;
77+
}
6478
$tp.="login $login\n" if ( $login ne scalar getpwuid $< );
6579

6680
my($uid) = scalar getpwuid $< || $<;
@@ -154,11 +168,11 @@ else { # uid=#(name) gid=#(name) euid=#(name) egid=#(name) groups=#(name) ...
154168
}
155169

156170
print "$tp\n";
157-
exit 0;
171+
exit EX_SUCCESS;
158172

159173
sub help {
160174
require Pod::Usage;
161-
Pod::Usage::pod2usage({ -exitval => 1, -verbose => 0 });
175+
Pod::Usage::pod2usage({ -exitval => EX_FAILURE, -verbose => 0 });
162176
}
163177

164178
=head1 NAME
@@ -222,9 +236,9 @@ Display the effective user ID.
222236
223237
=back
224238
225-
=head1 NOTES
239+
=head1 EXIT STATUS
226240
227-
id returns 0 on success or 1 if an error occurred.
241+
id exits with status 0 on success, or 1 if an error occurred.
228242
229243
=head1 AUTHOR
230244

0 commit comments

Comments
 (0)