OpenCoverage

basename.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/basename.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* basename -- strip directory and suffix from file names-
2 Copyright (C) 1990-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#include <config.h>-
18#include <getopt.h>-
19#include <stdio.h>-
20#include <sys/types.h>-
21-
22#include "system.h"-
23#include "error.h"-
24#include "quote.h"-
25-
26/* The official name of this program (e.g., no 'g' prefix). */-
27#define PROGRAM_NAME "basename"-
28-
29#define AUTHORS proper_name ("David MacKenzie")-
30-
31static struct option const longopts[] =-
32{-
33 {"multiple", no_argument, NULL, 'a'},-
34 {"suffix", required_argument, NULL, 's'},-
35 {"zero", no_argument, NULL, 'z'},-
36 {GETOPT_HELP_OPTION_DECL},-
37 {GETOPT_VERSION_OPTION_DECL},-
38 {NULL, 0, NULL, 0}-
39};-
40-
41void-
42usage (int status)-
43{-
44 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 9 times by 1 test
Evaluated by:
  • basename
5-9
45 emit_try_help ();
executed 5 times by 1 test: end of block
Executed by:
  • basename
5
46 else-
47 {-
48 printf (_("\-
49Usage: %s NAME [SUFFIX]\n\-
50 or: %s OPTION... NAME...\n\-
51"),-
52 program_name, program_name);-
53 fputs (_("\-
54Print NAME with any leading directory components removed.\n\-
55If specified, also remove a trailing SUFFIX.\n\-
56"), stdout);-
57-
58 emit_mandatory_arg_note ();-
59-
60 fputs (_("\-
61 -a, --multiple support multiple arguments and treat each as a NAME\n\-
62 -s, --suffix=SUFFIX remove a trailing SUFFIX; implies -a\n\-
63 -z, --zero end each output line with NUL, not newline\n\-
64"), stdout);-
65 fputs (HELP_OPTION_DESCRIPTION, stdout);-
66 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
67 printf (_("\-
68\n\-
69Examples:\n\-
70 %s /usr/bin/sort -> \"sort\"\n\-
71 %s include/stdio.h .h -> \"stdio\"\n\-
72 %s -s .h include/stdio.h -> \"stdio\"\n\-
73 %s -a any/str1 any/str2 -> \"str1\" followed by \"str2\"\n\-
74"),-
75 program_name, program_name, program_name, program_name);-
76 emit_ancillary_info (PROGRAM_NAME);-
77 }
executed 9 times by 1 test: end of block
Executed by:
  • basename
9
78 exit (status);
executed 14 times by 1 test: exit (status);
Executed by:
  • basename
14
79}-
80-
81/* Remove SUFFIX from the end of NAME if it is there, unless NAME-
82 consists entirely of SUFFIX. */-
83-
84static void-
85remove_suffix (char *name, const char *suffix)-
86{-
87 char *np;-
88 const char *sp;-
89-
90 np = name + strlen (name);-
91 sp = suffix + strlen (suffix);-
92-
93 while (np > name && sp > suffix)
np > nameDescription
TRUEevaluated 31 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 1 time by 1 test
Evaluated by:
  • basename
sp > suffixDescription
TRUEevaluated 20 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 11 times by 1 test
Evaluated by:
  • basename
1-31
94 if (*--np != *--sp)
*--np != *--spDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 17 times by 1 test
Evaluated by:
  • basename
3-17
95 return;
executed 3 times by 1 test: return;
Executed by:
  • basename
3
96 if (np > name)
np > nameDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 1 time by 1 test
Evaluated by:
  • basename
1-11
97 *np = '\0';
executed 11 times by 1 test: *np = '\0';
Executed by:
  • basename
11
98}
executed 12 times by 1 test: end of block
Executed by:
  • basename
12
99-
100/* Perform the basename operation on STRING. If SUFFIX is non-NULL, remove-
101 the trailing SUFFIX. Finally, output the result string. */-
102-
103static void-
104perform_basename (const char *string, const char *suffix, bool use_nuls)-
105{-
106 char *name = base_name (string);-
107 strip_trailing_slashes (name);-
108-
109 /* Per POSIX, 'basename // /' must return '//' on platforms with-
110 distinct //. On platforms with drive letters, this generalizes-
111 to making 'basename c: :' return 'c:'. This rule is captured by-
112 skipping suffix stripping if base_name returned an absolute path-
113 or a drive letter (only possible if name is a file-system-
114 root). */-
115 if (suffix && IS_RELATIVE_FILE_NAME (name) && ! FILE_SYSTEM_PREFIX_LEN (name))
suffixDescription
TRUEevaluated 17 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 23 times by 1 test
Evaluated by:
  • basename
(((name)[0]) == '/')Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 15 times by 1 test
Evaluated by:
  • basename
0 != 0Description
TRUEnever evaluated
FALSEevaluated 15 times by 1 test
Evaluated by:
  • basename
0-23
116 remove_suffix (name, suffix);
executed 15 times by 1 test: remove_suffix (name, suffix);
Executed by:
  • basename
15
117-
118 fputs (name, stdout);-
119 putchar (use_nuls ? '\0' : '\n');-
120 free (name);-
121}
executed 40 times by 1 test: end of block
Executed by:
  • basename
40
122-
123int-
124main (int argc, char **argv)-
125{-
126 bool multiple_names = false;-
127 bool use_nuls = false;-
128 const char *suffix = NULL;-
129-
130 initialize_main (&argc, &argv);-
131 set_program_name (argv[0]);-
132 setlocale (LC_ALL, "");-
133 bindtextdomain (PACKAGE, LOCALEDIR);-
134 textdomain (PACKAGE);-
135-
136 atexit (close_stdout);-
137-
138 while (true)-
139 {-
140 int c = getopt_long (argc, argv, "+as:z", longopts, NULL);-
141-
142 if (c == -1)
c == -1Description
TRUEevaluated 38 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 31 times by 1 test
Evaluated by:
  • basename
31-38
143 break;
executed 38 times by 1 test: break;
Executed by:
  • basename
38
144-
145 switch (c)-
146 {-
147 case 's':
executed 4 times by 1 test: case 's':
Executed by:
  • basename
4
148 suffix = optarg;-
149 /* -s implies -a, so... */-
150 FALLTHROUGH;-
151-
152 case 'a':
code before this statement executed 4 times by 1 test: case 'a':
Executed by:
  • basename
executed 4 times by 1 test: case 'a':
Executed by:
  • basename
4
153 multiple_names = true;-
154 break;
executed 8 times by 1 test: break;
Executed by:
  • basename
8
155-
156 case 'z':
executed 7 times by 1 test: case 'z':
Executed by:
  • basename
7
157 use_nuls = true;-
158 break;
executed 7 times by 1 test: break;
Executed by:
  • basename
7
159-
160 case_GETOPT_HELP_CHAR;
never executed: break;
executed 9 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • basename
0-9
161 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 4 times by 1 test: exit ( 0 );
Executed by:
  • basename
never executed: break;
executed 4 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • basename
0-4
162-
163 default:
executed 3 times by 1 test: default:
Executed by:
  • basename
3
164 usage (EXIT_FAILURE);-
165 }
never executed: end of block
0
166 }-
167-
168 if (argc < optind + 1)
argc < optind + 1Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • basename
FALSEevaluated 37 times by 1 test
Evaluated by:
  • basename
1-37
169 {-
170 error (0, 0, _("missing operand"));-
171 usage (EXIT_FAILURE);-
172 }
never executed: end of block
0
173-
174 if (!multiple_names && optind + 2 < argc)
!multiple_namesDescription
TRUEevaluated 33 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 4 times by 1 test
Evaluated by:
  • basename
optind + 2 < argcDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • basename
FALSEevaluated 32 times by 1 test
Evaluated by:
  • basename
1-33
175 {-
176 error (0, 0, _("extra operand %s"), quote (argv[optind + 2]));-
177 usage (EXIT_FAILURE);-
178 }
never executed: end of block
0
179-
180 if (multiple_names)
multiple_namesDescription
TRUEevaluated 4 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 32 times by 1 test
Evaluated by:
  • basename
4-32
181 {-
182 for (; optind < argc; optind++)
optind < argcDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • basename
FALSEevaluated 4 times by 1 test
Evaluated by:
  • basename
4-8
183 perform_basename (argv[optind], suffix, use_nuls);
executed 8 times by 1 test: perform_basename (argv[optind], suffix, use_nuls);
Executed by:
  • basename
8
184 }
executed 4 times by 1 test: end of block
Executed by:
  • basename
4
185 else-
186 perform_basename (argv[optind],
executed 32 times by 1 test: perform_basename (argv[optind], optind + 2 == argc ? argv[optind + 1] : ((void *)0) , use_nuls);
Executed by:
  • basename
32
187 optind + 2 == argc ? argv[optind + 1] : NULL, use_nuls);
executed 32 times by 1 test: perform_basename (argv[optind], optind + 2 == argc ? argv[optind + 1] : ((void *)0) , use_nuls);
Executed by:
  • basename
32
188-
189 return EXIT_SUCCESS;
executed 36 times by 1 test: return 0 ;
Executed by:
  • basename
36
190}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2