doc: clarify the operation of wc -L
[coreutils.git] / src / chown.c
blobba1814b41dbea6d18df3b76c157af3d6fdeec6ef
1 /* chown -- change user and group ownership of files
2 Copyright (C) 1989-2015 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
19 #include <config.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <getopt.h>
24 #include "system.h"
25 #include "chown-core.h"
26 #include "error.h"
27 #include "fts_.h"
28 #include "quote.h"
29 #include "root-dev-ino.h"
30 #include "userspec.h"
32 /* The official name of this program (e.g., no 'g' prefix). */
33 #define PROGRAM_NAME "chown"
35 #define AUTHORS \
36 proper_name ("David MacKenzie"), \
37 proper_name ("Jim Meyering")
39 /* The argument to the --reference option. Use the owner and group IDs
40 of this file. This file must exist. */
41 static char *reference_file;
43 /* For long options that have no equivalent short option, use a
44 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
45 enum
47 DEREFERENCE_OPTION = CHAR_MAX + 1,
48 FROM_OPTION,
49 NO_PRESERVE_ROOT,
50 PRESERVE_ROOT,
51 REFERENCE_FILE_OPTION
54 static struct option const long_options[] =
56 {"recursive", no_argument, NULL, 'R'},
57 {"changes", no_argument, NULL, 'c'},
58 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
59 {"from", required_argument, NULL, FROM_OPTION},
60 {"no-dereference", no_argument, NULL, 'h'},
61 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
62 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
63 {"quiet", no_argument, NULL, 'f'},
64 {"silent", no_argument, NULL, 'f'},
65 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
66 {"verbose", no_argument, NULL, 'v'},
67 {GETOPT_HELP_OPTION_DECL},
68 {GETOPT_VERSION_OPTION_DECL},
69 {NULL, 0, NULL, 0}
72 void
73 usage (int status)
75 if (status != EXIT_SUCCESS)
76 emit_try_help ();
77 else
79 printf (_("\
80 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
81 or: %s [OPTION]... --reference=RFILE FILE...\n\
82 "),
83 program_name, program_name);
84 fputs (_("\
85 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
86 With --reference, change the owner and group of each FILE to those of RFILE.\n\
87 \n\
88 "), stdout);
89 fputs (_("\
90 -c, --changes like verbose but report only when a change is made\n\
91 -f, --silent, --quiet suppress most error messages\n\
92 -v, --verbose output a diagnostic for every file processed\n\
93 "), stdout);
94 fputs (_("\
95 --dereference affect the referent of each symbolic link (this is\n\
96 the default), rather than the symbolic link itself\n\
97 -h, --no-dereference affect symbolic links instead of any referenced file\n\
98 "), stdout);
99 fputs (_("\
100 (useful only on systems that can change the\n\
101 ownership of a symlink)\n\
102 "), stdout);
103 fputs (_("\
104 --from=CURRENT_OWNER:CURRENT_GROUP\n\
105 change the owner and/or group of each file only if\n\
106 its current owner and/or group match those specified\n\
107 here. Either may be omitted, in which case a match\n\
108 is not required for the omitted attribute\n\
109 "), stdout);
110 fputs (_("\
111 --no-preserve-root do not treat '/' specially (the default)\n\
112 --preserve-root fail to operate recursively on '/'\n\
113 "), stdout);
114 fputs (_("\
115 --reference=RFILE use RFILE's owner and group rather than\n\
116 specifying OWNER:GROUP values\n\
117 "), stdout);
118 fputs (_("\
119 -R, --recursive operate on files and directories recursively\n\
120 "), stdout);
121 fputs (_("\
123 The following options modify how a hierarchy is traversed when the -R\n\
124 option is also specified. If more than one is specified, only the final\n\
125 one takes effect.\n\
127 -H if a command line argument is a symbolic link\n\
128 to a directory, traverse it\n\
129 -L traverse every symbolic link to a directory\n\
130 encountered\n\
131 -P do not traverse any symbolic links (default)\n\
133 "), stdout);
134 fputs (HELP_OPTION_DESCRIPTION, stdout);
135 fputs (VERSION_OPTION_DESCRIPTION, stdout);
136 fputs (_("\
138 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
139 to login group if implied by a ':' following a symbolic OWNER.\n\
140 OWNER and GROUP may be numeric as well as symbolic.\n\
141 "), stdout);
142 printf (_("\
144 Examples:\n\
145 %s root /u Change the owner of /u to \"root\".\n\
146 %s root:staff /u Likewise, but also change its group to \"staff\".\n\
147 %s -hR root /u Change the owner of /u and subfiles to \"root\".\n\
149 program_name, program_name, program_name);
150 emit_ancillary_info (PROGRAM_NAME);
152 exit (status);
156 main (int argc, char **argv)
158 bool preserve_root = false;
160 uid_t uid = -1; /* Specified uid; -1 if not to be changed. */
161 gid_t gid = -1; /* Specified gid; -1 if not to be changed. */
163 /* Change the owner (group) of a file only if it has this uid (gid).
164 -1 means there's no restriction. */
165 uid_t required_uid = -1;
166 gid_t required_gid = -1;
168 /* Bit flags that control how fts works. */
169 int bit_flags = FTS_PHYSICAL;
171 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
172 specified. */
173 int dereference = -1;
175 struct Chown_option chopt;
176 bool ok;
177 int optc;
179 initialize_main (&argc, &argv);
180 set_program_name (argv[0]);
181 setlocale (LC_ALL, "");
182 bindtextdomain (PACKAGE, LOCALEDIR);
183 textdomain (PACKAGE);
185 atexit (close_stdout);
187 chopt_init (&chopt);
189 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
190 != -1)
192 switch (optc)
194 case 'H': /* Traverse command-line symlinks-to-directories. */
195 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
196 break;
198 case 'L': /* Traverse all symlinks-to-directories. */
199 bit_flags = FTS_LOGICAL;
200 break;
202 case 'P': /* Traverse no symlinks-to-directories. */
203 bit_flags = FTS_PHYSICAL;
204 break;
206 case 'h': /* --no-dereference: affect symlinks */
207 dereference = 0;
208 break;
210 case DEREFERENCE_OPTION: /* --dereference: affect the referent
211 of each symlink */
212 dereference = 1;
213 break;
215 case NO_PRESERVE_ROOT:
216 preserve_root = false;
217 break;
219 case PRESERVE_ROOT:
220 preserve_root = true;
221 break;
223 case REFERENCE_FILE_OPTION:
224 reference_file = optarg;
225 break;
227 case FROM_OPTION:
229 const char *e = parse_user_spec (optarg,
230 &required_uid, &required_gid,
231 NULL, NULL);
232 if (e)
233 error (EXIT_FAILURE, 0, "%s: %s", e, quote (optarg));
234 break;
237 case 'R':
238 chopt.recurse = true;
239 break;
241 case 'c':
242 chopt.verbosity = V_changes_only;
243 break;
245 case 'f':
246 chopt.force_silent = true;
247 break;
249 case 'v':
250 chopt.verbosity = V_high;
251 break;
253 case_GETOPT_HELP_CHAR;
254 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
255 default:
256 usage (EXIT_FAILURE);
260 if (chopt.recurse)
262 if (bit_flags == FTS_PHYSICAL)
264 if (dereference == 1)
265 error (EXIT_FAILURE, 0,
266 _("-R --dereference requires either -H or -L"));
267 dereference = 0;
270 else
272 bit_flags = FTS_PHYSICAL;
274 chopt.affect_symlink_referent = (dereference != 0);
276 if (argc - optind < (reference_file ? 1 : 2))
278 if (argc <= optind)
279 error (0, 0, _("missing operand"));
280 else
281 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
282 usage (EXIT_FAILURE);
285 if (reference_file)
287 struct stat ref_stats;
288 if (stat (reference_file, &ref_stats))
289 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
290 quote (reference_file));
292 uid = ref_stats.st_uid;
293 gid = ref_stats.st_gid;
294 chopt.user_name = uid_to_name (ref_stats.st_uid);
295 chopt.group_name = gid_to_name (ref_stats.st_gid);
297 else
299 const char *e = parse_user_spec (argv[optind], &uid, &gid,
300 &chopt.user_name, &chopt.group_name);
301 if (e)
302 error (EXIT_FAILURE, 0, "%s: %s", e, quote (argv[optind]));
304 /* If a group is specified but no user, set the user name to the
305 empty string so that diagnostics say "ownership :GROUP"
306 rather than "group GROUP". */
307 if (!chopt.user_name && chopt.group_name)
308 chopt.user_name = bad_cast ("");
310 optind++;
313 if (chopt.recurse && preserve_root)
315 static struct dev_ino dev_ino_buf;
316 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
317 if (chopt.root_dev_ino == NULL)
318 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
319 quote ("/"));
322 bit_flags |= FTS_DEFER_STAT;
323 ok = chown_files (argv + optind, bit_flags,
324 uid, gid,
325 required_uid, required_gid, &chopt);
327 chopt_free (&chopt);
329 return ok ? EXIT_SUCCESS : EXIT_FAILURE;