'sort' race condition fixes.
[coreutils.git] / src / chgrp.c
blob60a18a63d006a43d45bba6befbf618e6e8888b28
1 /* chgrp -- change group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2001 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 2, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <grp.h>
24 #include <getopt.h>
26 #include "system.h"
27 #include "error.h"
28 #include "lchown.h"
29 #include "group-member.h"
30 #include "quote.h"
31 #include "savedir.h"
32 #include "xstrtol.h"
33 #include "chown-core.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME "chgrp"
38 #define AUTHORS "David MacKenzie"
40 /* MAXUID may come from limits.h *or* sys/params.h (via system.h) above. */
41 #ifndef MAXUID
42 # define MAXUID UID_T_MAX
43 #endif
44 #ifndef MAXGID
45 # define MAXGID GID_T_MAX
46 #endif
48 #ifndef _POSIX_VERSION
49 struct group *getgrnam ();
50 #endif
52 #if ! HAVE_ENDGRENT
53 # define endgrent() ((void) 0)
54 #endif
56 /* The name the program was run with. */
57 char *program_name;
59 /* The argument to the --reference option. Use the group ID of this file.
60 This file must exist. */
61 static char *reference_file;
63 /* For long options that have no equivalent short option, use a
64 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
65 enum
67 REFERENCE_FILE_OPTION = CHAR_MAX + 1,
68 DEREFERENCE_OPTION
71 static struct option const long_options[] =
73 {"recursive", no_argument, 0, 'R'},
74 {"changes", no_argument, 0, 'c'},
75 {"dereference", no_argument, 0, DEREFERENCE_OPTION},
76 {"no-dereference", no_argument, 0, 'h'},
77 {"quiet", no_argument, 0, 'f'},
78 {"silent", no_argument, 0, 'f'},
79 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
80 {"verbose", no_argument, 0, 'v'},
81 {GETOPT_HELP_OPTION_DECL},
82 {GETOPT_VERSION_OPTION_DECL},
83 {0, 0, 0, 0}
86 /* Set *G according to NAME. */
88 static void
89 parse_group (const char *name, gid_t *g)
91 struct group *grp;
93 if (*name == '\0')
94 error (1, 0, _("cannot change to null group"));
96 grp = getgrnam (name);
97 if (grp == NULL)
99 strtol_error s_err;
100 unsigned long int tmp_long;
102 if (!ISDIGIT (*name))
103 error (1, 0, _("invalid group name %s"), quote (name));
105 s_err = xstrtoul (name, NULL, 0, &tmp_long, NULL);
106 if (s_err != LONGINT_OK)
107 STRTOL_FATAL_ERROR (name, _("group number"), s_err);
109 if (tmp_long > MAXGID)
110 error (1, 0, _("invalid group number %s"), quote (name));
112 *g = tmp_long;
114 else
115 *g = grp->gr_gid;
116 endgrent (); /* Save a file descriptor. */
119 void
120 usage (int status)
122 if (status != 0)
123 fprintf (stderr, _("Try `%s --help' for more information.\n"),
124 program_name);
125 else
127 printf (_("\
128 Usage: %s [OPTION]... GROUP FILE...\n\
129 or: %s [OPTION]... --reference=RFILE FILE...\n\
131 program_name, program_name);
132 printf (_("\
133 Change the group membership of each FILE to GROUP.\n\
135 -c, --changes like verbose but report only when a change is made\n\
136 --dereference affect the referent of each symbolic link, rather\n\
137 than the symbolic link itself\n\
138 -h, --no-dereference affect symbolic links instead of any referenced file\n\
139 (available only on systems that can change the\n\
140 ownership of a symlink)\n\
141 -f, --silent, --quiet suppress most error messages\n\
142 --reference=RFILE use RFILE's group rather than the specified\n\
143 GROUP value\n\
144 -R, --recursive operate on files and directories recursively\n\
145 -v, --verbose output a diagnostic for every file processed\n\
146 --help display this help and exit\n\
147 --version output version information and exit\n\
148 "));
149 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
151 exit (status);
155 main (int argc, char **argv)
157 gid_t gid;
158 int errors = 0;
159 int optc;
160 struct Chown_option chopt;
162 program_name = argv[0];
163 setlocale (LC_ALL, "");
164 bindtextdomain (PACKAGE, LOCALEDIR);
165 textdomain (PACKAGE);
167 atexit (close_stdout);
169 chopt_init (&chopt);
171 while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
173 switch (optc)
175 case 0:
176 break;
177 case REFERENCE_FILE_OPTION:
178 reference_file = optarg;
179 break;
180 case DEREFERENCE_OPTION:
181 chopt.dereference = DEREF_ALWAYS;
182 break;
183 case 'R':
184 chopt.recurse = 1;
185 break;
186 case 'c':
187 chopt.verbosity = V_changes_only;
188 break;
189 case 'f':
190 chopt.force_silent = 1;
191 break;
192 case 'h':
193 chopt.dereference = DEREF_NEVER;
194 break;
195 case 'v':
196 chopt.verbosity = V_high;
197 break;
198 case_GETOPT_HELP_CHAR;
199 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
200 default:
201 usage (1);
205 if (argc - optind + (reference_file ? 1 : 0) <= 1)
207 error (0, 0, _("too few arguments"));
208 usage (1);
211 if (reference_file)
213 struct stat ref_stats;
214 if (stat (reference_file, &ref_stats))
215 error (1, errno, _("getting attributes of %s"), quote (reference_file));
217 chopt.group_name = gid_to_name (ref_stats.st_gid);
218 gid = ref_stats.st_gid;
220 else
222 chopt.group_name = argv[optind++];
223 parse_group (chopt.group_name, &gid);
226 for (; optind < argc; ++optind)
227 errors |= change_file_owner (1, argv[optind], (uid_t) -1, gid,
228 (uid_t) -1, (gid_t) -1, &chopt);
230 chopt_free (&chopt);
232 exit (errors);