Update FSF's address.
[platform/upstream/coreutils.git] / src / chgrp.c
1 /* chgrp -- change group ownership of files
2    Copyright (C) 89, 90, 91, 1995 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 #if HAVE_LIMITS_H
27 # include <limits.h>
28 #endif
29
30 #ifndef UINT_MAX
31 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
32 #endif
33
34 #ifndef INT_MAX
35 # define INT_MAX ((int) (UINT_MAX >> 1))
36 #endif
37
38 #include "system.h"
39 #include "xstrtoul.h"
40 #include "error.h"
41
42 #ifndef _POSIX_VERSION
43 struct group *getgrnam ();
44 #endif
45
46 #ifndef HAVE_ENDGRENT
47 # define endgrent() ((void) 0)
48 #endif
49
50 char *group_member ();
51 char *savedir ();
52 char *xmalloc ();
53 char *xrealloc ();
54
55 static int change_dir_group __P ((char *dir, int group, struct stat *statp));
56
57 /* The name the program was run with. */
58 char *program_name;
59
60 /* If nonzero, change the ownership of directories recursively. */
61 static int recurse;
62
63 /* If nonzero, force silence (no error messages). */
64 static int force_silent;
65
66 /* If nonzero, describe the files we process. */
67 static int verbose;
68
69 /* If nonzero, describe only owners or groups that change. */
70 static int changes_only;
71
72 /* The name of the group to which ownership of the files is being given. */
73 static char *groupname;
74
75 /* If nonzero, display usage information and exit.  */
76 static int show_help;
77
78 /* If nonzero, print the version on standard output and exit.  */
79 static int show_version;
80
81 static struct option const long_options[] =
82 {
83   {"recursive", no_argument, 0, 'R'},
84   {"changes", no_argument, 0, 'c'},
85   {"silent", no_argument, 0, 'f'},
86   {"quiet", no_argument, 0, 'f'},
87   {"verbose", no_argument, 0, 'v'},
88   {"help", no_argument, &show_help, 1},
89   {"version", no_argument, &show_version, 1},
90   {0, 0, 0, 0}
91 };
92
93 /* Tell the user the group name to which ownership of FILE
94    has been given; if CHANGED is zero, FILE was that group already. */
95
96 static void
97 describe_change (char *file, int changed)
98 {
99   if (changed)
100     printf (_("group of %s changed to %s\n"), file, groupname);
101   else
102     printf (_("group of %s retained as %s\n"), file, groupname);
103 }
104
105 /* Set *G according to NAME. */
106
107 static void
108 parse_group (char *name, int *g)
109 {
110   struct group *grp;
111
112   groupname = name;
113   if (*name == '\0')
114     error (1, 0, _("can not change to null group"));
115
116   grp = getgrnam (name);
117   if (grp == NULL)
118     {
119       strtol_error s_err;
120       unsigned long int tmp_long;
121
122       s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
123       *g = tmp_long;
124       if (s_err == LONGINT_OVERFLOW || tmp_long > INT_MAX)
125         {
126           STRTOL_FATAL_ERROR (name, _("group number"), s_err);
127         }
128     }
129   else
130     *g = grp->gr_gid;
131   endgrent ();          /* Save a file descriptor. */
132 }
133
134 /* Change the ownership of FILE to GID GROUP.
135    If it is a directory and -R is given, recurse.
136    Return 0 if successful, 1 if errors occurred. */
137
138 static int
139 change_file_group (char *file, int group)
140 {
141   struct stat file_stats;
142   int errors = 0;
143
144   if (lstat (file, &file_stats))
145     {
146       if (force_silent == 0)
147         error (0, errno, "%s", file);
148       return 1;
149     }
150
151   if (group != file_stats.st_gid)
152     {
153       if (verbose)
154         describe_change (file, 1);
155       if (chown (file, file_stats.st_uid, group))
156         {
157           errors = 1;
158           if (force_silent == 0)
159             {
160               /* Give a more specific message.  Some systems set errno
161                  to EPERM for both `inaccessible file' and `user not a member
162                  of the specified group' errors.  */
163               if (errno == EPERM && !group_member (group))
164                 {
165                   error (0, errno, _("you are not a member of group `%s'"),
166                          groupname);
167                 }
168 #ifdef MAXUID
169               else if (errno == EINVAL && group > MAXUID)
170                 {
171                   error (0, 0, _("%s: invalid group number"), groupname);
172                 }
173 #endif
174               else
175                 {
176                   error (0, errno, "%s", file);
177                 }
178             }
179         }
180     }
181   else if (verbose && changes_only == 0)
182     describe_change (file, 0);
183
184   if (recurse && S_ISDIR (file_stats.st_mode))
185     errors |= change_dir_group (file, group, &file_stats);
186   return errors;
187 }
188
189 /* Recursively change the ownership of the files in directory DIR
190    to GID GROUP.
191    STATP points to the results of lstat on DIR.
192    Return 0 if successful, 1 if errors occurred. */
193
194 static int
195 change_dir_group (char *dir, int group, struct stat *statp)
196 {
197   char *name_space, *namep;
198   char *path;                   /* Full path of each entry to process. */
199   unsigned dirlength;           /* Length of `dir' and '\0'. */
200   unsigned filelength;          /* Length of each pathname to process. */
201   unsigned pathlength;          /* Bytes allocated for `path'. */
202   int errors = 0;
203
204   errno = 0;
205   name_space = savedir (dir, statp->st_size);
206   if (name_space == NULL)
207     {
208       if (errno)
209         {
210           if (force_silent == 0)
211             error (0, errno, "%s", dir);
212           return 1;
213         }
214       else
215         error (1, 0, _("virtual memory exhausted"));
216     }
217
218   dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
219   pathlength = dirlength + 1;
220   /* Give `path' a dummy value; it will be reallocated before first use. */
221   path = xmalloc (pathlength);
222   strcpy (path, dir);
223   path[dirlength - 1] = '/';
224
225   for (namep = name_space; *namep; namep += filelength - dirlength)
226     {
227       filelength = dirlength + strlen (namep) + 1;
228       if (filelength > pathlength)
229         {
230           pathlength = filelength * 2;
231           path = xrealloc (path, pathlength);
232         }
233       strcpy (path + dirlength, namep);
234       errors |= change_file_group (path, group);
235     }
236   free (path);
237   free (name_space);
238   return errors;
239 }
240
241 static void
242 usage (int status)
243 {
244   if (status != 0)
245     fprintf (stderr, _("Try `%s --help' for more information.\n"),
246              program_name);
247   else
248     {
249       printf (_("Usage: %s [OPTION]... GROUP FILE...\n"), program_name);
250       printf (_("\
251 Change the group membership of each FILE to GROUP.\n\
252 \n\
253   -c, --changes           like verbose but report only when a change is made\n\
254   -f, --silent, --quiet   suppress most error messages\n\
255   -v, --verbose           output a diagnostic for every file processed\n\
256   -R, --recursive         change files and directories recursively\n\
257       --help              display this help and exit\n\
258       --version           output version information and exit\n"));
259     }
260   exit (status);
261 }
262
263 int
264 main (int argc, char **argv)
265 {
266   int group;
267   int errors = 0;
268   int optc;
269
270   program_name = argv[0];
271   recurse = force_silent = verbose = changes_only = 0;
272
273   while ((optc = getopt_long (argc, argv, "Rcfv", long_options, (int *) 0))
274          != EOF)
275     {
276       switch (optc)
277         {
278         case 0:
279           break;
280         case 'R':
281           recurse = 1;
282           break;
283         case 'c':
284           verbose = 1;
285           changes_only = 1;
286           break;
287         case 'f':
288           force_silent = 1;
289           break;
290         case 'v':
291           verbose = 1;
292           break;
293         default:
294           usage (1);
295         }
296     }
297
298   if (show_version)
299     {
300       printf ("chgrp - %s\n", PACKAGE_VERSION);
301       exit (0);
302     }
303
304   if (show_help)
305     usage (0);
306
307   if (argc - optind <= 1)
308     {
309       error (0, 0, _("too few arguments"));
310       usage (1);
311     }
312
313   parse_group (argv[optind++], &group);
314
315   for (; optind < argc; ++optind)
316     errors |= change_file_group (argv[optind], group);
317
318   exit (errors);
319 }