1 /* chown, chgrp -- change user and group ownership of files
2 Copyright (C) 1989-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21 #include <sys/types.h>
26 #include "chown-core.h"
29 #include "root-dev-ino.h"
32 /* The official name of this program (e.g., no 'g' prefix). */
33 #define PROGRAM_NAME (chown_mode == CHOWN_CHOWN ? "chown" : "chgrp")
36 proper_name ("David MacKenzie"), \
37 proper_name ("Jim Meyering")
39 /* The argument to the --reference option. Use the owner and group IDs
40 of this file. This file must exist. */
41 static char *reference_file
;
43 /* For long options that have no equivalent short option, use a
44 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
47 DEREFERENCE_OPTION
= CHAR_MAX
+ 1,
54 static struct option
const long_options
[] =
56 {"recursive", no_argument
, nullptr, 'R'},
57 {"changes", no_argument
, nullptr, 'c'},
58 {"dereference", no_argument
, nullptr, DEREFERENCE_OPTION
},
59 {"from", required_argument
, nullptr, FROM_OPTION
},
60 {"no-dereference", no_argument
, nullptr, 'h'},
61 {"no-preserve-root", no_argument
, nullptr, NO_PRESERVE_ROOT
},
62 {"preserve-root", no_argument
, nullptr, PRESERVE_ROOT
},
63 {"quiet", no_argument
, nullptr, 'f'},
64 {"silent", no_argument
, nullptr, 'f'},
65 {"reference", required_argument
, nullptr, REFERENCE_FILE_OPTION
},
66 {"verbose", no_argument
, nullptr, 'v'},
67 {GETOPT_HELP_OPTION_DECL
},
68 {GETOPT_VERSION_OPTION_DECL
},
69 {nullptr, 0, nullptr, 0}
75 if (status
!= EXIT_SUCCESS
)
80 Usage: %s [OPTION]... %s FILE...\n\
81 or: %s [OPTION]... --reference=RFILE FILE...\n\
84 chown_mode
== CHOWN_CHOWN
? "[OWNER][:[GROUP]]" : "GROUP",
86 if (chown_mode
== CHOWN_CHOWN
)
88 Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\
89 With --reference, change the owner and group of each FILE to those of RFILE.\n\
94 Change the group of each FILE to GROUP.\n\
95 With --reference, change the group of each FILE to that of RFILE.\n\
99 -c, --changes like verbose but report only when a change is made\n\
100 -f, --silent, --quiet suppress most error messages\n\
101 -v, --verbose output a diagnostic for every file processed\n\
104 --dereference affect the referent of each symbolic link (this is\n\
105 the default), rather than the symbolic link itself\n\
106 -h, --no-dereference affect symbolic links instead of any referenced file\n\
109 (useful only on systems that can change the\n\
110 ownership of a symlink)\n\
112 emit_from_option_description (chown_mode
== CHOWN_CHOWN
);
114 --no-preserve-root do not treat '/' specially (the default)\n\
115 --preserve-root fail to operate recursively on '/'\n\
118 --reference=RFILE use RFILE's ownership rather than specifying values\n\
119 RFILE is always dereferenced if a symbolic link.\n\
122 -R, --recursive operate on files and directories recursively\n\
124 emit_symlink_recurse_options ("-P");
125 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
126 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
127 if (chown_mode
== CHOWN_CHOWN
)
130 Owner is unchanged if missing. Group is unchanged if missing, but changed\n\
131 to login group if implied by a ':' following a symbolic OWNER.\n\
132 OWNER and GROUP may be numeric as well as symbolic.\n\
135 if (chown_mode
== CHOWN_CHOWN
)
139 %s root /u Change the owner of /u to \"root\".\n\
140 %s root:staff /u Likewise, but also change its group to \"staff\".\n\
141 %s -hR root /u Change the owner of /u and subfiles to \"root\".\n\
143 program_name
, program_name
, program_name
);
148 %s staff /u Change the group of /u to \"staff\".\n\
149 %s -hR staff /u Change the group of /u and subfiles to \"staff\".\n\
151 program_name
, program_name
);
152 emit_ancillary_info (PROGRAM_NAME
);
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 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been
175 int dereference
= -1;
177 struct Chown_option chopt
;
181 initialize_main (&argc
, &argv
);
182 set_program_name (argv
[0]);
183 setlocale (LC_ALL
, "");
184 bindtextdomain (PACKAGE
, LOCALEDIR
);
185 textdomain (PACKAGE
);
187 atexit (close_stdout
);
191 while ((optc
= getopt_long (argc
, argv
, "HLPRcfhv", long_options
, nullptr))
196 case 'H': /* Traverse command-line symlinks-to-directories. */
197 bit_flags
= FTS_COMFOLLOW
| FTS_PHYSICAL
;
200 case 'L': /* Traverse all symlinks-to-directories. */
201 bit_flags
= FTS_LOGICAL
;
204 case 'P': /* Traverse no symlinks-to-directories. */
205 bit_flags
= FTS_PHYSICAL
;
208 case 'h': /* --no-dereference: affect symlinks */
212 case DEREFERENCE_OPTION
: /* --dereference: affect the referent
217 case NO_PRESERVE_ROOT
:
218 preserve_root
= false;
222 preserve_root
= true;
225 case REFERENCE_FILE_OPTION
:
226 reference_file
= optarg
;
232 char const *e
= parse_user_spec_warn (optarg
,
233 &required_uid
, &required_gid
,
234 nullptr, nullptr, &warn
);
236 error (warn
? 0 : EXIT_FAILURE
, 0, "%s: %s", e
, quote (optarg
));
241 chopt
.recurse
= true;
245 chopt
.verbosity
= V_changes_only
;
249 chopt
.force_silent
= true;
253 chopt
.verbosity
= V_high
;
256 case_GETOPT_HELP_CHAR
;
257 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
259 usage (EXIT_FAILURE
);
265 if (bit_flags
== FTS_PHYSICAL
)
267 if (dereference
== 1)
268 error (EXIT_FAILURE
, 0,
269 _("-R --dereference requires either -H or -L"));
275 bit_flags
= FTS_PHYSICAL
;
277 chopt
.affect_symlink_referent
= (dereference
!= 0);
279 if (argc
- optind
< (reference_file
? 1 : 2))
282 error (0, 0, _("missing operand"));
284 error (0, 0, _("missing operand after %s"), quote (argv
[argc
- 1]));
285 usage (EXIT_FAILURE
);
290 struct stat ref_stats
;
291 if (stat (reference_file
, &ref_stats
))
292 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
293 quoteaf (reference_file
));
295 if (chown_mode
== CHOWN_CHOWN
)
297 uid
= ref_stats
.st_uid
;
298 chopt
.user_name
= uid_to_name (ref_stats
.st_uid
);
300 gid
= ref_stats
.st_gid
;
301 chopt
.group_name
= gid_to_name (ref_stats
.st_gid
);
305 char *ug
= argv
[optind
];
306 if (chown_mode
== CHOWN_CHGRP
)
308 ug
= xmalloc (1 + strlen (argv
[optind
]) + 1);
309 stpcpy (stpcpy (ug
, ":"), argv
[optind
]);
313 char const *e
= parse_user_spec_warn (ug
, &uid
, &gid
,
315 &chopt
.group_name
, &warn
);
317 if (ug
!= argv
[optind
])
321 error (warn
? 0 : EXIT_FAILURE
, 0, "%s: %s", e
, quote (argv
[optind
]));
323 /* If a group is specified but no user, set the user name to the
324 empty string so that diagnostics say "ownership :GROUP"
325 rather than "group GROUP". */
326 if (chown_mode
== CHOWN_CHOWN
&& !chopt
.user_name
&& chopt
.group_name
)
327 chopt
.user_name
= xstrdup ("");
332 if (chopt
.recurse
&& preserve_root
)
334 static struct dev_ino dev_ino_buf
;
335 chopt
.root_dev_ino
= get_root_dev_ino (&dev_ino_buf
);
336 if (chopt
.root_dev_ino
== nullptr)
337 error (EXIT_FAILURE
, errno
, _("failed to get attributes of %s"),
341 bit_flags
|= FTS_DEFER_STAT
;
342 ok
= chown_files (argv
+ optind
, bit_flags
,
344 required_uid
, required_gid
, &chopt
);
346 main_exit (ok
? EXIT_SUCCESS
: EXIT_FAILURE
);