id: fix infinite loop on some systems
[coreutils/ericb.git] / src / chown.c
blob00cdb242b36aa247b228672ec2975bfbb6c94e63
1 /* chown -- change user and group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2009 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/>. */
18 | user
19 | unchanged explicit
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>. */
30 #include <config.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <getopt.h>
35 #include "system.h"
36 #include "chown-core.h"
37 #include "error.h"
38 #include "fts_.h"
39 #include "quote.h"
40 #include "root-dev-ino.h"
41 #include "userspec.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "chown"
46 #define AUTHORS \
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. */
56 enum
58 DEREFERENCE_OPTION = CHAR_MAX + 1,
59 FROM_OPTION,
60 NO_PRESERVE_ROOT,
61 PRESERVE_ROOT,
62 REFERENCE_FILE_OPTION
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},
80 {NULL, 0, NULL, 0}
83 void
84 usage (int status)
86 if (status != EXIT_SUCCESS)
87 fprintf (stderr, _("Try `%s --help' for more information.\n"),
88 program_name);
89 else
91 printf (_("\
92 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
93 or: %s [OPTION]... --reference=RFILE FILE...\n\
94 "),
95 program_name, program_name);
96 fputs (_("\
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\
99 \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\
103 "), stdout);
104 fputs (_("\
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\
108 "), stdout);
109 fputs (_("\
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\
115 "), stdout);
116 fputs (_("\
117 --no-preserve-root do not treat `/' specially (the default)\n\
118 --preserve-root fail to operate recursively on `/'\n\
119 "), stdout);
120 fputs (_("\
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\
127 "), stdout);
128 fputs (_("\
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\
131 one takes effect.\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\
136 encountered\n\
137 -P do not traverse any symbolic links (default)\n\
139 "), stdout);
140 fputs (HELP_OPTION_DESCRIPTION, stdout);
141 fputs (VERSION_OPTION_DESCRIPTION, stdout);
142 fputs (_("\
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\
147 "), stdout);
148 printf (_("\
150 Examples:\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 ();
158 exit (status);
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
178 specified. */
179 int dereference = -1;
181 struct Chown_option chopt;
182 bool ok;
183 int optc;
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);
193 chopt_init (&chopt);
195 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
196 != -1)
198 switch (optc)
200 case 'H': /* Traverse command-line symlinks-to-directories. */
201 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
202 break;
204 case 'L': /* Traverse all symlinks-to-directories. */
205 bit_flags = FTS_LOGICAL;
206 break;
208 case 'P': /* Traverse no symlinks-to-directories. */
209 bit_flags = FTS_PHYSICAL;
210 break;
212 case 'h': /* --no-dereference: affect symlinks */
213 dereference = 0;
214 break;
216 case DEREFERENCE_OPTION: /* --dereference: affect the referent
217 of each symlink */
218 dereference = 1;
219 break;
221 case NO_PRESERVE_ROOT:
222 preserve_root = false;
223 break;
225 case PRESERVE_ROOT:
226 preserve_root = true;
227 break;
229 case REFERENCE_FILE_OPTION:
230 reference_file = optarg;
231 break;
233 case FROM_OPTION:
235 char *u_dummy, *g_dummy;
236 const char *e = parse_user_spec (optarg,
237 &required_uid, &required_gid,
238 &u_dummy, &g_dummy);
239 if (e)
240 error (EXIT_FAILURE, 0, "%s: %s", e, quote (optarg));
241 break;
244 case 'R':
245 chopt.recurse = true;
246 break;
248 case 'c':
249 chopt.verbosity = V_changes_only;
250 break;
252 case 'f':
253 chopt.force_silent = true;
254 break;
256 case 'v':
257 chopt.verbosity = V_high;
258 break;
260 case_GETOPT_HELP_CHAR;
261 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
262 default:
263 usage (EXIT_FAILURE);
267 if (chopt.recurse)
269 if (bit_flags == FTS_PHYSICAL)
271 if (dereference == 1)
272 error (EXIT_FAILURE, 0,
273 _("-R --dereference requires either -H or -L"));
274 dereference = 0;
277 else
279 bit_flags = FTS_PHYSICAL;
281 chopt.affect_symlink_referent = (dereference != 0);
283 if (argc - optind < (reference_file ? 1 : 2))
285 if (argc <= optind)
286 error (0, 0, _("missing operand"));
287 else
288 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
289 usage (EXIT_FAILURE);
292 if (reference_file)
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);
304 else
306 const char *e = parse_user_spec (argv[optind], &uid, &gid,
307 &chopt.user_name, &chopt.group_name);
308 if (e)
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 = bad_cast ("");
317 optind++;
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"),
326 quote ("/"));
329 bit_flags |= FTS_DEFER_STAT;
330 ok = chown_files (argv + optind, bit_flags,
331 uid, gid,
332 required_uid, required_gid, &chopt);
334 chopt_free (&chopt);
336 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);