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)
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>
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 ();
55 # define endpwent() ((void) 0)
58 char *parse_user_spec ();
59 void strip_trailing_slashes ();
61 /* The name the program was run with. */
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. */
72 REFERENCE_FILE_OPTION
= CHAR_MAX
+ 1,
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
},
97 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
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
);
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\
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\
135 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
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
;
152 program_name
= argv
[0];
153 setlocale (LC_ALL
, "");
154 bindtextdomain (PACKAGE
, LOCALEDIR
);
155 textdomain (PACKAGE
);
157 atexit (close_stdout
);
161 while ((optc
= getopt_long (argc
, argv
, "Rcfhv", long_options
, NULL
)) != -1)
167 case REFERENCE_FILE_OPTION
:
168 reference_file
= optarg
;
170 case DEREFERENCE_OPTION
:
171 chopt
.dereference
= DEREF_ALWAYS
;
175 char *u_dummy
, *g_dummy
;
176 const char *e
= parse_user_spec (argv
[optind
],
180 error (1, 0, "%s: %s", quote (argv
[optind
]), e
);
187 chopt
.verbosity
= V_changes_only
;
190 chopt
.force_silent
= 1;
193 chopt
.dereference
= DEREF_NEVER
;
196 chopt
.verbosity
= V_high
;
198 case_GETOPT_HELP_CHAR
;
199 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
205 if (argc
- optind
+ (reference_file
? 1 : 0) <= 1)
207 error (0, 0, _("too few arguments"));
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
);
225 const char *e
= parse_user_spec (argv
[optind
], &uid
, &gid
,
226 &chopt
.user_name
, &chopt
.group_name
);
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
= "";
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
);