Make the new printf-surprise test more precise.
[coreutils/ericb.git] / src / chgrp.c
blobb15c385aaa7d5227ae1ee624785a50a663276c75
1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2007 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 "group-member.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 "David MacKenzie", "Jim Meyering"
39 #if ! HAVE_ENDGRENT
40 # define endgrent() ((void) 0)
41 #endif
43 /* The name the program was run with. */
44 char *program_name;
46 /* The argument to the --reference option. Use the group ID of this file.
47 This file must exist. */
48 static char *reference_file;
50 /* For long options that have no equivalent short option, use a
51 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
52 enum
54 DEREFERENCE_OPTION = CHAR_MAX + 1,
55 NO_PRESERVE_ROOT,
56 PRESERVE_ROOT,
57 REFERENCE_FILE_OPTION
60 static struct option const long_options[] =
62 {"recursive", no_argument, NULL, 'R'},
63 {"changes", no_argument, NULL, 'c'},
64 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
65 {"no-dereference", no_argument, NULL, 'h'},
66 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
67 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
68 {"quiet", no_argument, NULL, 'f'},
69 {"silent", no_argument, NULL, 'f'},
70 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
71 {"verbose", no_argument, NULL, 'v'},
72 {GETOPT_HELP_OPTION_DECL},
73 {GETOPT_VERSION_OPTION_DECL},
74 {NULL, 0, NULL, 0}
77 /* Return the group ID of NAME, or -1 if no name was specified. */
79 static gid_t
80 parse_group (const char *name)
82 gid_t gid = -1;
84 if (*name)
86 struct group *grp = getgrnam (name);
87 if (grp)
88 gid = grp->gr_gid;
89 else
91 unsigned long int tmp;
92 if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
93 && tmp <= GID_T_MAX))
94 error (EXIT_FAILURE, 0, _("invalid group: %s"), 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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
108 program_name);
109 else
111 printf (_("\
112 Usage: %s [OPTION]... GROUP FILE...\n\
113 or: %s [OPTION]... --reference=RFILE FILE...\n\
115 program_name, program_name);
116 fputs (_("\
117 Change the group of each FILE to GROUP.\n\
118 With --reference, change the group of each FILE to that of RFILE.\n\
120 -c, --changes like verbose but report only when a change is made\n\
121 --dereference affect the referent of each symbolic link (this is\n\
122 the default), rather than the symbolic link itself\n\
123 "), stdout);
124 fputs (_("\
125 -h, --no-dereference affect each symbolic link instead of any referenced\n\
126 file (useful only on systems that can change the\n\
127 ownership of a symlink)\n\
128 "), stdout);
129 fputs (_("\
130 --no-preserve-root do not treat `/' specially (the default)\n\
131 --preserve-root fail to operate recursively on `/'\n\
132 "), stdout);
133 fputs (_("\
134 -f, --silent, --quiet suppress most error messages\n\
135 --reference=RFILE use RFILE's group rather than specifying a\n\
136 GROUP value\n\
137 -R, --recursive operate on files and directories recursively\n\
138 -v, --verbose output a diagnostic for every file processed\n\
140 "), stdout);
141 fputs (_("\
142 The following options modify how a hierarchy is traversed when the -R\n\
143 option is also specified. If more than one is specified, only the final\n\
144 one takes effect.\n\
146 -H if a command line argument is a symbolic link\n\
147 to a directory, traverse it\n\
148 -L traverse every symbolic link to a directory\n\
149 encountered\n\
150 -P do not traverse any symbolic links (default)\n\
152 "), stdout);
153 fputs (HELP_OPTION_DESCRIPTION, stdout);
154 fputs (VERSION_OPTION_DESCRIPTION, stdout);
155 printf (_("\
157 Examples:\n\
158 %s staff /u Change the group of /u to \"staff\".\n\
159 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
161 program_name, program_name);
162 emit_bug_reporting_address ();
164 exit (status);
168 main (int argc, char **argv)
170 bool preserve_root = false;
171 gid_t gid;
173 /* Bit flags that control how fts works. */
174 int bit_flags = FTS_PHYSICAL;
176 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
177 specified. */
178 int dereference = -1;
180 struct Chown_option chopt;
181 bool ok;
182 int optc;
184 initialize_main (&argc, &argv);
185 program_name = argv[0];
186 setlocale (LC_ALL, "");
187 bindtextdomain (PACKAGE, LOCALEDIR);
188 textdomain (PACKAGE);
190 atexit (close_stdout);
192 chopt_init (&chopt);
194 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
195 != -1)
197 switch (optc)
199 case 'H': /* Traverse command-line symlinks-to-directories. */
200 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
201 break;
203 case 'L': /* Traverse all symlinks-to-directories. */
204 bit_flags = FTS_LOGICAL;
205 break;
207 case 'P': /* Traverse no symlinks-to-directories. */
208 bit_flags = FTS_PHYSICAL;
209 break;
211 case 'h': /* --no-dereference: affect symlinks */
212 dereference = 0;
213 break;
215 case DEREFERENCE_OPTION: /* --dereference: affect the referent
216 of each symlink */
217 dereference = 1;
218 break;
220 case NO_PRESERVE_ROOT:
221 preserve_root = false;
222 break;
224 case PRESERVE_ROOT:
225 preserve_root = true;
226 break;
228 case REFERENCE_FILE_OPTION:
229 reference_file = optarg;
230 break;
232 case 'R':
233 chopt.recurse = true;
234 break;
236 case 'c':
237 chopt.verbosity = V_changes_only;
238 break;
240 case 'f':
241 chopt.force_silent = true;
242 break;
244 case 'v':
245 chopt.verbosity = V_high;
246 break;
248 case_GETOPT_HELP_CHAR;
249 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
250 default:
251 usage (EXIT_FAILURE);
255 if (chopt.recurse)
257 if (bit_flags == FTS_PHYSICAL)
259 if (dereference == 1)
260 error (EXIT_FAILURE, 0,
261 _("-R --dereference requires either -H or -L"));
262 dereference = 0;
265 else
267 bit_flags = FTS_PHYSICAL;
269 chopt.affect_symlink_referent = (dereference != 0);
271 if (argc - optind < (reference_file ? 1 : 2))
273 if (argc <= optind)
274 error (0, 0, _("missing operand"));
275 else
276 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
277 usage (EXIT_FAILURE);
280 if (reference_file)
282 struct stat ref_stats;
283 if (stat (reference_file, &ref_stats))
284 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
285 quote (reference_file));
287 gid = ref_stats.st_gid;
288 chopt.group_name = gid_to_name (ref_stats.st_gid);
290 else
292 char *group_name = argv[optind++];
293 chopt.group_name = (*group_name ? group_name : NULL);
294 gid = parse_group (group_name);
297 if (chopt.recurse & preserve_root)
299 static struct dev_ino dev_ino_buf;
300 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
301 if (chopt.root_dev_ino == NULL)
302 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
303 quote ("/"));
306 ok = chown_files (argv + optind, bit_flags,
307 (uid_t) -1, gid,
308 (uid_t) -1, (gid_t) -1, &chopt);
310 chopt_free (&chopt);
312 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);