1 /* chown -- change user and group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2003 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)
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. */
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>. */
33 #include <sys/types.h>
37 #include "chown-core.h"
44 /* The official name of this program (e.g., no `g' prefix). */
45 #define PROGRAM_NAME "chown"
47 #define WRITTEN_BY _("Written by David MacKenzie and Jim Meyering.")
49 /* The name the program was run with. */
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;
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. */
60 FROM_OPTION = CHAR_MAX + 1,
65 static struct option const long_options[] =
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},
85 fprintf (stderr, _("Try `%s --help' for more information.\n"),
90 Usage: %s [OPTION]... OWNER[:[GROUP]] FILE...\n\
91 or: %s [OPTION]... :GROUP FILE...\n\
92 or: %s [OPTION]... --reference=RFILE FILE...\n\
94 program_name, program_name, program_name);
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\
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\
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\
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\
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\
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\
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\
132 -P do not traverse any symbolic links (default)\n\
135 fputs (HELP_OPTION_DESCRIPTION, stdout);
136 fputs (VERSION_OPTION_DESCRIPTION, stdout);
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\
143 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
149 main (int argc, char **argv)
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. */
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;
159 /* Bit flags that control how fts works. */
160 int bit_flags = FTS_PHYSICAL;
162 struct Chown_option chopt;
166 initialize_main (&argc, &argv);
167 program_name = argv[0];
168 setlocale (LC_ALL, "");
169 bindtextdomain (PACKAGE, LOCALEDIR);
170 textdomain (PACKAGE);
172 atexit (close_stdout);
176 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
184 case 'H': /* Traverse command-line symlinks-to-directories. */
185 bit_flags = FTS_COMFOLLOW;
188 case 'L': /* Traverse all symlinks-to-directories. */
189 bit_flags = FTS_LOGICAL;
192 case 'P': /* Traverse no symlinks-to-directories. */
193 bit_flags = FTS_PHYSICAL;
196 case 'h': /* --no-dereference: affect symlinks */
197 chopt.affect_symlink_referent = false;
200 case DEREFERENCE_OPTION: /* --dereference: affect the referent
202 chopt.affect_symlink_referent = true;
205 case REFERENCE_FILE_OPTION:
206 reference_file = optarg;
211 char *u_dummy, *g_dummy;
212 const char *e = parse_user_spec (optarg,
213 &required_uid, &required_gid,
216 error (EXIT_FAILURE, 0, "%s: %s", quote (optarg), e);
221 chopt.recurse = true;
225 chopt.verbosity = V_changes_only;
229 chopt.force_silent = true;
233 chopt.verbosity = V_high;
236 case_GETOPT_HELP_CHAR;
237 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
239 usage (EXIT_FAILURE);
243 if (argc - optind + (reference_file ? 1 : 0) <= 1)
245 error (0, 0, _("too few arguments"));
246 usage (EXIT_FAILURE);
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));
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);
263 const char *e = parse_user_spec (argv[optind], &uid, &gid,
264 &chopt.user_name, &chopt.group_name);
266 error (EXIT_FAILURE, 0, "%s: %s", quote (argv[optind]), e);
268 /* FIXME: set it to the empty string? */
269 if (chopt.user_name == NULL)
270 chopt.user_name = "";
275 fail = chown_files (argv + optind, bit_flags,
277 required_uid, required_gid, &chopt);
281 exit (fail ? EXIT_FAILURE : EXIT_SUCCESS);