tail: avoid infloop with -c on /dev/zero
[coreutils.git] / src / chown.c
blobc3e6938150bacfa7eee60485538f990f107da1c7
1 /* chown, chgrp -- change user and group ownership of files
2 Copyright (C) 1989-2024 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 <https://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.h"
26 #include "chown-core.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_mode == CHOWN_CHOWN ? "chown" : "chgrp")
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, nullptr, 'R'},
57 {"changes", no_argument, nullptr, 'c'},
58 {"dereference", no_argument, nullptr, DEREFERENCE_OPTION},
59 {"from", required_argument, nullptr, FROM_OPTION},
60 {"no-dereference", no_argument, nullptr, 'h'},
61 {"no-preserve-root", no_argument, nullptr, NO_PRESERVE_ROOT},
62 {"preserve-root", no_argument, nullptr, PRESERVE_ROOT},
63 {"quiet", no_argument, nullptr, 'f'},
64 {"silent", no_argument, nullptr, 'f'},
65 {"reference", required_argument, nullptr, REFERENCE_FILE_OPTION},
66 {"verbose", no_argument, nullptr, 'v'},
67 {GETOPT_HELP_OPTION_DECL},
68 {GETOPT_VERSION_OPTION_DECL},
69 {nullptr, 0, nullptr, 0}
72 void
73 usage (int status)
75 if (status != EXIT_SUCCESS)
76 emit_try_help ();
77 else
79 printf (_("\
80 Usage: %s [OPTION]... %s FILE...\n\
81 or: %s [OPTION]... --reference=RFILE FILE...\n\
82 "),
83 program_name,
84 chown_mode == CHOWN_CHOWN ? _("[OWNER][:[GROUP]]") : _("GROUP"),
85 program_name);
86 if (chown_mode == CHOWN_CHOWN)
87 fputs (_("\
88 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
89 With --reference, change the owner and group of each FILE to those of RFILE.\n\
90 \n\
91 "), stdout);
92 else
93 fputs (_("\
94 Change the group of each FILE to GROUP.\n\
95 With --reference, change the group of each FILE to that of RFILE.\n\
96 \n\
97 "), stdout);
98 fputs (_("\
99 -c, --changes like verbose but report only when a change is made\n\
100 -f, --silent, --quiet suppress most error messages\n\
101 -v, --verbose output a diagnostic for every file processed\n\
102 "), stdout);
103 fputs (_("\
104 --dereference affect the referent of each symbolic link (this is\n\
105 the default), rather than the symbolic link itself\n\
106 -h, --no-dereference affect symbolic links instead of any referenced file\n\
107 "), stdout);
108 fputs (_("\
109 (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 ownership 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 --reference=RFILE use RFILE's ownership rather than specifying values.\n\
125 RFILE is always dereferenced if a symbolic link.\n\
126 "), stdout);
127 fputs (_("\
128 -R, --recursive operate on files and directories recursively\n\
129 "), stdout);
130 emit_symlink_recurse_options ("-P");
131 fputs (HELP_OPTION_DESCRIPTION, stdout);
132 fputs (VERSION_OPTION_DESCRIPTION, stdout);
133 if (chown_mode == CHOWN_CHOWN)
134 fputs (_("\
136 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
137 to login group if implied by a ':' following a symbolic OWNER.\n\
138 OWNER and GROUP may be numeric as well as symbolic.\n\
139 "), stdout);
141 if (chown_mode == CHOWN_CHOWN)
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 else
151 printf (_("\
153 Examples:\n\
154 %s staff /u Change the group of /u to \"staff\".\n\
155 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
157 program_name, program_name);
158 emit_ancillary_info (PROGRAM_NAME);
160 exit (status);
164 main (int argc, char **argv)
166 bool preserve_root = false;
168 uid_t uid = -1; /* Specified uid; -1 if not to be changed. */
169 gid_t gid = -1; /* Specified gid; -1 if not to be changed. */
171 /* Change the owner (group) of a file only if it has this uid (gid).
172 -1 means there's no restriction. */
173 uid_t required_uid = -1;
174 gid_t required_gid = -1;
176 /* Bit flags that control how fts works. */
177 int bit_flags = FTS_PHYSICAL;
179 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
180 specified. */
181 int dereference = -1;
183 struct Chown_option chopt;
184 bool ok;
185 int optc;
187 initialize_main (&argc, &argv);
188 set_program_name (argv[0]);
189 setlocale (LC_ALL, "");
190 bindtextdomain (PACKAGE, LOCALEDIR);
191 textdomain (PACKAGE);
193 atexit (close_stdout);
195 chopt_init (&chopt);
197 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, nullptr))
198 != -1)
200 switch (optc)
202 case 'H': /* Traverse command-line symlinks-to-directories. */
203 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
204 break;
206 case 'L': /* Traverse all symlinks-to-directories. */
207 bit_flags = FTS_LOGICAL;
208 break;
210 case 'P': /* Traverse no symlinks-to-directories. */
211 bit_flags = FTS_PHYSICAL;
212 break;
214 case 'h': /* --no-dereference: affect symlinks */
215 dereference = 0;
216 break;
218 case DEREFERENCE_OPTION: /* --dereference: affect the referent
219 of each symlink */
220 dereference = 1;
221 break;
223 case NO_PRESERVE_ROOT:
224 preserve_root = false;
225 break;
227 case PRESERVE_ROOT:
228 preserve_root = true;
229 break;
231 case REFERENCE_FILE_OPTION:
232 reference_file = optarg;
233 break;
235 case FROM_OPTION:
237 bool warn;
238 char const *e = parse_user_spec_warn (optarg,
239 &required_uid, &required_gid,
240 nullptr, nullptr, &warn);
241 if (e)
242 error (warn ? 0 : EXIT_FAILURE, 0, "%s: %s", e, quote (optarg));
243 break;
246 case 'R':
247 chopt.recurse = true;
248 break;
250 case 'c':
251 chopt.verbosity = V_changes_only;
252 break;
254 case 'f':
255 chopt.force_silent = true;
256 break;
258 case 'v':
259 chopt.verbosity = V_high;
260 break;
262 case_GETOPT_HELP_CHAR;
263 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
264 default:
265 usage (EXIT_FAILURE);
269 if (chopt.recurse)
271 if (bit_flags == FTS_PHYSICAL)
273 if (dereference == 1)
274 error (EXIT_FAILURE, 0,
275 _("-R --dereference requires either -H or -L"));
276 dereference = 0;
279 else
281 bit_flags = FTS_PHYSICAL;
283 chopt.affect_symlink_referent = (dereference != 0);
285 if (argc - optind < (reference_file ? 1 : 2))
287 if (argc <= optind)
288 error (0, 0, _("missing operand"));
289 else
290 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
291 usage (EXIT_FAILURE);
294 if (reference_file)
296 struct stat ref_stats;
297 if (stat (reference_file, &ref_stats))
298 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
299 quoteaf (reference_file));
301 if (chown_mode == CHOWN_CHOWN)
303 uid = ref_stats.st_uid;
304 chopt.user_name = uid_to_name (ref_stats.st_uid);
306 gid = ref_stats.st_gid;
307 chopt.group_name = gid_to_name (ref_stats.st_gid);
309 else
311 char *ug = argv[optind];
312 if (chown_mode == CHOWN_CHGRP)
314 ug = xmalloc (1 + strlen (argv[optind]) + 1);
315 stpcpy (stpcpy (ug, ":"), argv[optind]);
318 bool warn;
319 char const *e = parse_user_spec_warn (ug, &uid, &gid,
320 &chopt.user_name,
321 &chopt.group_name, &warn);
323 if (ug != argv[optind])
324 free (ug);
326 if (e)
327 error (warn ? 0 : EXIT_FAILURE, 0, "%s: %s", e, quote (argv[optind]));
329 /* If a group is specified but no user, set the user name to the
330 empty string so that diagnostics say "ownership :GROUP"
331 rather than "group GROUP". */
332 if (chown_mode == CHOWN_CHOWN && !chopt.user_name && chopt.group_name)
333 chopt.user_name = xstrdup ("");
335 optind++;
338 if (chopt.recurse && preserve_root)
340 static struct dev_ino dev_ino_buf;
341 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
342 if (chopt.root_dev_ino == nullptr)
343 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
344 quoteaf ("/"));
347 bit_flags |= FTS_DEFER_STAT;
348 ok = chown_files (argv + optind, bit_flags,
349 uid, gid,
350 required_uid, required_gid, &chopt);
352 main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);