1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2007 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"
37 #define AUTHORS "David MacKenzie", "Jim Meyering"
40 # define endgrent() ((void) 0)
43 /* The name the program was run with. */
46 /* The argument to the --reference option. Use the group ID of this file.
47 This file must exist. */
48 static char *reference_file
;
50 /* For long options that have no equivalent short option, use a
51 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
54 DEREFERENCE_OPTION
= CHAR_MAX
+ 1,
60 static struct option
const long_options
[] =
62 {"recursive", no_argument
, NULL
, 'R'},
63 {"changes", no_argument
, NULL
, 'c'},
64 {"dereference", no_argument
, NULL
, DEREFERENCE_OPTION
},
65 {"no-dereference", no_argument
, NULL
, 'h'},
66 {"no-preserve-root", no_argument
, NULL
, NO_PRESERVE_ROOT
},
67 {"preserve-root", no_argument
, NULL
, PRESERVE_ROOT
},
68 {"quiet", no_argument
, NULL
, 'f'},
69 {"silent", no_argument
, NULL
, 'f'},
70 {"reference", required_argument
, NULL
, REFERENCE_FILE_OPTION
},
71 {"verbose", no_argument
, NULL
, 'v'},
72 {GETOPT_HELP_OPTION_DECL
},
73 {GETOPT_VERSION_OPTION_DECL
},
77 /* Return the group ID of NAME, or -1 if no name was specified. */
80 parse_group (const char *name
)
86 struct group
*grp
= getgrnam (name
);
91 unsigned long int tmp
;
92 if (! (xstrtoul (name
, NULL
, 10, &tmp
, "") == LONGINT_OK
94 error (EXIT_FAILURE
, 0, _("invalid group: %s"), quote (name
));
97 endgrent (); /* Save a file descriptor. */
106 if (status
!= EXIT_SUCCESS
)
107 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
112 Usage: %s [OPTION]... GROUP FILE...\n\
113 or: %s [OPTION]... --reference=RFILE FILE...\n\
115 program_name
, program_name
);
117 Change the group of each FILE to GROUP.\n\
118 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 --dereference affect the referent of each symbolic link (this is\n\
122 the default), rather than the symbolic link itself\n\
125 -h, --no-dereference affect each symbolic link instead of any referenced\n\
126 file (useful only on systems that can change the\n\
127 ownership of a symlink)\n\
130 --no-preserve-root do not treat `/' specially (the default)\n\
131 --preserve-root fail to operate recursively on `/'\n\
134 -f, --silent, --quiet suppress most error messages\n\
135 --reference=RFILE use RFILE's group rather than specifying a\n\
137 -R, --recursive operate on files and directories recursively\n\
138 -v, --verbose output a diagnostic for every file processed\n\
142 The following options modify how a hierarchy is traversed when the -R\n\
143 option is also specified. If more than one is specified, only the final\n\
146 -H if a command line argument is a symbolic link\n\
147 to a directory, traverse it\n\
148 -L traverse every symbolic link to a directory\n\
150 -P do not traverse any symbolic links (default)\n\
153 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
154 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
158 %s staff /u Change the group of /u to \"staff\".\n\
159 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
161 program_name
, program_name
);
162 emit_bug_reporting_address ();
168 main (int argc
, char **argv
)
170 bool preserve_root
= false;
173 /* Bit flags that control how fts works. */
174 int bit_flags
= FTS_PHYSICAL
;
176 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
178 int dereference
= -1;
180 struct Chown_option chopt
;
184 initialize_main (&argc
, &argv
);
185 program_name
= argv
[0];
186 setlocale (LC_ALL
, "");
187 bindtextdomain (PACKAGE
, LOCALEDIR
);
188 textdomain (PACKAGE
);
190 atexit (close_stdout
);
194 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, NULL
))
199 case 'H': /* Traverse command-line symlinks-to-directories. */
200 bit_flags
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
203 case 'L': /* Traverse all symlinks-to-directories. */
204 bit_flags
= FTS_LOGICAL
;
207 case 'P': /* Traverse no symlinks-to-directories. */
208 bit_flags
= FTS_PHYSICAL
;
211 case 'h': /* --no-dereference: affect symlinks */
215 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
220 case NO_PRESERVE_ROOT
:
221 preserve_root
= false;
225 preserve_root
= true;
228 case REFERENCE_FILE_OPTION
:
229 reference_file
= optarg
;
233 chopt
.recurse
= true;
237 chopt
.verbosity
= V_changes_only
;
241 chopt
.force_silent
= true;
245 chopt
.verbosity
= V_high
;
248 case_GETOPT_HELP_CHAR
;
249 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
251 usage (EXIT_FAILURE
);
257 if (bit_flags
== FTS_PHYSICAL
)
259 if (dereference
== 1)
260 error (EXIT_FAILURE
, 0,
261 _("-R --dereference requires either -H or -L"));
267 bit_flags
= FTS_PHYSICAL
;
269 chopt
.affect_symlink_referent
= (dereference
!= 0);
271 if (argc
- optind
< (reference_file
? 1 : 2))
274 error (0, 0, _("missing operand"));
276 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
277 usage (EXIT_FAILURE
);
282 struct stat ref_stats
;
283 if (stat (reference_file
, &ref_stats
))
284 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
285 quote (reference_file
));
287 gid
= ref_stats
.st_gid
;
288 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
292 char *group_name
= argv
[optind
++];
293 chopt
.group_name
= (*group_name
? group_name
: NULL
);
294 gid
= parse_group (group_name
);
297 if (chopt
.recurse
& preserve_root
)
299 static struct dev_ino dev_ino_buf
;
300 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
301 if (chopt
.root_dev_ino
== NULL
)
302 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
306 ok
= chown_files (argv
+ optind
, bit_flags
,
308 (uid_t
) -1, (gid_t
) -1, &chopt
);
312 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);