Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/src/realpath.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 | - | |||||||||||||
35 | enum | - | ||||||||||||
36 | { | - | ||||||||||||
37 | RELATIVE_TO_OPTION = CHAR_MAX + 1, | - | ||||||||||||
38 | RELATIVE_BASE_OPTION | - | ||||||||||||
39 | }; | - | ||||||||||||
40 | - | |||||||||||||
41 | static bool verbose = true; | - | ||||||||||||
42 | static bool logical; | - | ||||||||||||
43 | static bool use_nuls; | - | ||||||||||||
44 | static const char *can_relative_to; | - | ||||||||||||
45 | static const char *can_relative_base; | - | ||||||||||||
46 | - | |||||||||||||
47 | static 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 | - | |||||||||||||
64 | void | - | ||||||||||||
65 | usage (int status) | - | ||||||||||||
66 | { | - | ||||||||||||
67 | if (status != EXIT_SUCCESS)
| 6-20 | ||||||||||||
68 | emit_try_help (); executed 6 times by 1 test: end of block Executed by:
| 6 | ||||||||||||
69 | else | - | ||||||||||||
70 | { | - | ||||||||||||
71 | printf (_("Usage: %s [OPTION]... FILE...\n"), program_name); | - | ||||||||||||
72 | fputs (_("\ | - | ||||||||||||
73 | Print the resolved absolute file name;\n\ | - | ||||||||||||
74 | all 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:
| 20 | ||||||||||||
94 | exit (status); executed 26 times by 1 test: exit (status); Executed by:
| 26 | ||||||||||||
95 | } | - | ||||||||||||
96 | - | |||||||||||||
97 | /* A wrapper around canonicalize_filename_mode(), | - | ||||||||||||
98 | to call it twice when in LOGICAL mode. */ | - | ||||||||||||
99 | static char * | - | ||||||||||||
100 | realpath_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. */
| 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:
| 1 | ||||||||||||
109 | } | - | ||||||||||||
110 | return can_fname; executed 60 times by 1 test: return can_fname; Executed by:
| 60 | ||||||||||||
111 | } | - | ||||||||||||
112 | - | |||||||||||||
113 | /* Test whether canonical prefix is parent or match of path. */ | - | ||||||||||||
114 | static bool _GL_ATTRIBUTE_PURE | - | ||||||||||||
115 | path_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)
| 8 | ||||||||||||
124 | return *path != '/'; executed 8 times by 1 test: return *path != '/'; Executed by:
| 8 | ||||||||||||
125 | - | |||||||||||||
126 | /* Likewise, '//' is a prefix of any double-slash path. */ | - | ||||||||||||
127 | if (*prefix == '/' && !prefix[1])
| 0-8 | ||||||||||||
128 | return *path == '/'; never executed: return *path == '/'; | 0 | ||||||||||||
129 | - | |||||||||||||
130 | /* Any other prefix has a non-slash portion. */ | - | ||||||||||||
131 | while (*prefix && *path)
| 1-171 | ||||||||||||
132 | { | - | ||||||||||||
133 | if (*prefix != *path)
| 2-168 | ||||||||||||
134 | break; executed 2 times by 1 test: break; Executed by:
| 2 | ||||||||||||
135 | prefix++; | - | ||||||||||||
136 | path++; | - | ||||||||||||
137 | } executed 168 times by 1 test: end of block Executed by:
| 168 | ||||||||||||
138 | return (!*prefix && (*path == '/' || !*path)); executed 8 times by 1 test: return (!*prefix && (*path == '/' || !*path)); Executed by:
| 8 | ||||||||||||
139 | } | - | ||||||||||||
140 | - | |||||||||||||
141 | static bool | - | ||||||||||||
142 | isdir (const char *path) | - | ||||||||||||
143 | { | - | ||||||||||||
144 | struct stat sb; | - | ||||||||||||
145 | if (stat (path, &sb) != 0)
| 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:
| 3 | ||||||||||||
148 | } | - | ||||||||||||
149 | - | |||||||||||||
150 | static bool | - | ||||||||||||
151 | process_path (const char *fname, int can_mode) | - | ||||||||||||
152 | { | - | ||||||||||||
153 | char *can_fname = realpath_canon (fname, can_mode); | - | ||||||||||||
154 | if (!can_fname)
| 1-38 | ||||||||||||
155 | { | - | ||||||||||||
156 | if (verbose)
| 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:
| 1 | ||||||||||||
158 | return false; executed 1 time by 1 test: return 0 ; Executed by:
| 1 | ||||||||||||
159 | } | - | ||||||||||||
160 | - | |||||||||||||
161 | if (!can_relative_to
| 14-24 | ||||||||||||
162 | || (can_relative_base && !path_prefix (can_relative_base, can_fname))
| 2-13 | ||||||||||||
163 | || (can_relative_to && !relpath (can_fname, can_relative_to, NULL, 0)))
| 0-22 | ||||||||||||
164 | fputs (can_fname, stdout); executed 16 times by 1 test: fputs_unlocked (can_fname, stdout ); Executed by:
| 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:
| 38 | ||||||||||||
171 | } | - | ||||||||||||
172 | - | |||||||||||||
173 | int | - | ||||||||||||
174 | main (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)
| 28-99 | ||||||||||||
193 | break; executed 28 times by 1 test: break; Executed by:
| 28 | ||||||||||||
194 | switch (c) | - | ||||||||||||
195 | { | - | ||||||||||||
196 | case 'e': executed 4 times by 1 test: case 'e': Executed by:
| 4 | ||||||||||||
197 | can_mode &= ~CAN_MODE_MASK; | - | ||||||||||||
198 | can_mode |= CAN_EXISTING; | - | ||||||||||||
199 | break; executed 4 times by 1 test: break; Executed by:
| 4 | ||||||||||||
200 | case 'm': executed 15 times by 1 test: case 'm': Executed by:
| 15 | ||||||||||||
201 | can_mode &= ~CAN_MODE_MASK; | - | ||||||||||||
202 | can_mode |= CAN_MISSING; | - | ||||||||||||
203 | break; executed 15 times by 1 test: break; Executed by:
| 15 | ||||||||||||
204 | case 'L': executed 3 times by 1 test: case 'L': Executed by:
| 3 | ||||||||||||
205 | can_mode |= CAN_NOLINKS; | - | ||||||||||||
206 | logical = true; | - | ||||||||||||
207 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||
208 | case 's': executed 13 times by 1 test: case 's': Executed by:
| 13 | ||||||||||||
209 | can_mode |= CAN_NOLINKS; | - | ||||||||||||
210 | logical = false; | - | ||||||||||||
211 | break; executed 13 times by 1 test: break; Executed by:
| 13 | ||||||||||||
212 | case 'P': executed 3 times by 1 test: case 'P': Executed by:
| 3 | ||||||||||||
213 | can_mode &= ~CAN_NOLINKS; | - | ||||||||||||
214 | logical = false; | - | ||||||||||||
215 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||
216 | case 'q': executed 3 times by 1 test: case 'q': Executed by:
| 3 | ||||||||||||
217 | verbose = false; | - | ||||||||||||
218 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||
219 | case 'z': executed 3 times by 1 test: case 'z': Executed by:
| 3 | ||||||||||||
220 | use_nuls = true; | - | ||||||||||||
221 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||
222 | case RELATIVE_TO_OPTION: executed 16 times by 1 test: case RELATIVE_TO_OPTION: Executed by:
| 16 | ||||||||||||
223 | relative_to = optarg; | - | ||||||||||||
224 | break; executed 16 times by 1 test: break; Executed by:
| 16 | ||||||||||||
225 | case RELATIVE_BASE_OPTION: executed 12 times by 1 test: case RELATIVE_BASE_OPTION: Executed by:
| 12 | ||||||||||||
226 | relative_base = optarg; | - | ||||||||||||
227 | break; executed 12 times by 1 test: break; Executed by:
| 12 | ||||||||||||
228 | case_GETOPT_HELP_CHAR; never executed: break; executed 20 times by 1 test: case GETOPT_HELP_CHAR: Executed by:
| 0-20 | ||||||||||||
229 | case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); executed 4 times by 1 test: exit ( 0 ); Executed by:
never executed: break; executed 4 times by 1 test: case GETOPT_VERSION_CHAR: Executed by:
| 0-4 | ||||||||||||
230 | default: executed 3 times by 1 test: default: Executed by:
| 3 | ||||||||||||
231 | usage (EXIT_FAILURE); | - | ||||||||||||
232 | } never executed: end of block | 0 | ||||||||||||
233 | } | - | ||||||||||||
234 | - | |||||||||||||
235 | if (optind >= argc)
| 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)
| 2-16 | ||||||||||||
242 | relative_to = relative_base; executed 2 times by 1 test: relative_to = relative_base; Executed by:
| 2 | ||||||||||||
243 | - | |||||||||||||
244 | bool need_dir = (can_mode & CAN_MODE_MASK) == CAN_EXISTING; | - | ||||||||||||
245 | if (relative_to)
| 9-16 | ||||||||||||
246 | { | - | ||||||||||||
247 | can_relative_to = realpath_canon (relative_to, can_mode); | - | ||||||||||||
248 | if (!can_relative_to)
| 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))
| 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:
| 1 | ||||||||||||
252 | } executed 15 times by 1 test: end of block Executed by:
| 15 | ||||||||||||
253 | if (relative_base == relative_to)
| 11-13 | ||||||||||||
254 | can_relative_base = can_relative_to; executed 11 times by 1 test: can_relative_base = can_relative_to; Executed by:
| 11 | ||||||||||||
255 | else if (relative_base)
| 6-7 | ||||||||||||
256 | { | - | ||||||||||||
257 | char *base = realpath_canon (relative_base, can_mode); | - | ||||||||||||
258 | if (!base)
| 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:
| 1 | ||||||||||||
260 | if (need_dir && !isdir (base))
| 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))
| 1-4 | ||||||||||||
265 | can_relative_base = base; executed 4 times by 1 test: can_relative_base = base; Executed by:
| 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:
| 1 | ||||||||||||
272 | } | - | ||||||||||||
273 | - | |||||||||||||
274 | for (; optind < argc; ++optind)
| 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:
| 39 | ||||||||||||
276 | - | |||||||||||||
277 | return ok ? EXIT_SUCCESS : EXIT_FAILURE; executed 23 times by 1 test: return ok ? 0 : 1 ; Executed by:
| 23 | ||||||||||||
278 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |