dc731a26f6bc4a791044e5eaa3e745821d349b0c
[platform/upstream/coreutils.git] / src / chgrp.c
1 /* chgrp -- change group ownership of files
2    Copyright (C) 89, 90, 91, 1995-2003 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., 59 Temple Place - Suite 330, 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 WRITTEN_BY _("Written by David MacKenzie and 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, 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},
75   {0, 0, 0, 0}
76 };
77
78 /* Set *G according to NAME. */
79
80 static void
81 parse_group (const char *name, gid_t *g)
82 {
83   struct group *grp;
84
85   if (*name == '\0')
86     error (EXIT_FAILURE, 0, _("cannot change to null group"));
87
88   grp = getgrnam (name);
89   if (grp == NULL)
90     {
91       strtol_error s_err;
92       unsigned long int tmp_long;
93
94       if (!ISDIGIT (*name))
95         error (EXIT_FAILURE, 0, _("invalid group name %s"), quote (name));
96
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);
100
101       if (tmp_long > GID_T_MAX)
102         error (EXIT_FAILURE, 0, _("invalid group number %s"), quote (name));
103
104       *g = tmp_long;
105     }
106   else
107     *g = grp->gr_gid;
108   endgrent ();          /* Save a file descriptor. */
109 }
110
111 void
112 usage (int status)
113 {
114   if (status != 0)
115     fprintf (stderr, _("Try `%s --help' for more information.\n"),
116              program_name);
117   else
118     {
119       printf (_("\
120 Usage: %s [OPTION]... GROUP FILE...\n\
121   or:  %s [OPTION]... --reference=RFILE FILE...\n\
122 "),
123               program_name, program_name);
124       fputs (_("\
125 Change the group of each FILE to GROUP.\n\
126 With --reference, change the group of each FILE to that of RFILE.\n\
127 \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\n\
131 "), stdout);
132       fputs (_("\
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\
136 "), stdout);
137       fputs (_("\
138   -f, --silent, --quiet  suppress most error messages\n\
139       --reference=RFILE  use RFILE's group rather than the specifying\n\
140                          GROUP value\n\
141   -R, --recursive        operate on files and directories recursively\n\
142   -v, --verbose          output a diagnostic for every file processed\n\
143 \n\
144 "), stdout);
145       fputs (_("\
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 (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
160     }
161   exit (status);
162 }
163
164 int
165 main (int argc, char **argv)
166 {
167   gid_t gid;
168   /* Bit flags that control how fts works.  */
169   int bit_flags = FTS_PHYSICAL;
170   struct Chown_option chopt;
171   int fail = 0;
172   int optc;
173
174   initialize_main (&argc, &argv);
175   program_name = argv[0];
176   setlocale (LC_ALL, "");
177   bindtextdomain (PACKAGE, LOCALEDIR);
178   textdomain (PACKAGE);
179
180   atexit (close_stdout);
181
182   chopt_init (&chopt);
183
184   while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
185          != -1)
186     {
187       switch (optc)
188         {
189         case 0:
190           break;
191
192         case 'H': /* Traverse command-line symlinks-to-directories.  */
193           bit_flags = FTS_COMFOLLOW;
194           break;
195
196         case 'L': /* Traverse all symlinks-to-directories.  */
197           bit_flags = FTS_LOGICAL;
198           break;
199
200         case 'P': /* Traverse no symlinks-to-directories.  */
201           bit_flags = FTS_PHYSICAL;
202           break;
203
204         case 'h': /* --no-dereference: affect symlinks */
205           chopt.affect_symlink_referent = false;
206           break;
207
208         case DEREFERENCE_OPTION: /* --dereference: affect the referent
209                                     of each symlink */
210           chopt.affect_symlink_referent = true;
211           break;
212
213         case REFERENCE_FILE_OPTION:
214           reference_file = optarg;
215           break;
216
217         case 'R':
218           chopt.recurse = true;
219           break;
220
221         case 'c':
222           chopt.verbosity = V_changes_only;
223           break;
224
225         case 'f':
226           chopt.force_silent = true;
227           break;
228
229         case 'v':
230           chopt.verbosity = V_high;
231           break;
232
233         case_GETOPT_HELP_CHAR;
234         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
235         default:
236           usage (EXIT_FAILURE);
237         }
238     }
239
240   if (argc - optind + (reference_file ? 1 : 0) <= 1)
241     {
242       error (0, 0, _("too few arguments"));
243       usage (EXIT_FAILURE);
244     }
245
246   if (reference_file)
247     {
248       struct stat ref_stats;
249       if (stat (reference_file, &ref_stats))
250         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
251                quote (reference_file));
252
253       chopt.group_name = gid_to_name (ref_stats.st_gid);
254       gid = ref_stats.st_gid;
255     }
256   else
257     {
258       chopt.group_name = argv[optind++];
259       parse_group (chopt.group_name, &gid);
260     }
261
262   fail = chown_files (argv + optind, bit_flags,
263                       (uid_t) -1, gid,
264                       (uid_t) -1, (gid_t) -1, &chopt);
265
266   chopt_free (&chopt);
267
268   exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);
269 }