Skip to content

Commit 8e8af73

Browse files
authored
chown: allow numeric UID argument (#804)
* This is similar to the patch for chgrp command * Fall back to treating numeric argument as a uid if username resolution failed * The code surrounding stat() had the same bug as for chgrp, i.e. existing uid=0 was treated as a failed stat()
1 parent 8895e4b commit 8e8af73

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

bin/chown

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use constant EX_SUCCESS => 0;
2020
use constant EX_FAILURE => 1;
2121

2222
my $Program = basename($0);
23-
my ($VERSION) = '1.2';
23+
my ($VERSION) = '1.3';
2424

2525
my $rc = EX_SUCCESS;
2626

@@ -54,6 +54,9 @@ usage() unless @ARGV;
5454
my ($owner, $group) = split /:/ => $mode, 2;
5555

5656
my $uid = getpwnam $owner;
57+
unless (defined $uid) {
58+
$uid = $owner if $owner =~ m/\A[0-9]+\z/;
59+
}
5760
unless (defined $uid) {
5861
warn "$Program: invalid user: '$owner'\n";
5962
exit EX_FAILURE;
@@ -103,11 +106,13 @@ sub modify_file {
103106
return;
104107
}
105108
unless (defined $group) {
106-
$gid = (stat $file)[5] or do {
109+
my @st = stat $file;
110+
unless (@st) {
107111
warn "$Program: failed to stat '$file': $!\n";
108112
$rc = EX_FAILURE;
109113
return;
110-
};
114+
}
115+
$gid = $st[5];
111116
}
112117
unless (chown $uid, $gid, $file) {
113118
warn "$Program: '$file': $!\n";
@@ -137,7 +142,7 @@ is changed as well.
137142
=head2 OPTIONS
138143
139144
I<chown> accepts the options described below. The options I<-L>,
140-
I<-H> and I<-P> are mutally exclusive, and only the last given
145+
I<-H> and I<-P> are mutually exclusive, and only the last given
141146
option will be honoured. All of I<-L>, I<-H> and I<-P> require the
142147
I<-R> option to be set first.
143148

0 commit comments

Comments
 (0)