3ebff168b9852fc649afcfb1b7713b2e9eff7966
[platform/upstream/coreutils.git] / src / chown.c
1 /* chown -- change user and group ownership of files
2    Copyright (C) 89, 90, 91, 1995-2003 Free Software Foundation, Inc.
3
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.
8
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.
13
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.  */
17
18 /*
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               |-------------------------+-------------------------|
28
29    Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
30
31 #include <config.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include <getopt.h>
35
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 "userspec.h"
43
44 /* The official name of this program (e.g., no `g' prefix).  */
45 #define PROGRAM_NAME "chown"
46
47 #define WRITTEN_BY _("Written by David MacKenzie and Jim Meyering.")
48
49 /* The name the program was run with. */
50 char *program_name;
51
52 /* The argument to the --reference option.  Use the owner and group IDs
53    of this file.  This file must exist.  */
54 static char *reference_file;
55
56 /* For long options that have no equivalent short option, use a
57    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
58 enum
59 {
60   FROM_OPTION = CHAR_MAX + 1,
61   DEREFERENCE_OPTION,
62   REFERENCE_FILE_OPTION
63 };
64
65 static struct option const long_options[] =
66 {
67   {"recursive", no_argument, 0, 'R'},
68   {"changes", no_argument, 0, 'c'},
69   {"dereference", no_argument, 0, DEREFERENCE_OPTION},
70   {"from", required_argument, 0, FROM_OPTION},
71   {"no-dereference", no_argument, 0, 'h'},
72   {"quiet", no_argument, 0, 'f'},
73   {"silent", no_argument, 0, 'f'},
74   {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
75   {"verbose", no_argument, 0, 'v'},
76   {GETOPT_HELP_OPTION_DECL},
77   {GETOPT_VERSION_OPTION_DECL},
78   {0, 0, 0, 0}
79 };
80
81 void
82 usage (int status)
83 {
84   if (status != 0)
85     fprintf (stderr, _("Try `%s --help' for more information.\n"),
86              program_name);
87   else
88     {
89       printf (_("\
90 Usage: %s [OPTION]... OWNER[:[GROUP]] FILE...\n\
91   or:  %s [OPTION]... :GROUP FILE...\n\
92   or:  %s [OPTION]... --reference=RFILE FILE...\n\
93 "),
94               program_name, program_name, program_name);
95       fputs (_("\
96 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
97 With --reference, change the owner and group of each FILE to those of RFILE.\n\
98 \n\
99   -c, --changes          like verbose but report only when a change is made\n\
100       --dereference      affect the referent of each symbolic link, rather\n\
101                          than the symbolic link itself\n\
102 "), stdout);
103       fputs (_("\
104   -h, --no-dereference   affect each symbolic link instead of any referenced\n\
105                          file (useful only on systems that can change the\n\
106                          ownership of a symlink)\n\
107 "), stdout);
108       fputs (_("\
109       --from=CURRENT_OWNER:CURRENT_GROUP\n\
110                          change the owner and/or group of each file only if\n\
111                          its current owner and/or group match those specified\n\
112                          here.  Either may be omitted, in which case a match\n\
113                          is not required for the omitted attribute.\n\
114 "), stdout);
115       fputs (_("\
116   -f, --silent, --quiet  suppress most error messages\n\
117       --reference=RFILE  use RFILE's owner and group rather than\n\
118                          the specifying OWNER:GROUP values\n\
119   -R, --recursive        operate on files and directories recursively\n\
120   -v, --verbose          output a diagnostic for every file processed\n\
121 \n\
122 "), stdout);
123       fputs (_("\
124 The following options modify how a hierarchy is traversed when the -R\n\
125 option is also specified.  If more than one is specified, only the final\n\
126 one takes effect.\n\
127 \n\
128   -H                     if a command line argument is a symbolic link\n\
129                          to a directory, traverse it\n\
130   -L                     traverse every symbolic link to a directory\n\
131                          encountered\n\
132   -P                     do not traverse any symbolic links (default)\n\
133 \n\
134 "), stdout);
135       fputs (HELP_OPTION_DESCRIPTION, stdout);
136       fputs (VERSION_OPTION_DESCRIPTION, stdout);
137       fputs (_("\
138 \n\
139 Owner is unchanged if missing.  Group is unchanged if missing, but changed\n\
140 to login group if implied by a `:'.  OWNER and GROUP may be numeric as well\n\
141 as symbolic.\n\
142 "), stdout);
143       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
144     }
145   exit (status);
146 }
147
148 int
149 main (int argc, char **argv)
150 {
151   uid_t uid = -1;       /* Specified uid; -1 if not to be changed. */
152   gid_t gid = -1;       /* Specified gid; -1 if not to be changed. */
153
154   /* Change the owner (group) of a file only if it has this uid (gid).
155      -1 means there's no restriction.  */
156   uid_t required_uid = -1;
157   gid_t required_gid = -1;
158
159   /* Bit flags that control how fts works.  */
160   int bit_flags = FTS_PHYSICAL;
161
162   struct Chown_option chopt;
163   int fail = 0;
164   int optc;
165
166   initialize_main (&argc, &argv);
167   program_name = argv[0];
168   setlocale (LC_ALL, "");
169   bindtextdomain (PACKAGE, LOCALEDIR);
170   textdomain (PACKAGE);
171
172   atexit (close_stdout);
173
174   chopt_init (&chopt);
175
176   while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
177          != -1)
178     {
179       switch (optc)
180         {
181         case 0:
182           break;
183
184         case 'H': /* Traverse command-line symlinks-to-directories.  */
185           bit_flags = FTS_COMFOLLOW;
186           break;
187
188         case 'L': /* Traverse all symlinks-to-directories.  */
189           bit_flags = FTS_LOGICAL;
190           break;
191
192         case 'P': /* Traverse no symlinks-to-directories.  */
193           bit_flags = FTS_PHYSICAL;
194           break;
195
196         case 'h': /* --no-dereference: affect symlinks */
197           chopt.affect_symlink_referent = false;
198           break;
199
200         case DEREFERENCE_OPTION: /* --dereference: affect the referent
201                                     of each symlink */
202           chopt.affect_symlink_referent = true;
203           break;
204
205         case REFERENCE_FILE_OPTION:
206           reference_file = optarg;
207           break;
208
209         case FROM_OPTION:
210           {
211             char *u_dummy, *g_dummy;
212             const char *e = parse_user_spec (optarg,
213                                              &required_uid, &required_gid,
214                                              &u_dummy, &g_dummy);
215             if (e)
216               error (EXIT_FAILURE, 0, "%s: %s", quote (optarg), e);
217             break;
218           }
219
220         case 'R':
221           chopt.recurse = true;
222           break;
223
224         case 'c':
225           chopt.verbosity = V_changes_only;
226           break;
227
228         case 'f':
229           chopt.force_silent = true;
230           break;
231
232         case 'v':
233           chopt.verbosity = V_high;
234           break;
235
236         case_GETOPT_HELP_CHAR;
237         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
238         default:
239           usage (EXIT_FAILURE);
240         }
241     }
242
243   if (argc - optind + (reference_file ? 1 : 0) <= 1)
244     {
245       error (0, 0, _("too few arguments"));
246       usage (EXIT_FAILURE);
247     }
248
249   if (reference_file)
250     {
251       struct stat ref_stats;
252       if (stat (reference_file, &ref_stats))
253         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
254                quote (reference_file));
255
256       uid = ref_stats.st_uid;
257       gid = ref_stats.st_gid;
258       chopt.user_name = uid_to_name (ref_stats.st_uid);
259       chopt.group_name = gid_to_name (ref_stats.st_gid);
260     }
261   else
262     {
263       const char *e = parse_user_spec (argv[optind], &uid, &gid,
264                                        &chopt.user_name, &chopt.group_name);
265       if (e)
266         error (EXIT_FAILURE, 0, "%s: %s", quote (argv[optind]), e);
267
268       /* FIXME: set it to the empty string?  */
269       if (chopt.user_name == NULL)
270         chopt.user_name = "";
271
272       optind++;
273     }
274
275   fail = chown_files (argv + optind, bit_flags,
276                       uid, gid,
277                       required_uid, required_gid, &chopt);
278
279   chopt_free (&chopt);
280
281   exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);
282 }