1 /* chgrp -- change group ownership of files
2 Copyright (C) 1989-2017 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 3 of the License, or
7 (at your option) 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, see <https://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21 #include <sys/types.h>
26 #include "chown-core.h"
31 #include "root-dev-ino.h"
34 /* The official name of this program (e.g., no 'g' prefix). */
35 #define PROGRAM_NAME "chgrp"
38 proper_name ("David MacKenzie"), \
39 proper_name ("Jim Meyering")
42 # define endgrent() ((void) 0)
45 /* The argument to the --reference option. Use the group ID of this file.
46 This file must exist. */
47 static char *reference_file
;
49 /* For long options that have no equivalent short option, use a
50 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
53 DEREFERENCE_OPTION
= CHAR_MAX
+ 1,
59 static struct option
const long_options
[] =
61 {"recursive", no_argument
, NULL
, 'R'},
62 {"changes", no_argument
, NULL
, 'c'},
63 {"dereference", no_argument
, NULL
, DEREFERENCE_OPTION
},
64 {"no-dereference", no_argument
, NULL
, 'h'},
65 {"no-preserve-root", no_argument
, NULL
, NO_PRESERVE_ROOT
},
66 {"preserve-root", no_argument
, NULL
, PRESERVE_ROOT
},
67 {"quiet", no_argument
, NULL
, 'f'},
68 {"silent", no_argument
, NULL
, 'f'},
69 {"reference", required_argument
, NULL
, REFERENCE_FILE_OPTION
},
70 {"verbose", no_argument
, NULL
, 'v'},
71 {GETOPT_HELP_OPTION_DECL
},
72 {GETOPT_VERSION_OPTION_DECL
},
76 /* Return the group ID of NAME, or -1 if no name was specified. */
79 parse_group (const char *name
)
85 struct group
*grp
= getgrnam (name
);
90 unsigned long int tmp
;
91 if (! (xstrtoul (name
, NULL
, 10, &tmp
, "") == LONGINT_OK
93 die (EXIT_FAILURE
, 0, _("invalid group: %s"),
97 endgrent (); /* Save a file descriptor. */
106 if (status
!= EXIT_SUCCESS
)
111 Usage: %s [OPTION]... GROUP FILE...\n\
112 or: %s [OPTION]... --reference=RFILE FILE...\n\
114 program_name
, program_name
);
116 Change the group of each FILE to GROUP.\n\
117 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 -f, --silent, --quiet suppress most error messages\n\
123 -v, --verbose output a diagnostic for every file processed\n\
126 --dereference affect the referent of each symbolic link (this is\n\
127 the default), rather than the symbolic link itself\n\
128 -h, --no-dereference affect symbolic links instead of any referenced file\n\
131 (useful only on systems that can change the\n\
132 ownership of a symlink)\n\
135 --no-preserve-root do not treat '/' specially (the default)\n\
136 --preserve-root fail to operate recursively on '/'\n\
139 --reference=RFILE use RFILE's group rather than specifying a\n\
143 -R, --recursive operate on files and directories recursively\n\
147 The following options modify how a hierarchy is traversed when the -R\n\
148 option is also specified. If more than one is specified, only the final\n\
151 -H if a command line argument is a symbolic link\n\
152 to a directory, traverse it\n\
153 -L traverse every symbolic link to a directory\n\
155 -P do not traverse any symbolic links (default)\n\
158 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
159 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
163 %s staff /u Change the group of /u to \"staff\".\n\
164 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
166 program_name
, program_name
);
167 emit_ancillary_info (PROGRAM_NAME
);
173 main (int argc
, char **argv
)
175 bool preserve_root
= false;
178 /* Bit flags that control how fts works. */
179 int bit_flags
= FTS_PHYSICAL
;
181 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
183 int dereference
= -1;
185 struct Chown_option chopt
;
189 initialize_main (&argc
, &argv
);
190 set_program_name (argv
[0]);
191 setlocale (LC_ALL
, "");
192 bindtextdomain (PACKAGE
, LOCALEDIR
);
193 textdomain (PACKAGE
);
195 atexit (close_stdout
);
199 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, NULL
))
204 case 'H': /* Traverse command-line symlinks-to-directories. */
205 bit_flags
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
208 case 'L': /* Traverse all symlinks-to-directories. */
209 bit_flags
= FTS_LOGICAL
;
212 case 'P': /* Traverse no symlinks-to-directories. */
213 bit_flags
= FTS_PHYSICAL
;
216 case 'h': /* --no-dereference: affect symlinks */
220 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
225 case NO_PRESERVE_ROOT
:
226 preserve_root
= false;
230 preserve_root
= true;
233 case REFERENCE_FILE_OPTION
:
234 reference_file
= optarg
;
238 chopt
.recurse
= true;
242 chopt
.verbosity
= V_changes_only
;
246 chopt
.force_silent
= true;
250 chopt
.verbosity
= V_high
;
253 case_GETOPT_HELP_CHAR
;
254 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
256 usage (EXIT_FAILURE
);
262 if (bit_flags
== FTS_PHYSICAL
)
264 if (dereference
== 1)
265 die (EXIT_FAILURE
, 0,
266 _("-R --dereference requires either -H or -L"));
272 bit_flags
= FTS_PHYSICAL
;
274 chopt
.affect_symlink_referent
= (dereference
!= 0);
276 if (argc
- optind
< (reference_file
? 1 : 2))
279 error (0, 0, _("missing operand"));
281 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
282 usage (EXIT_FAILURE
);
287 struct stat ref_stats
;
288 if (stat (reference_file
, &ref_stats
))
289 die (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
290 quoteaf (reference_file
));
292 gid
= ref_stats
.st_gid
;
293 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
297 char *group_name
= argv
[optind
++];
298 chopt
.group_name
= (*group_name
? group_name
: NULL
);
299 gid
= parse_group (group_name
);
302 if (chopt
.recurse
&& preserve_root
)
304 static struct dev_ino dev_ino_buf
;
305 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
306 if (chopt
.root_dev_ino
== NULL
)
307 die (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
311 bit_flags
|= FTS_DEFER_STAT
;
312 ok
= chown_files (argv
+ optind
, bit_flags
,
314 (uid_t
) -1, (gid_t
) -1, &chopt
);
318 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;