1 /* chgrp -- change group ownership of files
2 Copyright (C) 1989-2015 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 <http://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21 #include <sys/types.h>
26 #include "chown-core.h"
30 #include "root-dev-ino.h"
33 /* The official name of this program (e.g., no 'g' prefix). */
34 #define PROGRAM_NAME "chgrp"
37 proper_name ("David MacKenzie"), \
38 proper_name ("Jim Meyering")
41 # define endgrent() ((void) 0)
44 /* The argument to the --reference option. Use the group ID of this file.
45 This file must exist. */
46 static char *reference_file
;
48 /* For long options that have no equivalent short option, use a
49 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
52 DEREFERENCE_OPTION
= CHAR_MAX
+ 1,
58 static struct option
const long_options
[] =
60 {"recursive", no_argument
, NULL
, 'R'},
61 {"changes", no_argument
, NULL
, 'c'},
62 {"dereference", no_argument
, NULL
, DEREFERENCE_OPTION
},
63 {"no-dereference", no_argument
, NULL
, 'h'},
64 {"no-preserve-root", no_argument
, NULL
, NO_PRESERVE_ROOT
},
65 {"preserve-root", no_argument
, NULL
, PRESERVE_ROOT
},
66 {"quiet", no_argument
, NULL
, 'f'},
67 {"silent", no_argument
, NULL
, 'f'},
68 {"reference", required_argument
, NULL
, REFERENCE_FILE_OPTION
},
69 {"verbose", no_argument
, NULL
, 'v'},
70 {GETOPT_HELP_OPTION_DECL
},
71 {GETOPT_VERSION_OPTION_DECL
},
75 /* Return the group ID of NAME, or -1 if no name was specified. */
78 parse_group (const char *name
)
84 struct group
*grp
= getgrnam (name
);
89 unsigned long int tmp
;
90 if (! (xstrtoul (name
, NULL
, 10, &tmp
, "") == LONGINT_OK
92 error (EXIT_FAILURE
, 0, _("invalid group: %s"),
96 endgrent (); /* Save a file descriptor. */
105 if (status
!= EXIT_SUCCESS
)
110 Usage: %s [OPTION]... GROUP FILE...\n\
111 or: %s [OPTION]... --reference=RFILE FILE...\n\
113 program_name
, program_name
);
115 Change the group of each FILE to GROUP.\n\
116 With --reference, change the group of each FILE to that of RFILE.\n\
120 -c, --changes like verbose but report only when a change is made\n\
121 -f, --silent, --quiet suppress most error messages\n\
122 -v, --verbose output a diagnostic for every file processed\n\
125 --dereference affect the referent of each symbolic link (this is\n\
126 the default), rather than the symbolic link itself\n\
127 -h, --no-dereference affect symbolic links instead of any referenced file\n\
130 (useful only on systems that can change the\n\
131 ownership of a symlink)\n\
134 --no-preserve-root do not treat '/' specially (the default)\n\
135 --preserve-root fail to operate recursively on '/'\n\
138 --reference=RFILE use RFILE's group rather than specifying a\n\
142 -R, --recursive operate on files and directories recursively\n\
146 The following options modify how a hierarchy is traversed when the -R\n\
147 option is also specified. If more than one is specified, only the final\n\
150 -H if a command line argument is a symbolic link\n\
151 to a directory, traverse it\n\
152 -L traverse every symbolic link to a directory\n\
154 -P do not traverse any symbolic links (default)\n\
157 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
158 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
162 %s staff /u Change the group of /u to \"staff\".\n\
163 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
165 program_name
, program_name
);
166 emit_ancillary_info (PROGRAM_NAME
);
172 main (int argc
, char **argv
)
174 bool preserve_root
= false;
177 /* Bit flags that control how fts works. */
178 int bit_flags
= FTS_PHYSICAL
;
180 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
182 int dereference
= -1;
184 struct Chown_option chopt
;
188 initialize_main (&argc
, &argv
);
189 set_program_name (argv
[0]);
190 setlocale (LC_ALL
, "");
191 bindtextdomain (PACKAGE
, LOCALEDIR
);
192 textdomain (PACKAGE
);
194 atexit (close_stdout
);
198 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, NULL
))
203 case 'H': /* Traverse command-line symlinks-to-directories. */
204 bit_flags
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
207 case 'L': /* Traverse all symlinks-to-directories. */
208 bit_flags
= FTS_LOGICAL
;
211 case 'P': /* Traverse no symlinks-to-directories. */
212 bit_flags
= FTS_PHYSICAL
;
215 case 'h': /* --no-dereference: affect symlinks */
219 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
224 case NO_PRESERVE_ROOT
:
225 preserve_root
= false;
229 preserve_root
= true;
232 case REFERENCE_FILE_OPTION
:
233 reference_file
= optarg
;
237 chopt
.recurse
= true;
241 chopt
.verbosity
= V_changes_only
;
245 chopt
.force_silent
= true;
249 chopt
.verbosity
= V_high
;
252 case_GETOPT_HELP_CHAR
;
253 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
255 usage (EXIT_FAILURE
);
261 if (bit_flags
== FTS_PHYSICAL
)
263 if (dereference
== 1)
264 error (EXIT_FAILURE
, 0,
265 _("-R --dereference requires either -H or -L"));
271 bit_flags
= FTS_PHYSICAL
;
273 chopt
.affect_symlink_referent
= (dereference
!= 0);
275 if (argc
- optind
< (reference_file
? 1 : 2))
278 error (0, 0, _("missing operand"));
280 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
281 usage (EXIT_FAILURE
);
286 struct stat ref_stats
;
287 if (stat (reference_file
, &ref_stats
))
288 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
289 quoteaf (reference_file
));
291 gid
= ref_stats
.st_gid
;
292 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
296 char *group_name
= argv
[optind
++];
297 chopt
.group_name
= (*group_name
? group_name
: NULL
);
298 gid
= parse_group (group_name
);
301 if (chopt
.recurse
&& preserve_root
)
303 static struct dev_ino dev_ino_buf
;
304 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
305 if (chopt
.root_dev_ino
== NULL
)
306 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
310 bit_flags
|= FTS_DEFER_STAT
;
311 ok
= chown_files (argv
+ optind
, bit_flags
,
313 (uid_t
) -1, (gid_t
) -1, &chopt
);
317 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;