split: port ‘split -n N /dev/null’ better to macOS
[coreutils.git] / src / chown.c
blobd4ae24bec7e70fdb3fe49eb98d53cc145d96884e
1 /* chown -- change user and group ownership of files
2 Copyright (C) 1989-2023 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-core.h"
26 #include "die.h"
27 #include "error.h"
28 #include "fts_.h"
29 #include "quote.h"
30 #include "root-dev-ino.h"
31 #include "userspec.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "chown"
36 #define AUTHORS \
37 proper_name ("David MacKenzie"), \
38 proper_name ("Jim Meyering")
40 /* The argument to the --reference option. Use the owner and group IDs
41 of this file. This file must exist. */
42 static char *reference_file;
44 /* For long options that have no equivalent short option, use a
45 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
46 enum
48 DEREFERENCE_OPTION = CHAR_MAX + 1,
49 FROM_OPTION,
50 NO_PRESERVE_ROOT,
51 PRESERVE_ROOT,
52 REFERENCE_FILE_OPTION
55 static struct option const long_options[] =
57 {"recursive", no_argument, NULL, 'R'},
58 {"changes", no_argument, NULL, 'c'},
59 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
60 {"from", required_argument, NULL, FROM_OPTION},
61 {"no-dereference", no_argument, NULL, 'h'},
62 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
63 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
64 {"quiet", no_argument, NULL, 'f'},
65 {"silent", no_argument, NULL, 'f'},
66 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
67 {"verbose", no_argument, NULL, 'v'},
68 {GETOPT_HELP_OPTION_DECL},
69 {GETOPT_VERSION_OPTION_DECL},
70 {NULL, 0, NULL, 0}
73 void
74 usage (int status)
76 if (status != EXIT_SUCCESS)
77 emit_try_help ();
78 else
80 printf (_("\
81 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
82 or: %s [OPTION]... --reference=RFILE FILE...\n\
83 "),
84 program_name, program_name);
85 fputs (_("\
86 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
87 With --reference, change the owner and group of each FILE to those of RFILE.\n\
88 \n\
89 "), stdout);
90 fputs (_("\
91 -c, --changes like verbose but report only when a change is made\n\
92 -f, --silent, --quiet suppress most error messages\n\
93 -v, --verbose output a diagnostic for every file processed\n\
94 "), stdout);
95 fputs (_("\
96 --dereference affect the referent of each symbolic link (this is\n\
97 the default), rather than the symbolic link itself\n\
98 -h, --no-dereference affect symbolic links instead of any referenced file\n\
99 "), stdout);
100 fputs (_("\
101 (useful only on systems that can change the\n\
102 ownership of a symlink)\n\
103 "), stdout);
104 fputs (_("\
105 --from=CURRENT_OWNER:CURRENT_GROUP\n\
106 change the owner and/or group of each file only if\n\
107 its current owner and/or group match those specified\n\
108 here. Either may be omitted, in which case a match\n\
109 is not required for the omitted attribute\n\
110 "), stdout);
111 fputs (_("\
112 --no-preserve-root do not treat '/' specially (the default)\n\
113 --preserve-root fail to operate recursively on '/'\n\
114 "), stdout);
115 fputs (_("\
116 --reference=RFILE use RFILE's owner and group rather than specifying\n\
117 OWNER:GROUP values. RFILE is always dereferenced.\n\
118 "), stdout);
119 fputs (_("\
120 -R, --recursive operate on files and directories recursively\n\
121 "), stdout);
122 fputs (_("\
124 The following options modify how a hierarchy is traversed when the -R\n\
125 option is also specified. If more than one is specified, only the final\n\
126 one takes effect.\n\
128 -H if a command line argument is a symbolic link\n\
129 to a directory, traverse it\n\
130 -L traverse every symbolic link to a directory\n\
131 encountered\n\
132 -P do not traverse any symbolic links (default)\n\
134 "), stdout);
135 fputs (HELP_OPTION_DESCRIPTION, stdout);
136 fputs (VERSION_OPTION_DESCRIPTION, stdout);
137 fputs (_("\
139 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
140 to login group if implied by a ':' following a symbolic OWNER.\n\
141 OWNER and GROUP may be numeric as well as symbolic.\n\
142 "), stdout);
143 printf (_("\
145 Examples:\n\
146 %s root /u Change the owner of /u to \"root\".\n\
147 %s root:staff /u Likewise, but also change its group to \"staff\".\n\
148 %s -hR root /u Change the owner of /u and subfiles to \"root\".\n\
150 program_name, program_name, program_name);
151 emit_ancillary_info (PROGRAM_NAME);
153 exit (status);
157 main (int argc, char **argv)
159 bool preserve_root = false;
161 uid_t uid = -1; /* Specified uid; -1 if not to be changed. */
162 gid_t gid = -1; /* Specified gid; -1 if not to be changed. */
164 /* Change the owner (group) of a file only if it has this uid (gid).
165 -1 means there's no restriction. */
166 uid_t required_uid = -1;
167 gid_t required_gid = -1;
169 /* Bit flags that control how fts works. */
170 int bit_flags = FTS_PHYSICAL;
172 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
173 specified. */
174 int dereference = -1;
176 struct Chown_option chopt;
177 bool ok;
178 int optc;
180 initialize_main (&argc, &argv);
181 set_program_name (argv[0]);
182 setlocale (LC_ALL, "");
183 bindtextdomain (PACKAGE, LOCALEDIR);
184 textdomain (PACKAGE);
186 atexit (close_stdout);
188 chopt_init (&chopt);
190 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
191 != -1)
193 switch (optc)
195 case 'H': /* Traverse command-line symlinks-to-directories. */
196 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
197 break;
199 case 'L': /* Traverse all symlinks-to-directories. */
200 bit_flags = FTS_LOGICAL;
201 break;
203 case 'P': /* Traverse no symlinks-to-directories. */
204 bit_flags = FTS_PHYSICAL;
205 break;
207 case 'h': /* --no-dereference: affect symlinks */
208 dereference = 0;
209 break;
211 case DEREFERENCE_OPTION: /* --dereference: affect the referent
212 of each symlink */
213 dereference = 1;
214 break;
216 case NO_PRESERVE_ROOT:
217 preserve_root = false;
218 break;
220 case PRESERVE_ROOT:
221 preserve_root = true;
222 break;
224 case REFERENCE_FILE_OPTION:
225 reference_file = optarg;
226 break;
228 case FROM_OPTION:
230 bool warn;
231 char const *e = parse_user_spec_warn (optarg,
232 &required_uid, &required_gid,
233 NULL, NULL, &warn);
234 if (e)
235 error (warn ? 0 : EXIT_FAILURE, 0, "%s: %s", e, quote (optarg));
236 break;
239 case 'R':
240 chopt.recurse = true;
241 break;
243 case 'c':
244 chopt.verbosity = V_changes_only;
245 break;
247 case 'f':
248 chopt.force_silent = true;
249 break;
251 case 'v':
252 chopt.verbosity = V_high;
253 break;
255 case_GETOPT_HELP_CHAR;
256 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
257 default:
258 usage (EXIT_FAILURE);
262 if (chopt.recurse)
264 if (bit_flags == FTS_PHYSICAL)
266 if (dereference == 1)
267 die (EXIT_FAILURE, 0,
268 _("-R --dereference requires either -H or -L"));
269 dereference = 0;
272 else
274 bit_flags = FTS_PHYSICAL;
276 chopt.affect_symlink_referent = (dereference != 0);
278 if (argc - optind < (reference_file ? 1 : 2))
280 if (argc <= optind)
281 error (0, 0, _("missing operand"));
282 else
283 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
284 usage (EXIT_FAILURE);
287 if (reference_file)
289 struct stat ref_stats;
290 if (stat (reference_file, &ref_stats))
291 die (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
292 quoteaf (reference_file));
294 uid = ref_stats.st_uid;
295 gid = ref_stats.st_gid;
296 chopt.user_name = uid_to_name (ref_stats.st_uid);
297 chopt.group_name = gid_to_name (ref_stats.st_gid);
299 else
301 bool warn;
302 char const *e = parse_user_spec_warn (argv[optind], &uid, &gid,
303 &chopt.user_name,
304 &chopt.group_name, &warn);
305 if (e)
306 error (warn ? 0 : EXIT_FAILURE, 0, "%s: %s", e, quote (argv[optind]));
308 /* If a group is specified but no user, set the user name to the
309 empty string so that diagnostics say "ownership :GROUP"
310 rather than "group GROUP". */
311 if (!chopt.user_name && chopt.group_name)
312 chopt.user_name = xstrdup ("");
314 optind++;
317 if (chopt.recurse && preserve_root)
319 static struct dev_ino dev_ino_buf;
320 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
321 if (chopt.root_dev_ino == NULL)
322 die (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
323 quoteaf ("/"));
326 bit_flags |= FTS_DEFER_STAT;
327 ok = chown_files (argv + optind, bit_flags,
328 uid, gid,
329 required_uid, required_gid, &chopt);
331 main_exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);