OpenCoverage

rm.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/rm.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* 'rm' file deletion utility for GNU.-
2 Copyright (C) 1988-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/* Initially written by Paul Rubin, David MacKenzie, and Richard Stallman.-
18 Reworked to use chdir and avoid recursion, and later, rewritten-
19 once again, to use fts, by Jim Meyering. */-
20-
21#include <config.h>-
22#include <stdio.h>-
23#include <getopt.h>-
24#include <sys/types.h>-
25#include <assert.h>-
26-
27#include "system.h"-
28#include "argmatch.h"-
29#include "die.h"-
30#include "error.h"-
31#include "remove.h"-
32#include "root-dev-ino.h"-
33#include "yesno.h"-
34#include "priv-set.h"-
35-
36/* The official name of this program (e.g., no 'g' prefix). */-
37#define PROGRAM_NAME "rm"-
38-
39#define AUTHORS \-
40 proper_name ("Paul Rubin"), \-
41 proper_name ("David MacKenzie"), \-
42 proper_name ("Richard M. Stallman"), \-
43 proper_name ("Jim Meyering")-
44-
45/* For long options that have no equivalent short option, use a-
46 non-character as a pseudo short option, starting with CHAR_MAX + 1. */-
47enum-
48{-
49 INTERACTIVE_OPTION = CHAR_MAX + 1,-
50 ONE_FILE_SYSTEM,-
51 NO_PRESERVE_ROOT,-
52 PRESERVE_ROOT,-
53 PRESUME_INPUT_TTY_OPTION-
54};-
55-
56enum interactive_type-
57 {-
58 interactive_never, /* 0: no option or --interactive=never */-
59 interactive_once, /* 1: -I or --interactive=once */-
60 interactive_always /* 2: default, -i or --interactive=always */-
61 };-
62-
63static struct option const long_opts[] =-
64{-
65 {"force", no_argument, NULL, 'f'},-
66 {"interactive", optional_argument, NULL, INTERACTIVE_OPTION},-
67-
68 {"one-file-system", no_argument, NULL, ONE_FILE_SYSTEM},-
69 {"no-preserve-root", no_argument, NULL, NO_PRESERVE_ROOT},-
70 {"preserve-root", no_argument, NULL, PRESERVE_ROOT},-
71-
72 /* This is solely for testing. Do not document. */-
73 /* It is relatively difficult to ensure that there is a tty on stdin.-
74 Since rm acts differently depending on that, without this option,-
75 it'd be harder to test the parts of rm that depend on that setting. */-
76 {"-presume-input-tty", no_argument, NULL, PRESUME_INPUT_TTY_OPTION},-
77-
78 {"recursive", no_argument, NULL, 'r'},-
79 {"dir", no_argument, NULL, 'd'},-
80 {"verbose", no_argument, NULL, 'v'},-
81 {GETOPT_HELP_OPTION_DECL},-
82 {GETOPT_VERSION_OPTION_DECL},-
83 {NULL, 0, NULL, 0}-
84};-
85-
86static char const *const interactive_args[] =-
87{-
88 "never", "no", "none",-
89 "once",-
90 "always", "yes", NULL-
91};-
92static enum interactive_type const interactive_types[] =-
93{-
94 interactive_never, interactive_never, interactive_never,-
95 interactive_once,-
96 interactive_always, interactive_always-
97};-
98ARGMATCH_VERIFY (interactive_args, interactive_types);-
99-
100/* Advise the user about invalid usages like "rm -foo" if the file-
101 "-foo" exists, assuming ARGC and ARGV are as with 'main'. */-
102-
103static void-
104diagnose_leading_hyphen (int argc, char **argv)-
105{-
106 /* OPTIND is unreliable, so iterate through the arguments looking-
107 for a file name that looks like an option. */-
108-
109 for (int i = 1; i < argc; i++)
i < argcDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • rm
FALSEevaluated 1 time by 1 test
Evaluated by:
  • rm
1
110 {-
111 char const *arg = argv[i];-
112 struct stat st;-
113-
114 if (arg[0] == '-' && arg[1] && lstat (arg, &st) == 0)
arg[0] == '-'Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • rm
FALSEnever evaluated
arg[1]Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • rm
FALSEnever evaluated
lstat (arg, &st) == 0Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • rm
0-1
115 {-
116 fprintf (stderr,-
117 _("Try '%s ./%s' to remove the file %s.\n"),-
118 argv[0],-
119 quotearg_n_style (1, shell_escape_quoting_style, arg),-
120 quoteaf (arg));-
121 break;
never executed: break;
0
122 }-
123 }
executed 1 time by 1 test: end of block
Executed by:
  • rm
1
124}
executed 1 time by 1 test: end of block
Executed by:
  • rm
1
125-
126void-
127usage (int status)-
128{-
129 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • rm
FALSEevaluated 2 times by 1 test
Evaluated by:
  • rm
1-2
130 emit_try_help ();
executed 1 time by 1 test: end of block
Executed by:
  • rm
1
131 else-
132 {-
133 printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);-
134 fputs (_("\-
135Remove (unlink) the FILE(s).\n\-
136\n\-
137 -f, --force ignore nonexistent files and arguments, never prompt\n\-
138 -i prompt before every removal\n\-
139"), stdout);-
140 fputs (_("\-
141 -I prompt once before removing more than three files, or\n\-
142 when removing recursively; less intrusive than -i,\n\-
143 while still giving protection against most mistakes\n\-
144 --interactive[=WHEN] prompt according to WHEN: never, once (-I), or\n\-
145 always (-i); without WHEN, prompt always\n\-
146"), stdout);-
147 fputs (_("\-
148 --one-file-system when removing a hierarchy recursively, skip any\n\-
149 directory that is on a file system different from\n\-
150 that of the corresponding command line argument\n\-
151"), stdout);-
152 fputs (_("\-
153 --no-preserve-root do not treat '/' specially\n\-
154 --preserve-root do not remove '/' (default)\n\-
155 -r, -R, --recursive remove directories and their contents recursively\n\-
156 -d, --dir remove empty directories\n\-
157 -v, --verbose explain what is being done\n\-
158"), stdout);-
159 fputs (HELP_OPTION_DESCRIPTION, stdout);-
160 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
161 fputs (_("\-
162\n\-
163By default, rm does not remove directories. Use the --recursive (-r or -R)\n\-
164option to remove each listed directory, too, along with all of its contents.\n\-
165"), stdout);-
166 printf (_("\-
167\n\-
168To remove a file whose name starts with a '-', for example '-foo',\n\-
169use one of these commands:\n\-
170 %s -- -foo\n\-
171\n\-
172 %s ./-foo\n\-
173"),-
174 program_name, program_name);-
175 fputs (_("\-
176\n\-
177Note that if you use rm to remove a file, it might be possible to recover\n\-
178some of its contents, given sufficient expertise and/or time. For greater\n\-
179assurance that the contents are truly unrecoverable, consider using shred.\n\-
180"), stdout);-
181 emit_ancillary_info (PROGRAM_NAME);-
182 }
executed 2 times by 1 test: end of block
Executed by:
  • rm
2
183 exit (status);
executed 3 times by 1 test: exit (status);
Executed by:
  • rm
3
184}-
185-
186static void-
187rm_option_init (struct rm_options *x)-
188{-
189 x->ignore_missing_files = false;-
190 x->interactive = RMI_SOMETIMES;-
191 x->one_file_system = false;-
192 x->remove_empty_directories = false;-
193 x->recursive = false;-
194 x->root_dev_ino = NULL;-
195 x->stdin_tty = isatty (STDIN_FILENO);-
196 x->verbose = false;-
197-
198 /* Since this program exits immediately after calling 'rm', rm need not-
199 expend unnecessary effort to preserve the initial working directory. */-
200 x->require_restore_cwd = false;-
201}
executed 533 times by 1 test: end of block
Executed by:
  • rm
533
202-
203int-
204main (int argc, char **argv)-
205{-
206 bool preserve_root = true;-
207 struct rm_options x;-
208 bool prompt_once = false;-
209 int c;-
210-
211 initialize_main (&argc, &argv);-
212 set_program_name (argv[0]);-
213 setlocale (LC_ALL, "");-
214 bindtextdomain (PACKAGE, LOCALEDIR);-
215 textdomain (PACKAGE);-
216-
217 atexit (close_stdin);-
218-
219 rm_option_init (&x);-
220-
221 /* Try to disable the ability to unlink a directory. */-
222 priv_set_remove_linkdir ();-
223-
224 while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1)
(c = getopt_lo... *)0) )) != -1Description
TRUEevaluated 889 times by 1 test
Evaluated by:
  • rm
FALSEevaluated 482 times by 1 test
Evaluated by:
  • rm
482-889
225 {-
226 switch (c)-
227 {-
228 case 'd':
executed 3 times by 1 test: case 'd':
Executed by:
  • rm
3
229 x.remove_empty_directories = true;-
230 break;
executed 3 times by 1 test: break;
Executed by:
  • rm
3
231-
232 case 'f':
executed 429 times by 1 test: case 'f':
Executed by:
  • rm
429
233 x.interactive = RMI_NEVER;-
234 x.ignore_missing_files = true;-
235 prompt_once = false;-
236 break;
executed 429 times by 1 test: break;
Executed by:
  • rm
429
237-
238 case 'i':
executed 7 times by 1 test: case 'i':
Executed by:
  • rm
7
239 x.interactive = RMI_ALWAYS;-
240 x.ignore_missing_files = false;-
241 prompt_once = false;-
242 break;
executed 7 times by 1 test: break;
Executed by:
  • rm
7
243-
244 case 'I':
executed 10 times by 1 test: case 'I':
Executed by:
  • rm
10
245 x.interactive = RMI_SOMETIMES;-
246 x.ignore_missing_files = false;-
247 prompt_once = true;-
248 break;
executed 10 times by 1 test: break;
Executed by:
  • rm
10
249-
250 case 'r':
executed 355 times by 1 test: case 'r':
Executed by:
  • rm
355
251 case 'R':
executed 14 times by 1 test: case 'R':
Executed by:
  • rm
14
252 x.recursive = true;-
253 break;
executed 369 times by 1 test: break;
Executed by:
  • rm
369
254-
255 case INTERACTIVE_OPTION:
executed 7 times by 1 test: case INTERACTIVE_OPTION:
Executed by:
  • rm
7
256 {-
257 int i;-
258 if (optarg)
optargDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • rm
FALSEevaluated 1 time by 1 test
Evaluated by:
  • rm
1-6
259 i = XARGMATCH ("--interactive", optarg, interactive_args,
executed 6 times by 1 test: i = ((interactive_types) [__xargmatch_internal ("--interactive", optarg, interactive_args, (char const *) (interactive_types), sizeof *(interactive_types), argmatch_die)]) ;
Executed by:
  • rm
6
260 interactive_types);
executed 6 times by 1 test: i = ((interactive_types) [__xargmatch_internal ("--interactive", optarg, interactive_args, (char const *) (interactive_types), sizeof *(interactive_types), argmatch_die)]) ;
Executed by:
  • rm
6
261 else-
262 i = interactive_always;
executed 1 time by 1 test: i = interactive_always;
Executed by:
  • rm
1
263 switch (i)-
264 {-
265 case interactive_never:
executed 2 times by 1 test: case interactive_never:
Executed by:
  • rm
2
266 x.interactive = RMI_NEVER;-
267 prompt_once = false;-
268 break;
executed 2 times by 1 test: break;
Executed by:
  • rm
2
269-
270 case interactive_once:
executed 3 times by 1 test: case interactive_once:
Executed by:
  • rm
3
271 x.interactive = RMI_SOMETIMES;-
272 x.ignore_missing_files = false;-
273 prompt_once = true;-
274 break;
executed 3 times by 1 test: break;
Executed by:
  • rm
3
275-
276 case interactive_always:
executed 2 times by 1 test: case interactive_always:
Executed by:
  • rm
2
277 x.interactive = RMI_ALWAYS;-
278 x.ignore_missing_files = false;-
279 prompt_once = false;-
280 break;
executed 2 times by 1 test: break;
Executed by:
  • rm
2
281 }-
282 break;
executed 7 times by 1 test: break;
Executed by:
  • rm
7
283 }-
284-
285 case ONE_FILE_SYSTEM:
executed 1 time by 1 test: case ONE_FILE_SYSTEM:
Executed by:
  • rm
1
286 x.one_file_system = true;-
287 break;
executed 1 time by 1 test: break;
Executed by:
  • rm
1
288-
289 case NO_PRESERVE_ROOT:
never executed: case NO_PRESERVE_ROOT:
0
290 if (! STREQ (argv[optind - 1], "--no-preserve-root"))
never executed: __result = (((const unsigned char *) (const char *) ( argv[optind - 1] ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "--no-preserve-root" ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
! ( __extensio...)))); }) == 0)Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
291 die (EXIT_FAILURE, 0,
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"you may not abbreviate the --no-preserve-root option\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "... abbreviate the --no-preserve-root option" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "you may not abbreviate the --no-preserve-root option" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
292 _("you may not abbreviate the --no-preserve-root option"));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 0, dcgettext (((void *)0), \"you may not abbreviate the --no-preserve-root option\", 5)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 0, dcgettext (((void *)0), "... abbreviate the --no-preserve-root option" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 0, dcgettext (((void *)0), "you may not abbreviate the --no-preserve-root option" , 5) ), (( 0 ) ? (void) 0 : __builtin_unreachable ())))) ;
0
293 preserve_root = false;-
294 break;
never executed: break;
0
295-
296 case PRESERVE_ROOT:
never executed: case PRESERVE_ROOT:
0
297 preserve_root = true;-
298 break;
never executed: break;
0
299-
300 case PRESUME_INPUT_TTY_OPTION:
executed 6 times by 1 test: case PRESUME_INPUT_TTY_OPTION:
Executed by:
  • rm
6
301 x.stdin_tty = true;-
302 break;
executed 6 times by 1 test: break;
Executed by:
  • rm
6
303-
304 case 'v':
executed 6 times by 1 test: case 'v':
Executed by:
  • rm
6
305 x.verbose = true;-
306 break;
executed 6 times by 1 test: break;
Executed by:
  • rm
6
307-
308 case_GETOPT_HELP_CHAR;
never executed: break;
executed 2 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • rm
0-2
309 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 48 times by 1 test: exit ( 0 );
Executed by:
  • rm
never executed: break;
executed 48 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • rm
0-48
310 default:
executed 1 time by 1 test: default:
Executed by:
  • rm
1
311 diagnose_leading_hyphen (argc, argv);-
312 usage (EXIT_FAILURE);-
313 }
never executed: end of block
0
314 }-
315-
316 if (argc <= optind)
argc <= optindDescription
TRUEnever evaluated
FALSEevaluated 482 times by 1 test
Evaluated by:
  • rm
0-482
317 {-
318 if (x.ignore_missing_files)
x.ignore_missing_filesDescription
TRUEnever evaluated
FALSEnever evaluated
0
319 return EXIT_SUCCESS;
never executed: return 0 ;
0
320 else-
321 {-
322 error (0, 0, _("missing operand"));-
323 usage (EXIT_FAILURE);-
324 }
never executed: end of block
0
325 }-
326-
327 if (x.recursive && preserve_root)
x.recursiveDescription
TRUEevaluated 369 times by 1 test
Evaluated by:
  • rm
FALSEevaluated 113 times by 1 test
Evaluated by:
  • rm
preserve_rootDescription
TRUEevaluated 369 times by 1 test
Evaluated by:
  • rm
FALSEnever evaluated
0-369
328 {-
329 static struct dev_ino dev_ino_buf;-
330 x.root_dev_ino = get_root_dev_ino (&dev_ino_buf);-
331 if (x.root_dev_ino == NULL)
x.root_dev_ino == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 369 times by 1 test
Evaluated by:
  • rm
0-369
332 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
333 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
334 }
executed 369 times by 1 test: end of block
Executed by:
  • rm
369
335-
336 uintmax_t n_files = argc - optind;-
337 char **file = argv + optind;-
338-
339 if (prompt_once && (x.recursive || 3 < n_files))
prompt_onceDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • rm
FALSEevaluated 470 times by 1 test
Evaluated by:
  • rm
x.recursiveDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • rm
FALSEevaluated 6 times by 1 test
Evaluated by:
  • rm
3 < n_filesDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • rm
FALSEevaluated 3 times by 1 test
Evaluated by:
  • rm
3-470
340 {-
341 fprintf (stderr,-
342 (x.recursive-
343 ? ngettext ("%s: remove %"PRIuMAX" argument recursively? ",-
344 "%s: remove %"PRIuMAX" arguments recursively? ",-
345 select_plural (n_files))-
346 : ngettext ("%s: remove %"PRIuMAX" argument? ",-
347 "%s: remove %"PRIuMAX" arguments? ",-
348 select_plural (n_files))),-
349 program_name, n_files);-
350 if (!yesno ())
!yesno ()Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • rm
FALSEevaluated 4 times by 1 test
Evaluated by:
  • rm
4-5
351 return EXIT_SUCCESS;
executed 5 times by 1 test: return 0 ;
Executed by:
  • rm
5
352 }
executed 4 times by 1 test: end of block
Executed by:
  • rm
4
353-
354 enum RM_status status = rm (file, &x);-
355 assert (VALID_STATUS (status));-
356 return status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS;
executed 477 times by 1 test: return status == RM_ERROR ? 1 : 0 ;
Executed by:
  • rm
477
357}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2