Sanitize environment.
[coreutils.git] / src / chgrp.c
blob824f40ee2857f148bcf42bf43e9d544865c2ebf7
1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2004 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)
7 any later version.
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>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <grp.h>
24 #include <getopt.h>
26 #include "system.h"
27 #include "chown-core.h"
28 #include "error.h"
29 #include "fts_.h"
30 #include "group-member.h"
31 #include "lchown.h"
32 #include "quote.h"
33 #include "xstrtol.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "chgrp"
38 #define AUTHORS "David MacKenzie", "Jim Meyering"
40 #ifndef _POSIX_VERSION
41 struct group *getgrnam ();
42 #endif
44 #if ! HAVE_ENDGRENT
45 # define endgrent() ((void) 0)
46 #endif
48 /* The name the program was run with. */
49 char *program_name;
51 /* The argument to the --reference option. Use the group ID of this file.
52 This file must exist. */
53 static char *reference_file;
55 /* For long options that have no equivalent short option, use a
56 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
57 enum
59 DEREFERENCE_OPTION = CHAR_MAX + 1,
60 REFERENCE_FILE_OPTION
63 static struct option const long_options[] =
65 {"recursive", no_argument, 0, 'R'},
66 {"changes", no_argument, 0, 'c'},
67 {"dereference", no_argument, 0, DEREFERENCE_OPTION},
68 {"no-dereference", no_argument, 0, 'h'},
69 {"quiet", no_argument, 0, 'f'},
70 {"silent", no_argument, 0, 'f'},
71 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
72 {"verbose", no_argument, 0, 'v'},
73 {GETOPT_HELP_OPTION_DECL},
74 {GETOPT_VERSION_OPTION_DECL},
75 {0, 0, 0, 0}
78 /* Return the group ID of NAME, or -1 if no name was specified. */
80 static gid_t
81 parse_group (const char *name)
83 gid_t gid = -1;
85 if (*name)
87 struct group *grp = getgrnam (name);
88 if (grp)
89 gid = grp->gr_gid;
90 else
92 unsigned long int tmp;
93 if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
94 && tmp <= GID_T_MAX))
95 error (EXIT_FAILURE, 0, _("invalid group %s"), quote (name));
96 gid = tmp;
98 endgrent (); /* Save a file descriptor. */
101 return gid;
104 void
105 usage (int status)
107 if (status != EXIT_SUCCESS)
108 fprintf (stderr, _("Try `%s --help' for more information.\n"),
109 program_name);
110 else
112 printf (_("\
113 Usage: %s [OPTION]... GROUP FILE...\n\
114 or: %s [OPTION]... --reference=RFILE FILE...\n\
116 program_name, program_name);
117 fputs (_("\
118 Change the group of each FILE to GROUP.\n\
119 With --reference, change the group of each FILE to that of RFILE.\n\
121 -c, --changes like verbose but report only when a change is made\n\
122 --dereference affect the referent of each symbolic link, rather\n\
123 than the symbolic link itself (this is the default)\n\
124 "), stdout);
125 fputs (_("\
126 -h, --no-dereference affect each symbolic link instead of any referenced\n\
127 file (useful only on systems that can change the\n\
128 ownership of a symlink)\n\
129 "), stdout);
130 fputs (_("\
131 --no-preserve-root do not treat `/' specially (the default)\n\
132 --preserve-root fail to operate recursively on `/'\n\
133 "), stdout);
134 fputs (_("\
135 -f, --silent, --quiet suppress most error messages\n\
136 --reference=RFILE use RFILE's group rather than the specifying\n\
137 GROUP value\n\
138 -R, --recursive operate on files and directories recursively\n\
139 -v, --verbose output a diagnostic for every file processed\n\
141 "), stdout);
142 fputs (_("\
143 The following options modify how a hierarchy is traversed when the -R\n\
144 option is also specified. If more than one is specified, only the final\n\
145 one takes effect.\n\
147 -H if a command line argument is a symbolic link\n\
148 to a directory, traverse it\n\
149 -L traverse every symbolic link to a directory\n\
150 encountered\n\
151 -P do not traverse any symbolic links (default)\n\
153 "), stdout);
154 fputs (HELP_OPTION_DESCRIPTION, stdout);
155 fputs (VERSION_OPTION_DESCRIPTION, stdout);
156 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
158 exit (status);
162 main (int argc, char **argv)
164 gid_t gid;
166 /* Bit flags that control how fts works. */
167 int bit_flags = FTS_PHYSICAL;
169 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
170 specified. */
171 int dereference = -1;
173 struct Chown_option chopt;
174 bool ok;
175 int optc;
177 initialize_main (&argc, &argv);
178 program_name = argv[0];
179 setlocale (LC_ALL, "");
180 bindtextdomain (PACKAGE, LOCALEDIR);
181 textdomain (PACKAGE);
183 atexit (close_stdout);
185 chopt_init (&chopt);
187 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
188 != -1)
190 switch (optc)
192 case 'H': /* Traverse command-line symlinks-to-directories. */
193 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
194 break;
196 case 'L': /* Traverse all symlinks-to-directories. */
197 bit_flags = FTS_LOGICAL;
198 break;
200 case 'P': /* Traverse no symlinks-to-directories. */
201 bit_flags = FTS_PHYSICAL;
202 break;
204 case 'h': /* --no-dereference: affect symlinks */
205 dereference = 0;
206 break;
208 case DEREFERENCE_OPTION: /* --dereference: affect the referent
209 of each symlink */
210 dereference = 1;
211 break;
213 case REFERENCE_FILE_OPTION:
214 reference_file = optarg;
215 break;
217 case 'R':
218 chopt.recurse = true;
219 break;
221 case 'c':
222 chopt.verbosity = V_changes_only;
223 break;
225 case 'f':
226 chopt.force_silent = true;
227 break;
229 case 'v':
230 chopt.verbosity = V_high;
231 break;
233 case_GETOPT_HELP_CHAR;
234 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
235 default:
236 usage (EXIT_FAILURE);
240 if (chopt.recurse)
242 if (bit_flags == FTS_PHYSICAL)
244 if (dereference == 1)
245 error (EXIT_FAILURE, 0,
246 _("-R --dereference requires either -H or -L"));
247 chopt.affect_symlink_referent = false;
249 else
251 if (dereference == 0)
252 error (EXIT_FAILURE, 0, _("-R -h requires -P"));
253 chopt.affect_symlink_referent = true;
256 else
258 bit_flags = FTS_PHYSICAL;
259 chopt.affect_symlink_referent = (dereference != 0);
262 if (argc - optind < (reference_file ? 1 : 2))
264 if (argc <= optind)
265 error (0, 0, _("missing operand"));
266 else
267 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
268 usage (EXIT_FAILURE);
271 if (reference_file)
273 struct stat ref_stats;
274 if (stat (reference_file, &ref_stats))
275 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
276 quote (reference_file));
278 gid = ref_stats.st_gid;
279 chopt.group_name = gid_to_name (ref_stats.st_gid);
281 else
283 char *group_name = argv[optind++];
284 chopt.group_name = (*group_name ? group_name : NULL);
285 gid = parse_group (group_name);
288 ok = chown_files (argv + optind, bit_flags,
289 (uid_t) -1, gid,
290 (uid_t) -1, (gid_t) -1, &chopt);
292 chopt_free (&chopt);
294 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);