OpenCoverage

realpath.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/realpath.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* realpath - print the resolved path-
2 Copyright (C) 2011-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 Pádraig Brady. */-
18-
19#include <config.h>-
20#include <getopt.h>-
21#include <stdio.h>-
22#include <sys/types.h>-
23-
24#include "system.h"-
25#include "canonicalize.h"-
26#include "die.h"-
27#include "error.h"-
28#include "relpath.h"-
29-
30/* The official name of this program (e.g., no 'g' prefix). */-
31#define PROGRAM_NAME "realpath"-
32-
33#define AUTHORS proper_name ("Padraig Brady")-
34-
35enum-
36{-
37 RELATIVE_TO_OPTION = CHAR_MAX + 1,-
38 RELATIVE_BASE_OPTION-
39};-
40-
41static bool verbose = true;-
42static bool logical;-
43static bool use_nuls;-
44static const char *can_relative_to;-
45static const char *can_relative_base;-
46-
47static struct option const longopts[] =-
48{-
49 {"canonicalize-existing", no_argument, NULL, 'e'},-
50 {"canonicalize-missing", no_argument, NULL, 'm'},-
51 {"relative-to", required_argument, NULL, RELATIVE_TO_OPTION},-
52 {"relative-base", required_argument, NULL, RELATIVE_BASE_OPTION},-
53 {"quiet", no_argument, NULL, 'q'},-
54 {"strip", no_argument, NULL, 's'},-
55 {"no-symlinks", no_argument, NULL, 's'},-
56 {"zero", no_argument, NULL, 'z'},-
57 {"logical", no_argument, NULL, 'L'},-
58 {"physical", no_argument, NULL, 'P'},-
59 {GETOPT_HELP_OPTION_DECL},-
60 {GETOPT_VERSION_OPTION_DECL},-
61 {NULL, 0, NULL, 0}-
62};-
63-
64void-
65usage (int status)-
66{-
67 if (status != EXIT_SUCCESS)
status != 0Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 20 times by 1 test
Evaluated by:
  • realpath
6-20
68 emit_try_help ();
executed 6 times by 1 test: end of block
Executed by:
  • realpath
6
69 else-
70 {-
71 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);-
72 fputs (_("\-
73Print the resolved absolute file name;\n\-
74all but the last component must exist\n\-
75\n\-
76"), stdout);-
77 fputs (_("\-
78 -e, --canonicalize-existing all components of the path must exist\n\-
79 -m, --canonicalize-missing no path components need exist or be a directory\-
80\n\-
81 -L, --logical resolve '..' components before symlinks\n\-
82 -P, --physical resolve symlinks as encountered (default)\n\-
83 -q, --quiet suppress most error messages\n\-
84 --relative-to=DIR print the resolved path relative to DIR\n\-
85 --relative-base=DIR print absolute paths unless paths below DIR\n\-
86 -s, --strip, --no-symlinks don't expand symlinks\n\-
87 -z, --zero end each output line with NUL, not newline\n\-
88\n\-
89"), stdout);-
90 fputs (HELP_OPTION_DESCRIPTION, stdout);-
91 fputs (VERSION_OPTION_DESCRIPTION, stdout);-
92 emit_ancillary_info (PROGRAM_NAME);-
93 }
executed 20 times by 1 test: end of block
Executed by:
  • realpath
20
94 exit (status);
executed 26 times by 1 test: exit (status);
Executed by:
  • realpath
26
95}-
96-
97/* A wrapper around canonicalize_filename_mode(),-
98 to call it twice when in LOGICAL mode. */-
99static char *-
100realpath_canon (const char *fname, int can_mode)-
101{-
102 char *can_fname = canonicalize_filename_mode (fname, can_mode);-
103 if (logical && can_fname) /* canonicalize again to resolve symlinks. */
logicalDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • realpath
FALSEevaluated 60 times by 1 test
Evaluated by:
  • realpath
can_fnameDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • realpath
FALSEnever evaluated
0-60
104 {-
105 can_mode &= ~CAN_NOLINKS;-
106 char *can_fname2 = canonicalize_filename_mode (can_fname, can_mode);-
107 free (can_fname);-
108 return can_fname2;
executed 1 time by 1 test: return can_fname2;
Executed by:
  • realpath
1
109 }-
110 return can_fname;
executed 60 times by 1 test: return can_fname;
Executed by:
  • realpath
60
111}-
112-
113/* Test whether canonical prefix is parent or match of path. */-
114static bool _GL_ATTRIBUTE_PURE-
115path_prefix (const char *prefix, const char *path)-
116{-
117 /* We already know prefix[0] and path[0] are '/'. */-
118 prefix++;-
119 path++;-
120-
121 /* '/' is the prefix of everything except '//' (since we know '//'-
122 is only present after canonicalization if it is distinct). */-
123 if (!*prefix)
!*prefixDescription
TRUEevaluated 8 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 8 times by 1 test
Evaluated by:
  • realpath
8
124 return *path != '/';
executed 8 times by 1 test: return *path != '/';
Executed by:
  • realpath
8
125-
126 /* Likewise, '//' is a prefix of any double-slash path. */-
127 if (*prefix == '/' && !prefix[1])
*prefix == '/'Description
TRUEnever evaluated
FALSEevaluated 8 times by 1 test
Evaluated by:
  • realpath
!prefix[1]Description
TRUEnever evaluated
FALSEnever evaluated
0-8
128 return *path == '/';
never executed: return *path == '/';
0
129-
130 /* Any other prefix has a non-slash portion. */-
131 while (*prefix && *path)
*prefixDescription
TRUEevaluated 171 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 5 times by 1 test
Evaluated by:
  • realpath
*pathDescription
TRUEevaluated 170 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 1 time by 1 test
Evaluated by:
  • realpath
1-171
132 {-
133 if (*prefix != *path)
*prefix != *pathDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 168 times by 1 test
Evaluated by:
  • realpath
2-168
134 break;
executed 2 times by 1 test: break;
Executed by:
  • realpath
2
135 prefix++;-
136 path++;-
137 }
executed 168 times by 1 test: end of block
Executed by:
  • realpath
168
138 return (!*prefix && (*path == '/' || !*path));
executed 8 times by 1 test: return (!*prefix && (*path == '/' || !*path));
Executed by:
  • realpath
8
139}-
140-
141static bool-
142isdir (const char *path)-
143{-
144 struct stat sb;-
145 if (stat (path, &sb) != 0)
stat (path, &sb) != 0Description
TRUEnever evaluated
FALSEevaluated 3 times by 1 test
Evaluated by:
  • realpath
0-3
146 die (EXIT_FAILURE, errno, _("cannot stat %s"), quoteaf (path));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), dcgettext (((void *)0), \"cannot stat %s\", 5), quotearg_style (shell_escape_always_quoting_style, path)), assume (false))" ")"); int _gl_dummy; })) ? (..., path)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , dcgettext (((void *)0), "cannot stat %s" , 5) , quotearg_style (shell_escape_always_quoting_style, path)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
147 return S_ISDIR (sb.st_mode);
executed 3 times by 1 test: return (((( sb.st_mode )) & 0170000) == (0040000)) ;
Executed by:
  • realpath
3
148}-
149-
150static bool-
151process_path (const char *fname, int can_mode)-
152{-
153 char *can_fname = realpath_canon (fname, can_mode);-
154 if (!can_fname)
!can_fnameDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • realpath
FALSEevaluated 38 times by 1 test
Evaluated by:
  • realpath
1-38
155 {-
156 if (verbose)
verboseDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • realpath
FALSEnever evaluated
0-1
157 error (0, errno, "%s", quotef (fname));
executed 1 time by 1 test: error (0, (*__errno_location ()) , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, fname));
Executed by:
  • realpath
1
158 return false;
executed 1 time by 1 test: return 0 ;
Executed by:
  • realpath
1
159 }-
160-
161 if (!can_relative_to
!can_relative_toDescription
TRUEevaluated 14 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 24 times by 1 test
Evaluated by:
  • realpath
14-24
162 || (can_relative_base && !path_prefix (can_relative_base, can_fname))
can_relative_baseDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 13 times by 1 test
Evaluated by:
  • realpath
!path_prefix (...se, can_fname)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 9 times by 1 test
Evaluated by:
  • realpath
2-13
163 || (can_relative_to && !relpath (can_fname, can_relative_to, NULL, 0)))
can_relative_toDescription
TRUEevaluated 22 times by 1 test
Evaluated by:
  • realpath
FALSEnever evaluated
!relpath (can_...void *)0) , 0)Description
TRUEnever evaluated
FALSEevaluated 22 times by 1 test
Evaluated by:
  • realpath
0-22
164 fputs (can_fname, stdout);
executed 16 times by 1 test: fputs_unlocked (can_fname, stdout );
Executed by:
  • realpath
16
165-
166 putchar (use_nuls ? '\0' : '\n');-
167-
168 free (can_fname);-
169-
170 return true;
executed 38 times by 1 test: return 1 ;
Executed by:
  • realpath
38
171}-
172-
173int-
174main (int argc, char **argv)-
175{-
176 bool ok = true;-
177 int can_mode = CAN_ALL_BUT_LAST;-
178 const char *relative_to = NULL;-
179 const char *relative_base = NULL;-
180-
181 initialize_main (&argc, &argv);-
182 set_program_name (argv[0]);-
183 setlocale (LC_ALL, "");-
184 bindtextdomain (PACKAGE, LOCALEDIR);-
185 textdomain (PACKAGE);-
186-
187 atexit (close_stdout);-
188-
189 while (1)-
190 {-
191 int c = getopt_long (argc, argv, "eLmPqsz", longopts, NULL);-
192 if (c == -1)
c == -1Description
TRUEevaluated 28 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 99 times by 1 test
Evaluated by:
  • realpath
28-99
193 break;
executed 28 times by 1 test: break;
Executed by:
  • realpath
28
194 switch (c)-
195 {-
196 case 'e':
executed 4 times by 1 test: case 'e':
Executed by:
  • realpath
4
197 can_mode &= ~CAN_MODE_MASK;-
198 can_mode |= CAN_EXISTING;-
199 break;
executed 4 times by 1 test: break;
Executed by:
  • realpath
4
200 case 'm':
executed 15 times by 1 test: case 'm':
Executed by:
  • realpath
15
201 can_mode &= ~CAN_MODE_MASK;-
202 can_mode |= CAN_MISSING;-
203 break;
executed 15 times by 1 test: break;
Executed by:
  • realpath
15
204 case 'L':
executed 3 times by 1 test: case 'L':
Executed by:
  • realpath
3
205 can_mode |= CAN_NOLINKS;-
206 logical = true;-
207 break;
executed 3 times by 1 test: break;
Executed by:
  • realpath
3
208 case 's':
executed 13 times by 1 test: case 's':
Executed by:
  • realpath
13
209 can_mode |= CAN_NOLINKS;-
210 logical = false;-
211 break;
executed 13 times by 1 test: break;
Executed by:
  • realpath
13
212 case 'P':
executed 3 times by 1 test: case 'P':
Executed by:
  • realpath
3
213 can_mode &= ~CAN_NOLINKS;-
214 logical = false;-
215 break;
executed 3 times by 1 test: break;
Executed by:
  • realpath
3
216 case 'q':
executed 3 times by 1 test: case 'q':
Executed by:
  • realpath
3
217 verbose = false;-
218 break;
executed 3 times by 1 test: break;
Executed by:
  • realpath
3
219 case 'z':
executed 3 times by 1 test: case 'z':
Executed by:
  • realpath
3
220 use_nuls = true;-
221 break;
executed 3 times by 1 test: break;
Executed by:
  • realpath
3
222 case RELATIVE_TO_OPTION:
executed 16 times by 1 test: case RELATIVE_TO_OPTION:
Executed by:
  • realpath
16
223 relative_to = optarg;-
224 break;
executed 16 times by 1 test: break;
Executed by:
  • realpath
16
225 case RELATIVE_BASE_OPTION:
executed 12 times by 1 test: case RELATIVE_BASE_OPTION:
Executed by:
  • realpath
12
226 relative_base = optarg;-
227 break;
executed 12 times by 1 test: break;
Executed by:
  • realpath
12
228 case_GETOPT_HELP_CHAR;
never executed: break;
executed 20 times by 1 test: case GETOPT_HELP_CHAR:
Executed by:
  • realpath
0-20
229 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
executed 4 times by 1 test: exit ( 0 );
Executed by:
  • realpath
never executed: break;
executed 4 times by 1 test: case GETOPT_VERSION_CHAR:
Executed by:
  • realpath
0-4
230 default:
executed 3 times by 1 test: default:
Executed by:
  • realpath
3
231 usage (EXIT_FAILURE);-
232 }
never executed: end of block
0
233 }-
234-
235 if (optind >= argc)
optind >= argcDescription
TRUEevaluated 3 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 25 times by 1 test
Evaluated by:
  • realpath
3-25
236 {-
237 error (0, 0, _("missing operand"));-
238 usage (EXIT_FAILURE);-
239 }
never executed: end of block
0
240-
241 if (relative_base && !relative_to)
relative_baseDescription
TRUEevaluated 9 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 16 times by 1 test
Evaluated by:
  • realpath
!relative_toDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 7 times by 1 test
Evaluated by:
  • realpath
2-16
242 relative_to = relative_base;
executed 2 times by 1 test: relative_to = relative_base;
Executed by:
  • realpath
2
243-
244 bool need_dir = (can_mode & CAN_MODE_MASK) == CAN_EXISTING;-
245 if (relative_to)
relative_toDescription
TRUEevaluated 16 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 9 times by 1 test
Evaluated by:
  • realpath
9-16
246 {-
247 can_relative_to = realpath_canon (relative_to, can_mode);-
248 if (!can_relative_to)
!can_relative_toDescription
TRUEnever evaluated
FALSEevaluated 16 times by 1 test
Evaluated by:
  • realpath
0-16
249 die (EXIT_FAILURE, errno, "%s", quotef (relative_to));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), \"%s\", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_to)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_locati...ell_escape_quoting_style, relative_to)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_to)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
250 if (need_dir && !isdir (can_relative_to))
need_dirDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 14 times by 1 test
Evaluated by:
  • realpath
!isdir (can_relative_to)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • realpath
FALSEevaluated 1 time by 1 test
Evaluated by:
  • realpath
1-14
251 die (EXIT_FAILURE, ENOTDIR, "%s", quotef (relative_to));
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 20, \"%s\", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_to)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 20 , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_to)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 20 , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_to)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
Executed by:
  • realpath
1
252 }
executed 15 times by 1 test: end of block
Executed by:
  • realpath
15
253 if (relative_base == relative_to)
relative_base == relative_toDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 13 times by 1 test
Evaluated by:
  • realpath
11-13
254 can_relative_base = can_relative_to;
executed 11 times by 1 test: can_relative_base = can_relative_to;
Executed by:
  • realpath
11
255 else if (relative_base)
relative_baseDescription
TRUEevaluated 6 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 7 times by 1 test
Evaluated by:
  • realpath
6-7
256 {-
257 char *base = realpath_canon (relative_base, can_mode);-
258 if (!base)
!baseDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • realpath
FALSEevaluated 5 times by 1 test
Evaluated by:
  • realpath
1-5
259 die (EXIT_FAILURE, errno, "%s", quotef (relative_base));
executed 1 time by 1 test: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, (*__errno_location ()), \"%s\", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_base)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , (*__errno_loca...escape_quoting_style, relative_base)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , (*__errno_location ()) , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_base)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
Executed by:
  • realpath
1
260 if (need_dir && !isdir (base))
need_dirDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • realpath
FALSEevaluated 4 times by 1 test
Evaluated by:
  • realpath
!isdir (base)Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • realpath
0-4
261 die (EXIT_FAILURE, ENOTDIR, "%s", quotef (relative_base));
never executed: ((!!sizeof (struct { _Static_assert ( 1 , "verify_expr (" "1" ", " "(error (1, 20, \"%s\", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_base)), assume (false))" ")"); int _gl_dummy; })) ? ((error ( 1 , 20 , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_base)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))) : ((error ( 1 , 20 , "%s", quotearg_n_style_colon (0, shell_escape_quoting_style, relative_base)), (( 0 ) ? (void) 0 : __builtin_unreachable ()))));
0
262 /* --relative-to is a no-op if it does not have --relative-base-
263 as a prefix */-
264 if (path_prefix (base, can_relative_to))
path_prefix (b...n_relative_to)Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 1 time by 1 test
Evaluated by:
  • realpath
1-4
265 can_relative_base = base;
executed 4 times by 1 test: can_relative_base = base;
Executed by:
  • realpath
4
266 else-
267 {-
268 free (base);-
269 can_relative_base = can_relative_to;-
270 can_relative_to = NULL;-
271 }
executed 1 time by 1 test: end of block
Executed by:
  • realpath
1
272 }-
273-
274 for (; optind < argc; ++optind)
optind < argcDescription
TRUEevaluated 39 times by 1 test
Evaluated by:
  • realpath
FALSEevaluated 23 times by 1 test
Evaluated by:
  • realpath
23-39
275 ok &= process_path (argv[optind], can_mode);
executed 39 times by 1 test: ok &= process_path (argv[optind], can_mode);
Executed by:
  • realpath
39
276-
277 return ok ? EXIT_SUCCESS : EXIT_FAILURE;
executed 23 times by 1 test: return ok ? 0 : 1 ;
Executed by:
  • realpath
23
278}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2