1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 95, 1996 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 /* MAXUID may come from limits.h *or* sys/params.h (via system.h) above. */
44 # define MAXUID INT_MAX
47 #ifndef _POSIX_VERSION
48 struct group *getgrnam ();
52 # define endgrent() ((void) 0)
56 # define LCHOWN(FILE, OWNER, GROUP) lchown (FILE, OWNER, GROUP)
58 # define LCHOWN(FILE, OWNER, GROUP) 1
61 char *group_member ();
66 static int change_dir_group __P ((char *dir, int group, struct stat *statp));
68 /* The name the program was run with. */
71 /* If nonzero, and the systems has support for it, change the ownership
72 of symbolic links rather than any files they point to. */
73 static int change_symlinks;
75 /* If nonzero, change the ownership of directories recursively. */
78 /* If nonzero, force silence (no error messages). */
79 static int force_silent;
81 /* If nonzero, describe the files we process. */
84 /* If nonzero, describe only owners or groups that change. */
85 static int changes_only;
87 /* The name of the group to which ownership of the files is being given. */
88 static char *groupname;
90 /* If nonzero, display usage information and exit. */
93 /* If nonzero, print the version on standard output and exit. */
94 static int show_version;
96 static struct option const long_options[] =
98 {"recursive", no_argument, 0, 'R'},
99 {"changes", no_argument, 0, 'c'},
100 {"no-dereference", no_argument, 0, 'h'},
101 {"silent", no_argument, 0, 'f'},
102 {"quiet", no_argument, 0, 'f'},
103 {"verbose", no_argument, 0, 'v'},
104 {"help", no_argument, &show_help, 1},
105 {"version", no_argument, &show_version, 1},
109 /* Tell the user the group name to which ownership of FILE
110 has been given; if CHANGED is zero, FILE was that group already. */
113 describe_change (char *file, int changed)
116 printf (_("group of %s changed to %s\n"), file, groupname);
118 printf (_("group of %s retained as %s\n"), file, groupname);
121 /* Set *G according to NAME. */
124 parse_group (char *name, int *g)
130 error (1, 0, _("can not change to null group"));
132 grp = getgrnam (name);
136 unsigned long int tmp_long;
138 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
140 if (s_err == LONGINT_OVERFLOW || tmp_long > INT_MAX)
142 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
147 endgrent (); /* Save a file descriptor. */
150 /* Change the ownership of FILE to GID GROUP.
151 If it is a directory and -R is given, recurse.
152 Return 0 if successful, 1 if errors occurred. */
155 change_file_group (char *file, int group)
157 struct stat file_stats;
160 if (lstat (file, &file_stats))
162 if (force_silent == 0)
163 error (0, errno, "%s", file);
167 if (group != file_stats.st_gid)
172 describe_change (file, 1);
175 fail = LCHOWN (file, file_stats.st_uid, group);
177 fail = chown (file, file_stats.st_uid, group);
182 if (force_silent == 0)
184 /* Give a more specific message. Some systems set errno
185 to EPERM for both `inaccessible file' and `user not a member
186 of the specified group' errors. */
187 if (errno == EPERM && !group_member (group))
189 error (0, errno, _("you are not a member of group `%s'"),
192 else if (errno == EINVAL && group > MAXUID)
194 error (0, 0, _("%s: invalid group number"), groupname);
198 error (0, errno, "%s", file);
203 else if (verbose && changes_only == 0)
204 describe_change (file, 0);
206 if (recurse && S_ISDIR (file_stats.st_mode))
207 errors |= change_dir_group (file, group, &file_stats);
211 /* Recursively change the ownership of the files in directory DIR
213 STATP points to the results of lstat on DIR.
214 Return 0 if successful, 1 if errors occurred. */
217 change_dir_group (char *dir, int group, struct stat *statp)
219 char *name_space, *namep;
220 char *path; /* Full path of each entry to process. */
221 unsigned dirlength; /* Length of `dir' and '\0'. */
222 unsigned filelength; /* Length of each pathname to process. */
223 unsigned pathlength; /* Bytes allocated for `path'. */
227 name_space = savedir (dir, statp->st_size);
228 if (name_space == NULL)
232 if (force_silent == 0)
233 error (0, errno, "%s", dir);
237 error (1, 0, _("virtual memory exhausted"));
240 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
241 pathlength = dirlength + 1;
242 /* Give `path' a dummy value; it will be reallocated before first use. */
243 path = xmalloc (pathlength);
245 path[dirlength - 1] = '/';
247 for (namep = name_space; *namep; namep += filelength - dirlength)
249 filelength = dirlength + strlen (namep) + 1;
250 if (filelength > pathlength)
252 pathlength = filelength * 2;
253 path = xrealloc (path, pathlength);
255 strcpy (path + dirlength, namep);
256 errors |= change_file_group (path, group);
267 fprintf (stderr, _("Try `%s --help' for more information.\n"),
271 printf (_("Usage: %s [OPTION]... GROUP FILE...\n"), program_name);
273 Change the group membership of each FILE to GROUP.\n\
275 -c, --changes like verbose but report only when a change is made\n\
276 -h, --no-dereference affect symbolic links instead of any referenced file\n\
277 (available only on systems with lchown system call)\n\
278 -f, --silent, --quiet suppress most error messages\n\
279 -v, --verbose output a diagnostic for every file processed\n\
280 -R, --recursive change files and directories recursively\n\
281 --help display this help and exit\n\
282 --version output version information and exit\n"));
288 main (int argc, char **argv)
294 program_name = argv[0];
295 recurse = force_silent = verbose = changes_only = 0;
297 while ((optc = getopt_long (argc, argv, "Rcfnv", long_options, (int *) 0))
327 printf ("chgrp - %s\n", PACKAGE_VERSION);
334 if (argc - optind <= 1)
336 error (0, 0, _("too few arguments"));
343 error (1, 0, _("--no-dereference (-h) is not supported on this system"));
347 parse_group (argv[optind++], &group);
349 for (; optind < argc; ++optind)
350 errors |= change_file_group (argv[optind], group);