1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "chgrp"
38 #define AUTHORS "David MacKenzie", "Jim Meyering"
40 #ifndef _POSIX_VERSION
41 struct group *getgrnam ();
45 # define endgrent() ((void) 0)
48 /* The name the program was run with. */
51 /* The argument to the --reference option. Use the group ID of this file.
52 This file must exist. */
53 static char *reference_file;
55 /* For long options that have no equivalent short option, use a
56 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
59 DEREFERENCE_OPTION = CHAR_MAX + 1,
63 static struct option const long_options[] =
65 {"recursive", no_argument, 0, 'R'},
66 {"changes", no_argument, 0, 'c'},
67 {"dereference", no_argument, 0, DEREFERENCE_OPTION},
68 {"no-dereference", no_argument, 0, 'h'},
69 {"quiet", no_argument, 0, 'f'},
70 {"silent", no_argument, 0, 'f'},
71 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
72 {"verbose", no_argument, 0, 'v'},
73 {GETOPT_HELP_OPTION_DECL},
74 {GETOPT_VERSION_OPTION_DECL},
78 /* Set *G according to NAME. */
81 parse_group (const char *name, gid_t *g)
86 error (EXIT_FAILURE, 0, _("cannot change to null group"));
88 grp = getgrnam (name);
92 unsigned long int tmp_long;
95 error (EXIT_FAILURE, 0, _("invalid group name %s"), quote (name));
97 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
98 if (s_err != LONGINT_OK)
99 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
101 if (tmp_long > GID_T_MAX)
102 error (EXIT_FAILURE, 0, _("invalid group number %s"), quote (name));
108 endgrent (); /* Save a file descriptor. */
114 if (status != EXIT_SUCCESS)
115 fprintf (stderr, _("Try `%s --help' for more information.\n"),
120 Usage: %s [OPTION]... GROUP FILE...\n\
121 or: %s [OPTION]... --reference=RFILE FILE...\n\
123 program_name, program_name);
125 Change the group of each FILE to GROUP.\n\
126 With --reference, change the group of each FILE to that of RFILE.\n\
128 -c, --changes like verbose but report only when a change is made\n\
129 --dereference affect the referent of each symbolic link, rather\n\
130 than the symbolic link itself (this is the default)\n\
133 -h, --no-dereference affect each symbolic link instead of any referenced\n\
134 file (useful only on systems that can change the\n\
135 ownership of a symlink)\n\
138 --no-preserve-root do not treat `/' specially (the default)\n\
139 --preserve-root fail to operate recursively on `/'\n\
142 -f, --silent, --quiet suppress most error messages\n\
143 --reference=RFILE use RFILE's group rather than the specifying\n\
145 -R, --recursive operate on files and directories recursively\n\
146 -v, --verbose output a diagnostic for every file processed\n\
150 The following options modify how a hierarchy is traversed when the -R\n\
151 option is also specified. If more than one is specified, only the final\n\
154 -H if a command line argument is a symbolic link\n\
155 to a directory, traverse it\n\
156 -L traverse every symbolic link to a directory\n\
158 -P do not traverse any symbolic links (default)\n\
161 fputs (HELP_OPTION_DESCRIPTION, stdout);
162 fputs (VERSION_OPTION_DESCRIPTION, stdout);
163 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
169 main (int argc, char **argv)
172 /* Bit flags that control how fts works. */
173 int bit_flags = FTS_PHYSICAL;
174 struct Chown_option chopt;
178 initialize_main (&argc, &argv);
179 program_name = argv[0];
180 setlocale (LC_ALL, "");
181 bindtextdomain (PACKAGE, LOCALEDIR);
182 textdomain (PACKAGE);
184 atexit (close_stdout);
188 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
196 case 'H': /* Traverse command-line symlinks-to-directories. */
197 bit_flags = FTS_COMFOLLOW;
200 case 'L': /* Traverse all symlinks-to-directories. */
201 bit_flags = FTS_LOGICAL;
204 case 'P': /* Traverse no symlinks-to-directories. */
205 bit_flags = FTS_PHYSICAL;
208 case 'h': /* --no-dereference: affect symlinks */
209 chopt.affect_symlink_referent = false;
212 case DEREFERENCE_OPTION: /* --dereference: affect the referent
214 chopt.affect_symlink_referent = true;
217 case REFERENCE_FILE_OPTION:
218 reference_file = optarg;
222 chopt.recurse = true;
226 chopt.verbosity = V_changes_only;
230 chopt.force_silent = true;
234 chopt.verbosity = V_high;
237 case_GETOPT_HELP_CHAR;
238 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
240 usage (EXIT_FAILURE);
244 if (argc - optind + (reference_file ? 1 : 0) <= 1)
246 error (0, 0, _("too few arguments"));
247 usage (EXIT_FAILURE);
252 struct stat ref_stats;
253 if (stat (reference_file, &ref_stats))
254 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
255 quote (reference_file));
257 chopt.group_name = gid_to_name (ref_stats.st_gid);
258 gid = ref_stats.st_gid;
262 chopt.group_name = argv[optind++];
263 parse_group (chopt.group_name, &gid);
266 fail = chown_files (argv + optind, bit_flags,
268 (uid_t) -1, (gid_t) -1, &chopt);
272 exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);