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