1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 95, 96, 1997 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 ((const char *dir, int group,
67 const struct stat *statp));
69 /* The name the program was run with. */
72 /* If nonzero, and the systems has support for it, change the ownership
73 of symbolic links rather than any files they point to. */
74 static int change_symlinks;
76 /* If nonzero, change the ownership of directories recursively. */
79 /* If nonzero, force silence (no error messages). */
80 static int force_silent;
82 /* If nonzero, describe the files we process. */
85 /* If nonzero, describe only owners or groups that change. */
86 static int changes_only;
88 /* The name of the group to which ownership of the files is being given. */
89 static const char *groupname;
91 /* If nonzero, display usage information and exit. */
94 /* If nonzero, print the version on standard output and exit. */
95 static int show_version;
97 static struct option const long_options[] =
99 {"recursive", no_argument, 0, 'R'},
100 {"changes", no_argument, 0, 'c'},
101 {"no-dereference", no_argument, 0, 'h'},
102 {"silent", no_argument, 0, 'f'},
103 {"quiet", no_argument, 0, 'f'},
104 {"verbose", no_argument, 0, 'v'},
105 {"help", no_argument, &show_help, 1},
106 {"version", no_argument, &show_version, 1},
110 /* Tell the user the group name to which ownership of FILE
111 has been given; if CHANGED is zero, FILE was that group already. */
114 describe_change (const char *file, int changed)
117 printf (_("group of %s changed to %s\n"), file, groupname);
119 printf (_("group of %s retained as %s\n"), file, groupname);
122 /* Set *G according to NAME. */
125 parse_group (const char *name, int *g)
131 error (1, 0, _("can not change to null group"));
133 grp = getgrnam (name);
137 unsigned long int tmp_long;
139 if (!ISDIGIT (*name))
140 error (1, 0, _("invalid group name `%s'"), name);
142 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
143 if (s_err != LONGINT_OK)
144 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
146 if (tmp_long > INT_MAX)
147 error (1, 0, _("invalid group number `%s'"), name);
153 endgrent (); /* Save a file descriptor. */
156 /* Change the ownership of FILE to GID GROUP.
157 If it is a directory and -R is given, recurse.
158 Return 0 if successful, 1 if errors occurred. */
161 change_file_group (const char *file, int group)
163 struct stat file_stats;
166 if (lstat (file, &file_stats))
168 if (force_silent == 0)
169 error (0, errno, "%s", file);
173 if (group != file_stats.st_gid)
178 describe_change (file, 1);
181 fail = LCHOWN (file, file_stats.st_uid, group);
183 fail = chown (file, file_stats.st_uid, group);
188 if (force_silent == 0)
190 /* Give a more specific message. Some systems set errno
191 to EPERM for both `inaccessible file' and `user not a member
192 of the specified group' errors. */
193 if (errno == EPERM && !group_member (group))
195 error (0, errno, _("you are not a member of group `%s'"),
198 else if (errno == EINVAL && group > MAXUID)
200 error (0, 0, _("%s: invalid group number"), groupname);
204 error (0, errno, "%s", file);
209 else if (verbose && changes_only == 0)
210 describe_change (file, 0);
212 if (recurse && S_ISDIR (file_stats.st_mode))
213 errors |= change_dir_group (file, group, &file_stats);
217 /* Recursively change the ownership of the files in directory DIR
219 STATP points to the results of lstat on DIR.
220 Return 0 if successful, 1 if errors occurred. */
223 change_dir_group (const char *dir, int group, const struct stat *statp)
225 char *name_space, *namep;
226 char *path; /* Full path of each entry to process. */
227 unsigned dirlength; /* Length of `dir' and '\0'. */
228 unsigned filelength; /* Length of each pathname to process. */
229 unsigned pathlength; /* Bytes allocated for `path'. */
233 name_space = savedir (dir, statp->st_size);
234 if (name_space == NULL)
238 if (force_silent == 0)
239 error (0, errno, "%s", dir);
243 error (1, 0, _("virtual memory exhausted"));
246 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
247 pathlength = dirlength + 1;
248 /* Give `path' a dummy value; it will be reallocated before first use. */
249 path = xmalloc (pathlength);
251 path[dirlength - 1] = '/';
253 for (namep = name_space; *namep; namep += filelength - dirlength)
255 filelength = dirlength + strlen (namep) + 1;
256 if (filelength > pathlength)
258 pathlength = filelength * 2;
259 path = xrealloc (path, pathlength);
261 strcpy (path + dirlength, namep);
262 errors |= change_file_group (path, group);
273 fprintf (stderr, _("Try `%s --help' for more information.\n"),
277 printf (_("Usage: %s [OPTION]... GROUP FILE...\n"), program_name);
279 Change the group membership of each FILE to GROUP.\n\
281 -c, --changes like verbose but report only when a change is made\n\
282 -h, --no-dereference affect symbolic links instead of any referenced file\n\
283 (available only on systems with lchown system call)\n\
284 -f, --silent, --quiet suppress most error messages\n\
285 -R, --recursive change files and directories recursively\n\
286 -v, --verbose output a diagnostic for every file processed\n\
287 --help display this help and exit\n\
288 --version output version information and exit\n\
290 puts (_("\nReport bugs to <fileutils-bugs@gnu.ai.mit.edu>."));
296 main (int argc, char **argv)
302 program_name = argv[0];
303 setlocale (LC_ALL, "");
304 bindtextdomain (PACKAGE, LOCALEDIR);
305 textdomain (PACKAGE);
307 recurse = force_silent = verbose = changes_only = 0;
309 while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
338 printf ("chgrp (%s) %s\n", GNU_PACKAGE, VERSION);
345 if (argc - optind <= 1)
347 error (0, 0, _("too few arguments"));
354 error (1, 0, _("--no-dereference (-h) is not supported on this system"));
358 parse_group (argv[optind++], &group);
360 for (; optind < argc; ++optind)
361 errors |= change_file_group (argv[optind], group);