OpenCoverage

chown.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/chown.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* chown -- change user and group ownership of files-
2 Copyright (C) 1989-2018 Free Software Foundation, Inc.-
3-
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.-
8-
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.-
13-
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/>. */-
16-
17/* Written by David MacKenzie <djm@gnu.ai.mit.edu>. */-
18-
19#include <config.h>-
20#include <stdio.h>-
21#include <sys/types.h>-
22#include <getopt.h>-
23-
24#include "system.h"-
25#include "chown-core.h"-
26#include "die.h"-
27#include "error.h"-
28#include "fts_.h"-
29#include "quote.h"-
30#include "root-dev-ino.h"-
31#include "userspec.h"-
32-
33/* The official name of this program (e.g., no 'g' prefix). */-
34#define PROGRAM_NAME "chown"-
35-
36#define AUTHORS \-
37 proper_name ("David MacKenzie"), \-
38 proper_name ("Jim Meyering")-
39-
40/* The argument to the --reference option. Use the owner and group IDs-
41 of this file. This file must exist. */-
42static char *reference_file;-
43-
44/* For long options that have no equivalent short option, use a-
45 non-character as a pseudo short option, starting with CHAR_MAX + 1. */-
46enum-
47{-
48 DEREFERENCE_OPTION = CHAR_MAX + 1,-
49 FROM_OPTION,-
50 NO_PRESERVE_ROOT,-
51 PRESERVE_ROOT,-
52 REFERENCE_FILE_OPTION-
53};-
54-
55static struct option const long_options[] =-
56{-
57 {"recursive", no_argument, NULL, 'R'},-
58 {"changes", no_argument, NULL, 'c'},-
59 {"dereference", no_argument, NULL, DEREFERENCE_OPTION},-
60 {"from", required_argument, NULL, FROM_OPTION},-
61 {"no-dereference", no_argument, NULL, 'h'},-
62 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},-
63 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},-
64 {"quiet", no_argument, NULL, 'f'},-
65 {"silent", no_argument, NULL, 'f'},-
66 {"reference", required_argument, NULL, REFERENCE_FILE_OPTION},-
67 {"verbose", no_argument, NULL, 'v'},-
68 {GETOPT_HELP_OPTION_DECL},-
69 {GETOPT_VERSION_OPTION_DECL},-
70 {NULL, 0, NULL, 0}-
71};-
72-
73void-
74usage (int status)-
75{-
76 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 21 times by 1 test
Evaluated by:
  • chown
3-21
77 emit_try_help ();
executed 3 times by 1 test: end of block
Executed by:
  • chown
3
78 else-
79 {-
80 printf (_("\-
81Usage: %s [OPTION]... [OWNER][:[GROUP]] FILE...\n\-
82 or: %s [OPTION]... --reference=RFILE FILE...\n\-
83"),-
84 program_name, program_name);-
85 fputs (_("\-
86Change the owner and/or group of each FILE to OWNER and/or GROUP.\n\-
87With --reference, change the owner and group of each FILE to those of RFILE.\n\-
88\n\-
89"), stdout);-
90 fputs (_("\-
91 -c, --changes like verbose but report only when a change is made\n\-
92 -f, --silent, --quiet suppress most error messages\n\-
93 -v, --verbose output a diagnostic for every file processed\n\-
94"), stdout);-
95 fputs (_("\-
96 --dereference affect the referent of each symbolic link (this is\n\-
97 the default), rather than the symbolic link itself\n\-
98 -h, --no-dereference affect symbolic links instead of any referenced file\n\-
99"), stdout);-
100 fputs (_("\-
101 (useful only on systems that can change the\n\-
102 ownership of a symlink)\n\-
103"), stdout);-
104 fputs (_("\-
105 --from=CURRENT_OWNER:CURRENT_GROUP\n\-
106 change the owner and/or group of each file only if\n\-
107 its current owner and/or group match those specified\n\-
108 here. Either may be omitted, in which case a match\n\-
109 is not required for the omitted attribute\n\-
110"), stdout);-
111 fputs (_("\-
112 --no-preserve-root do not treat '/' specially (the default)\n\-
113 --preserve-root fail to operate recursively on '/'\n\-
114"), stdout);-
115 fputs (_("\-
116 --reference=RFILE use RFILE's owner and group rather than\n\-
117 specifying OWNER:GROUP values\n\-
118"), stdout);-
119 fputs (_("\-
120 -R, --recursive operate on files and directories recursively\n\-
121"), stdout);-
122 fputs (_("\-
123\n\-
124The following options modify how a hierarchy is traversed when the -R\n\-
125option is also specified. If more than one is specified, only the final\n\-
126one takes effect.\n\-
127\n\-
128 -H if a command line argument is a symbolic link\n\-
129 to a directory, traverse it\n\-
130 -L traverse every symbolic link to a directory\n\-
131 encountered\n\-
132 -P do not traverse any symbolic links (default)\n\-
133\n\-
134"), stdout);-
135 fputs (HELP_OPTION_DESCRIPTION, stdout);-
136 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
137 fputs (_("\-
138\n\-
139Owner is unchanged if missing. Group is unchanged if missing, but changed\n\-
140to login group if implied by a ':' following a symbolic OWNER.\n\-
141OWNER and GROUP may be numeric as well as symbolic.\n\-
142"), stdout);-
143 printf (_("\-
144\n\-
145Examples:\n\-
146 %s root /u Change the owner of /u to \"root\".\n\-
147 %s root:staff /u Likewise, but also change its group to \"staff\".\n\-
148 %s -hR root /u Change the owner of /u and subfiles to \"root\".\n\-
149"),-
150 program_name, program_name, program_name);-
151 emit_ancillary_info (PROGRAM_NAME);-
152 }
executed 21 times by 1 test: end of block
Executed by:
  • chown
21
153 exit (status);
executed 24 times by 1 test: exit (status);
Executed by:
  • chown
24
154}-
155-
156int-
157main (int argc, char **argv)-
158{-
159 bool preserve_root = false;-
160-
161 uid_t uid = -1; /* Specified uid; -1 if not to be changed. */-
162 gid_t gid = -1; /* Specified gid; -1 if not to be changed. */-
163-
164 /* Change the owner (group) of a file only if it has this uid (gid).-
165 -1 means there's no restriction. */-
166 uid_t required_uid = -1;-
167 gid_t required_gid = -1;-
168-
169 /* Bit flags that control how fts works. */-
170 int bit_flags = FTS_PHYSICAL;-
171-
172 /* 1 if --dereference, 0 if --no-dereference, -1 if neither has been-
173 specified. */-
174 int dereference = -1;-
175-
176 struct Chown_option chopt;-
177 bool ok;-
178 int optc;-
179-
180 initialize_main (&argc, &argv);-
181 set_program_name (argv[0]);-
182 setlocale (LC_ALL, "");-
183 bindtextdomain (PACKAGE, LOCALEDIR);-
184 textdomain (PACKAGE);-
185-
186 atexit (close_stdout);-
187-
188 chopt_init (&chopt);-
189-
190 while ((optc = getopt_long (argc, argv, "HLPRcfhv", long_options, NULL))
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 69 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 29 times by 1 test
Evaluated by:
  • chown
29-69
191 != -1)
(optc = getopt... *)0) )) != -1Description
TRUEevaluated 69 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 29 times by 1 test
Evaluated by:
  • chown
29-69
192 {-
193 switch (optc)-
194 {-
195 case 'H': /* Traverse command-line symlinks-to-directories. */
executed 2 times by 1 test: case 'H':
Executed by:
  • chown
2
196 bit_flags = FTS_COMFOLLOW | FTS_PHYSICAL;-
197 break;
executed 2 times by 1 test: break;
Executed by:
  • chown
2
198-
199 case 'L': /* Traverse all symlinks-to-directories. */
executed 2 times by 1 test: case 'L':
Executed by:
  • chown
2
200 bit_flags = FTS_LOGICAL;-
201 break;
executed 2 times by 1 test: break;
Executed by:
  • chown
2
202-
203 case 'P': /* Traverse no symlinks-to-directories. */
executed 1 time by 1 test: case 'P':
Executed by:
  • chown
1
204 bit_flags = FTS_PHYSICAL;-
205 break;
executed 1 time by 1 test: break;
Executed by:
  • chown
1
206-
207 case 'h': /* --no-dereference: affect symlinks */
executed 4 times by 1 test: case 'h':
Executed by:
  • chown
4
208 dereference = 0;-
209 break;
executed 4 times by 1 test: break;
Executed by:
  • chown
4
210-
211 case DEREFERENCE_OPTION: /* --dereference: affect the referent
executed 2 times by 1 test: case DEREFERENCE_OPTION:
Executed by:
  • chown
2
212 of each symlink */-
213 dereference = 1;-
214 break;
executed 2 times by 1 test: break;
Executed by:
  • chown
2
215-
216 case NO_PRESERVE_ROOT:
executed 1 time by 1 test: case NO_PRESERVE_ROOT:
Executed by:
  • chown
1
217 preserve_root = false;-
218 break;
executed 1 time by 1 test: break;
Executed by:
  • chown
1
219-
220 case PRESERVE_ROOT:
executed 4 times by 1 test: case PRESERVE_ROOT:
Executed by:
  • chown
4
221 preserve_root = true;-
222 break;
executed 4 times by 1 test: break;
Executed by:
  • chown
4
223-
224 case REFERENCE_FILE_OPTION:
executed 1 time by 1 test: case REFERENCE_FILE_OPTION:
Executed by:
  • chown
1
225 reference_file = optarg;-
226 break;
executed 1 time by 1 test: break;
Executed by:
  • chown
1
227-
228 case FROM_OPTION:
executed 6 times by 1 test: case FROM_OPTION:
Executed by:
  • chown
6
229 {-
230 const char *e = parse_user_spec (optarg,-
231 &required_uid, &required_gid,-
232 NULL, NULL);-
233 if (e)
eDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • chown
FALSEevaluated 5 times by 1 test
Evaluated by:
  • chown
1-5
234 die (EXIT_FAILURE, 0, "%s: %s", e, quote (optarg));
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, \"%s: %s\", e, quote (optarg)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, "%s: %s", e, quote (optarg)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, "%s: %s", e, quote (optarg)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
Executed by:
  • chown
1
235 break;
executed 5 times by 1 test: break;
Executed by:
  • chown
5
236 }-
237-
238 case 'R':
executed 5 times by 1 test: case 'R':
Executed by:
  • chown
5
239 chopt.recurse = true;-
240 break;
executed 5 times by 1 test: break;
Executed by:
  • chown
5
241-
242 case 'c':
executed 2 times by 1 test: case 'c':
Executed by:
  • chown
2
243 chopt.verbosity = V_changes_only;-
244 break;
executed 2 times by 1 test: break;
Executed by:
  • chown
2
245-
246 case 'f':
executed 4 times by 1 test: case 'f':
Executed by:
  • chown
4
247 chopt.force_silent = true;-
248 break;
executed 4 times by 1 test: break;
Executed by:
  • chown
4
249-
250 case 'v':
executed 2 times by 1 test: case 'v':
Executed by:
  • chown
2
251 chopt.verbosity = V_high;-
252 break;
executed 2 times by 1 test: break;
Executed by:
  • chown
2
253-
254 case_GETOPT_HELP_CHAR;
never executed: break;
executed 21 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • chown
0-21
255 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 9 times by 1 test: exit ( 0 );
Executed by:
  • chown
never executed: break;
executed 9 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • chown
0-9
256 default:
executed 3 times by 1 test: default:
Executed by:
  • chown
3
257 usage (EXIT_FAILURE);-
258 }
never executed: end of block
0
259 }-
260-
261 if (chopt.recurse)
chopt.recurseDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 26 times by 1 test
Evaluated by:
  • chown
3-26
262 {-
263 if (bit_flags == FTS_PHYSICAL)
bit_flags == 0x0010Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • chown
FALSEevaluated 2 times by 1 test
Evaluated by:
  • chown
1-2
264 {-
265 if (dereference == 1)
dereference == 1Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • chown
0-1
266 die (EXIT_FAILURE, 0,
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"-R --dereference requires either -H or -L\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "-R --dereference requires either -H or -L" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "-R --dereference requires either -H or -L" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
267 _("-R --dereference requires either -H or -L"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"-R --dereference requires either -H or -L\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "-R --dereference requires either -H or -L" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "-R --dereference requires either -H or -L" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
268 dereference = 0;-
269 }
executed 1 time by 1 test: end of block
Executed by:
  • chown
1
270 }
executed 3 times by 1 test: end of block
Executed by:
  • chown
3
271 else-
272 {-
273 bit_flags = FTS_PHYSICAL;-
274 }
executed 26 times by 1 test: end of block
Executed by:
  • chown
26
275 chopt.affect_symlink_referent = (dereference != 0);-
276-
277 if (argc - optind < (reference_file ? 1 : 2))
argc - optind ..._file ? 1 : 2)Description
TRUEnever evaluated
FALSEevaluated 29 times by 1 test
Evaluated by:
  • chown
0-29
278 {-
279 if (argc <= optind)
argc <= optindDescription
TRUEnever evaluated
FALSEnever evaluated
0
280 error (0, 0, _("missing operand"));
never executed: error (0, 0, dcgettext (((void *)0), "missing operand" , 5) );
0
281 else-
282 error (0, 0, _("missing operand after %s"), quote (argv[argc - 1]));
never executed: error (0, 0, dcgettext (((void *)0), "missing operand after %s" , 5) , quote (argv[argc - 1]));
0
283 usage (EXIT_FAILURE);-
284 }
never executed: end of block
0
285-
286 if (reference_file)
reference_fileDescription
TRUEnever evaluated
FALSEevaluated 29 times by 1 test
Evaluated by:
  • chown
0-29
287 {-
288 struct stat ref_stats;-
289 if (stat (reference_file, &ref_stats))
stat (referenc...e, &ref_stats)Description
TRUEnever evaluated
FALSEnever evaluated
0
290 die (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to get attributes of %s\", 5), quotearg_style (shell_escape_always_quoting_style, reference_file)), assume (false))" ")... : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to get attributes of %s" , 5) , quotearg_style (shell_escape_always_quoting_style, reference_file)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
291 quoteaf (reference_file));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to get attributes of %s\", 5), quotearg_style (shell_escape_always_quoting_style, reference_file)), assume (false))" ")... : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to get attributes of %s" , 5) , quotearg_style (shell_escape_always_quoting_style, reference_file)), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
292-
293 uid = ref_stats.st_uid;-
294 gid = ref_stats.st_gid;-
295 chopt.user_name = uid_to_name (ref_stats.st_uid);-
296 chopt.group_name = gid_to_name (ref_stats.st_gid);-
297 }
never executed: end of block
0
298 else-
299 {-
300 const char *e = parse_user_spec (argv[optind], &uid, &gid,-
301 &chopt.user_name, &chopt.group_name);-
302 if (e)
eDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 27 times by 1 test
Evaluated by:
  • chown
2-27
303 die (EXIT_FAILURE, 0, "%s: %s", e, quote (argv[optind]));
executed 2 times by 1 test: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, \"%s: %s\", e, quote (argv[optind])), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, "%s: %s", e, quote (argv[optind])), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, "%s: %s", e, quote (argv[optind])), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
Executed by:
  • chown
2
304-
305 /* If a group is specified but no user, set the user name to the-
306 empty string so that diagnostics say "ownership :GROUP"-
307 rather than "group GROUP". */-
308 if (!chopt.user_name && chopt.group_name)
!chopt.user_nameDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 15 times by 1 test
Evaluated by:
  • chown
chopt.group_nameDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 3 times by 1 test
Evaluated by:
  • chown
3-15
309 chopt.user_name = bad_cast ("");
executed 9 times by 1 test: chopt.user_name = bad_cast ("");
Executed by:
  • chown
9
310-
311 optind++;-
312 }
executed 27 times by 1 test: end of block
Executed by:
  • chown
27
313-
314 if (chopt.recurse && preserve_root)
chopt.recurseDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • chown
FALSEevaluated 24 times by 1 test
Evaluated by:
  • chown
preserve_rootDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • chown
FALSEnever evaluated
0-24
315 {-
316 static struct dev_ino dev_ino_buf;-
317 chopt.root_dev_ino = get_root_dev_ino (&dev_ino_buf);-
318 if (chopt.root_dev_ino == NULL)
chopt.root_dev...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • chown
0-3
319 die (EXIT_FAILURE, errno, _("failed to get attributes of %s"),
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to get attributes of %s\", 5), quotearg_style (shell_escape_always_quoting_style, \"/\")), assume (false))" ")"); int _... ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to get attributes of %s" , 5) , quotearg_style (shell_escape_always_quoting_style, "/")), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
320 quoteaf ("/"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"failed to get attributes of %s\", 5), quotearg_style (shell_escape_always_quoting_style, \"/\")), assume (false))" ")"); int _... ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "failed to get attributes of %s" , 5) , quotearg_style (shell_escape_always_quoting_style, "/")), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
321 }
executed 3 times by 1 test: end of block
Executed by:
  • chown
3
322-
323 bit_flags |= FTS_DEFER_STAT;-
324 ok = chown_files (argv + optind, bit_flags,-
325 uid, gid,-
326 required_uid, required_gid, &chopt);-
327-
328 chopt_free (&chopt);-
329-
330 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
executed 27 times by 1 test: return ok ? 0 : 1 ;
Executed by:
  • chown
27
331}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2