Tizen 2.0 Release
[external/tizen-coreutils.git] / src / chown.c
1 /* chown -- change user and group ownership of files
2    Copyright (C) 89, 90, 91, 1995-2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 "quote.h"
41 #include "root-dev-ino.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 AUTHORS "David MacKenzie", "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   DEREFERENCE_OPTION = CHAR_MAX + 1,
61   FROM_OPTION,
62   NO_PRESERVE_ROOT,
63   PRESERVE_ROOT,
64   REFERENCE_FILE_OPTION
65 };
66
67 static struct option const long_options[] =
68 {
69   {"recursive", no_argument, NULL, 'R'},
70   {"changes", no_argument, NULL, 'c'},
71   {"dereference", no_argument, NULL, DEREFERENCE_OPTION},
72   {"from", required_argument, NULL, FROM_OPTION},
73   {"no-dereference", no_argument, NULL, 'h'},
74   {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},
75   {"preserve-root", no_argument, NULL, PRESERVE_ROOT},
76   {"quiet", no_argument, NULL, 'f'},
77   {"silent", no_argument, NULL, 'f'},
78   {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},
79   {"verbose", no_argument, NULL, 'v'},
80   {GETOPT_HELP_OPTION_DECL},
81   {GETOPT_VERSION_OPTION_DECL},
82   {NULL, 0, NULL, 0}
83 };
84
85 void
86 usage (int status)
87 {
88   if (status != EXIT_SUCCESS)
89     fprintf (stderr, _("Try `%s --help' for more information.\n"),
90              program_name);
91   else
92     {
93       printf (_("\
94 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
95   or:  %s [OPTION]... --reference=RFILE FILE...\n\
96 "),
97               program_name, program_name);
98       fputs (_("\
99 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
100 With --reference, change the owner and group of each FILE to those of RFILE.\n\
101 \n\
102   -c, --changes          like verbose but report only when a change is made\n\
103       --dereference      affect the referent of each symbolic link (this is\n\
104                          the default), rather than the symbolic link itself\n\
105 "), stdout);
106       fputs (_("\
107   -h, --no-dereference   affect each symbolic link instead of any referenced\n\
108                          file (useful only on systems that can change the\n\
109                          ownership of a symlink)\n\
110 "), stdout);
111       fputs (_("\
112       --from=CURRENT_OWNER:CURRENT_GROUP\n\
113                          change the owner and/or group of each file only if\n\
114                          its current owner and/or group match those specified\n\
115                          here.  Either may be omitted, in which case a match\n\
116                          is not required for the omitted attribute.\n\
117 "), stdout);
118       fputs (_("\
119       --no-preserve-root  do not treat `/' specially (the default)\n\
120       --preserve-root    fail to operate recursively on `/'\n\
121 "), stdout);
122       fputs (_("\
123   -f, --silent, --quiet  suppress most error messages\n\
124       --reference=RFILE  use RFILE's owner and group rather than\n\
125                          specifying OWNER:GROUP values\n\
126   -R, --recursive        operate on files and directories recursively\n\
127   -v, --verbose          output a diagnostic for every file processed\n\
128 \n\
129 "), stdout);
130       fputs (_("\
131 The following options modify how a hierarchy is traversed when the -R\n\
132 option is also specified.  If more than one is specified, only the final\n\
133 one takes effect.\n\
134 \n\
135   -H                     if a command line argument is a symbolic link\n\
136                          to a directory, traverse it\n\
137   -L                     traverse every symbolic link to a directory\n\
138                          encountered\n\
139   -P                     do not traverse any symbolic links (default)\n\
140 \n\
141 "), stdout);
142       fputs (HELP_OPTION_DESCRIPTION, stdout);
143       fputs (VERSION_OPTION_DESCRIPTION, stdout);
144       fputs (_("\
145 \n\
146 Owner is unchanged if missing.  Group is unchanged if missing, but changed\n\
147 to login group if implied by a `:' following a symbolic OWNER.\n\
148 OWNER and GROUP may be numeric as well as symbolic.\n\
149 "), stdout);
150       printf (_("\
151 \n\
152 Examples:\n\
153   %s root /u        Change the owner of /u to \"root\".\n\
154   %s root:staff /u  Likewise, but also change its group to \"staff\".\n\
155   %s -hR root /u    Change the owner of /u and subfiles to \"root\".\n\
156 "),
157               program_name, program_name, program_name);
158       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
159     }
160   exit (status);
161 }
162
163 int
164 main (int argc, char **argv)
165 {
166   bool preserve_root = false;
167
168   uid_t uid = -1;       /* Specified uid; -1 if not to be changed. */
169   gid_t gid = -1;       /* Specified gid; -1 if not to be changed. */
170
171   /* Change the owner (group) of a file only if it has this uid (gid).
172      -1 means there's no restriction.  */
173   uid_t required_uid = -1;
174   gid_t required_gid = -1;
175
176   /* Bit flags that control how fts works.  */
177   int bit_flags = FTS_PHYSICAL;
178
179   /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
180      specified.  */
181   int dereference = -1;
182
183   struct Chown_option chopt;
184   bool ok;
185   int optc;
186
187   initialize_main (&argc, &argv);
188   program_name = argv[0];
189   setlocale (LC_ALL, "");
190   bindtextdomain (PACKAGE, LOCALEDIR);
191   textdomain (PACKAGE);
192
193   atexit (close_stdout);
194
195   chopt_init (&chopt);
196
197   while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
198          != -1)
199     {
200       switch (optc)
201         {
202         case 'H': /* Traverse command-line symlinks-to-directories.  */
203           bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;
204           break;
205
206         case 'L': /* Traverse all symlinks-to-directories.  */
207           bit_flags = FTS_LOGICAL;
208           break;
209
210         case 'P': /* Traverse no symlinks-to-directories.  */
211           bit_flags = FTS_PHYSICAL;
212           break;
213
214         case 'h': /* --no-dereference: affect symlinks */
215           dereference = 0;
216           break;
217
218         case DEREFERENCE_OPTION: /* --dereference: affect the referent
219                                     of each symlink */
220           dereference = 1;
221           break;
222
223         case NO_PRESERVE_ROOT:
224           preserve_root = false;
225           break;
226
227         case PRESERVE_ROOT:
228           preserve_root = true;
229           break;
230
231         case REFERENCE_FILE_OPTION:
232           reference_file = optarg;
233           break;
234
235         case FROM_OPTION:
236           {
237             char *u_dummy, *g_dummy;
238             const char *e = parse_user_spec (optarg,
239                                              &required_uid, &required_gid,
240                                              &u_dummy, &g_dummy);
241             if (e)
242               error (EXIT_FAILURE, 0, "%s: %s", quote (optarg), e);
243             break;
244           }
245
246         case 'R':
247           chopt.recurse = true;
248           break;
249
250         case 'c':
251           chopt.verbosity = V_changes_only;
252           break;
253
254         case 'f':
255           chopt.force_silent = true;
256           break;
257
258         case 'v':
259           chopt.verbosity = V_high;
260           break;
261
262         case_GETOPT_HELP_CHAR;
263         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
264         default:
265           usage (EXIT_FAILURE);
266         }
267     }
268
269   if (chopt.recurse)
270     {
271       if (bit_flags == FTS_PHYSICAL)
272         {
273           if (dereference == 1)
274             error (EXIT_FAILURE, 0,
275                    _("-R --dereference requires either -H or -L"));
276           dereference = 0;
277         }
278     }
279   else
280     {
281       bit_flags = FTS_PHYSICAL;
282     }
283   chopt.affect_symlink_referent = (dereference != 0);
284
285   if (argc - optind < (reference_file ? 1 : 2))
286     {
287       if (argc <= optind)
288         error (0, 0, _("missing operand"));
289       else
290         error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
291       usage (EXIT_FAILURE);
292     }
293
294   if (reference_file)
295     {
296       struct stat ref_stats;
297       if (stat (reference_file, &ref_stats))
298         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
299                quote (reference_file));
300
301       uid = ref_stats.st_uid;
302       gid = ref_stats.st_gid;
303       chopt.user_name = uid_to_name (ref_stats.st_uid);
304       chopt.group_name = gid_to_name (ref_stats.st_gid);
305     }
306   else
307     {
308       const char *e = parse_user_spec (argv[optind], &uid, &gid,
309                                        &chopt.user_name, &chopt.group_name);
310       if (e)
311         error (EXIT_FAILURE, 0, "%s: %s", quote (argv[optind]), e);
312
313       /* If a group is specified but no user, set the user name to the
314          empty string so that diagnostics say "ownership :GROUP"
315          rather than "group GROUP".  */
316       if (!chopt.user_name && chopt.group_name)
317         chopt.user_name = "";
318
319       optind++;
320     }
321
322   if (chopt.recurse & preserve_root)
323     {
324       static struct dev_ino dev_ino_buf;
325       chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);
326       if (chopt.root_dev_ino == NULL)
327         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
328                quote ("/"));
329     }
330
331   ok = chown_files (argv + optind, bit_flags,
332                     uid, gid,
333                     required_uid, required_gid, &chopt);
334
335   chopt_free (&chopt);
336
337   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
338 }