1 /* chown -- change user and group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2008 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/>. */
20 -------------|-------------------------+-------------------------|
21 g unchanged | --- | chown u |
22 r |-------------------------+-------------------------|
23 o explicit | chgrp g or chown .g | chown u.g |
24 u |-------------------------+-------------------------|
25 p from passwd| --- | chown u. |
26 |-------------------------+-------------------------|
28 Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
32 #include <sys/types.h>
36 #include "chown-core.h"
40 #include "root-dev-ino.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "chown"
47 proper_name ("David MacKenzie"), \
48 proper_name ("Jim Meyering")
50 /* The argument to the --reference option. Use the owner and group IDs
51 of this file. This file must exist. */
52 static char *reference_file;
54 /* For long options that have no equivalent short option, use a
55 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
58 DEREFERENCE_OPTION = CHAR_MAX + 1,
65 static struct option const long_options[] =
67 {"recursive", no_argument, NULL, 'R'},
68 {"changes", no_argument, NULL, 'c'},
69 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
70 {"from", required_argument, NULL, FROM_OPTION},
71 {"no-dereference", no_argument, NULL, 'h'},
72 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
73 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
74 {"quiet", no_argument, NULL, 'f'},
75 {"silent", no_argument, NULL, 'f'},
76 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
77 {"verbose", no_argument, NULL, 'v'},
78 {GETOPT_HELP_OPTION_DECL},
79 {GETOPT_VERSION_OPTION_DECL},
86 if (status != EXIT_SUCCESS)
87 fprintf (stderr, _("Try `%s --help' for more information.\n"),
92 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
93 or: %s [OPTION]... --reference=RFILE FILE...\n\
95 program_name, program_name);
97 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
98 With --reference, change the owner and group of each FILE to those of RFILE.\n\
100 -c, --changes like verbose but report only when a change is made\n\
101 --dereference affect the referent of each symbolic link (this is\n\
102 the default), rather than the symbolic link itself\n\
105 -h, --no-dereference affect each symbolic link instead of any referenced\n\
106 file (useful only on systems that can change the\n\
107 ownership of a symlink)\n\
110 --from=CURRENT_OWNER:CURRENT_GROUP\n\
111 change the owner and/or group of each file only if\n\
112 its current owner and/or group match those specified\n\
113 here. Either may be omitted, in which case a match\n\
114 is not required for the omitted attribute.\n\
117 --no-preserve-root do not treat `/' specially (the default)\n\
118 --preserve-root fail to operate recursively on `/'\n\
121 -f, --silent, --quiet suppress most error messages\n\
122 --reference=RFILE use RFILE's owner and group rather than\n\
123 specifying OWNER:GROUP values\n\
124 -R, --recursive operate on files and directories recursively\n\
125 -v, --verbose output a diagnostic for every file processed\n\
129 The following options modify how a hierarchy is traversed when the -R\n\
130 option is also specified. If more than one is specified, only the final\n\
133 -H if a command line argument is a symbolic link\n\
134 to a directory, traverse it\n\
135 -L traverse every symbolic link to a directory\n\
137 -P do not traverse any symbolic links (default)\n\
140 fputs (HELP_OPTION_DESCRIPTION, stdout);
141 fputs (VERSION_OPTION_DESCRIPTION, stdout);
144 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
145 to login group if implied by a `:' following a symbolic OWNER.\n\
146 OWNER and GROUP may be numeric as well as symbolic.\n\
151 %s root /u Change the owner of /u to \"root\".\n\
152 %s root:staff /u Likewise, but also change its group to \"staff\".\n\
153 %s -hR root /u Change the owner of /u and subfiles to \"root\".\n\
155 program_name, program_name, program_name);
156 emit_bug_reporting_address ();
162 main (int argc, char **argv)
164 bool preserve_root = false;
166 uid_t uid = -1; /* Specified uid; -1 if not to be changed. */
167 gid_t gid = -1; /* Specified gid; -1 if not to be changed. */
169 /* Change the owner (group) of a file only if it has this uid (gid).
170 -1 means there's no restriction. */
171 uid_t required_uid = -1;
172 gid_t required_gid = -1;
174 /* Bit flags that control how fts works. */
175 int bit_flags = FTS_PHYSICAL;
177 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
179 int dereference = -1;
181 struct Chown_option chopt;
185 initialize_main (&argc, &argv);
186 set_program_name (argv[0]);
187 setlocale (LC_ALL, "");
188 bindtextdomain (PACKAGE, LOCALEDIR);
189 textdomain (PACKAGE);
191 atexit (close_stdout);
195 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
200 case 'H': /* Traverse command-line symlinks-to-directories. */
201 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
204 case 'L': /* Traverse all symlinks-to-directories. */
205 bit_flags = FTS_LOGICAL;
208 case 'P': /* Traverse no symlinks-to-directories. */
209 bit_flags = FTS_PHYSICAL;
212 case 'h': /* --no-dereference: affect symlinks */
216 case DEREFERENCE_OPTION: /* --dereference: affect the referent
221 case NO_PRESERVE_ROOT:
222 preserve_root = false;
226 preserve_root = true;
229 case REFERENCE_FILE_OPTION:
230 reference_file = optarg;
235 char *u_dummy, *g_dummy;
236 const char *e = parse_user_spec (optarg,
237 &required_uid, &required_gid,
240 error (EXIT_FAILURE, 0, "%s: %s", e, quote (optarg));
245 chopt.recurse = true;
249 chopt.verbosity = V_changes_only;
253 chopt.force_silent = true;
257 chopt.verbosity = V_high;
260 case_GETOPT_HELP_CHAR;
261 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
263 usage (EXIT_FAILURE);
269 if (bit_flags == FTS_PHYSICAL)
271 if (dereference == 1)
272 error (EXIT_FAILURE, 0,
273 _("-R --dereference requires either -H or -L"));
279 bit_flags = FTS_PHYSICAL;
281 chopt.affect_symlink_referent = (dereference != 0);
283 if (argc - optind < (reference_file ? 1 : 2))
286 error (0, 0, _("missing operand"));
288 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
289 usage (EXIT_FAILURE);
294 struct stat ref_stats;
295 if (stat (reference_file, &ref_stats))
296 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
297 quote (reference_file));
299 uid = ref_stats.st_uid;
300 gid = ref_stats.st_gid;
301 chopt.user_name = uid_to_name (ref_stats.st_uid);
302 chopt.group_name = gid_to_name (ref_stats.st_gid);
306 const char *e = parse_user_spec (argv[optind], &uid, &gid,
307 &chopt.user_name, &chopt.group_name);
309 error (EXIT_FAILURE, 0, "%s: %s", e, quote (argv[optind]));
311 /* If a group is specified but no user, set the user name to the
312 empty string so that diagnostics say "ownership :GROUP"
313 rather than "group GROUP". */
314 if (!chopt.user_name && chopt.group_name)
315 chopt.user_name = "";
320 if (chopt.recurse & preserve_root)
322 static struct dev_ino dev_ino_buf;
323 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
324 if (chopt.root_dev_ino == NULL)
325 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
329 ok = chown_files (argv + optind, bit_flags,
331 required_uid, required_gid, &chopt);
335 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);