tests: exercise tests new "==" operator
[coreutils.git] / src / chgrp.c
blob6820bdbd1ac01fb106935e438efd8adbfa2c1e2f
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 "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 \
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 error (EXIT_FAILURE, 0, _("invalid group: %s"), 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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
107 program_name);
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 -c, --changes like verbose but report only when a change is made\n\
120 --dereference affect the referent of each symbolic link (this is\n\
121 the default), rather than the symbolic link itself\n\
122 "), stdout);
123 fputs (_("\
124 -h, --no-dereference affect each symbolic link instead of any referenced\n\
125 file (useful only on systems that can change the\n\
126 ownership of a symlink)\n\
127 "), stdout);
128 fputs (_("\
129 --no-preserve-root do not treat `/' specially (the default)\n\
130 --preserve-root fail to operate recursively on `/'\n\
131 "), stdout);
132 fputs (_("\
133 -f, --silent, --quiet suppress most error messages\n\
134 --reference=RFILE use RFILE's group rather than specifying a\n\
135 GROUP value\n\
136 -R, --recursive operate on files and directories recursively\n\
137 -v, --verbose output a diagnostic for every file processed\n\
139 "), stdout);
140 fputs (_("\
141 The following options modify how a hierarchy is traversed when the -R\n\
142 option is also specified. If more than one is specified, only the final\n\
143 one takes effect.\n\
145 -H if a command line argument is a symbolic link\n\
146 to a directory, traverse it\n\
147 -L traverse every symbolic link to a directory\n\
148 encountered\n\
149 -P do not traverse any symbolic links (default)\n\
151 "), stdout);
152 fputs (HELP_OPTION_DESCRIPTION, stdout);
153 fputs (VERSION_OPTION_DESCRIPTION, stdout);
154 printf (_("\
156 Examples:\n\
157 %s staff /u Change the group of /u to \"staff\".\n\
158 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
160 program_name, program_name);
161 emit_ancillary_info ();
163 exit (status);
167 main (int argc, char **argv)
169 bool preserve_root = false;
170 gid_t gid;
172 /* Bit flags that control how fts works. */
173 int bit_flags = FTS_PHYSICAL;
175 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
176 specified. */
177 int dereference = -1;
179 struct Chown_option chopt;
180 bool ok;
181 int optc;
183 initialize_main (&argc, &argv);
184 set_program_name (argv[0]);
185 setlocale (LC_ALL, "");
186 bindtextdomain (PACKAGE, LOCALEDIR);
187 textdomain (PACKAGE);
189 atexit (close_stdout);
191 chopt_init (&chopt);
193 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
194 != -1)
196 switch (optc)
198 case 'H': /* Traverse command-line symlinks-to-directories. */
199 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
200 break;
202 case 'L': /* Traverse all symlinks-to-directories. */
203 bit_flags = FTS_LOGICAL;
204 break;
206 case 'P': /* Traverse no symlinks-to-directories. */
207 bit_flags = FTS_PHYSICAL;
208 break;
210 case 'h': /* --no-dereference: affect symlinks */
211 dereference = 0;
212 break;
214 case DEREFERENCE_OPTION: /* --dereference: affect the referent
215 of each symlink */
216 dereference = 1;
217 break;
219 case NO_PRESERVE_ROOT:
220 preserve_root = false;
221 break;
223 case PRESERVE_ROOT:
224 preserve_root = true;
225 break;
227 case REFERENCE_FILE_OPTION:
228 reference_file = optarg;
229 break;
231 case 'R':
232 chopt.recurse = true;
233 break;
235 case 'c':
236 chopt.verbosity = V_changes_only;
237 break;
239 case 'f':
240 chopt.force_silent = true;
241 break;
243 case 'v':
244 chopt.verbosity = V_high;
245 break;
247 case_GETOPT_HELP_CHAR;
248 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
249 default:
250 usage (EXIT_FAILURE);
254 if (chopt.recurse)
256 if (bit_flags == FTS_PHYSICAL)
258 if (dereference == 1)
259 error (EXIT_FAILURE, 0,
260 _("-R --dereference requires either -H or -L"));
261 dereference = 0;
264 else
266 bit_flags = FTS_PHYSICAL;
268 chopt.affect_symlink_referent = (dereference != 0);
270 if (argc - optind < (reference_file ? 1 : 2))
272 if (argc <= optind)
273 error (0, 0, _("missing operand"));
274 else
275 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
276 usage (EXIT_FAILURE);
279 if (reference_file)
281 struct stat ref_stats;
282 if (stat (reference_file, &ref_stats))
283 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
284 quote (reference_file));
286 gid = ref_stats.st_gid;
287 chopt.group_name = gid_to_name (ref_stats.st_gid);
289 else
291 char *group_name = argv[optind++];
292 chopt.group_name = (*group_name ? group_name : NULL);
293 gid = parse_group (group_name);
296 if (chopt.recurse && preserve_root)
298 static struct dev_ino dev_ino_buf;
299 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
300 if (chopt.root_dev_ino == NULL)
301 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
302 quote ("/"));
305 bit_flags |= FTS_DEFER_STAT;
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);