OpenCoverage

chgrp.c

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

Generated by Squish Coco 4.1.2