build: update gnulib submodule to latest
[coreutils.git] / src / chgrp.c
blob1b4847d8b5a295a880977a98b01d547ba394e2fc
1 /* chgrp -- change group ownership of files
2 Copyright (C) 1989-1991, 1995-2011 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"), quote (name));
93 gid = tmp;
95 endgrent (); /* Save a file descriptor. */
98 return gid;
101 void
102 usage (int status)
104 if (status != EXIT_SUCCESS)
105 fprintf (stderr, _("Try `%s --help' for more information.\n"),
106 program_name);
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 -c, --changes like verbose but report only when a change is made\n\
119 --dereference affect the referent of each symbolic link (this is\n\
120 the default), rather than the symbolic link itself\n\
121 "), stdout);
122 fputs (_("\
123 -h, --no-dereference affect each symbolic link instead of any referenced\n\
124 file (useful only on systems that can change the\n\
125 ownership of a symlink)\n\
126 "), stdout);
127 fputs (_("\
128 --no-preserve-root do not treat `/' specially (the default)\n\
129 --preserve-root fail to operate recursively on `/'\n\
130 "), stdout);
131 fputs (_("\
132 -f, --silent, --quiet suppress most error messages\n\
133 --reference=RFILE use RFILE's group rather than specifying a\n\
134 GROUP value\n\
135 -R, --recursive operate on files and directories recursively\n\
136 -v, --verbose output a diagnostic for every file processed\n\
138 "), stdout);
139 fputs (_("\
140 The following options modify how a hierarchy is traversed when the -R\n\
141 option is also specified. If more than one is specified, only the final\n\
142 one takes effect.\n\
144 -H if a command line argument is a symbolic link\n\
145 to a directory, traverse it\n\
146 -L traverse every symbolic link to a directory\n\
147 encountered\n\
148 -P do not traverse any symbolic links (default)\n\
150 "), stdout);
151 fputs (HELP_OPTION_DESCRIPTION, stdout);
152 fputs (VERSION_OPTION_DESCRIPTION, stdout);
153 printf (_("\
155 Examples:\n\
156 %s staff /u Change the group of /u to \"staff\".\n\
157 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
159 program_name, program_name);
160 emit_ancillary_info ();
162 exit (status);
166 main (int argc, char **argv)
168 bool preserve_root = false;
169 gid_t gid;
171 /* Bit flags that control how fts works. */
172 int bit_flags = FTS_PHYSICAL;
174 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
175 specified. */
176 int dereference = -1;
178 struct Chown_option chopt;
179 bool ok;
180 int optc;
182 initialize_main (&argc, &argv);
183 set_program_name (argv[0]);
184 setlocale (LC_ALL, "");
185 bindtextdomain (PACKAGE, LOCALEDIR);
186 textdomain (PACKAGE);
188 atexit (close_stdout);
190 chopt_init (&chopt);
192 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
193 != -1)
195 switch (optc)
197 case 'H': /* Traverse command-line symlinks-to-directories. */
198 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
199 break;
201 case 'L': /* Traverse all symlinks-to-directories. */
202 bit_flags = FTS_LOGICAL;
203 break;
205 case 'P': /* Traverse no symlinks-to-directories. */
206 bit_flags = FTS_PHYSICAL;
207 break;
209 case 'h': /* --no-dereference: affect symlinks */
210 dereference = 0;
211 break;
213 case DEREFERENCE_OPTION: /* --dereference: affect the referent
214 of each symlink */
215 dereference = 1;
216 break;
218 case NO_PRESERVE_ROOT:
219 preserve_root = false;
220 break;
222 case PRESERVE_ROOT:
223 preserve_root = true;
224 break;
226 case REFERENCE_FILE_OPTION:
227 reference_file = optarg;
228 break;
230 case 'R':
231 chopt.recurse = true;
232 break;
234 case 'c':
235 chopt.verbosity = V_changes_only;
236 break;
238 case 'f':
239 chopt.force_silent = true;
240 break;
242 case 'v':
243 chopt.verbosity = V_high;
244 break;
246 case_GETOPT_HELP_CHAR;
247 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
248 default:
249 usage (EXIT_FAILURE);
253 if (chopt.recurse)
255 if (bit_flags == FTS_PHYSICAL)
257 if (dereference == 1)
258 error (EXIT_FAILURE, 0,
259 _("-R --dereference requires either -H or -L"));
260 dereference = 0;
263 else
265 bit_flags = FTS_PHYSICAL;
267 chopt.affect_symlink_referent = (dereference != 0);
269 if (argc - optind < (reference_file ? 1 : 2))
271 if (argc <= optind)
272 error (0, 0, _("missing operand"));
273 else
274 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
275 usage (EXIT_FAILURE);
278 if (reference_file)
280 struct stat ref_stats;
281 if (stat (reference_file, &ref_stats))
282 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
283 quote (reference_file));
285 gid = ref_stats.st_gid;
286 chopt.group_name = gid_to_name (ref_stats.st_gid);
288 else
290 char *group_name = argv[optind++];
291 chopt.group_name = (*group_name ? group_name : NULL);
292 gid = parse_group (group_name);
295 if (chopt.recurse && preserve_root)
297 static struct dev_ino dev_ino_buf;
298 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
299 if (chopt.root_dev_ino == NULL)
300 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
301 quote ("/"));
304 bit_flags |= FTS_DEFER_STAT;
305 ok = chown_files (argv + optind, bit_flags,
306 (uid_t) -1, gid,
307 (uid_t) -1, (gid_t) -1, &chopt);
309 chopt_free (&chopt);
311 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);