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