Update FSF postal mail address.
[platform/upstream/coreutils.git] / src / chgrp.c
1 /* chgrp -- change group ownership of files
2    Copyright (C) 89, 90, 91, 1995-2005 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 02111-1307, 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 "xstrtol.h"
34
35 /* The official name of this program (e.g., no `g' prefix).  */
36 #define PROGRAM_NAME "chgrp"
37
38 #define AUTHORS "David MacKenzie", "Jim Meyering"
39
40 #ifndef _POSIX_VERSION
41 struct group *getgrnam ();
42 #endif
43
44 #if ! HAVE_ENDGRENT
45 # define endgrent() ((void) 0)
46 #endif
47
48 /* The name the program was run with. */
49 char *program_name;
50
51 /* The argument to the --reference option.  Use the group ID of this file.
52    This file must exist.  */
53 static char *reference_file;
54
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.  */
57 enum
58 {
59   DEREFERENCE_OPTION = CHAR_MAX + 1,
60   REFERENCE_FILE_OPTION
61 };
62
63 static struct option const long_options[] =
64 {
65   {"recursive", no_argument, NULL, 'R'},
66   {"changes", no_argument, NULL, 'c'},
67   {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
68   {"no-dereference", no_argument, NULL, 'h'},
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},
75   {NULL, 0, NULL, 0}
76 };
77
78 /* Return the group ID of NAME, or -1 if no name was specified.  */
79
80 static gid_t
81 parse_group (const char *name)
82 {
83   gid_t gid = -1;
84
85   if (*name)
86     {
87       struct group *grp = getgrnam (name);
88       if (grp)
89         gid = grp->gr_gid;
90       else
91         {
92           unsigned long int tmp;
93           if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
94                  && tmp <= GID_T_MAX))
95             error (EXIT_FAILURE, 0, _("invalid group %s"), quote (name));
96           gid = tmp;
97         }
98       endgrent ();              /* Save a file descriptor. */
99     }
100
101   return gid;
102 }
103
104 void
105 usage (int status)
106 {
107   if (status != EXIT_SUCCESS)
108     fprintf (stderr, _("Try `%s --help' for more information.\n"),
109              program_name);
110   else
111     {
112       printf (_("\
113 Usage: %s [OPTION]... GROUP FILE...\n\
114   or:  %s [OPTION]... --reference=RFILE FILE...\n\
115 "),
116               program_name, program_name);
117       fputs (_("\
118 Change the group of each FILE to GROUP.\n\
119 With --reference, change the group of each FILE to that of RFILE.\n\
120 \n\
121   -c, --changes          like verbose but report only when a change is made\n\
122       --dereference      affect the referent of each symbolic link, rather\n\
123                          than the symbolic link itself (this is the default)\n\
124 "), stdout);
125       fputs (_("\
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\
129 "), stdout);
130       fputs (_("\
131       --no-preserve-root do not treat `/' specially (the default)\n\
132       --preserve-root    fail to operate recursively on `/'\n\
133 "), stdout);
134       fputs (_("\
135   -f, --silent, --quiet  suppress most error messages\n\
136       --reference=RFILE  use RFILE's group rather than the specifying\n\
137                          GROUP value\n\
138   -R, --recursive        operate on files and directories recursively\n\
139   -v, --verbose          output a diagnostic for every file processed\n\
140 \n\
141 "), stdout);
142       fputs (_("\
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\
145 one takes effect.\n\
146 \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\
150                          encountered\n\
151   -P                     do not traverse any symbolic links (default)\n\
152 \n\
153 "), stdout);
154       fputs (HELP_OPTION_DESCRIPTION, stdout);
155       fputs (VERSION_OPTION_DESCRIPTION, stdout);
156       printf (_("\
157 \n\
158 Examples:\n\
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\
161 "),
162               program_name, program_name);
163       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
164     }
165   exit (status);
166 }
167
168 int
169 main (int argc, char **argv)
170 {
171   gid_t gid;
172
173   /* Bit flags that control how fts works.  */
174   int bit_flags = FTS_PHYSICAL;
175
176   /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
177      specified.  */
178   int dereference = -1;
179
180   struct Chown_option chopt;
181   bool ok;
182   int optc;
183
184   initialize_main (&argc, &argv);
185   program_name = argv[0];
186   setlocale (LC_ALL, "");
187   bindtextdomain (PACKAGE, LOCALEDIR);
188   textdomain (PACKAGE);
189
190   atexit (close_stdout);
191
192   chopt_init (&chopt);
193
194   while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
195          != -1)
196     {
197       switch (optc)
198         {
199         case 'H': /* Traverse command-line symlinks-to-directories.  */
200           bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
201           break;
202
203         case 'L': /* Traverse all symlinks-to-directories.  */
204           bit_flags = FTS_LOGICAL;
205           break;
206
207         case 'P': /* Traverse no symlinks-to-directories.  */
208           bit_flags = FTS_PHYSICAL;
209           break;
210
211         case 'h': /* --no-dereference: affect symlinks */
212           dereference = 0;
213           break;
214
215         case DEREFERENCE_OPTION: /* --dereference: affect the referent
216                                     of each symlink */
217           dereference = 1;
218           break;
219
220         case REFERENCE_FILE_OPTION:
221           reference_file = optarg;
222           break;
223
224         case 'R':
225           chopt.recurse = true;
226           break;
227
228         case 'c':
229           chopt.verbosity = V_changes_only;
230           break;
231
232         case 'f':
233           chopt.force_silent = true;
234           break;
235
236         case 'v':
237           chopt.verbosity = V_high;
238           break;
239
240         case_GETOPT_HELP_CHAR;
241         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
242         default:
243           usage (EXIT_FAILURE);
244         }
245     }
246
247   if (chopt.recurse)
248     {
249       if (bit_flags == FTS_PHYSICAL)
250         {
251           if (dereference == 1)
252             error (EXIT_FAILURE, 0,
253                    _("-R --dereference requires either -H or -L"));
254           chopt.affect_symlink_referent = false;
255         }
256       else
257         {
258           if (dereference == 0)
259             error (EXIT_FAILURE, 0, _("-R -h requires -P"));
260           chopt.affect_symlink_referent = true;
261         }
262     }
263   else
264     {
265       bit_flags = FTS_PHYSICAL;
266       chopt.affect_symlink_referent = (dereference != 0);
267     }
268
269   if (argc - optind < (reference_file ? 1 : 2))
270     {
271       if (argc <= optind)
272         error (0, 0, _("missing operand"));
273       else
274         error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
275       usage (EXIT_FAILURE);
276     }
277
278   if (reference_file)
279     {
280       struct stat ref_stats;
281       if (stat (reference_file, &ref_stats))
282         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
283                quote (reference_file));
284
285       gid = ref_stats.st_gid;
286       chopt.group_name = gid_to_name (ref_stats.st_gid);
287     }
288   else
289     {
290       char *group_name = argv[optind++];
291       chopt.group_name = (*group_name ? group_name : NULL);
292       gid = parse_group (group_name);
293     }
294
295   ok = chown_files (argv + optind, bit_flags,
296                     (uid_t) -1, gid,
297                     (uid_t) -1, (gid_t) -1, &chopt);
298
299   chopt_free (&chopt);
300
301   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
302 }