*** empty log message ***
[platform/upstream/coreutils.git] / src / chgrp.c
1 /* chgrp -- change group ownership of files
2    Copyright (C) 89, 90, 91, 1995-2006 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <grp.h>
24 #include <getopt.h>
25
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"
35
36 /* The official name of this program (e.g., no `g' prefix).  */
37 #define PROGRAM_NAME "chgrp"
38
39 #define AUTHORS "David MacKenzie", "Jim Meyering"
40
41 #if ! HAVE_ENDGRENT
42 # define endgrent() ((void) 0)
43 #endif
44
45 /* The name the program was run with. */
46 char *program_name;
47
48 /* The argument to the --reference option.  Use the group ID of this file.
49    This file must exist.  */
50 static char *reference_file;
51
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
55 {
56   DEREFERENCE_OPTION = CHAR_MAX + 1,
57   NO_PRESERVE_ROOT,
58   PRESERVE_ROOT,
59   REFERENCE_FILE_OPTION
60 };
61
62 static struct option const long_options[] =
63 {
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}
77 };
78
79 /* Return the group ID of NAME, or -1 if no name was specified.  */
80
81 static gid_t
82 parse_group (const char *name)
83 {
84   gid_t gid = -1;
85
86   if (*name)
87     {
88       struct group *grp = getgrnam (name);
89       if (grp)
90         gid = grp->gr_gid;
91       else
92         {
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;
98         }
99       endgrent ();              /* Save a file descriptor. */
100     }
101
102   return gid;
103 }
104
105 void
106 usage (int status)
107 {
108   if (status != EXIT_SUCCESS)
109     fprintf (stderr, _("Try `%s --help' for more information.\n"),
110              program_name);
111   else
112     {
113       printf (_("\
114 Usage: %s [OPTION]... GROUP FILE...\n\
115   or:  %s [OPTION]... --reference=RFILE FILE...\n\
116 "),
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\
121 \n\
122   -c, --changes          like verbose but report only when a change is made\n\
123       --dereference      affect the referent of each symbolic link, rather\n\
124                          than the symbolic link itself (this is the default)\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 the specifying\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\
141 \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\
147 \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\
153 \n\
154 "), stdout);
155       fputs (HELP_OPTION_DESCRIPTION, stdout);
156       fputs (VERSION_OPTION_DESCRIPTION, stdout);
157       printf (_("\
158 \n\
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\
162 "),
163               program_name, program_name);
164       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
165     }
166   exit (status);
167 }
168
169 int
170 main (int argc, char **argv)
171 {
172   bool preserve_root = false;
173   gid_t gid;
174
175   /* Bit flags that control how fts works.  */
176   int bit_flags = FTS_PHYSICAL;
177
178   /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
179      specified.  */
180   int dereference = -1;
181
182   struct Chown_option chopt;
183   bool ok;
184   int optc;
185
186   initialize_main (&argc, &argv);
187   program_name = argv[0];
188   setlocale (LC_ALL, "");
189   bindtextdomain (PACKAGE, LOCALEDIR);
190   textdomain (PACKAGE);
191
192   atexit (close_stdout);
193
194   chopt_init (&chopt);
195
196   while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
197          != -1)
198     {
199       switch (optc)
200         {
201         case 'H': /* Traverse command-line symlinks-to-directories.  */
202           bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
203           break;
204
205         case 'L': /* Traverse all symlinks-to-directories.  */
206           bit_flags = FTS_LOGICAL;
207           break;
208
209         case 'P': /* Traverse no symlinks-to-directories.  */
210           bit_flags = FTS_PHYSICAL;
211           break;
212
213         case 'h': /* --no-dereference: affect symlinks */
214           dereference = 0;
215           break;
216
217         case DEREFERENCE_OPTION: /* --dereference: affect the referent
218                                     of each symlink */
219           dereference = 1;
220           break;
221
222         case NO_PRESERVE_ROOT:
223           preserve_root = false;
224           break;
225
226         case PRESERVE_ROOT:
227           preserve_root = true;
228           break;
229
230         case REFERENCE_FILE_OPTION:
231           reference_file = optarg;
232           break;
233
234         case 'R':
235           chopt.recurse = true;
236           break;
237
238         case 'c':
239           chopt.verbosity = V_changes_only;
240           break;
241
242         case 'f':
243           chopt.force_silent = true;
244           break;
245
246         case 'v':
247           chopt.verbosity = V_high;
248           break;
249
250         case_GETOPT_HELP_CHAR;
251         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
252         default:
253           usage (EXIT_FAILURE);
254         }
255     }
256
257   if (chopt.recurse)
258     {
259       if (bit_flags == FTS_PHYSICAL)
260         {
261           if (dereference == 1)
262             error (EXIT_FAILURE, 0,
263                    _("-R --dereference requires either -H or -L"));
264           chopt.affect_symlink_referent = false;
265         }
266       else
267         {
268           if (dereference == 0)
269             error (EXIT_FAILURE, 0, _("-R -h requires -P"));
270           chopt.affect_symlink_referent = true;
271         }
272     }
273   else
274     {
275       bit_flags = FTS_PHYSICAL;
276       chopt.affect_symlink_referent = (dereference != 0);
277     }
278
279   if (argc - optind < (reference_file ? 1 : 2))
280     {
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);
286     }
287
288   if (reference_file)
289     {
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));
294
295       gid = ref_stats.st_gid;
296       chopt.group_name = gid_to_name (ref_stats.st_gid);
297     }
298   else
299     {
300       char *group_name = argv[optind++];
301       chopt.group_name = (*group_name ? group_name : NULL);
302       gid = parse_group (group_name);
303     }
304
305   if (chopt.recurse & preserve_root)
306     {
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 ("/"));
312     }
313
314   ok = chown_files (argv + optind, bit_flags,
315                     (uid_t) -1, gid,
316                     (uid_t) -1, (gid_t) -1, &chopt);
317
318   chopt_free (&chopt);
319
320   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
321 }