.
[coreutils.git] / src / chown.c
bloba326fdcd560c1062cafba6e0f3b2c638e0042f27
1 /* chown -- change user and group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2004 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. */
19 | user
20 | unchanged explicit
21 -------------|-------------------------+-------------------------|
22 g unchanged | --- | chown u |
23 r |-------------------------+-------------------------|
24 o explicit | chgrp g or chown .g | chown u.g |
25 u |-------------------------+-------------------------|
26 p from passwd| --- | chown u. |
27 |-------------------------+-------------------------|
29 Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
31 #include <config.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <getopt.h>
36 #include "system.h"
37 #include "chown-core.h"
38 #include "error.h"
39 #include "fts_.h"
40 #include "lchown.h"
41 #include "quote.h"
42 #include "root-dev-ino.h"
43 #include "userspec.h"
45 /* The official name of this program (e.g., no `g' prefix). */
46 #define PROGRAM_NAME "chown"
48 #define AUTHORS "David MacKenzie", "Jim Meyering"
50 /* The name the program was run with. */
51 char *program_name;
53 /* The argument to the --reference option. Use the owner and group IDs
54 of this file. This file must exist. */
55 static char *reference_file;
57 /* For long options that have no equivalent short option, use a
58 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
59 enum
61 DEREFERENCE_OPTION = CHAR_MAX + 1,
62 FROM_OPTION,
63 NO_PRESERVE_ROOT,
64 PRESERVE_ROOT,
65 REFERENCE_FILE_OPTION
68 static struct option const long_options[] =
70 {"recursive", no_argument, 0, 'R'},
71 {"changes", no_argument, 0, 'c'},
72 {"dereference", no_argument, 0, DEREFERENCE_OPTION},
73 {"from", required_argument, 0, FROM_OPTION},
74 {"no-dereference", no_argument, 0, 'h'},
75 {"no-preserve-root", no_argument, 0, NO_PRESERVE_ROOT},
76 {"preserve-root", no_argument, 0, PRESERVE_ROOT},
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 void
87 usage (int status)
89 if (status != EXIT_SUCCESS)
90 fprintf (stderr, _("Try `%s --help' for more information.\n"),
91 program_name);
92 else
94 printf (_("\
95 Usage: %s [OPTION]... OWNER[:[GROUP]] FILE...\n\
96 or: %s [OPTION]... :GROUP FILE...\n\
97 or: %s [OPTION]... --reference=RFILE FILE...\n\
98 "),
99 program_name, program_name, program_name);
100 fputs (_("\
101 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
102 With --reference, change the owner and group of each FILE to those of RFILE.\n\
104 -c, --changes like verbose but report only when a change is made\n\
105 --dereference affect the referent of each symbolic link, rather\n\
106 than the symbolic link itself\n\
107 "), stdout);
108 fputs (_("\
109 -h, --no-dereference affect each symbolic link instead of any referenced\n\
110 file (useful only on systems that can change the\n\
111 ownership of a symlink)\n\
112 "), stdout);
113 fputs (_("\
114 --from=CURRENT_OWNER:CURRENT_GROUP\n\
115 change the owner and/or group of each file only if\n\
116 its current owner and/or group match those specified\n\
117 here. Either may be omitted, in which case a match\n\
118 is not required for the omitted attribute.\n\
119 "), stdout);
120 fputs (_("\
121 --no-preserve-root do not treat `/' specially (the default)\n\
122 --preserve-root fail to operate recursively on `/'\n\
123 "), stdout);
124 fputs (_("\
125 -f, --silent, --quiet suppress most error messages\n\
126 --reference=RFILE use RFILE's owner and group rather than\n\
127 the specifying OWNER:GROUP values\n\
128 -R, --recursive operate on files and directories recursively\n\
129 -v, --verbose output a diagnostic for every file processed\n\
131 "), stdout);
132 fputs (_("\
133 The following options modify how a hierarchy is traversed when the -R\n\
134 option is also specified. If more than one is specified, only the final\n\
135 one takes effect.\n\
137 -H if a command line argument is a symbolic link\n\
138 to a directory, traverse it\n\
139 -L traverse every symbolic link to a directory\n\
140 encountered\n\
141 -P do not traverse any symbolic links (default)\n\
143 "), stdout);
144 fputs (HELP_OPTION_DESCRIPTION, stdout);
145 fputs (VERSION_OPTION_DESCRIPTION, stdout);
146 fputs (_("\
148 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
149 to login group if implied by a `:'. OWNER and GROUP may be numeric as well\n\
150 as symbolic.\n\
151 "), stdout);
152 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
154 exit (status);
158 main (int argc, char **argv)
160 bool preserve_root = false;
162 uid_t uid = -1; /* Specified uid; -1 if not to be changed. */
163 gid_t gid = -1; /* Specified gid; -1 if not to be changed. */
165 /* Change the owner (group) of a file only if it has this uid (gid).
166 -1 means there's no restriction. */
167 uid_t required_uid = -1;
168 gid_t required_gid = -1;
170 /* Bit flags that control how fts works. */
171 int bit_flags = FTS_PHYSICAL;
173 struct Chown_option chopt;
174 int fail;
175 int optc;
177 initialize_main (&argc, &argv);
178 program_name = argv[0];
179 setlocale (LC_ALL, "");
180 bindtextdomain (PACKAGE, LOCALEDIR);
181 textdomain (PACKAGE);
183 atexit (close_stdout);
185 chopt_init (&chopt);
187 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
188 != -1)
190 switch (optc)
192 case 0:
193 break;
195 case 'H': /* Traverse command-line symlinks-to-directories. */
196 bit_flags = FTS_COMFOLLOW;
197 break;
199 case 'L': /* Traverse all symlinks-to-directories. */
200 bit_flags = FTS_LOGICAL;
201 break;
203 case 'P': /* Traverse no symlinks-to-directories. */
204 bit_flags = FTS_PHYSICAL;
205 break;
207 case 'h': /* --no-dereference: affect symlinks */
208 chopt.affect_symlink_referent = false;
209 break;
211 case DEREFERENCE_OPTION: /* --dereference: affect the referent
212 of each symlink */
213 chopt.affect_symlink_referent = true;
214 break;
216 case NO_PRESERVE_ROOT:
217 preserve_root = false;
218 break;
220 case PRESERVE_ROOT:
221 preserve_root = true;
222 break;
224 case REFERENCE_FILE_OPTION:
225 reference_file = optarg;
226 break;
228 case FROM_OPTION:
230 char *u_dummy, *g_dummy;
231 const char *e = parse_user_spec (optarg,
232 &required_uid, &required_gid,
233 &u_dummy, &g_dummy);
234 if (e)
235 error (EXIT_FAILURE, 0, "%s: %s", quote (optarg), e);
236 break;
239 case 'R':
240 chopt.recurse = true;
241 break;
243 case 'c':
244 chopt.verbosity = V_changes_only;
245 break;
247 case 'f':
248 chopt.force_silent = true;
249 break;
251 case 'v':
252 chopt.verbosity = V_high;
253 break;
255 case_GETOPT_HELP_CHAR;
256 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
257 default:
258 usage (EXIT_FAILURE);
262 if (argc - optind + (reference_file ? 1 : 0) <= 1)
264 error (0, 0, _("too few arguments"));
265 usage (EXIT_FAILURE);
268 if (reference_file)
270 struct stat ref_stats;
271 if (stat (reference_file, &ref_stats))
272 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
273 quote (reference_file));
275 uid = ref_stats.st_uid;
276 gid = ref_stats.st_gid;
277 chopt.user_name = uid_to_name (ref_stats.st_uid);
278 chopt.group_name = gid_to_name (ref_stats.st_gid);
280 else
282 const char *e = parse_user_spec (argv[optind], &uid, &gid,
283 &chopt.user_name, &chopt.group_name);
284 if (e)
285 error (EXIT_FAILURE, 0, "%s: %s", quote (argv[optind]), e);
287 /* FIXME: set it to the empty string? */
288 if (chopt.user_name == NULL)
289 chopt.user_name = "";
291 optind++;
294 if (chopt.recurse && preserve_root)
296 static struct dev_ino dev_ino_buf;
297 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
298 if (chopt.root_dev_ino == NULL)
299 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
300 quote ("/"));
303 fail = chown_files (argv + optind, bit_flags,
304 uid, gid,
305 required_uid, required_gid, &chopt);
307 chopt_free (&chopt);
309 exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);