*** empty log message ***
[coreutils.git] / src / chown.c
blob3b9a656de5dfa4e6ab04981a2bb2198cc54cd82a
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 "savedir.h"
41 #include "chown-core.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "chown"
46 #define AUTHORS "David MacKenzie"
48 #ifndef _POSIX_VERSION
49 struct passwd *getpwnam ();
50 struct group *getgrnam ();
51 struct group *getgrgid ();
52 #endif
54 #if ! HAVE_ENDPWENT
55 # define endpwent() ((void) 0)
56 #endif
58 char *parse_user_spec ();
59 void strip_trailing_slashes ();
61 /* The name the program was run with. */
62 char *program_name;
64 /* The argument to the --reference option. Use the owner and group IDs
65 of this file. This file must exist. */
66 static char *reference_file;
68 /* For long options that have no equivalent short option, use a
69 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
70 enum
72 REFERENCE_FILE_OPTION = CHAR_MAX + 1,
73 DEREFERENCE_OPTION,
74 FROM_OPTION
77 static struct option const long_options[] =
79 {"recursive", no_argument, 0, 'R'},
80 {"changes", no_argument, 0, 'c'},
81 {"dereference", no_argument, 0, DEREFERENCE_OPTION},
82 {"from", required_argument, 0, FROM_OPTION},
83 {"no-dereference", no_argument, 0, 'h'},
84 {"quiet", no_argument, 0, 'f'},
85 {"silent", no_argument, 0, 'f'},
86 {"reference", required_argument, 0, REFERENCE_FILE_OPTION},
87 {"verbose", no_argument, 0, 'v'},
88 {GETOPT_HELP_OPTION_DECL},
89 {GETOPT_VERSION_OPTION_DECL},
90 {0, 0, 0, 0}
93 void
94 usage (int status)
96 if (status != 0)
97 fprintf (stderr, _("Try `%s --help' for more information.\n"),
98 program_name);
99 else
101 printf (_("\
102 Usage: %s [OPTION]... OWNER[:[GROUP]] FILE...\n\
103 or: %s [OPTION]... :GROUP FILE...\n\
104 or: %s [OPTION]... --reference=RFILE FILE...\n\
106 program_name, program_name, program_name);
107 printf (_("\
108 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
110 -c, --changes like verbose but report only when a change is made\n\
111 --dereference affect the referent of each symbolic link, rather\n\
112 than the symbolic link itself\n\
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 --from=CURRENT_OWNER:CURRENT_GROUP\n\
117 change the owner and/or group of each file only if\n\
118 its current owner and/or group match those specified\n\
119 here. Either may be omitted, in which case a match\n\
120 is not required for the omitted attribute.\n\
121 -f, --silent, --quiet suppress most error messages\n\
122 --reference=RFILE use RFILE's owner and group rather than\n\
123 the specified OWNER:GROUP values\n\
124 -R, --recursive operate on files and directories recursively\n\
125 -v, --verbose output a diagnostic for every file processed\n\
126 --help display this help and exit\n\
127 --version output version information and exit\n\
129 "));
130 printf (_("\
131 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
132 to login group if implied by a `:'. OWNER and GROUP may be numeric as well\n\
133 as symbolic.\n\
134 "));
135 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
137 exit (status);
141 main (int argc, char **argv)
143 uid_t uid = (uid_t) -1; /* New uid; -1 if not to be changed. */
144 gid_t gid = (uid_t) -1; /* New gid; -1 if not to be changed. */
145 uid_t old_uid = (uid_t) -1; /* Old uid; -1 if unrestricted. */
146 gid_t old_gid = (uid_t) -1; /* Old gid; -1 if unrestricted. */
147 struct Chown_option chopt;
149 int errors = 0;
150 int optc;
152 program_name = argv[0];
153 setlocale (LC_ALL, "");
154 bindtextdomain (PACKAGE, LOCALEDIR);
155 textdomain (PACKAGE);
157 atexit (close_stdout);
159 chopt_init (&chopt);
161 while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
163 switch (optc)
165 case 0:
166 break;
167 case REFERENCE_FILE_OPTION:
168 reference_file = optarg;
169 break;
170 case DEREFERENCE_OPTION:
171 chopt.dereference = DEREF_ALWAYS;
172 break;
173 case FROM_OPTION:
175 char *u_dummy, *g_dummy;
176 const char *e = parse_user_spec (argv[optind],
177 &old_uid, &old_gid,
178 &u_dummy, &g_dummy);
179 if (e)
180 error (1, 0, "%s: %s", quote (argv[optind]), e);
181 break;
183 case 'R':
184 chopt.recurse = 1;
185 break;
186 case 'c':
187 chopt.verbosity = V_changes_only;
188 break;
189 case 'f':
190 chopt.force_silent = 1;
191 break;
192 case 'h':
193 chopt.dereference = DEREF_NEVER;
194 break;
195 case 'v':
196 chopt.verbosity = V_high;
197 break;
198 case_GETOPT_HELP_CHAR;
199 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
200 default:
201 usage (1);
205 if (argc - optind + (reference_file ? 1 : 0) <= 1)
207 error (0, 0, _("too few arguments"));
208 usage (1);
211 if (reference_file)
213 struct stat ref_stats;
215 if (stat (reference_file, &ref_stats))
216 error (1, errno, _("getting attributes of %s"), quote (reference_file));
218 uid = ref_stats.st_uid;
219 gid = ref_stats.st_gid;
220 chopt.user_name = uid_to_name (ref_stats.st_uid);
221 chopt.group_name = gid_to_name (ref_stats.st_gid);
223 else
225 const char *e = parse_user_spec (argv[optind], &uid, &gid,
226 &chopt.user_name, &chopt.group_name);
227 if (e)
228 error (1, 0, "%s: %s", quote (argv[optind]), e);
230 /* FIXME: set it to the empty string? */
231 if (chopt.user_name == NULL)
232 chopt.user_name = "";
234 optind++;
237 for (; optind < argc; ++optind)
239 strip_trailing_slashes (argv[optind]);
240 errors |= change_file_owner (1, argv[optind], uid, gid,
241 old_uid, old_gid, &chopt);
244 chopt_free (&chopt);
246 exit (errors);