* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / chgrp.c
blobe62e4917a1f749f311396a7f2c03a3d602e452da
1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2006 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <grp.h>
24 #include <getopt.h>
26 #include "system.h"
27 #include "chown-core.h"
28 #include "error.h"
29 #include "fts_.h"
30 #include "group-member.h"
31 #include "lchown.h"
32 #include "quote.h"
33 #include "root-dev-ino.h"
34 #include "xstrtol.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "chgrp"
39 #define AUTHORS "David MacKenzie", "Jim Meyering"
41 #if ! HAVE_ENDGRENT
42 # define endgrent() ((void) 0)
43 #endif
45 /* The name the program was run with. */
46 char *program_name;
48 /* The argument to the --reference option. Use the group ID of this file.
49 This file must exist. */
50 static char *reference_file;
52 /* For long options that have no equivalent short option, use a
53 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
54 enum
56 DEREFERENCE_OPTION = CHAR_MAX + 1,
57 NO_PRESERVE_ROOT,
58 PRESERVE_ROOT,
59 REFERENCE_FILE_OPTION
62 static struct option const long_options[] =
64 {"recursive", no_argument, NULL, 'R'},
65 {"changes", no_argument, NULL, 'c'},
66 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
67 {"no-dereference", no_argument, NULL, 'h'},
68 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
69 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
70 {"quiet", no_argument, NULL, 'f'},
71 {"silent", no_argument, NULL, 'f'},
72 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
73 {"verbose", no_argument, NULL, 'v'},
74 {GETOPT_HELP_OPTION_DECL},
75 {GETOPT_VERSION_OPTION_DECL},
76 {NULL, 0, NULL, 0}
79 /* Return the group ID of NAME, or -1 if no name was specified. */
81 static gid_t
82 parse_group (const char *name)
84 gid_t gid = -1;
86 if (*name)
88 struct group *grp = getgrnam (name);
89 if (grp)
90 gid = grp->gr_gid;
91 else
93 unsigned long int tmp;
94 if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
95 && tmp <= GID_T_MAX))
96 error (EXIT_FAILURE, 0, _("invalid group %s"), quote (name));
97 gid = tmp;
99 endgrent (); /* Save a file descriptor. */
102 return gid;
105 void
106 usage (int status)
108 if (status != EXIT_SUCCESS)
109 fprintf (stderr, _("Try `%s --help' for more information.\n"),
110 program_name);
111 else
113 printf (_("\
114 Usage: %s [OPTION]... GROUP FILE...\n\
115 or: %s [OPTION]... --reference=RFILE FILE...\n\
117 program_name, program_name);
118 fputs (_("\
119 Change the group of each FILE to GROUP.\n\
120 With --reference, change the group of each FILE to that of RFILE.\n\
122 -c, --changes like verbose but report only when a change is made\n\
123 --dereference affect the referent of each symbolic link (this is\n\
124 the default), rather than the symbolic link itself\n\
125 "), stdout);
126 fputs (_("\
127 -h, --no-dereference affect each symbolic link instead of any referenced\n\
128 file (useful only on systems that can change the\n\
129 ownership of a symlink)\n\
130 "), stdout);
131 fputs (_("\
132 --no-preserve-root do not treat `/' specially (the default)\n\
133 --preserve-root fail to operate recursively on `/'\n\
134 "), stdout);
135 fputs (_("\
136 -f, --silent, --quiet suppress most error messages\n\
137 --reference=RFILE use RFILE's group rather than specifying a\n\
138 GROUP value\n\
139 -R, --recursive operate on files and directories recursively\n\
140 -v, --verbose output a diagnostic for every file processed\n\
142 "), stdout);
143 fputs (_("\
144 The following options modify how a hierarchy is traversed when the -R\n\
145 option is also specified. If more than one is specified, only the final\n\
146 one takes effect.\n\
148 -H if a command line argument is a symbolic link\n\
149 to a directory, traverse it\n\
150 -L traverse every symbolic link to a directory\n\
151 encountered\n\
152 -P do not traverse any symbolic links (default)\n\
154 "), stdout);
155 fputs (HELP_OPTION_DESCRIPTION, stdout);
156 fputs (VERSION_OPTION_DESCRIPTION, stdout);
157 printf (_("\
159 Examples:\n\
160 %s staff /u Change the group of /u to \"staff\".\n\
161 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
163 program_name, program_name);
164 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
166 exit (status);
170 main (int argc, char **argv)
172 bool preserve_root = false;
173 gid_t gid;
175 /* Bit flags that control how fts works. */
176 int bit_flags = FTS_PHYSICAL;
178 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
179 specified. */
180 int dereference = -1;
182 struct Chown_option chopt;
183 bool ok;
184 int optc;
186 initialize_main (&argc, &argv);
187 program_name = argv[0];
188 setlocale (LC_ALL, "");
189 bindtextdomain (PACKAGE, LOCALEDIR);
190 textdomain (PACKAGE);
192 atexit (close_stdout);
194 chopt_init (&chopt);
196 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
197 != -1)
199 switch (optc)
201 case 'H': /* Traverse command-line symlinks-to-directories. */
202 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
203 break;
205 case 'L': /* Traverse all symlinks-to-directories. */
206 bit_flags = FTS_LOGICAL;
207 break;
209 case 'P': /* Traverse no symlinks-to-directories. */
210 bit_flags = FTS_PHYSICAL;
211 break;
213 case 'h': /* --no-dereference: affect symlinks */
214 dereference = 0;
215 break;
217 case DEREFERENCE_OPTION: /* --dereference: affect the referent
218 of each symlink */
219 dereference = 1;
220 break;
222 case NO_PRESERVE_ROOT:
223 preserve_root = false;
224 break;
226 case PRESERVE_ROOT:
227 preserve_root = true;
228 break;
230 case REFERENCE_FILE_OPTION:
231 reference_file = optarg;
232 break;
234 case 'R':
235 chopt.recurse = true;
236 break;
238 case 'c':
239 chopt.verbosity = V_changes_only;
240 break;
242 case 'f':
243 chopt.force_silent = true;
244 break;
246 case 'v':
247 chopt.verbosity = V_high;
248 break;
250 case_GETOPT_HELP_CHAR;
251 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
252 default:
253 usage (EXIT_FAILURE);
257 if (chopt.recurse)
259 if (bit_flags == FTS_PHYSICAL)
261 if (dereference == 1)
262 error (EXIT_FAILURE, 0,
263 _("-R --dereference requires either -H or -L"));
264 chopt.affect_symlink_referent = false;
266 else
268 if (dereference == 0)
269 error (EXIT_FAILURE, 0, _("-R -h requires -P"));
270 chopt.affect_symlink_referent = true;
273 else
275 bit_flags = FTS_PHYSICAL;
276 chopt.affect_symlink_referent = (dereference != 0);
279 if (argc - optind < (reference_file ? 1 : 2))
281 if (argc <= optind)
282 error (0, 0, _("missing operand"));
283 else
284 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
285 usage (EXIT_FAILURE);
288 if (reference_file)
290 struct stat ref_stats;
291 if (stat (reference_file, &ref_stats))
292 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
293 quote (reference_file));
295 gid = ref_stats.st_gid;
296 chopt.group_name = gid_to_name (ref_stats.st_gid);
298 else
300 char *group_name = argv[optind++];
301 chopt.group_name = (*group_name ? group_name : NULL);
302 gid = parse_group (group_name);
305 if (chopt.recurse & preserve_root)
307 static struct dev_ino dev_ino_buf;
308 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
309 if (chopt.root_dev_ino == NULL)
310 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
311 quote ("/"));
314 ok = chown_files (argv + optind, bit_flags,
315 (uid_t) -1, gid,
316 (uid_t) -1, (gid_t) -1, &chopt);
318 chopt_free (&chopt);
320 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);