1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-1999 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>
29 #include "group-member.h"
31 #include "version-etc.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "chgrp"
37 /* MAXUID may come from limits.h *or* sys/params.h (via system.h) above. */
39 # define MAXUID UID_T_MAX
42 # define MAXGID GID_T_MAX
45 #ifndef _POSIX_VERSION
46 struct group *getgrnam ();
50 # define endgrent() ((void) 0)
54 # define LCHOWN(FILE, OWNER, GROUP) lchown (FILE, OWNER, GROUP)
56 # define LCHOWN(FILE, OWNER, GROUP) 1
63 CH_NO_CHANGE_REQUESTED
68 /* Print a message for each file that is processed. */
71 /* Print a message for each file whose attributes we change. */
74 /* Do not be verbose. This is the default. */
78 static int change_dir_group PARAMS ((const char *dir, gid_t group,
79 const struct stat *statp));
81 /* The name the program was run with. */
84 /* If nonzero, and the systems has support for it, change the ownership
85 of symbolic links rather than any files they point to. */
86 static int change_symlinks;
88 /* If nonzero, change the ownership of directories recursively. */
91 /* If nonzero, force silence (no error messages). */
92 static int force_silent;
94 /* Level of verbosity. */
95 static enum Verbosity verbosity = V_off;
97 /* The name of the group to which ownership of the files is being given. */
98 static const char *groupname;
100 /* The argument to the --reference option. Use the group ID of this file.
101 This file must exist. */
102 static char *reference_file;
104 static struct option const long_options[] =
106 {"recursive", no_argument, 0, 'R'},
107 {"changes", no_argument, 0, 'c'},
108 {"no-dereference", no_argument, 0, 'h'},
109 {"silent", no_argument, 0, 'f'},
110 {"quiet", no_argument, 0, 'f'},
111 {"reference", required_argument, 0, CHAR_MAX + 1},
112 {"verbose", no_argument, 0, 'v'},
113 {GETOPT_HELP_OPTION_DECL},
114 {GETOPT_VERSION_OPTION_DECL},
118 /* Tell the user how/if the group of FILE has been changed.
119 CHANGED describes what (if anything) has happened. */
122 describe_change (const char *file, enum Change_status changed)
128 fmt = _("group of %s changed to %s\n");
131 fmt = _("failed to change group of %s to %s\n");
133 case CH_NO_CHANGE_REQUESTED:
134 fmt = _("group of %s retained as %s\n");
139 printf (fmt, file, groupname);
142 /* Set *G according to NAME. */
145 parse_group (const char *name, gid_t *g)
151 error (1, 0, _("can not change to null group"));
153 grp = getgrnam (name);
157 unsigned long int tmp_long;
159 if (!ISDIGIT (*name))
160 error (1, 0, _("invalid group name `%s'"), name);
162 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
163 if (s_err != LONGINT_OK)
164 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
166 if (tmp_long > MAXGID)
167 error (1, 0, _("invalid group number `%s'"), name);
173 endgrent (); /* Save a file descriptor. */
176 /* Change the ownership of FILE to GID GROUP.
177 If it is a directory and -R is given, recurse.
178 Return 0 if successful, 1 if errors occurred. */
181 change_file_group (const char *file, gid_t group)
183 struct stat file_stats;
186 if (lstat (file, &file_stats))
188 if (force_silent == 0)
189 error (0, errno, "%s", file);
193 if (group != file_stats.st_gid)
198 fail = LCHOWN (file, (uid_t) -1, group);
200 fail = chown (file, (uid_t) -1, group);
202 if (verbosity == V_high || (verbosity == V_changes_only && !fail))
203 describe_change (file, (fail ? CH_FAILED : CH_SUCCEEDED));
208 if (force_silent == 0)
210 /* Give a more specific message. Some systems set errno
211 to EPERM for both `inaccessible file' and `user not a member
212 of the specified group' errors. */
213 if (errno == EPERM && !group_member (group))
215 error (0, errno, _("you are not a member of group `%s'"),
218 else if (errno == EINVAL && group > MAXUID)
220 error (0, 0, _("%s: invalid group number"), groupname);
224 error (0, errno, "%s", file);
229 else if (verbosity == V_high)
231 describe_change (file, CH_NO_CHANGE_REQUESTED);
234 if (recurse && S_ISDIR (file_stats.st_mode))
235 errors |= change_dir_group (file, group, &file_stats);
240 /* Recursively change the ownership of the files in directory DIR
242 STATP points to the results of lstat on DIR.
243 Return 0 if successful, 1 if errors occurred. */
246 change_dir_group (const char *dir, gid_t group, const struct stat *statp)
248 char *name_space, *namep;
249 char *path; /* Full path of each entry to process. */
250 unsigned dirlength; /* Length of `dir' and '\0'. */
251 unsigned filelength; /* Length of each pathname to process. */
252 unsigned pathlength; /* Bytes allocated for `path'. */
256 name_space = savedir (dir, (unsigned int) statp->st_size);
257 if (name_space == NULL)
261 if (force_silent == 0)
262 error (0, errno, "%s", dir);
266 error (1, 0, _("virtual memory exhausted"));
269 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
270 pathlength = dirlength + 1;
271 /* Give `path' a dummy value; it will be reallocated before first use. */
272 path = xmalloc (pathlength);
274 path[dirlength - 1] = '/';
276 for (namep = name_space; *namep; namep += filelength - dirlength)
278 filelength = dirlength + strlen (namep) + 1;
279 if (filelength > pathlength)
281 pathlength = filelength * 2;
282 path = xrealloc (path, pathlength);
284 strcpy (path + dirlength, namep);
285 errors |= change_file_group (path, group);
296 fprintf (stderr, _("Try `%s --help' for more information.\n"),
301 Usage: %s [OPTION]... GROUP FILE...\n\
302 or: %s [OPTION]... --reference=RFILE FILE...\n\
304 program_name, program_name);
306 Change the group membership of each FILE to GROUP.\n\
308 -c, --changes like verbose but report only when a change is made\n\
309 -h, --no-dereference affect symbolic links instead of any referenced file\n\
310 (available only on systems with lchown system call)\n\
311 -f, --silent, --quiet suppress most error messages\n\
312 --reference=RFILE use RFILE's group instead of using a GROUP value\n\
313 -R, --recursive change files and directories recursively\n\
314 -v, --verbose output a diagnostic for every file processed\n\
315 --help display this help and exit\n\
316 --version output version information and exit\n\
318 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
325 main (int argc, char **argv)
331 program_name = argv[0];
332 setlocale (LC_ALL, "");
333 bindtextdomain (PACKAGE, LOCALEDIR);
334 textdomain (PACKAGE);
336 recurse = force_silent = 0;
338 while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
345 reference_file = optarg;
351 verbosity = V_changes_only;
362 case_GETOPT_HELP_CHAR;
363 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, "David MacKenzie");
369 if (argc - optind + (reference_file ? 1 : 0) <= 1)
371 error (0, 0, _("too few arguments"));
378 error (1, 0, _("--no-dereference (-h) is not supported on this system"));
384 struct stat ref_stats;
385 if (stat (reference_file, &ref_stats))
386 error (1, errno, "%s", reference_file);
388 group = ref_stats.st_gid;
391 parse_group (argv[optind++], &group);
393 for (; optind < argc; ++optind)
394 errors |= change_file_group (argv[optind], group);
396 if (verbosity != V_off)