summaryrefslogtreecommitdiff
path: root/src/chgrp.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/chgrp.c')
-rw-r--r--src/chgrp.c214
1 files changed, 109 insertions, 105 deletions
diff --git a/src/chgrp.c b/src/chgrp.c
index faf58d3..711737d 100644
--- a/src/chgrp.c
+++ b/src/chgrp.c
@@ -1,10 +1,10 @@
/* chgrp -- change group ownership of files
- Copyright (C) 89, 90, 91, 1995-2006 Free Software Foundation, Inc.
+ Copyright (C) 1989-2016 Free Software Foundation, Inc.
- This program is free software; you can redistribute it and/or modify
+ This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -12,8 +12,7 @@
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software Foundation,
- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
@@ -27,23 +26,21 @@
#include "chown-core.h"
#include "error.h"
#include "fts_.h"
-#include "group-member.h"
#include "quote.h"
#include "root-dev-ino.h"
#include "xstrtol.h"
-/* The official name of this program (e.g., no `g' prefix). */
+/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "chgrp"
-#define AUTHORS "David MacKenzie", "Jim Meyering"
+#define AUTHORS \
+ proper_name ("David MacKenzie"), \
+ proper_name ("Jim Meyering")
#if ! HAVE_ENDGRENT
# define endgrent() ((void) 0)
#endif
-/* The name the program was run with. */
-char *program_name;
-
/* The argument to the --reference option. Use the group ID of this file.
This file must exist. */
static char *reference_file;
@@ -86,15 +83,16 @@ parse_group (const char *name)
{
struct group *grp = getgrnam (name);
if (grp)
- gid = grp->gr_gid;
+ gid = grp->gr_gid;
else
- {
- unsigned long int tmp;
- if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
- && tmp <= GID_T_MAX))
- error (EXIT_FAILURE, 0, _("invalid group %s"), quote (name));
- gid = tmp;
- }
+ {
+ unsigned long int tmp;
+ if (! (xstrtoul (name, NULL, 10, &tmp, "") == LONGINT_OK
+ && tmp <= GID_T_MAX))
+ error (EXIT_FAILURE, 0, _("invalid group: %s"),
+ quote (name));
+ gid = tmp;
+ }
endgrent (); /* Save a file descriptor. */
}
@@ -105,41 +103,46 @@ void
usage (int status)
{
if (status != EXIT_SUCCESS)
- fprintf (stderr, _("Try `%s --help' for more information.\n"),
- program_name);
+ emit_try_help ();
else
{
printf (_("\
Usage: %s [OPTION]... GROUP FILE...\n\
or: %s [OPTION]... --reference=RFILE FILE...\n\
"),
- program_name, program_name);
+ program_name, program_name);
fputs (_("\
Change the group of each FILE to GROUP.\n\
With --reference, change the group of each FILE to that of RFILE.\n\
\n\
+"), stdout);
+ fputs (_("\
-c, --changes like verbose but report only when a change is made\n\
+ -f, --silent, --quiet suppress most error messages\n\
+ -v, --verbose output a diagnostic for every file processed\n\
+"), stdout);
+ fputs (_("\
--dereference affect the referent of each symbolic link (this is\n\
the default), rather than the symbolic link itself\n\
+ -h, --no-dereference affect symbolic links instead of any referenced file\n\
"), stdout);
fputs (_("\
- -h, --no-dereference affect each symbolic link instead of any referenced\n\
- file (useful only on systems that can change the\n\
+ (useful only on systems that can change the\n\
ownership of a symlink)\n\
"), stdout);
fputs (_("\
- --no-preserve-root do not treat `/' specially (the default)\n\
- --preserve-root fail to operate recursively on `/'\n\
+ --no-preserve-root do not treat '/' specially (the default)\n\
+ --preserve-root fail to operate recursively on '/'\n\
"), stdout);
fputs (_("\
- -f, --silent, --quiet suppress most error messages\n\
--reference=RFILE use RFILE's group rather than specifying a\n\
GROUP value\n\
+"), stdout);
+ fputs (_("\
-R, --recursive operate on files and directories recursively\n\
- -v, --verbose output a diagnostic for every file processed\n\
-\n\
"), stdout);
fputs (_("\
+\n\
The following options modify how a hierarchy is traversed when the -R\n\
option is also specified. If more than one is specified, only the final\n\
one takes effect.\n\
@@ -159,8 +162,8 @@ Examples:\n\
%s staff /u Change the group of /u to \"staff\".\n\
%s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
"),
- program_name, program_name);
- printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
+ program_name, program_name);
+ emit_ancillary_info (PROGRAM_NAME);
}
exit (status);
}
@@ -183,7 +186,7 @@ main (int argc, char **argv)
int optc;
initialize_main (&argc, &argv);
- program_name = argv[0];
+ set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
@@ -193,75 +196,75 @@ main (int argc, char **argv)
chopt_init (&chopt);
while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
- != -1)
+ != -1)
{
switch (optc)
- {
- case 'H': /* Traverse command-line symlinks-to-directories. */
- bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
- break;
-
- case 'L': /* Traverse all symlinks-to-directories. */
- bit_flags = FTS_LOGICAL;
- break;
-
- case 'P': /* Traverse no symlinks-to-directories. */
- bit_flags = FTS_PHYSICAL;
- break;
-
- case 'h': /* --no-dereference: affect symlinks */
- dereference = 0;
- break;
-
- case DEREFERENCE_OPTION: /* --dereference: affect the referent
- of each symlink */
- dereference = 1;
- break;
-
- case NO_PRESERVE_ROOT:
- preserve_root = false;
- break;
-
- case PRESERVE_ROOT:
- preserve_root = true;
- break;
-
- case REFERENCE_FILE_OPTION:
- reference_file = optarg;
- break;
-
- case 'R':
- chopt.recurse = true;
- break;
-
- case 'c':
- chopt.verbosity = V_changes_only;
- break;
-
- case 'f':
- chopt.force_silent = true;
- break;
-
- case 'v':
- chopt.verbosity = V_high;
- break;
-
- case_GETOPT_HELP_CHAR;
- case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
- default:
- usage (EXIT_FAILURE);
- }
+ {
+ case 'H': /* Traverse command-line symlinks-to-directories. */
+ bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
+ break;
+
+ case 'L': /* Traverse all symlinks-to-directories. */
+ bit_flags = FTS_LOGICAL;
+ break;
+
+ case 'P': /* Traverse no symlinks-to-directories. */
+ bit_flags = FTS_PHYSICAL;
+ break;
+
+ case 'h': /* --no-dereference: affect symlinks */
+ dereference = 0;
+ break;
+
+ case DEREFERENCE_OPTION: /* --dereference: affect the referent
+ of each symlink */
+ dereference = 1;
+ break;
+
+ case NO_PRESERVE_ROOT:
+ preserve_root = false;
+ break;
+
+ case PRESERVE_ROOT:
+ preserve_root = true;
+ break;
+
+ case REFERENCE_FILE_OPTION:
+ reference_file = optarg;
+ break;
+
+ case 'R':
+ chopt.recurse = true;
+ break;
+
+ case 'c':
+ chopt.verbosity = V_changes_only;
+ break;
+
+ case 'f':
+ chopt.force_silent = true;
+ break;
+
+ case 'v':
+ chopt.verbosity = V_high;
+ break;
+
+ case_GETOPT_HELP_CHAR;
+ case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
+ default:
+ usage (EXIT_FAILURE);
+ }
}
if (chopt.recurse)
{
if (bit_flags == FTS_PHYSICAL)
- {
- if (dereference == 1)
- error (EXIT_FAILURE, 0,
- _("-R --dereference requires either -H or -L"));
- dereference = 0;
- }
+ {
+ if (dereference == 1)
+ error (EXIT_FAILURE, 0,
+ _("-R --dereference requires either -H or -L"));
+ dereference = 0;
+ }
}
else
{
@@ -272,9 +275,9 @@ main (int argc, char **argv)
if (argc - optind < (reference_file ? 1 : 2))
{
if (argc <= optind)
- error (0, 0, _("missing operand"));
+ error (0, 0, _("missing operand"));
else
- error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
+ error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
usage (EXIT_FAILURE);
}
@@ -282,8 +285,8 @@ main (int argc, char **argv)
{
struct stat ref_stats;
if (stat (reference_file, &ref_stats))
- error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
- quote (reference_file));
+ error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
+ quoteaf (reference_file));
gid = ref_stats.st_gid;
chopt.group_name = gid_to_name (ref_stats.st_gid);
@@ -295,20 +298,21 @@ main (int argc, char **argv)
gid = parse_group (group_name);
}
- if (chopt.recurse & preserve_root)
+ if (chopt.recurse && preserve_root)
{
static struct dev_ino dev_ino_buf;
chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
if (chopt.root_dev_ino == NULL)
- error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
- quote ("/"));
+ error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
+ quoteaf ("/"));
}
+ bit_flags |= FTS_DEFER_STAT;
ok = chown_files (argv + optind, bit_flags,
- (uid_t) -1, gid,
- (uid_t) -1, (gid_t) -1, &chopt);
+ (uid_t) -1, gid,
+ (uid_t) -1, (gid_t) -1, &chopt);
chopt_free (&chopt);
- exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
+ return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}