.
[coreutils.git] / src / chown.c
blobc7ec784f068f637f39cb1f0d9dfba8516b91abb4
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)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 | user
20 | unchanged explicit
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>. */
31 #include <config.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <getopt.h>
36 #include "system.h"
37 #include "chown-core.h"
38 #include "error.h"
39 #include "fts_.h"
40 #include "lchown.h"
41 #include "quote.h"
42 #include "root-dev-ino.h"
43 #include "userspec.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. */
51 char *program_name;
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. */
59 enum
61 DEREFERENCE_OPTION = CHAR_MAX + 1,
62 FROM_OPTION,
63 NO_PRESERVE_ROOT,
64 PRESERVE_ROOT,
65 REFERENCE_FILE_OPTION
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},
83 {NULL, 0, NULL, 0}
86 void
87 usage (int status)
89 if (status != EXIT_SUCCESS)
90 fprintf (stderr, _("Try `%s --help' for more information.\n"),
91 program_name);
92 else
94 printf (_("\
95 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
96 or: %s [OPTION]... --reference=RFILE FILE...\n\
97 "),
98 program_name, program_name);
99 fputs (_("\
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\
106 "), stdout);
107 fputs (_("\
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\
111 "), stdout);
112 fputs (_("\
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\
118 "), stdout);
119 fputs (_("\
120 --no-preserve-root do not treat `/' specially (the default)\n\
121 --preserve-root fail to operate recursively on `/'\n\
122 "), stdout);
123 fputs (_("\
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\
130 "), stdout);
131 fputs (_("\
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\
134 one takes effect.\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\
139 encountered\n\
140 -P do not traverse any symbolic links (default)\n\
142 "), stdout);
143 fputs (HELP_OPTION_DESCRIPTION, stdout);
144 fputs (VERSION_OPTION_DESCRIPTION, stdout);
145 fputs (_("\
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\
150 "), stdout);
151 printf (_("\
153 Examples:\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);
161 exit (status);
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
181 specified. */
182 int dereference = -1;
184 struct Chown_option chopt;
185 bool ok;
186 int optc;
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);
196 chopt_init (&chopt);
198 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
199 != -1)
201 switch (optc)
203 case 'H': /* Traverse command-line symlinks-to-directories. */
204 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
205 break;
207 case 'L': /* Traverse all symlinks-to-directories. */
208 bit_flags = FTS_LOGICAL;
209 break;
211 case 'P': /* Traverse no symlinks-to-directories. */
212 bit_flags = FTS_PHYSICAL;
213 break;
215 case 'h': /* --no-dereference: affect symlinks */
216 dereference = 0;
217 break;
219 case DEREFERENCE_OPTION: /* --dereference: affect the referent
220 of each symlink */
221 dereference = 1;
222 break;
224 case NO_PRESERVE_ROOT:
225 preserve_root = false;
226 break;
228 case PRESERVE_ROOT:
229 preserve_root = true;
230 break;
232 case REFERENCE_FILE_OPTION:
233 reference_file = optarg;
234 break;
236 case FROM_OPTION:
238 char *u_dummy, *g_dummy;
239 const char *e = parse_user_spec (optarg,
240 &required_uid, &required_gid,
241 &u_dummy, &g_dummy);
242 if (e)
243 error (EXIT_FAILURE, 0, "%s: %s", quote (optarg), e);
244 break;
247 case 'R':
248 chopt.recurse = true;
249 break;
251 case 'c':
252 chopt.verbosity = V_changes_only;
253 break;
255 case 'f':
256 chopt.force_silent = true;
257 break;
259 case 'v':
260 chopt.verbosity = V_high;
261 break;
263 case_GETOPT_HELP_CHAR;
264 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
265 default:
266 usage (EXIT_FAILURE);
270 if (chopt.recurse)
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;
279 else
281 if (dereference == 0)
282 error (EXIT_FAILURE, 0, _("-R -h requires -P"));
283 chopt.affect_symlink_referent = true;
286 else
288 bit_flags = FTS_PHYSICAL;
289 chopt.affect_symlink_referent = (dereference != 0);
292 if (argc - optind < (reference_file ? 1 : 2))
294 if (argc <= optind)
295 error (0, 0, _("missing operand"));
296 else
297 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
298 usage (EXIT_FAILURE);
301 if (reference_file)
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);
313 else
315 const char *e = parse_user_spec (argv[optind], &uid, &gid,
316 &chopt.user_name, &chopt.group_name);
317 if (e)
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 = "";
326 optind++;
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"),
335 quote ("/"));
338 ok = chown_files (argv + optind, bit_flags,
339 uid, gid,
340 required_uid, required_gid, &chopt);
342 chopt_free (&chopt);
344 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);