all: avoid quoting file names when possible
[coreutils.git] / src / chgrp.c
blobc07270cc9e005ff2670b9e65a09f3df03f362a19
1 /* chgrp -- change group ownership of files
2 Copyright (C) 1989-2015 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/>. */
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 "error.h"
28 #include "fts_.h"
29 #include "quote.h"
30 #include "root-dev-ino.h"
31 #include "xstrtol.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "chgrp"
36 #define AUTHORS \
37 proper_name ("David MacKenzie"), \
38 proper_name ("Jim Meyering")
40 #if ! HAVE_ENDGRENT
41 # define endgrent() ((void) 0)
42 #endif
44 /* The argument to the --reference option. Use the group ID of this file.
45 This file must exist. */
46 static char *reference_file;
48 /* For long options that have no equivalent short option, use a
49 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
50 enum
52 DEREFERENCE_OPTION = CHAR_MAX + 1,
53 NO_PRESERVE_ROOT,
54 PRESERVE_ROOT,
55 REFERENCE_FILE_OPTION
58 static struct option const long_options[] =
60 {"recursive", no_argument, NULL, 'R'},
61 {"changes", no_argument, NULL, 'c'},
62 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
63 {"no-dereference", no_argument, NULL, 'h'},
64 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
65 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
66 {"quiet", no_argument, NULL, 'f'},
67 {"silent", no_argument, NULL, 'f'},
68 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
69 {"verbose", no_argument, NULL, 'v'},
70 {GETOPT_HELP_OPTION_DECL},
71 {GETOPT_VERSION_OPTION_DECL},
72 {NULL, 0, NULL, 0}
75 /* Return the group ID of NAME, or -1 if no name was specified. */
77 static gid_t
78 parse_group (const char *name)
80 gid_t gid = -1;
82 if (*name)
84 struct group *grp = getgrnam (name);
85 if (grp)
86 gid = grp->gr_gid;
87 else
89 unsigned long int tmp;
90 if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
91 && tmp <= GID_T_MAX))
92 error (EXIT_FAILURE, 0, _("invalid group: %s"),
93 quote (name));
94 gid = tmp;
96 endgrent (); /* Save a file descriptor. */
99 return gid;
102 void
103 usage (int status)
105 if (status != EXIT_SUCCESS)
106 emit_try_help ();
107 else
109 printf (_("\
110 Usage: %s [OPTION]... GROUP FILE...\n\
111 or: %s [OPTION]... --reference=RFILE FILE...\n\
113 program_name, program_name);
114 fputs (_("\
115 Change the group of each FILE to GROUP.\n\
116 With --reference, change the group of each FILE to that of RFILE.\n\
118 "), stdout);
119 fputs (_("\
120 -c, --changes like verbose but report only when a change is made\n\
121 -f, --silent, --quiet suppress most error messages\n\
122 -v, --verbose output a diagnostic for every file processed\n\
123 "), stdout);
124 fputs (_("\
125 --dereference affect the referent of each symbolic link (this is\n\
126 the default), rather than the symbolic link itself\n\
127 -h, --no-dereference affect symbolic links instead of any referenced file\n\
128 "), stdout);
129 fputs (_("\
130 (useful only on systems that can change the\n\
131 ownership of a symlink)\n\
132 "), stdout);
133 fputs (_("\
134 --no-preserve-root do not treat '/' specially (the default)\n\
135 --preserve-root fail to operate recursively on '/'\n\
136 "), stdout);
137 fputs (_("\
138 --reference=RFILE use RFILE's group rather than specifying a\n\
139 GROUP value\n\
140 "), stdout);
141 fputs (_("\
142 -R, --recursive operate on files and directories recursively\n\
143 "), stdout);
144 fputs (_("\
146 The following options modify how a hierarchy is traversed when the -R\n\
147 option is also specified. If more than one is specified, only the final\n\
148 one takes effect.\n\
150 -H if a command line argument is a symbolic link\n\
151 to a directory, traverse it\n\
152 -L traverse every symbolic link to a directory\n\
153 encountered\n\
154 -P do not traverse any symbolic links (default)\n\
156 "), stdout);
157 fputs (HELP_OPTION_DESCRIPTION, stdout);
158 fputs (VERSION_OPTION_DESCRIPTION, stdout);
159 printf (_("\
161 Examples:\n\
162 %s staff /u Change the group of /u to \"staff\".\n\
163 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
165 program_name, program_name);
166 emit_ancillary_info (PROGRAM_NAME);
168 exit (status);
172 main (int argc, char **argv)
174 bool preserve_root = false;
175 gid_t gid;
177 /* Bit flags that control how fts works. */
178 int bit_flags = FTS_PHYSICAL;
180 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
181 specified. */
182 int dereference = -1;
184 struct Chown_option chopt;
185 bool ok;
186 int optc;
188 initialize_main (&argc, &argv);
189 set_program_name (argv[0]);
190 setlocale (LC_ALL, "");
191 bindtextdomain (PACKAGE, LOCALEDIR);
192 textdomain (PACKAGE);
194 atexit (close_stdout);
196 chopt_init (&chopt);
198 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
199 != -1)
201 switch (optc)
203 case 'H': /* Traverse command-line symlinks-to-directories. */
204 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
205 break;
207 case 'L': /* Traverse all symlinks-to-directories. */
208 bit_flags = FTS_LOGICAL;
209 break;
211 case 'P': /* Traverse no symlinks-to-directories. */
212 bit_flags = FTS_PHYSICAL;
213 break;
215 case 'h': /* --no-dereference: affect symlinks */
216 dereference = 0;
217 break;
219 case DEREFERENCE_OPTION: /* --dereference: affect the referent
220 of each symlink */
221 dereference = 1;
222 break;
224 case NO_PRESERVE_ROOT:
225 preserve_root = false;
226 break;
228 case PRESERVE_ROOT:
229 preserve_root = true;
230 break;
232 case REFERENCE_FILE_OPTION:
233 reference_file = optarg;
234 break;
236 case 'R':
237 chopt.recurse = true;
238 break;
240 case 'c':
241 chopt.verbosity = V_changes_only;
242 break;
244 case 'f':
245 chopt.force_silent = true;
246 break;
248 case 'v':
249 chopt.verbosity = V_high;
250 break;
252 case_GETOPT_HELP_CHAR;
253 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
254 default:
255 usage (EXIT_FAILURE);
259 if (chopt.recurse)
261 if (bit_flags == FTS_PHYSICAL)
263 if (dereference == 1)
264 error (EXIT_FAILURE, 0,
265 _("-R --dereference requires either -H or -L"));
266 dereference = 0;
269 else
271 bit_flags = FTS_PHYSICAL;
273 chopt.affect_symlink_referent = (dereference != 0);
275 if (argc - optind < (reference_file ? 1 : 2))
277 if (argc <= optind)
278 error (0, 0, _("missing operand"));
279 else
280 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
281 usage (EXIT_FAILURE);
284 if (reference_file)
286 struct stat ref_stats;
287 if (stat (reference_file, &ref_stats))
288 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
289 quoteaf (reference_file));
291 gid = ref_stats.st_gid;
292 chopt.group_name = gid_to_name (ref_stats.st_gid);
294 else
296 char *group_name = argv[optind++];
297 chopt.group_name = (*group_name ? group_name : NULL);
298 gid = parse_group (group_name);
301 if (chopt.recurse && preserve_root)
303 static struct dev_ino dev_ino_buf;
304 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
305 if (chopt.root_dev_ino == NULL)
306 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
307 quoteaf ("/"));
310 bit_flags |= FTS_DEFER_STAT;
311 ok = chown_files (argv + optind, bit_flags,
312 (uid_t) -1, gid,
313 (uid_t) -1, (gid_t) -1, &chopt);
315 chopt_free (&chopt);
317 return ok ? EXIT_SUCCESS : EXIT_FAILURE;