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