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)
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>. */
22 #include <sys/types.h>
31 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
35 # define INT_MAX ((int) (UINT_MAX >> 1))
42 #ifndef _POSIX_VERSION
43 struct group *getgrnam ();
47 # define endgrent() ((void) 0)
50 char *group_member ();
55 static int change_dir_group __P ((char *dir, int group, struct stat *statp));
57 /* The name the program was run with. */
60 /* If nonzero, change the ownership of directories recursively. */
63 /* If nonzero, force silence (no error messages). */
64 static int force_silent;
66 /* If nonzero, describe the files we process. */
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. */
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},
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. */
97 describe_change (char *file, int changed)
100 printf (_("group of %s changed to %s\n"), file, groupname);
102 printf (_("group of %s retained as %s\n"), file, groupname);
105 /* Set *G according to NAME. */
108 parse_group (char *name, int *g)
114 error (1, 0, _("can not change to null group"));
116 grp = getgrnam (name);
120 unsigned long int tmp_long;
122 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
124 if (s_err == LONGINT_OVERFLOW || tmp_long > INT_MAX)
126 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
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. */
139 change_file_group (char *file, int group)
141 struct stat file_stats;
144 if (lstat (file, &file_stats))
146 if (force_silent == 0)
147 error (0, errno, "%s", file);
151 if (group != file_stats.st_gid)
154 describe_change (file, 1);
155 if (chown (file, file_stats.st_uid, group))
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'"),
169 else if (errno == EINVAL && group > MAXUID)
171 error (0, 0, _("%s: invalid group number"), groupname);
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);
189 /* Recursively change the ownership of the files in directory DIR
191 STATP points to the results of lstat on DIR.
192 Return 0 if successful, 1 if errors occurred. */
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'. */
205 name_space = savedir (dir, statp->st_size);
206 if (name_space == NULL)
210 if (force_silent == 0)
211 error (0, errno, "%s", dir);
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);
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);
245 fprintf (stderr, _("Try `%s --help' for more information.\n"),
249 printf (_("Usage: %s [OPTION]... GROUP FILE...\n"), program_name);
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"));
264 main (int argc, char **argv)
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))
300 printf ("chgrp - %s\n", PACKAGE_VERSION);
307 if (argc - optind <= 1)
309 error (0, 0, _("too few arguments"));
313 parse_group (argv[optind++], &group);
315 for (; optind < argc; ++optind)
316 errors |= change_file_group (argv[optind], group);