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