Update FSF's address.
[coreutils.git] / src / chgrp.c
blob1181e909eddf3c4ed031d43c178aef8875f0d34b
1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995 Free Software Foundation, Inc.
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.
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.
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. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <grp.h>
24 #include <getopt.h>
26 #if HAVE_LIMITS_H
27 # include <limits.h>
28 #endif
30 #ifndef UINT_MAX
31 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
32 #endif
34 #ifndef INT_MAX
35 # define INT_MAX ((int) (UINT_MAX >> 1))
36 #endif
38 #include "system.h"
39 #include "xstrtoul.h"
40 #include "error.h"
42 #ifndef _POSIX_VERSION
43 struct group *getgrnam ();
44 #endif
46 #ifndef HAVE_ENDGRENT
47 # define endgrent() ((void) 0)
48 #endif
50 char *group_member ();
51 char *savedir ();
52 char *xmalloc ();
53 char *xrealloc ();
55 static int change_dir_group __P ((char *dir, int group, struct stat *statp));
57 /* The name the program was run with. */
58 char *program_name;
60 /* If nonzero, change the ownership of directories recursively. */
61 static int recurse;
63 /* If nonzero, force silence (no error messages). */
64 static int force_silent;
66 /* If nonzero, describe the files we process. */
67 static int verbose;
69 /* If nonzero, describe only owners or groups that change. */
70 static int changes_only;
72 /* The name of the group to which ownership of the files is being given. */
73 static char *groupname;
75 /* If nonzero, display usage information and exit. */
76 static int show_help;
78 /* If nonzero, print the version on standard output and exit. */
79 static int show_version;
81 static struct option const long_options[] =
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}
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. */
96 static void
97 describe_change (char *file, int changed)
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);
105 /* Set *G according to NAME. */
107 static void
108 parse_group (char *name, int *g)
110 struct group *grp;
112 groupname = name;
113 if (*name == '\0')
114 error (1, 0, _("can not change to null group"));
116 grp = getgrnam (name);
117 if (grp == NULL)
119 strtol_error s_err;
120 unsigned long int tmp_long;
122 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
123 *g = tmp_long;
124 if (s_err == LONGINT_OVERFLOW || tmp_long > INT_MAX)
126 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
129 else
130 *g = grp->gr_gid;
131 endgrent (); /* Save a file descriptor. */
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. */
138 static int
139 change_file_group (char *file, int group)
141 struct stat file_stats;
142 int errors = 0;
144 if (lstat (file, &file_stats))
146 if (force_silent == 0)
147 error (0, errno, "%s", file);
148 return 1;
151 if (group != file_stats.st_gid)
153 if (verbose)
154 describe_change (file, 1);
155 if (chown (file, file_stats.st_uid, group))
157 errors = 1;
158 if (force_silent == 0)
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))
165 error (0, errno, _("you are not a member of group `%s'"),
166 groupname);
168 #ifdef MAXUID
169 else if (errno == EINVAL && group > MAXUID)
171 error (0, 0, _("%s: invalid group number"), groupname);
173 #endif
174 else
176 error (0, errno, "%s", file);
181 else if (verbose && changes_only == 0)
182 describe_change (file, 0);
184 if (recurse && S_ISDIR (file_stats.st_mode))
185 errors |= change_dir_group (file, group, &file_stats);
186 return errors;
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. */
194 static int
195 change_dir_group (char *dir, int group, struct stat *statp)
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;
204 errno = 0;
205 name_space = savedir (dir, statp->st_size);
206 if (name_space == NULL)
208 if (errno)
210 if (force_silent == 0)
211 error (0, errno, "%s", dir);
212 return 1;
214 else
215 error (1, 0, _("virtual memory exhausted"));
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] = '/';
225 for (namep = name_space; *namep; namep += filelength - dirlength)
227 filelength = dirlength + strlen (namep) + 1;
228 if (filelength > pathlength)
230 pathlength = filelength * 2;
231 path = xrealloc (path, pathlength);
233 strcpy (path + dirlength, namep);
234 errors |= change_file_group (path, group);
236 free (path);
237 free (name_space);
238 return errors;
241 static void
242 usage (int status)
244 if (status != 0)
245 fprintf (stderr, _("Try `%s --help' for more information.\n"),
246 program_name);
247 else
249 printf (_("Usage: %s [OPTION]... GROUP FILE...\n"), program_name);
250 printf (_("\
251 Change the group membership of each FILE to GROUP.\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"));
260 exit (status);
264 main (int argc, char **argv)
266 int group;
267 int errors = 0;
268 int optc;
270 program_name = argv[0];
271 recurse = force_silent = verbose = changes_only = 0;
273 while ((optc = getopt_long (argc, argv, "Rcfv", long_options, (int *) 0))
274 != EOF)
276 switch (optc)
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);
298 if (show_version)
300 printf ("chgrp - %s\n", PACKAGE_VERSION);
301 exit (0);
304 if (show_help)
305 usage (0);
307 if (argc - optind <= 1)
309 error (0, 0, _("too few arguments"));
310 usage (1);
313 parse_group (argv[optind++], &group);
315 for (; optind < argc; ++optind)
316 errors |= change_file_group (argv[optind], group);
318 exit (errors);