1 /* chgrp -- change group ownership of files
2 Copyright (C) 1989-1991, 1995-2011 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"
29 #include "group-member.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 error (EXIT_FAILURE
, 0, _("invalid group: %s"), quote (name
));
96 endgrent (); /* Save a file descriptor. */
105 if (status
!= EXIT_SUCCESS
)
106 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
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\
119 -c, --changes like verbose but report only when a change is made\n\
120 --dereference affect the referent of each symbolic link (this is\n\
121 the default), rather than the symbolic link itself\n\
124 -h, --no-dereference affect each symbolic link instead of any referenced\n\
125 file (useful only on systems that can change the\n\
126 ownership of a symlink)\n\
129 --no-preserve-root do not treat `/' specially (the default)\n\
130 --preserve-root fail to operate recursively on `/'\n\
133 -f, --silent, --quiet suppress most error messages\n\
134 --reference=RFILE use RFILE's group rather than specifying a\n\
136 -R, --recursive operate on files and directories recursively\n\
137 -v, --verbose output a diagnostic for every file processed\n\
141 The following options modify how a hierarchy is traversed when the -R\n\
142 option is also specified. If more than one is specified, only the final\n\
145 -H if a command line argument is a symbolic link\n\
146 to a directory, traverse it\n\
147 -L traverse every symbolic link to a directory\n\
149 -P do not traverse any symbolic links (default)\n\
152 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
153 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
157 %s staff /u Change the group of /u to \"staff\".\n\
158 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
160 program_name
, program_name
);
161 emit_ancillary_info ();
167 main (int argc
, char **argv
)
169 bool preserve_root
= false;
172 /* Bit flags that control how fts works. */
173 int bit_flags
= FTS_PHYSICAL
;
175 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
177 int dereference
= -1;
179 struct Chown_option chopt
;
183 initialize_main (&argc
, &argv
);
184 set_program_name (argv
[0]);
185 setlocale (LC_ALL
, "");
186 bindtextdomain (PACKAGE
, LOCALEDIR
);
187 textdomain (PACKAGE
);
189 atexit (close_stdout
);
193 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, NULL
))
198 case 'H': /* Traverse command-line symlinks-to-directories. */
199 bit_flags
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
202 case 'L': /* Traverse all symlinks-to-directories. */
203 bit_flags
= FTS_LOGICAL
;
206 case 'P': /* Traverse no symlinks-to-directories. */
207 bit_flags
= FTS_PHYSICAL
;
210 case 'h': /* --no-dereference: affect symlinks */
214 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
219 case NO_PRESERVE_ROOT
:
220 preserve_root
= false;
224 preserve_root
= true;
227 case REFERENCE_FILE_OPTION
:
228 reference_file
= optarg
;
232 chopt
.recurse
= true;
236 chopt
.verbosity
= V_changes_only
;
240 chopt
.force_silent
= true;
244 chopt
.verbosity
= V_high
;
247 case_GETOPT_HELP_CHAR
;
248 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
250 usage (EXIT_FAILURE
);
256 if (bit_flags
== FTS_PHYSICAL
)
258 if (dereference
== 1)
259 error (EXIT_FAILURE
, 0,
260 _("-R --dereference requires either -H or -L"));
266 bit_flags
= FTS_PHYSICAL
;
268 chopt
.affect_symlink_referent
= (dereference
!= 0);
270 if (argc
- optind
< (reference_file
? 1 : 2))
273 error (0, 0, _("missing operand"));
275 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
276 usage (EXIT_FAILURE
);
281 struct stat ref_stats
;
282 if (stat (reference_file
, &ref_stats
))
283 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
284 quote (reference_file
));
286 gid
= ref_stats
.st_gid
;
287 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
291 char *group_name
= argv
[optind
++];
292 chopt
.group_name
= (*group_name
? group_name
: NULL
);
293 gid
= parse_group (group_name
);
296 if (chopt
.recurse
&& preserve_root
)
298 static struct dev_ino dev_ino_buf
;
299 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
300 if (chopt
.root_dev_ino
== NULL
)
301 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
305 bit_flags
|= FTS_DEFER_STAT
;
306 ok
= chown_files (argv
+ optind
, bit_flags
,
308 (uid_t
) -1, (gid_t
) -1, &chopt
);
312 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);