1 /* chown -- change user and group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2005 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 -------------|-------------------------+-------------------------|
22 g unchanged | --- | chown u |
23 r |-------------------------+-------------------------|
24 o explicit | chgrp g or chown .g | chown u.g |
25 u |-------------------------+-------------------------|
26 p from passwd| --- | chown u. |
27 |-------------------------+-------------------------|
29 Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
33 #include <sys/types.h>
37 #include "chown-core.h"
42 #include "root-dev-ino.h"
45 /* The official name of this program (e.g., no `g' prefix). */
46 #define PROGRAM_NAME "chown"
48 #define AUTHORS "David MacKenzie", "Jim Meyering"
50 /* The name the program was run with. */
53 /* The argument to the --reference option. Use the owner and group IDs
54 of this file. This file must exist. */
55 static char *reference_file
;
57 /* For long options that have no equivalent short option, use a
58 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
61 DEREFERENCE_OPTION
= CHAR_MAX
+ 1,
68 static struct option
const long_options
[] =
70 {"recursive", no_argument
, NULL
, 'R'},
71 {"changes", no_argument
, NULL
, 'c'},
72 {"dereference", no_argument
, NULL
, DEREFERENCE_OPTION
},
73 {"from", required_argument
, NULL
, FROM_OPTION
},
74 {"no-dereference", no_argument
, NULL
, 'h'},
75 {"no-preserve-root", no_argument
, NULL
, NO_PRESERVE_ROOT
},
76 {"preserve-root", no_argument
, NULL
, PRESERVE_ROOT
},
77 {"quiet", no_argument
, NULL
, 'f'},
78 {"silent", no_argument
, NULL
, 'f'},
79 {"reference", required_argument
, NULL
, REFERENCE_FILE_OPTION
},
80 {"verbose", no_argument
, NULL
, 'v'},
81 {GETOPT_HELP_OPTION_DECL
},
82 {GETOPT_VERSION_OPTION_DECL
},
89 if (status
!= EXIT_SUCCESS
)
90 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
95 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
96 or: %s [OPTION]... --reference=RFILE FILE...\n\
98 program_name
, program_name
);
100 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
101 With --reference, change the owner and group of each FILE to those of RFILE.\n\
103 -c, --changes like verbose but report only when a change is made\n\
104 --dereference affect the referent of each symbolic link, rather\n\
105 than the symbolic link itself (this is the default)\n\
108 -h, --no-dereference affect each symbolic link instead of any referenced\n\
109 file (useful only on systems that can change the\n\
110 ownership of a symlink)\n\
113 --from=CURRENT_OWNER:CURRENT_GROUP\n\
114 change the owner and/or group of each file only if\n\
115 its current owner and/or group match those specified\n\
116 here. Either may be omitted, in which case a match\n\
117 is not required for the omitted attribute.\n\
120 --no-preserve-root do not treat `/' specially (the default)\n\
121 --preserve-root fail to operate recursively on `/'\n\
124 -f, --silent, --quiet suppress most error messages\n\
125 --reference=RFILE use RFILE's owner and group rather than\n\
126 the specifying OWNER:GROUP values\n\
127 -R, --recursive operate on files and directories recursively\n\
128 -v, --verbose output a diagnostic for every file processed\n\
132 The following options modify how a hierarchy is traversed when the -R\n\
133 option is also specified. If more than one is specified, only the final\n\
136 -H if a command line argument is a symbolic link\n\
137 to a directory, traverse it\n\
138 -L traverse every symbolic link to a directory\n\
140 -P do not traverse any symbolic links (default)\n\
143 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
144 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
147 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
148 to login group if implied by a `:' following a symbolic OWNER.\n\
149 OWNER and GROUP may be numeric as well as symbolic.\n\
154 %s root /u Change the owner of /u to \"root\".\n\
155 %s root:staff /u Likewise, but also change its group to \"staff\".\n\
156 %s -hR root /u Change the owner of /u and subfiles to \"root\".\n\
158 program_name
, program_name
, program_name
);
159 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
165 main (int argc
, char **argv
)
167 bool preserve_root
= false;
169 uid_t uid
= -1; /* Specified uid; -1 if not to be changed. */
170 gid_t gid
= -1; /* Specified gid; -1 if not to be changed. */
172 /* Change the owner (group) of a file only if it has this uid (gid).
173 -1 means there's no restriction. */
174 uid_t required_uid
= -1;
175 gid_t required_gid
= -1;
177 /* Bit flags that control how fts works. */
178 int bit_flags
= FTS_PHYSICAL
;
180 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
182 int dereference
= -1;
184 struct Chown_option chopt
;
188 initialize_main (&argc
, &argv
);
189 program_name
= argv
[0];
190 setlocale (LC_ALL
, "");
191 bindtextdomain (PACKAGE
, LOCALEDIR
);
192 textdomain (PACKAGE
);
194 atexit (close_stdout
);
198 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, NULL
))
203 case 'H': /* Traverse command-line symlinks-to-directories. */
204 bit_flags
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
207 case 'L': /* Traverse all symlinks-to-directories. */
208 bit_flags
= FTS_LOGICAL
;
211 case 'P': /* Traverse no symlinks-to-directories. */
212 bit_flags
= FTS_PHYSICAL
;
215 case 'h': /* --no-dereference: affect symlinks */
219 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
224 case NO_PRESERVE_ROOT
:
225 preserve_root
= false;
229 preserve_root
= true;
232 case REFERENCE_FILE_OPTION
:
233 reference_file
= optarg
;
238 char *u_dummy
, *g_dummy
;
239 const char *e
= parse_user_spec (optarg
,
240 &required_uid
, &required_gid
,
243 error (EXIT_FAILURE
, 0, "%s: %s", quote (optarg
), e
);
248 chopt
.recurse
= true;
252 chopt
.verbosity
= V_changes_only
;
256 chopt
.force_silent
= true;
260 chopt
.verbosity
= V_high
;
263 case_GETOPT_HELP_CHAR
;
264 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
266 usage (EXIT_FAILURE
);
272 if (bit_flags
== FTS_PHYSICAL
)
274 if (dereference
== 1)
275 error (EXIT_FAILURE
, 0,
276 _("-R --dereference requires either -H or -L"));
277 chopt
.affect_symlink_referent
= false;
281 if (dereference
== 0)
282 error (EXIT_FAILURE
, 0, _("-R -h requires -P"));
283 chopt
.affect_symlink_referent
= true;
288 bit_flags
= FTS_PHYSICAL
;
289 chopt
.affect_symlink_referent
= (dereference
!= 0);
292 if (argc
- optind
< (reference_file
? 1 : 2))
295 error (0, 0, _("missing operand"));
297 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
298 usage (EXIT_FAILURE
);
303 struct stat ref_stats
;
304 if (stat (reference_file
, &ref_stats
))
305 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
306 quote (reference_file
));
308 uid
= ref_stats
.st_uid
;
309 gid
= ref_stats
.st_gid
;
310 chopt
.user_name
= uid_to_name (ref_stats
.st_uid
);
311 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
315 const char *e
= parse_user_spec (argv
[optind
], &uid
, &gid
,
316 &chopt
.user_name
, &chopt
.group_name
);
318 error (EXIT_FAILURE
, 0, "%s: %s", quote (argv
[optind
]), e
);
320 /* If a group is specified but no user, set the user name to the
321 empty string so that diagnostics say "ownership :GROUP"
322 rather than "group GROUP". */
323 if (!chopt
.user_name
&& chopt
.group_name
)
324 chopt
.user_name
= "";
329 if (chopt
.recurse
& preserve_root
)
331 static struct dev_ino dev_ino_buf
;
332 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
333 if (chopt
.root_dev_ino
== NULL
)
334 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
338 ok
= chown_files (argv
+ optind
, bit_flags
,
340 required_uid
, required_gid
, &chopt
);
344 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);