1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2008 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>. */
21 #include <sys/types.h>
26 #include "chown-core.h"
29 #include "group-member.h"
31 #include "root-dev-ino.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "chgrp"
38 proper_name ("David MacKenzie"), \
39 proper_name ("Jim Meyering")
42 # define endgrent() ((void) 0)
45 /* The name the program was run with. */
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. */
56 DEREFERENCE_OPTION = CHAR_MAX + 1,
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},
79 /* Return the group ID of NAME, or -1 if no name was specified. */
82 parse_group (const char *name)
88 struct group *grp = getgrnam (name);
93 unsigned long int tmp;
94 if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
96 error (EXIT_FAILURE, 0, _("invalid group: %s"), quote (name));
99 endgrent (); /* Save a file descriptor. */
108 if (status != EXIT_SUCCESS)
109 fprintf (stderr, _("Try `%s --help' for more information.\n"),
114 Usage: %s [OPTION]... GROUP FILE...\n\
115 or: %s [OPTION]... --reference=RFILE FILE...\n\
117 program_name, program_name);
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\
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\
132 --no-preserve-root do not treat `/' specially (the default)\n\
133 --preserve-root fail to operate recursively on `/'\n\
136 -f, --silent, --quiet suppress most error messages\n\
137 --reference=RFILE use RFILE's group rather than specifying a\n\
139 -R, --recursive operate on files and directories recursively\n\
140 -v, --verbose output a diagnostic for every file processed\n\
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\
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\
152 -P do not traverse any symbolic links (default)\n\
155 fputs (HELP_OPTION_DESCRIPTION, stdout);
156 fputs (VERSION_OPTION_DESCRIPTION, stdout);
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 emit_bug_reporting_address ();
170 main (int argc, char **argv)
172 bool preserve_root = false;
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
180 int dereference = -1;
182 struct Chown_option chopt;
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);
196 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
201 case 'H': /* Traverse command-line symlinks-to-directories. */
202 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
205 case 'L': /* Traverse all symlinks-to-directories. */
206 bit_flags = FTS_LOGICAL;
209 case 'P': /* Traverse no symlinks-to-directories. */
210 bit_flags = FTS_PHYSICAL;
213 case 'h': /* --no-dereference: affect symlinks */
217 case DEREFERENCE_OPTION: /* --dereference: affect the referent
222 case NO_PRESERVE_ROOT:
223 preserve_root = false;
227 preserve_root = true;
230 case REFERENCE_FILE_OPTION:
231 reference_file = optarg;
235 chopt.recurse = true;
239 chopt.verbosity = V_changes_only;
243 chopt.force_silent = true;
247 chopt.verbosity = V_high;
250 case_GETOPT_HELP_CHAR;
251 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
253 usage (EXIT_FAILURE);
259 if (bit_flags == FTS_PHYSICAL)
261 if (dereference == 1)
262 error (EXIT_FAILURE, 0,
263 _("-R --dereference requires either -H or -L"));
269 bit_flags = FTS_PHYSICAL;
271 chopt.affect_symlink_referent = (dereference != 0);
273 if (argc - optind < (reference_file ? 1 : 2))
276 error (0, 0, _("missing operand"));
278 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
279 usage (EXIT_FAILURE);
284 struct stat ref_stats;
285 if (stat (reference_file, &ref_stats))
286 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
287 quote (reference_file));
289 gid = ref_stats.st_gid;
290 chopt.group_name = gid_to_name (ref_stats.st_gid);
294 char *group_name = argv[optind++];
295 chopt.group_name = (*group_name ? group_name : NULL);
296 gid = parse_group (group_name);
299 if (chopt.recurse & preserve_root)
301 static struct dev_ino dev_ino_buf;
302 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
303 if (chopt.root_dev_ino == NULL)
304 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
308 ok = chown_files (argv + optind, bit_flags,
310 (uid_t) -1, (gid_t) -1, &chopt);
314 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);