1 /* chown -- change user and group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2004 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"
42 #include "root-dev-ino.h"
45 /* The official name of this program (e.g., no `g' prefix). */
46 #define PROGRAM_NAME "chown"
48 #define AUTHORS "David MacKenzie", "Jim Meyering"
50 /* The name the program was run with. */
53 /* The argument to the --reference option. Use the owner and group IDs
54 of this file. This file must exist. */
55 static char *reference_file
;
57 /* For long options that have no equivalent short option, use a
58 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
61 DEREFERENCE_OPTION
= CHAR_MAX
+ 1,
68 static struct option
const long_options
[] =
70 {"recursive", no_argument
, 0, 'R'},
71 {"changes", no_argument
, 0, 'c'},
72 {"dereference", no_argument
, 0, DEREFERENCE_OPTION
},
73 {"from", required_argument
, 0, FROM_OPTION
},
74 {"no-dereference", no_argument
, 0, 'h'},
75 {"no-preserve-root", no_argument
, 0, NO_PRESERVE_ROOT
},
76 {"preserve-root", no_argument
, 0, PRESERVE_ROOT
},
77 {"quiet", no_argument
, 0, 'f'},
78 {"silent", no_argument
, 0, 'f'},
79 {"reference", required_argument
, 0, REFERENCE_FILE_OPTION
},
80 {"verbose", no_argument
, 0, 'v'},
81 {GETOPT_HELP_OPTION_DECL
},
82 {GETOPT_VERSION_OPTION_DECL
},
89 if (status
!= EXIT_SUCCESS
)
90 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
95 Usage: %s [OPTION]... OWNER[:[GROUP]] FILE...\n\
96 or: %s [OPTION]... :GROUP FILE...\n\
97 or: %s [OPTION]... --reference=RFILE FILE...\n\
99 program_name
, program_name
, program_name
);
101 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
102 With --reference, change the owner and group of each FILE to those of RFILE.\n\
104 -c, --changes like verbose but report only when a change is made\n\
105 --dereference affect the referent of each symbolic link, rather\n\
106 than the symbolic link itself\n\
109 -h, --no-dereference affect each symbolic link instead of any referenced\n\
110 file (useful only on systems that can change the\n\
111 ownership of a symlink)\n\
114 --from=CURRENT_OWNER:CURRENT_GROUP\n\
115 change the owner and/or group of each file only if\n\
116 its current owner and/or group match those specified\n\
117 here. Either may be omitted, in which case a match\n\
118 is not required for the omitted attribute.\n\
121 --no-preserve-root do not treat `/' specially (the default)\n\
122 --preserve-root fail to operate recursively on `/'\n\
125 -f, --silent, --quiet suppress most error messages\n\
126 --reference=RFILE use RFILE's owner and group rather than\n\
127 the specifying 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\
133 The following options modify how a hierarchy is traversed when the -R\n\
134 option is also specified. If more than one is specified, only the final\n\
137 -H if a command line argument is a symbolic link\n\
138 to a directory, traverse it\n\
139 -L traverse every symbolic link to a directory\n\
141 -P do not traverse any symbolic links (default)\n\
144 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
145 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
148 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
149 to login group if implied by a `:'. OWNER and GROUP may be numeric as well\n\
152 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
158 main (int argc
, char **argv
)
160 bool preserve_root
= false;
162 uid_t uid
= -1; /* Specified uid; -1 if not to be changed. */
163 gid_t gid
= -1; /* Specified gid; -1 if not to be changed. */
165 /* Change the owner (group) of a file only if it has this uid (gid).
166 -1 means there's no restriction. */
167 uid_t required_uid
= -1;
168 gid_t required_gid
= -1;
170 /* Bit flags that control how fts works. */
171 int bit_flags
= FTS_PHYSICAL
;
173 struct Chown_option chopt
;
177 initialize_main (&argc
, &argv
);
178 program_name
= argv
[0];
179 setlocale (LC_ALL
, "");
180 bindtextdomain (PACKAGE
, LOCALEDIR
);
181 textdomain (PACKAGE
);
183 atexit (close_stdout
);
187 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, NULL
))
195 case 'H': /* Traverse command-line symlinks-to-directories. */
196 bit_flags
= FTS_COMFOLLOW
;
199 case 'L': /* Traverse all symlinks-to-directories. */
200 bit_flags
= FTS_LOGICAL
;
203 case 'P': /* Traverse no symlinks-to-directories. */
204 bit_flags
= FTS_PHYSICAL
;
207 case 'h': /* --no-dereference: affect symlinks */
208 chopt
.affect_symlink_referent
= false;
211 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
213 chopt
.affect_symlink_referent
= true;
216 case NO_PRESERVE_ROOT
:
217 preserve_root
= false;
221 preserve_root
= true;
224 case REFERENCE_FILE_OPTION
:
225 reference_file
= optarg
;
230 char *u_dummy
, *g_dummy
;
231 const char *e
= parse_user_spec (optarg
,
232 &required_uid
, &required_gid
,
235 error (EXIT_FAILURE
, 0, "%s: %s", quote (optarg
), e
);
240 chopt
.recurse
= true;
244 chopt
.verbosity
= V_changes_only
;
248 chopt
.force_silent
= true;
252 chopt
.verbosity
= V_high
;
255 case_GETOPT_HELP_CHAR
;
256 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
258 usage (EXIT_FAILURE
);
262 if (argc
- optind
+ (reference_file
? 1 : 0) <= 1)
264 error (0, 0, _("too few arguments"));
265 usage (EXIT_FAILURE
);
270 struct stat ref_stats
;
271 if (stat (reference_file
, &ref_stats
))
272 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
273 quote (reference_file
));
275 uid
= ref_stats
.st_uid
;
276 gid
= ref_stats
.st_gid
;
277 chopt
.user_name
= uid_to_name (ref_stats
.st_uid
);
278 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
282 const char *e
= parse_user_spec (argv
[optind
], &uid
, &gid
,
283 &chopt
.user_name
, &chopt
.group_name
);
285 error (EXIT_FAILURE
, 0, "%s: %s", quote (argv
[optind
]), e
);
287 /* FIXME: set it to the empty string? */
288 if (chopt
.user_name
== NULL
)
289 chopt
.user_name
= "";
294 if (chopt
.recurse
&& preserve_root
)
296 static struct dev_ino dev_ino_buf
;
297 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
298 if (chopt
.root_dev_ino
== NULL
)
299 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
303 fail
= chown_files (argv
+ optind
, bit_flags
,
305 required_uid
, required_gid
, &chopt
);
309 exit (fail
? EXIT_FAILURE
: EXIT_SUCCESS
);