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 2, or (at your option)
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>. */
22 #include <sys/types.h>
27 #include "chown-core.h"
30 #include "group-member.h"
32 #include "root-dev-ino.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "chgrp"
38 #define AUTHORS "David MacKenzie", "Jim Meyering"
41 # define endgrent() ((void) 0)
44 /* The name the program was run with. */
47 /* The argument to the --reference option. Use the group ID of this file.
48 This file must exist. */
49 static char *reference_file;
51 /* For long options that have no equivalent short option, use a
52 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
55 DEREFERENCE_OPTION = CHAR_MAX + 1,
61 static struct option const long_options[] =
63 {"recursive", no_argument, NULL, 'R'},
64 {"changes", no_argument, NULL, 'c'},
65 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
66 {"no-dereference", no_argument, NULL, 'h'},
67 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
68 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
69 {"quiet", no_argument, NULL, 'f'},
70 {"silent", no_argument, NULL, 'f'},
71 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
72 {"verbose", no_argument, NULL, 'v'},
73 {GETOPT_HELP_OPTION_DECL},
74 {GETOPT_VERSION_OPTION_DECL},
78 /* Return the group ID of NAME, or -1 if no name was specified. */
81 parse_group (const char *name)
87 struct group *grp = getgrnam (name);
92 unsigned long int tmp;
93 if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
95 error (EXIT_FAILURE, 0, _("invalid group: %s"), quote (name));
98 endgrent (); /* Save a file descriptor. */
107 if (status != EXIT_SUCCESS)
108 fprintf (stderr, _("Try `%s --help' for more information.\n"),
113 Usage: %s [OPTION]... GROUP FILE...\n\
114 or: %s [OPTION]... --reference=RFILE FILE...\n\
116 program_name, program_name);
118 Change the group of each FILE to GROUP.\n\
119 With --reference, change the group of each FILE to that of RFILE.\n\
121 -c, --changes like verbose but report only when a change is made\n\
122 --dereference affect the referent of each symbolic link (this is\n\
123 the default), rather than the symbolic link itself\n\
126 -h, --no-dereference affect each symbolic link instead of any referenced\n\
127 file (useful only on systems that can change the\n\
128 ownership of a symlink)\n\
131 --no-preserve-root do not treat `/' specially (the default)\n\
132 --preserve-root fail to operate recursively on `/'\n\
135 -f, --silent, --quiet suppress most error messages\n\
136 --reference=RFILE use RFILE's group rather than specifying a\n\
138 -R, --recursive operate on files and directories recursively\n\
139 -v, --verbose output a diagnostic for every file processed\n\
143 The following options modify how a hierarchy is traversed when the -R\n\
144 option is also specified. If more than one is specified, only the final\n\
147 -H if a command line argument is a symbolic link\n\
148 to a directory, traverse it\n\
149 -L traverse every symbolic link to a directory\n\
151 -P do not traverse any symbolic links (default)\n\
154 fputs (HELP_OPTION_DESCRIPTION, stdout);
155 fputs (VERSION_OPTION_DESCRIPTION, stdout);
159 %s staff /u Change the group of /u to \"staff\".\n\
160 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
162 program_name, program_name);
163 emit_bug_reporting_address ();
169 main (int argc, char **argv)
171 bool preserve_root = false;
174 /* Bit flags that control how fts works. */
175 int bit_flags = FTS_PHYSICAL;
177 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
179 int dereference = -1;
181 struct Chown_option chopt;
185 initialize_main (&argc, &argv);
186 program_name = argv[0];
187 setlocale (LC_ALL, "");
188 bindtextdomain (PACKAGE, LOCALEDIR);
189 textdomain (PACKAGE);
191 atexit (close_stdout);
195 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
200 case 'H': /* Traverse command-line symlinks-to-directories. */
201 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
204 case 'L': /* Traverse all symlinks-to-directories. */
205 bit_flags = FTS_LOGICAL;
208 case 'P': /* Traverse no symlinks-to-directories. */
209 bit_flags = FTS_PHYSICAL;
212 case 'h': /* --no-dereference: affect symlinks */
216 case DEREFERENCE_OPTION: /* --dereference: affect the referent
221 case NO_PRESERVE_ROOT:
222 preserve_root = false;
226 preserve_root = true;
229 case REFERENCE_FILE_OPTION:
230 reference_file = optarg;
234 chopt.recurse = true;
238 chopt.verbosity = V_changes_only;
242 chopt.force_silent = true;
246 chopt.verbosity = V_high;
249 case_GETOPT_HELP_CHAR;
250 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
252 usage (EXIT_FAILURE);
258 if (bit_flags == FTS_PHYSICAL)
260 if (dereference == 1)
261 error (EXIT_FAILURE, 0,
262 _("-R --dereference requires either -H or -L"));
268 bit_flags = FTS_PHYSICAL;
270 chopt.affect_symlink_referent = (dereference != 0);
272 if (argc - optind < (reference_file ? 1 : 2))
275 error (0, 0, _("missing operand"));
277 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
278 usage (EXIT_FAILURE);
283 struct stat ref_stats;
284 if (stat (reference_file, &ref_stats))
285 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
286 quote (reference_file));
288 gid = ref_stats.st_gid;
289 chopt.group_name = gid_to_name (ref_stats.st_gid);
293 char *group_name = argv[optind++];
294 chopt.group_name = (*group_name ? group_name : NULL);
295 gid = parse_group (group_name);
298 if (chopt.recurse & preserve_root)
300 static struct dev_ino dev_ino_buf;
301 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
302 if (chopt.root_dev_ino == NULL)
303 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
307 ok = chown_files (argv + optind, bit_flags,
309 (uid_t) -1, (gid_t) -1, &chopt);
313 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);