*** empty log message ***
[coreutils.git] / src / chown.c
bloba95ff017e761c13bde6c8a2fc670be8dcdb5a338
1 /* chown -- change user and 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. */
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 "error.h"
38 #include "lchown.h"
39 #include "quote.h"
40 #include "chown-core.h"
42 /* The official name of this program (e.g., no `g' prefix). */
43 #define PROGRAM_NAME "chown"
45 #define AUTHORS "David MacKenzie"
47 #ifndef _POSIX_VERSION
48 struct passwd *getpwnam ();
49 struct group *getgrnam ();
50 struct group *getgrgid ();
51 #endif
53 #if ! HAVE_ENDPWENT
54 # define endpwent() ((void) 0)
55 #endif
57 char *parse_user_spec ();
59 /* The name the program was run with. */
60 char *program_name;
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;
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
70 REFERENCE_FILE_OPTION = CHAR_MAX + 1,
71 DEREFERENCE_OPTION,
72 FROM_OPTION
75 static struct option const long_options[] =
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}
91 void
92 usage (int status)
94 if (status != 0)
95 fprintf (stderr, _("Try `%s --help' for more information.\n"),
96 program_name);
97 else
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\
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\
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 (_("\
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);
141 exit (status);
145 main (int argc, char **argv)
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;
153 int errors = 0;
154 int optc;
156 program_name = argv[0];
157 setlocale (LC_ALL, "");
158 bindtextdomain (PACKAGE, LOCALEDIR);
159 textdomain (PACKAGE);
161 atexit (close_stdout);
163 chopt_init (&chopt);
165 while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
167 switch (optc)
169 case 0:
170 break;
171 case REFERENCE_FILE_OPTION:
172 reference_file = optarg;
173 break;
174 case DEREFERENCE_OPTION:
175 chopt.dereference = DEREF_ALWAYS;
176 break;
177 case FROM_OPTION:
179 char *u_dummy, *g_dummy;
180 const char *e = parse_user_spec (optarg,
181 &old_uid, &old_gid,
182 &u_dummy, &g_dummy);
183 if (e)
184 error (1, 0, "%s: %s", quote (optarg), e);
185 break;
187 case 'R':
188 chopt.recurse = 1;
189 break;
190 case 'c':
191 chopt.verbosity = V_changes_only;
192 break;
193 case 'f':
194 chopt.force_silent = 1;
195 break;
196 case 'h':
197 chopt.dereference = DEREF_NEVER;
198 break;
199 case 'v':
200 chopt.verbosity = V_high;
201 break;
202 case_GETOPT_HELP_CHAR;
203 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
204 default:
205 usage (1);
209 if (argc - optind + (reference_file ? 1 : 0) <= 1)
211 error (0, 0, _("too few arguments"));
212 usage (1);
215 if (reference_file)
217 struct stat ref_stats;
219 if (stat (reference_file, &ref_stats))
220 error (1, errno, _("failed to get attributes of %s"),
221 quote (reference_file));
223 uid = ref_stats.st_uid;
224 gid = ref_stats.st_gid;
225 chopt.user_name = uid_to_name (ref_stats.st_uid);
226 chopt.group_name = gid_to_name (ref_stats.st_gid);
228 else
230 const char *e = parse_user_spec (argv[optind], &uid, &gid,
231 &chopt.user_name, &chopt.group_name);
232 if (e)
233 error (1, 0, "%s: %s", quote (argv[optind]), e);
235 /* FIXME: set it to the empty string? */
236 if (chopt.user_name == NULL)
237 chopt.user_name = "";
239 optind++;
242 for (; optind < argc; ++optind)
243 errors |= change_file_owner (1, argv[optind], uid, gid,
244 old_uid, old_gid, &chopt);
246 chopt_free (&chopt);
248 exit (errors);