id: support multiple specified users
[coreutils.git] / src / chgrp.c
blobec3bb13b7b9488fb3903db2302aa368995b0ef51
1 /* chgrp -- change group ownership of files
2 Copyright (C) 1989-2018 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 <grp.h>
23 #include <getopt.h>
25 #include "system.h"
26 #include "chown-core.h"
27 #include "die.h"
28 #include "error.h"
29 #include "fts_.h"
30 #include "quote.h"
31 #include "root-dev-ino.h"
32 #include "xstrtol.h"
34 /* The official name of this program (e.g., no 'g' prefix). */
35 #define PROGRAM_NAME "chgrp"
37 #define AUTHORS \
38 proper_name ("David MacKenzie"), \
39 proper_name ("Jim Meyering")
41 #if ! HAVE_ENDGRENT
42 # define endgrent() ((void) 0)
43 #endif
45 /* The argument to the --reference option. Use the group ID of this file.
46 This file must exist. */
47 static char *reference_file;
49 /* For long options that have no equivalent short option, use a
50 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
51 enum
53 DEREFERENCE_OPTION = CHAR_MAX + 1,
54 NO_PRESERVE_ROOT,
55 PRESERVE_ROOT,
56 REFERENCE_FILE_OPTION
59 static struct option const long_options[] =
61 {"recursive", no_argument, NULL, 'R'},
62 {"changes", no_argument, NULL, 'c'},
63 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
64 {"no-dereference", no_argument, NULL, 'h'},
65 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
66 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
67 {"quiet", no_argument, NULL, 'f'},
68 {"silent", no_argument, NULL, 'f'},
69 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
70 {"verbose", no_argument, NULL, 'v'},
71 {GETOPT_HELP_OPTION_DECL},
72 {GETOPT_VERSION_OPTION_DECL},
73 {NULL, 0, NULL, 0}
76 /* Return the group ID of NAME, or -1 if no name was specified. */
78 static gid_t
79 parse_group (const char *name)
81 gid_t gid = -1;
83 if (*name)
85 struct group *grp = getgrnam (name);
86 if (grp)
87 gid = grp->gr_gid;
88 else
90 unsigned long int tmp;
91 if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
92 && tmp <= GID_T_MAX))
93 die (EXIT_FAILURE, 0, _("invalid group: %s"),
94 quote (name));
95 gid = tmp;
97 endgrent (); /* Save a file descriptor. */
100 return gid;
103 void
104 usage (int status)
106 if (status != EXIT_SUCCESS)
107 emit_try_help ();
108 else
110 printf (_("\
111 Usage: %s [OPTION]... GROUP FILE...\n\
112 or: %s [OPTION]... --reference=RFILE FILE...\n\
114 program_name, program_name);
115 fputs (_("\
116 Change the group of each FILE to GROUP.\n\
117 With --reference, change the group of each FILE to that of RFILE.\n\
119 "), stdout);
120 fputs (_("\
121 -c, --changes like verbose but report only when a change is made\n\
122 -f, --silent, --quiet suppress most error messages\n\
123 -v, --verbose output a diagnostic for every file processed\n\
124 "), stdout);
125 fputs (_("\
126 --dereference affect the referent of each symbolic link (this is\n\
127 the default), rather than the symbolic link itself\n\
128 -h, --no-dereference affect symbolic links instead of any referenced file\n\
129 "), stdout);
130 fputs (_("\
131 (useful only on systems that can change the\n\
132 ownership of a symlink)\n\
133 "), stdout);
134 fputs (_("\
135 --no-preserve-root do not treat '/' specially (the default)\n\
136 --preserve-root fail to operate recursively on '/'\n\
137 "), stdout);
138 fputs (_("\
139 --reference=RFILE use RFILE's group rather than specifying a\n\
140 GROUP value\n\
141 "), stdout);
142 fputs (_("\
143 -R, --recursive operate on files and directories recursively\n\
144 "), stdout);
145 fputs (_("\
147 The following options modify how a hierarchy is traversed when the -R\n\
148 option is also specified. If more than one is specified, only the final\n\
149 one takes effect.\n\
151 -H if a command line argument is a symbolic link\n\
152 to a directory, traverse it\n\
153 -L traverse every symbolic link to a directory\n\
154 encountered\n\
155 -P do not traverse any symbolic links (default)\n\
157 "), stdout);
158 fputs (HELP_OPTION_DESCRIPTION, stdout);
159 fputs (VERSION_OPTION_DESCRIPTION, stdout);
160 printf (_("\
162 Examples:\n\
163 %s staff /u Change the group of /u to \"staff\".\n\
164 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
166 program_name, program_name);
167 emit_ancillary_info (PROGRAM_NAME);
169 exit (status);
173 main (int argc, char **argv)
175 bool preserve_root = false;
176 gid_t gid;
178 /* Bit flags that control how fts works. */
179 int bit_flags = FTS_PHYSICAL;
181 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
182 specified. */
183 int dereference = -1;
185 struct Chown_option chopt;
186 bool ok;
187 int optc;
189 initialize_main (&argc, &argv);
190 set_program_name (argv[0]);
191 setlocale (LC_ALL, "");
192 bindtextdomain (PACKAGE, LOCALEDIR);
193 textdomain (PACKAGE);
195 atexit (close_stdout);
197 chopt_init (&chopt);
199 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
200 != -1)
202 switch (optc)
204 case 'H': /* Traverse command-line symlinks-to-directories. */
205 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
206 break;
208 case 'L': /* Traverse all symlinks-to-directories. */
209 bit_flags = FTS_LOGICAL;
210 break;
212 case 'P': /* Traverse no symlinks-to-directories. */
213 bit_flags = FTS_PHYSICAL;
214 break;
216 case 'h': /* --no-dereference: affect symlinks */
217 dereference = 0;
218 break;
220 case DEREFERENCE_OPTION: /* --dereference: affect the referent
221 of each symlink */
222 dereference = 1;
223 break;
225 case NO_PRESERVE_ROOT:
226 preserve_root = false;
227 break;
229 case PRESERVE_ROOT:
230 preserve_root = true;
231 break;
233 case REFERENCE_FILE_OPTION:
234 reference_file = optarg;
235 break;
237 case 'R':
238 chopt.recurse = true;
239 break;
241 case 'c':
242 chopt.verbosity = V_changes_only;
243 break;
245 case 'f':
246 chopt.force_silent = true;
247 break;
249 case 'v':
250 chopt.verbosity = V_high;
251 break;
253 case_GETOPT_HELP_CHAR;
254 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
255 default:
256 usage (EXIT_FAILURE);
260 if (chopt.recurse)
262 if (bit_flags == FTS_PHYSICAL)
264 if (dereference == 1)
265 die (EXIT_FAILURE, 0,
266 _("-R --dereference requires either -H or -L"));
267 dereference = 0;
270 else
272 bit_flags = FTS_PHYSICAL;
274 chopt.affect_symlink_referent = (dereference != 0);
276 if (argc - optind < (reference_file ? 1 : 2))
278 if (argc <= optind)
279 error (0, 0, _("missing operand"));
280 else
281 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
282 usage (EXIT_FAILURE);
285 if (reference_file)
287 struct stat ref_stats;
288 if (stat (reference_file, &ref_stats))
289 die (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
290 quoteaf (reference_file));
292 gid = ref_stats.st_gid;
293 chopt.group_name = gid_to_name (ref_stats.st_gid);
295 else
297 char *group_name = argv[optind++];
298 chopt.group_name = (*group_name ? xstrdup (group_name) : NULL);
299 gid = parse_group (group_name);
302 if (chopt.recurse && preserve_root)
304 static struct dev_ino dev_ino_buf;
305 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
306 if (chopt.root_dev_ino == NULL)
307 die (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
308 quoteaf ("/"));
311 bit_flags |= FTS_DEFER_STAT;
312 ok = chown_files (argv + optind, bit_flags,
313 (uid_t) -1, gid,
314 (uid_t) -1, (gid_t) -1, &chopt);
316 IF_LINT (chopt_free (&chopt));
318 return ok ? EXIT_SUCCESS : EXIT_FAILURE;