(WRITTEN_BY): Rename from AUTHORS.
[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 "error.h"
38 #include "lchown.h"
39 #include "quote.h"
40 #include "chown-core.h"
41
42 /* The official name of this program (e.g., no `g' prefix).  */
43 #define PROGRAM_NAME "chown"
44
45 #define WRITTEN_BY _("Written by David MacKenzie.")
46
47 #ifndef _POSIX_VERSION
48 struct passwd *getpwnam ();
49 struct group *getgrnam ();
50 struct group *getgrgid ();
51 #endif
52
53 #if ! HAVE_ENDPWENT
54 # define endpwent() ((void) 0)
55 #endif
56
57 char *parse_user_spec ();
58
59 /* The name the program was run with. */
60 char *program_name;
61
62 /* The argument to the --reference option.  Use the owner and group IDs
63    of this file.  This file must exist.  */
64 static char *reference_file;
65
66 /* For long options that have no equivalent short option, use a
67    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
68 enum
69 {
70   REFERENCE_FILE_OPTION = CHAR_MAX + 1,
71   DEREFERENCE_OPTION,
72   FROM_OPTION
73 };
74
75 static struct option const long_options[] =
76 {
77   {"recursive", no_argument, 0, 'R'},
78   {"changes", no_argument, 0, 'c'},
79   {"dereference", no_argument, 0, DEREFERENCE_OPTION},
80   {"from", required_argument, 0, FROM_OPTION},
81   {"no-dereference", no_argument, 0, 'h'},
82   {"quiet", no_argument, 0, 'f'},
83   {"silent", no_argument, 0, 'f'},
84   {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
85   {"verbose", no_argument, 0, 'v'},
86   {GETOPT_HELP_OPTION_DECL},
87   {GETOPT_VERSION_OPTION_DECL},
88   {0, 0, 0, 0}
89 };
90
91 void
92 usage (int status)
93 {
94   if (status != 0)
95     fprintf (stderr, _("Try `%s --help' for more information.\n"),
96              program_name);
97   else
98     {
99       printf (_("\
100 Usage: %s [OPTION]... OWNER[:[GROUP]] FILE...\n\
101   or:  %s [OPTION]... :GROUP FILE...\n\
102   or:  %s [OPTION]... --reference=RFILE FILE...\n\
103 "),
104               program_name, program_name, program_name);
105       fputs (_("\
106 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
107 \n\
108   -c, --changes          like verbose but report only when a change is made\n\
109       --dereference      affect the referent of each symbolic link, rather\n\
110                          than the symbolic link itself\n\
111 "), stdout);
112       fputs (_("\
113   -h, --no-dereference   affect symbolic links instead of any referenced file\n\
114                          (available only on systems that can change the\n\
115                          ownership of a symlink)\n\
116 "), stdout);
117       fputs (_("\
118       --from=CURRENT_OWNER:CURRENT_GROUP\n\
119                          change the owner and/or group of each file only if\n\
120                          its current owner and/or group match those specified\n\
121                          here.  Either may be omitted, in which case a match\n\
122                          is not required for the omitted attribute.\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 specified 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\
130 "), stdout);
131       fputs (HELP_OPTION_DESCRIPTION, stdout);
132       fputs (VERSION_OPTION_DESCRIPTION, stdout);
133       fputs (_("\
134 \n\
135 Owner is unchanged if missing.  Group is unchanged if missing, but changed\n\
136 to login group if implied by a `:'.  OWNER and GROUP may be numeric as well\n\
137 as symbolic.\n\
138 "), stdout);
139       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
140     }
141   exit (status);
142 }
143
144 int
145 main (int argc, char **argv)
146 {
147   uid_t uid = (uid_t) -1;       /* New uid; -1 if not to be changed. */
148   gid_t gid = (uid_t) -1;       /* New gid; -1 if not to be changed. */
149   uid_t old_uid = (uid_t) -1;   /* Old uid; -1 if unrestricted. */
150   gid_t old_gid = (uid_t) -1;   /* Old gid; -1 if unrestricted. */
151   struct Chown_option chopt;
152
153   int errors = 0;
154   int optc;
155
156   initialize_main (&argc, &argv);
157   program_name = argv[0];
158   setlocale (LC_ALL, "");
159   bindtextdomain (PACKAGE, LOCALEDIR);
160   textdomain (PACKAGE);
161
162   atexit (close_stdout);
163
164   chopt_init (&chopt);
165
166   while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
167     {
168       switch (optc)
169         {
170         case 0:
171           break;
172         case REFERENCE_FILE_OPTION:
173           reference_file = optarg;
174           break;
175         case DEREFERENCE_OPTION:
176           chopt.dereference = DEREF_ALWAYS;
177           break;
178         case FROM_OPTION:
179           {
180             char *u_dummy, *g_dummy;
181             const char *e = parse_user_spec (optarg,
182                                              &old_uid, &old_gid,
183                                              &u_dummy, &g_dummy);
184             if (e)
185               error (EXIT_FAILURE, 0, "%s: %s", quote (optarg), e);
186             break;
187           }
188         case 'R':
189           chopt.recurse = 1;
190           break;
191         case 'c':
192           chopt.verbosity = V_changes_only;
193           break;
194         case 'f':
195           chopt.force_silent = 1;
196           break;
197         case 'h':
198           chopt.dereference = DEREF_NEVER;
199           break;
200         case 'v':
201           chopt.verbosity = V_high;
202           break;
203         case_GETOPT_HELP_CHAR;
204         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
205         default:
206           usage (EXIT_FAILURE);
207         }
208     }
209
210   if (argc - optind + (reference_file ? 1 : 0) <= 1)
211     {
212       error (0, 0, _("too few arguments"));
213       usage (EXIT_FAILURE);
214     }
215
216   if (reference_file)
217     {
218       struct stat ref_stats;
219
220       if (stat (reference_file, &ref_stats))
221         error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
222                quote (reference_file));
223
224       uid = ref_stats.st_uid;
225       gid = ref_stats.st_gid;
226       chopt.user_name = uid_to_name (ref_stats.st_uid);
227       chopt.group_name = gid_to_name (ref_stats.st_gid);
228     }
229   else
230     {
231       const char *e = parse_user_spec (argv[optind], &uid, &gid,
232                                        &chopt.user_name, &chopt.group_name);
233       if (e)
234         error (EXIT_FAILURE, 0, "%s: %s", quote (argv[optind]), e);
235
236       /* FIXME: set it to the empty string?  */
237       if (chopt.user_name == NULL)
238         chopt.user_name = "";
239
240       optind++;
241     }
242
243   for (; optind < argc; ++optind)
244     errors |= change_file_owner (1, argv[optind], uid, gid,
245                                  old_uid, old_gid, &chopt);
246
247   chopt_free (&chopt);
248
249   exit (errors);
250 }