chgrp and chown now dereference symlinks by default, per POSIX.
[platform/upstream/coreutils.git] / src / chgrp.c
1 /* chgrp -- change group ownership of files
2    Copyright (C) 89, 90, 91, 1995-2004 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 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, 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 != EXIT_SUCCESS)
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 (this is the default)\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       --no-preserve-root do not treat `/' specially (the default)\n\
139       --preserve-root    fail to operate recursively on `/'\n\
140 "), stdout);
141       fputs (_("\
142   -f, --silent, --quiet  suppress most error messages\n\
143       --reference=RFILE  use RFILE's group rather than the specifying\n\
144                          GROUP value\n\
145   -R, --recursive        operate on files and directories recursively\n\
146   -v, --verbose          output a diagnostic for every file processed\n\
147 \n\
148 "), stdout);
149       fputs (_("\
150 The following options modify how a hierarchy is traversed when the -R\n\
151 option is also specified.  If more than one is specified, only the final\n\
152 one takes effect.\n\
153 \n\
154   -H                     if a command line argument is a symbolic link\n\
155                          to a directory, traverse it\n\
156   -L                     traverse every symbolic link to a directory\n\
157                          encountered\n\
158   -P                     do not traverse any symbolic links (default)\n\
159 \n\
160 "), stdout);
161       fputs (HELP_OPTION_DESCRIPTION, stdout);
162       fputs (VERSION_OPTION_DESCRIPTION, stdout);
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   /* Bit flags that control how fts works.  */
173   int bit_flags = FTS_PHYSICAL;
174   struct Chown_option chopt;
175   int fail = 0;
176   int optc;
177
178   initialize_main (&argc, &argv);
179   program_name = argv[0];
180   setlocale (LC_ALL, "");
181   bindtextdomain (PACKAGE, LOCALEDIR);
182   textdomain (PACKAGE);
183
184   atexit (close_stdout);
185
186   chopt_init (&chopt);
187
188   while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
189          != -1)
190     {
191       switch (optc)
192         {
193         case 0:
194           break;
195
196         case 'H': /* Traverse command-line symlinks-to-directories.  */
197           bit_flags = FTS_COMFOLLOW;
198           break;
199
200         case 'L': /* Traverse all symlinks-to-directories.  */
201           bit_flags = FTS_LOGICAL;
202           break;
203
204         case 'P': /* Traverse no symlinks-to-directories.  */
205           bit_flags = FTS_PHYSICAL;
206           break;
207
208         case 'h': /* --no-dereference: affect symlinks */
209           chopt.affect_symlink_referent = false;
210           break;
211
212         case DEREFERENCE_OPTION: /* --dereference: affect the referent
213                                     of each symlink */
214           chopt.affect_symlink_referent = true;
215           break;
216
217         case REFERENCE_FILE_OPTION:
218           reference_file = optarg;
219           break;
220
221         case 'R':
222           chopt.recurse = true;
223           break;
224
225         case 'c':
226           chopt.verbosity = V_changes_only;
227           break;
228
229         case 'f':
230           chopt.force_silent = true;
231           break;
232
233         case 'v':
234           chopt.verbosity = V_high;
235           break;
236
237         case_GETOPT_HELP_CHAR;
238         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
239         default:
240           usage (EXIT_FAILURE);
241         }
242     }
243
244   if (argc - optind + (reference_file ? 1 : 0) <= 1)
245     {
246       error (0, 0, _("too few arguments"));
247       usage (EXIT_FAILURE);
248     }
249
250   if (reference_file)
251     {
252       struct stat ref_stats;
253       if (stat (reference_file, &ref_stats))
254         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
255                quote (reference_file));
256
257       chopt.group_name = gid_to_name (ref_stats.st_gid);
258       gid = ref_stats.st_gid;
259     }
260   else
261     {
262       chopt.group_name = argv[optind++];
263       parse_group (chopt.group_name, &gid);
264     }
265
266   fail = chown_files (argv + optind, bit_flags,
267                       (uid_t) -1, gid,
268                       (uid_t) -1, (gid_t) -1, &chopt);
269
270   chopt_free (&chopt);
271
272   exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);
273 }