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 /* MAXUID may come from limits.h *or* sys/params.h (via system.h) above. */
33 # define MAXUID INT_MAX
36 #ifndef _POSIX_VERSION
37 struct group *getgrnam ();
41 # define endgrent() ((void) 0)
45 # define LCHOWN(FILE, OWNER, GROUP) lchown (FILE, OWNER, GROUP)
47 # define LCHOWN(FILE, OWNER, GROUP) 1
54 CH_NO_CHANGE_REQUESTED
57 char *group_member ();
59 static int change_dir_group PARAMS ((const char *dir, int group,
60 const struct stat *statp));
62 /* The name the program was run with. */
65 /* If nonzero, and the systems has support for it, change the ownership
66 of symbolic links rather than any files they point to. */
67 static int change_symlinks;
69 /* If nonzero, change the ownership of directories recursively. */
72 /* If nonzero, force silence (no error messages). */
73 static int force_silent;
75 /* If nonzero, describe the files we process. */
78 /* If nonzero, describe only owners or groups that change. */
79 static int changes_only;
81 /* The name of the group to which ownership of the files is being given. */
82 static const char *groupname;
84 /* The argument to the --reference option. Use the group ID of this file.
85 This file must exist. */
86 static char *reference_file;
88 /* If nonzero, display usage information and exit. */
91 /* If nonzero, print the version on standard output and exit. */
92 static int show_version;
94 static struct option const long_options[] =
96 {"recursive", no_argument, 0, 'R'},
97 {"changes", no_argument, 0, 'c'},
98 {"no-dereference", no_argument, 0, 'h'},
99 {"silent", no_argument, 0, 'f'},
100 {"quiet", no_argument, 0, 'f'},
101 {"reference", required_argument, 0, 12},
102 {"verbose", no_argument, 0, 'v'},
103 {"help", no_argument, &show_help, 1},
104 {"version", no_argument, &show_version, 1},
108 /* Tell the user how/if the group of FILE has been changed.
109 CHANGED describes what (if anything) has happened. */
112 describe_change (const char *file, enum Change_status changed)
118 fmt = _("group of %s changed to %s\n");
121 fmt = _("failed to change group of %s to %s\n");
123 case CH_NO_CHANGE_REQUESTED:
124 fmt = _("group of %s retained as %s\n");
129 printf (fmt, file, groupname);
132 /* Set *G according to NAME. */
135 parse_group (const char *name, int *g)
141 error (1, 0, _("can not change to null group"));
143 grp = getgrnam (name);
147 unsigned long int tmp_long;
149 if (!ISDIGIT (*name))
150 error (1, 0, _("invalid group name `%s'"), name);
152 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
153 if (s_err != LONGINT_OK)
154 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
156 if (tmp_long > INT_MAX)
157 error (1, 0, _("invalid group number `%s'"), name);
163 endgrent (); /* Save a file descriptor. */
166 /* Change the ownership of FILE to GID GROUP.
167 If it is a directory and -R is given, recurse.
168 Return 0 if successful, 1 if errors occurred. */
171 change_file_group (const char *file, int group)
173 struct stat file_stats;
176 if (lstat (file, &file_stats))
178 if (force_silent == 0)
179 error (0, errno, "%s", file);
183 if (group != file_stats.st_gid)
188 fail = LCHOWN (file, (uid_t) -1, group);
190 fail = chown (file, (uid_t) -1, group);
192 if (verbose || (changes_only && !fail))
193 describe_change (file, (fail ? CH_FAILED : CH_SUCCEEDED));
198 if (force_silent == 0)
200 /* Give a more specific message. Some systems set errno
201 to EPERM for both `inaccessible file' and `user not a member
202 of the specified group' errors. */
203 if (errno == EPERM && !group_member (group))
205 error (0, errno, _("you are not a member of group `%s'"),
208 else if (errno == EINVAL && group > MAXUID)
210 error (0, 0, _("%s: invalid group number"), groupname);
214 error (0, errno, "%s", file);
219 else if (verbose && changes_only == 0)
221 describe_change (file, CH_NO_CHANGE_REQUESTED);
224 if (recurse && S_ISDIR (file_stats.st_mode))
225 errors |= change_dir_group (file, group, &file_stats);
230 /* Recursively change the ownership of the files in directory DIR
232 STATP points to the results of lstat on DIR.
233 Return 0 if successful, 1 if errors occurred. */
236 change_dir_group (const char *dir, int group, const struct stat *statp)
238 char *name_space, *namep;
239 char *path; /* Full path of each entry to process. */
240 unsigned dirlength; /* Length of `dir' and '\0'. */
241 unsigned filelength; /* Length of each pathname to process. */
242 unsigned pathlength; /* Bytes allocated for `path'. */
246 name_space = savedir (dir, (unsigned int) statp->st_size);
247 if (name_space == NULL)
251 if (force_silent == 0)
252 error (0, errno, "%s", dir);
256 error (1, 0, _("virtual memory exhausted"));
259 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
260 pathlength = dirlength + 1;
261 /* Give `path' a dummy value; it will be reallocated before first use. */
262 path = xmalloc (pathlength);
264 path[dirlength - 1] = '/';
266 for (namep = name_space; *namep; namep += filelength - dirlength)
268 filelength = dirlength + strlen (namep) + 1;
269 if (filelength > pathlength)
271 pathlength = filelength * 2;
272 path = xrealloc (path, pathlength);
274 strcpy (path + dirlength, namep);
275 errors |= change_file_group (path, group);
286 fprintf (stderr, _("Try `%s --help' for more information.\n"),
291 Usage: %s [OPTION]... GROUP FILE...\n\
292 or: %s [OPTION]... --reference=RFILE FILE...\n\
294 program_name, program_name);
296 Change the group membership of each FILE to GROUP.\n\
298 -c, --changes like verbose but report only when a change is made\n\
299 -h, --no-dereference affect symbolic links instead of any referenced file\n\
300 (available only on systems with lchown system call)\n\
301 -f, --silent, --quiet suppress most error messages\n\
302 --reference=RFILE use RFILE's group instead of using a GROUP value\n\
303 -R, --recursive change files and directories recursively\n\
304 -v, --verbose output a diagnostic for every file processed\n\
305 --help display this help and exit\n\
306 --version output version information and exit\n\
308 puts (_("\nReport bugs to <fileutils-bugs@gnu.org>."));
314 main (int argc, char **argv)
320 program_name = argv[0];
321 setlocale (LC_ALL, "");
322 bindtextdomain (PACKAGE, LOCALEDIR);
323 textdomain (PACKAGE);
325 recurse = force_silent = verbose = changes_only = 0;
327 while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
334 reference_file = optarg;
359 printf ("chgrp (%s) %s\n", GNU_PACKAGE, VERSION);
366 if (argc - optind + (reference_file ? 1 : 0) <= 1)
368 error (0, 0, _("too few arguments"));
375 error (1, 0, _("--no-dereference (-h) is not supported on this system"));
381 struct stat ref_stats;
382 if (stat (reference_file, &ref_stats))
383 error (1, errno, "%s", reference_file);
385 group = ref_stats.st_gid;
388 parse_group (argv[optind++], &group);
390 for (; optind < argc; ++optind)
391 errors |= change_file_group (argv[optind], group);