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>
40 #include "chown-core.h"
42 /* The official name of this program (e.g., no `g' prefix). */
43 #define PROGRAM_NAME "chown"
45 #define WRITTEN_BY _("Written by David MacKenzie.")
47 #ifndef _POSIX_VERSION
48 struct passwd *getpwnam ();
49 struct group *getgrnam ();
50 struct group *getgrgid ();
54 # define endpwent() ((void) 0)
57 char *parse_user_spec ();
59 /* The name the program was run with. */
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. */
70 REFERENCE_FILE_OPTION = CHAR_MAX + 1,
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},
95 fprintf (stderr, _("Try `%s --help' for more information.\n"),
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);
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\
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\
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\
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\
131 fputs (HELP_OPTION_DESCRIPTION, stdout);
132 fputs (VERSION_OPTION_DESCRIPTION, stdout);
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\
139 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
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;
156 initialize_main (&argc, &argv);
157 program_name = argv[0];
158 setlocale (LC_ALL, "");
159 bindtextdomain (PACKAGE, LOCALEDIR);
160 textdomain (PACKAGE);
162 atexit (close_stdout);
166 while ((optc = getopt_long (argc, argv, "Rcfhv", long_options, NULL)) != -1)
172 case REFERENCE_FILE_OPTION:
173 reference_file = optarg;
175 case DEREFERENCE_OPTION:
176 chopt.dereference = DEREF_ALWAYS;
180 char *u_dummy, *g_dummy;
181 const char *e = parse_user_spec (optarg,
185 error (EXIT_FAILURE, 0, "%s: %s", quote (optarg), e);
192 chopt.verbosity = V_changes_only;
195 chopt.force_silent = 1;
198 chopt.dereference = DEREF_NEVER;
201 chopt.verbosity = V_high;
203 case_GETOPT_HELP_CHAR;
204 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
206 usage (EXIT_FAILURE);
210 if (argc - optind + (reference_file ? 1 : 0) <= 1)
212 error (0, 0, _("too few arguments"));
213 usage (EXIT_FAILURE);
218 struct stat ref_stats;
220 if (stat (reference_file, &ref_stats))
221 error (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
222 quote (reference_file));
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);
231 const char *e = parse_user_spec (argv[optind], &uid, &gid,
232 &chopt.user_name, &chopt.group_name);
234 error (EXIT_FAILURE, 0, "%s: %s", quote (argv[optind]), e);
236 /* FIXME: set it to the empty string? */
237 if (chopt.user_name == NULL)
238 chopt.user_name = "";
243 for (; optind < argc; ++optind)
244 errors |= change_file_owner (1, argv[optind], uid, gid,
245 old_uid, old_gid, &chopt);