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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
22 #include <sys/types.h>
28 #include "safe-lstat.h"
31 #ifndef _POSIX_VERSION
32 struct group *getgrnam ();
39 char *group_member ();
44 static int change_file_group ();
45 static int change_dir_group ();
46 static int isnumber ();
47 static void describe_change ();
48 static void parse_group ();
51 /* The name the program was run with. */
54 /* If nonzero, change the ownership of directories recursively. */
57 /* If nonzero, force silence (no error messages). */
58 static int force_silent;
60 /* If nonzero, describe the files we process. */
63 /* If nonzero, describe only owners or groups that change. */
64 static int changes_only;
66 /* The name of the group to which ownership of the files is being given. */
67 static char *groupname;
69 /* If non-zero, display usage information and exit. */
72 /* If non-zero, print the version on standard output and exit. */
73 static int show_version;
75 static struct option const long_options[] =
77 {"recursive", no_argument, 0, 'R'},
78 {"changes", no_argument, 0, 'c'},
79 {"silent", no_argument, 0, 'f'},
80 {"quiet", no_argument, 0, 'f'},
81 {"verbose", no_argument, 0, 'v'},
82 {"help", no_argument, &show_help, 1},
83 {"version", no_argument, &show_version, 1},
96 program_name = argv[0];
97 recurse = force_silent = verbose = changes_only = 0;
99 while ((optc = getopt_long (argc, argv, "Rcfv", long_options, (int *) 0))
126 printf ("%s\n", version_string);
133 if (argc - optind <= 1)
135 error (0, 0, "too few arguments");
139 parse_group (argv[optind++], &group);
141 for (; optind < argc; ++optind)
142 errors |= change_file_group (argv[optind], group);
147 /* Set *G according to NAME. */
150 parse_group (name, g)
158 error (1, 0, "can not change to null group");
160 grp = getgrnam (name);
163 if (!isnumber (name))
164 error (1, 0, "invalid group `%s'", name);
169 endgrent (); /* Save a file descriptor. */
172 /* Change the ownership of FILE to GID GROUP.
173 If it is a directory and -R is given, recurse.
174 Return 0 if successful, 1 if errors occurred. */
177 change_file_group (file, group)
181 struct stat file_stats;
184 if (safe_lstat (file, &file_stats))
186 if (force_silent == 0)
187 error (0, errno, "%s", file);
191 if (group != file_stats.st_gid)
194 describe_change (file, 1);
195 if (chown (file, file_stats.st_uid, group))
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'",
210 error (0, errno, "%s", file);
215 else if (verbose && changes_only == 0)
216 describe_change (file, 0);
218 if (recurse && S_ISDIR (file_stats.st_mode))
219 errors |= change_dir_group (file, group, &file_stats);
223 /* Recursively change the ownership of the files in directory DIR
225 STATP points to the results of lstat on DIR.
226 Return 0 if successful, 1 if errors occurred. */
229 change_dir_group (dir, group, statp)
234 char *name_space, *namep;
235 char *path; /* Full path of each entry to process. */
236 unsigned dirlength; /* Length of `dir' and '\0'. */
237 unsigned filelength; /* Length of each pathname to process. */
238 unsigned pathlength; /* Bytes allocated for `path'. */
242 name_space = savedir (dir, statp->st_size);
243 if (name_space == NULL)
247 if (force_silent == 0)
248 error (0, errno, "%s", dir);
252 error (1, 0, "virtual memory exhausted");
255 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
256 pathlength = dirlength + 1;
257 /* Give `path' a dummy value; it will be reallocated before first use. */
258 path = xmalloc (pathlength);
260 path[dirlength - 1] = '/';
262 for (namep = name_space; *namep; namep += filelength - dirlength)
264 filelength = dirlength + strlen (namep) + 1;
265 if (filelength > pathlength)
267 pathlength = filelength * 2;
268 path = xrealloc (path, pathlength);
270 strcpy (path + dirlength, namep);
271 errors |= change_file_group (path, group);
278 /* Tell the user the group name to which ownership of FILE
279 has been given; if CHANGED is zero, FILE was that group already. */
282 describe_change (file, changed)
287 printf ("group of %s changed to %s\n", file, groupname);
289 printf ("group of %s retained as %s\n", file, groupname);
292 /* Return nonzero if STR represents an unsigned decimal integer,
293 otherwise return 0. */
310 fprintf (stderr, "Try `%s --help' for more information.\n",
314 printf ("Usage: %s [OPTION]... GROUP FILE...\n", program_name);
317 -c, --changes like verbose but report only when a change is made\n\
318 -f, --silent, --quiet suppress most error messages\n\
319 -v, --verbose output a diagnostic for every file processed\n\
320 -R, --recursive change files and directories recursively\n\
321 --help display this help and exit\n\
322 --version output version information and exit\n");