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>
30 #ifndef _POSIX_VERSION
31 struct group *getgrnam ();
38 char *group_member ();
43 static int change_file_group ();
44 static int change_dir_group ();
45 static int isnumber ();
46 static void describe_change ();
47 static void parse_group ();
50 /* The name the program was run with. */
53 /* If nonzero, change the ownership of directories recursively. */
56 /* If nonzero, force silence (no error messages). */
57 static int force_silent;
59 /* If nonzero, describe the files we process. */
62 /* If nonzero, describe only owners or groups that change. */
63 static int changes_only;
65 /* The name of the group to which ownership of the files is being given. */
66 static char *groupname;
68 /* If non-zero, display usage information and exit. */
71 /* If non-zero, print the version on standard output and exit. */
72 static int show_version;
74 static struct option const long_options[] =
76 {"recursive", no_argument, 0, 'R'},
77 {"changes", no_argument, 0, 'c'},
78 {"silent", no_argument, 0, 'f'},
79 {"quiet", no_argument, 0, 'f'},
80 {"verbose", no_argument, 0, 'v'},
81 {"help", no_argument, &show_help, 1},
82 {"version", no_argument, &show_version, 1},
95 program_name = argv[0];
96 recurse = force_silent = verbose = changes_only = 0;
98 while ((optc = getopt_long (argc, argv, "Rcfv", long_options, (int *) 0))
125 printf ("%s\n", version_string);
132 if (argc - optind <= 1)
134 error (0, 0, "too few arguments");
138 parse_group (argv[optind++], &group);
140 for (; optind < argc; ++optind)
141 errors |= change_file_group (argv[optind], group);
146 /* Set *G according to NAME. */
149 parse_group (name, g)
157 error (1, 0, "can not change to null group");
159 grp = getgrnam (name);
162 if (!isnumber (name))
163 error (1, 0, "invalid group `%s'", name);
168 endgrent (); /* Save a file descriptor. */
171 /* Change the ownership of FILE to GID GROUP.
172 If it is a directory and -R is given, recurse.
173 Return 0 if successful, 1 if errors occurred. */
176 change_file_group (file, group)
180 struct stat file_stats;
183 if (lstat (file, &file_stats))
185 if (force_silent == 0)
186 error (0, errno, "%s", file);
190 if (group != file_stats.st_gid)
193 describe_change (file, 1);
194 if (chown (file, file_stats.st_uid, group))
197 if (force_silent == 0)
199 /* Give a more specific message. Some systems set errno
200 to EPERM for both `inaccessible file' and `user not a member
201 of the specified group' errors. */
202 if (errno == EPERM && !group_member (group))
204 error (0, errno, "you are not a member of group `%s'",
209 error (0, errno, "%s", file);
214 else if (verbose && changes_only == 0)
215 describe_change (file, 0);
217 if (recurse && S_ISDIR (file_stats.st_mode))
218 errors |= change_dir_group (file, group, &file_stats);
222 /* Recursively change the ownership of the files in directory DIR
224 STATP points to the results of lstat on DIR.
225 Return 0 if successful, 1 if errors occurred. */
228 change_dir_group (dir, group, statp)
233 char *name_space, *namep;
234 char *path; /* Full path of each entry to process. */
235 unsigned dirlength; /* Length of `dir' and '\0'. */
236 unsigned filelength; /* Length of each pathname to process. */
237 unsigned pathlength; /* Bytes allocated for `path'. */
241 name_space = savedir (dir, statp->st_size);
242 if (name_space == NULL)
246 if (force_silent == 0)
247 error (0, errno, "%s", dir);
251 error (1, 0, "virtual memory exhausted");
254 dirlength = strlen (dir) + 1; /* + 1 is for the trailing '/'. */
255 pathlength = dirlength + 1;
256 /* Give `path' a dummy value; it will be reallocated before first use. */
257 path = xmalloc (pathlength);
259 path[dirlength - 1] = '/';
261 for (namep = name_space; *namep; namep += filelength - dirlength)
263 filelength = dirlength + strlen (namep) + 1;
264 if (filelength > pathlength)
266 pathlength = filelength * 2;
267 path = xrealloc (path, pathlength);
269 strcpy (path + dirlength, namep);
270 errors |= change_file_group (path, group);
277 /* Tell the user the group name to which ownership of FILE
278 has been given; if CHANGED is zero, FILE was that group already. */
281 describe_change (file, changed)
286 printf ("group of %s changed to %s\n", file, groupname);
288 printf ("group of %s retained as %s\n", file, groupname);
291 /* Return nonzero if STR represents an unsigned decimal integer,
292 otherwise return 0. */
309 fprintf (stderr, "Try `%s --help' for more information.\n",
313 printf ("Usage: %s [OPTION]... GROUP FILE...\n", program_name);
316 -c, --changes like verbose but report only when a change is made\n\
317 -f, --silent, --quiet suppress most error messages\n\
318 -v, --verbose output a diagnostic for every file processed\n\
319 -R, --recursive change files and directories recursively\n\
320 --help display this help and exit\n\
321 --version output version information and exit\n");