1 /* chown -- change user and group ownership of files
2 Copyright (C) 89, 90, 91, 1995-2007 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 3 of the License, or
7 (at your option) 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, see <http://www.gnu.org/licenses/>. */
20 -------------|-------------------------+-------------------------|
21 g unchanged | --- | chown u |
22 r |-------------------------+-------------------------|
23 o explicit | chgrp g or chown .g | chown u.g |
24 u |-------------------------+-------------------------|
25 p from passwd| --- | chown u. |
26 |-------------------------+-------------------------|
28 Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
32 #include <sys/types.h>
36 #include "chown-core.h"
40 #include "root-dev-ino.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "chown"
46 #define AUTHORS "David MacKenzie", "Jim Meyering"
48 /* The name the program was run with. */
51 /* The argument to the --reference option. Use the owner and group IDs
52 of this file. This file must exist. */
53 static char *reference_file
;
55 /* For long options that have no equivalent short option, use a
56 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
59 DEREFERENCE_OPTION
= CHAR_MAX
+ 1,
66 static struct option
const long_options
[] =
68 {"recursive", no_argument
, NULL
, 'R'},
69 {"changes", no_argument
, NULL
, 'c'},
70 {"dereference", no_argument
, NULL
, DEREFERENCE_OPTION
},
71 {"from", required_argument
, NULL
, FROM_OPTION
},
72 {"no-dereference", no_argument
, NULL
, 'h'},
73 {"no-preserve-root", no_argument
, NULL
, NO_PRESERVE_ROOT
},
74 {"preserve-root", no_argument
, NULL
, PRESERVE_ROOT
},
75 {"quiet", no_argument
, NULL
, 'f'},
76 {"silent", no_argument
, NULL
, 'f'},
77 {"reference", required_argument
, NULL
, REFERENCE_FILE_OPTION
},
78 {"verbose", no_argument
, NULL
, 'v'},
79 {GETOPT_HELP_OPTION_DECL
},
80 {GETOPT_VERSION_OPTION_DECL
},
87 if (status
!= EXIT_SUCCESS
)
88 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
93 Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\
94 or: %s [OPTION]... --reference=RFILE FILE...\n\
96 program_name
, program_name
);
98 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
99 With --reference, change the owner and group of each FILE to those of RFILE.\n\
101 -c, --changes like verbose but report only when a change is made\n\
102 --dereference affect the referent of each symbolic link (this is\n\
103 the default), rather than the symbolic link itself\n\
106 -h, --no-dereference affect each symbolic link instead of any referenced\n\
107 file (useful only on systems that can change the\n\
108 ownership of a symlink)\n\
111 --from=CURRENT_OWNER:CURRENT_GROUP\n\
112 change the owner and/or group of each file only if\n\
113 its current owner and/or group match those specified\n\
114 here. Either may be omitted, in which case a match\n\
115 is not required for the omitted attribute.\n\
118 --no-preserve-root do not treat `/' specially (the default)\n\
119 --preserve-root fail to operate recursively on `/'\n\
122 -f, --silent, --quiet suppress most error messages\n\
123 --reference=RFILE use RFILE's owner and group rather than\n\
124 specifying OWNER:GROUP values\n\
125 -R, --recursive operate on files and directories recursively\n\
126 -v, --verbose output a diagnostic for every file processed\n\
130 The following options modify how a hierarchy is traversed when the -R\n\
131 option is also specified. If more than one is specified, only the final\n\
134 -H if a command line argument is a symbolic link\n\
135 to a directory, traverse it\n\
136 -L traverse every symbolic link to a directory\n\
138 -P do not traverse any symbolic links (default)\n\
141 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
142 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
145 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
146 to login group if implied by a `:' following a symbolic OWNER.\n\
147 OWNER and GROUP may be numeric as well as symbolic.\n\
152 %s root /u Change the owner of /u to \"root\".\n\
153 %s root:staff /u Likewise, but also change its group to \"staff\".\n\
154 %s -hR root /u Change the owner of /u and subfiles to \"root\".\n\
156 program_name
, program_name
, program_name
);
157 emit_bug_reporting_address ();
163 main (int argc
, char **argv
)
165 bool preserve_root
= false;
167 uid_t uid
= -1; /* Specified uid; -1 if not to be changed. */
168 gid_t gid
= -1; /* Specified gid; -1 if not to be changed. */
170 /* Change the owner (group) of a file only if it has this uid (gid).
171 -1 means there's no restriction. */
172 uid_t required_uid
= -1;
173 gid_t required_gid
= -1;
175 /* Bit flags that control how fts works. */
176 int bit_flags
= FTS_PHYSICAL
;
178 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
180 int dereference
= -1;
182 struct Chown_option chopt
;
186 initialize_main (&argc
, &argv
);
187 program_name
= argv
[0];
188 setlocale (LC_ALL
, "");
189 bindtextdomain (PACKAGE
, LOCALEDIR
);
190 textdomain (PACKAGE
);
192 atexit (close_stdout
);
196 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, NULL
))
201 case 'H': /* Traverse command-line symlinks-to-directories. */
202 bit_flags
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
205 case 'L': /* Traverse all symlinks-to-directories. */
206 bit_flags
= FTS_LOGICAL
;
209 case 'P': /* Traverse no symlinks-to-directories. */
210 bit_flags
= FTS_PHYSICAL
;
213 case 'h': /* --no-dereference: affect symlinks */
217 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
222 case NO_PRESERVE_ROOT
:
223 preserve_root
= false;
227 preserve_root
= true;
230 case REFERENCE_FILE_OPTION
:
231 reference_file
= optarg
;
236 char *u_dummy
, *g_dummy
;
237 const char *e
= parse_user_spec (optarg
,
238 &required_uid
, &required_gid
,
241 error (EXIT_FAILURE
, 0, "%s: %s", e
, quote (optarg
));
246 chopt
.recurse
= true;
250 chopt
.verbosity
= V_changes_only
;
254 chopt
.force_silent
= true;
258 chopt
.verbosity
= V_high
;
261 case_GETOPT_HELP_CHAR
;
262 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
264 usage (EXIT_FAILURE
);
270 if (bit_flags
== FTS_PHYSICAL
)
272 if (dereference
== 1)
273 error (EXIT_FAILURE
, 0,
274 _("-R --dereference requires either -H or -L"));
280 bit_flags
= FTS_PHYSICAL
;
282 chopt
.affect_symlink_referent
= (dereference
!= 0);
284 if (argc
- optind
< (reference_file
? 1 : 2))
287 error (0, 0, _("missing operand"));
289 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
290 usage (EXIT_FAILURE
);
295 struct stat ref_stats
;
296 if (stat (reference_file
, &ref_stats
))
297 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
298 quote (reference_file
));
300 uid
= ref_stats
.st_uid
;
301 gid
= ref_stats
.st_gid
;
302 chopt
.user_name
= uid_to_name (ref_stats
.st_uid
);
303 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
307 const char *e
= parse_user_spec (argv
[optind
], &uid
, &gid
,
308 &chopt
.user_name
, &chopt
.group_name
);
310 error (EXIT_FAILURE
, 0, "%s: %s", e
, quote (argv
[optind
]));
312 /* If a group is specified but no user, set the user name to the
313 empty string so that diagnostics say "ownership :GROUP"
314 rather than "group GROUP". */
315 if (!chopt
.user_name
&& chopt
.group_name
)
316 chopt
.user_name
= "";
321 if (chopt
.recurse
& preserve_root
)
323 static struct dev_ino dev_ino_buf
;
324 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
325 if (chopt
.root_dev_ino
== NULL
)
326 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
330 ok
= chown_files (argv
+ optind
, bit_flags
,
332 required_uid
, required_gid
, &chopt
);
336 exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);