tests: remove crufty test=test_name code from old tests
[coreutils/ericb.git] / src / realpath.c
blobb39f7bfb007099c0d3da9e6087f9ff926d64fca6
1 /* realpath - print the resolved path
2 Copyright (C) 2011-2012 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Pádraig Brady. */
19 #include <config.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <sys/types.h>
24 #include "system.h"
25 #include "canonicalize.h"
26 #include "error.h"
27 #include "quote.h"
29 /* The official name of this program (e.g., no 'g' prefix). */
30 #define PROGRAM_NAME "realpath"
32 #define AUTHORS proper_name_utf8 ("Padraig Brady", "P\303\241draig Brady")
34 enum
36 RELATIVE_TO_OPTION = CHAR_MAX + 1,
37 RELATIVE_BASE_OPTION
40 static bool verbose = true;
41 static bool logical;
42 static bool use_nuls;
43 static const char *can_relative_to;
44 static const char *can_relative_base;
46 static struct option const longopts[] =
48 {"canonicalize-existing", no_argument, NULL, 'e'},
49 {"canonicalize-missing", no_argument, NULL, 'm'},
50 {"relative-to", required_argument, NULL, RELATIVE_TO_OPTION},
51 {"relative-base", required_argument, NULL, RELATIVE_BASE_OPTION},
52 {"quiet", no_argument, NULL, 'q'},
53 {"strip", no_argument, NULL, 's' /* FIXME: deprecate in 2013 or so */},
54 {"no-symlinks", no_argument, NULL, 's'},
55 {"zero", no_argument, NULL, 'z'},
56 {"logical", no_argument, NULL, 'L'},
57 {"physical", no_argument, NULL, 'P'},
58 {GETOPT_HELP_OPTION_DECL},
59 {GETOPT_VERSION_OPTION_DECL},
60 {NULL, 0, NULL, 0}
63 void
64 usage (int status)
66 if (status != EXIT_SUCCESS)
67 emit_try_help ();
68 else
70 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
71 fputs (_("\
72 Print the resolved absolute file name;\n\
73 all but the last component must exist\n\
74 \n\
75 "), stdout);
76 fputs (_("\
77 -e, --canonicalize-existing all components of the path must exist\n\
78 -m, --canonicalize-missing no components of the path need exist\n\
79 -L, --logical resolve '..' components before symlinks\n\
80 -P, --physical resolve symlinks as encountered (default)\n\
81 -q, --quiet suppress most error messages\n\
82 --relative-to=FILE print the resolved path relative to FILE\n\
83 --relative-base=FILE print absolute paths unless paths below FILE\n\
84 -s, --strip, --no-symlinks don't expand symlinks\n\
85 -z, --zero separate output with NUL rather than newline\n\
86 \n\
87 "), stdout);
88 fputs (HELP_OPTION_DESCRIPTION, stdout);
89 fputs (VERSION_OPTION_DESCRIPTION, stdout);
90 emit_ancillary_info ();
92 exit (status);
95 /* A wrapper around canonicalize_filename_mode(),
96 to call it twice when in LOGICAL mode. */
97 static char *
98 realpath_canon (const char *fname, int can_mode)
100 char *can_fname = canonicalize_filename_mode (fname, can_mode);
101 if (logical && can_fname) /* canonicalize again to resolve symlinks. */
103 can_mode &= ~CAN_NOLINKS;
104 char *can_fname2 = canonicalize_filename_mode (can_fname, can_mode);
105 free (can_fname);
106 return can_fname2;
108 return can_fname;
111 /* Test whether prefix is parent or match of path. */
112 static bool _GL_ATTRIBUTE_PURE
113 path_prefix (const char *prefix, const char *path)
115 while (*prefix && *path)
117 if (*prefix != *path)
118 break;
119 prefix++;
120 path++;
122 return (!*prefix && (*path == '/' || !*path));
125 /* Return the length of the longest common prefix
126 of PATH1 and PATH2, ensuring only full path components
127 are matched. Return 0 on no match. */
128 static int _GL_ATTRIBUTE_PURE
129 path_common_prefix (const char *path1, const char *path2)
131 int i = 0;
132 int ret = 0;
134 while (*path1 && *path2)
136 if (*path1 != *path2)
137 break;
138 if (*path1 == '/')
139 ret = i;
140 path1++;
141 path2++;
142 i++;
145 if (!*path1 && !*path2)
146 ret = i;
147 if (!*path1 && *path2 == '/')
148 ret = i;
149 if (!*path2 && *path1 == '/')
150 ret = i;
152 return ret;
155 /* Output the relative representation if requested. */
156 static bool
157 relpath (const char *can_fname)
159 if (can_relative_to)
161 /* Enforce --relative-base. */
162 if (can_relative_base)
164 if (!path_prefix (can_relative_base, can_fname)
165 || !path_prefix (can_relative_base, can_relative_to))
166 return false;
169 /* Skip the prefix common to --relative-to and path. */
170 int common_index = path_common_prefix (can_relative_to, can_fname);
171 const char *relto_suffix = can_relative_to + common_index;
172 const char *fname_suffix = can_fname + common_index;
174 /* Replace remaining components of --relative-to with '..', to get
175 to a common directory. Then output the remainder of fname. */
176 if (*relto_suffix)
178 ++relto_suffix;
179 printf ("%s", "..");
180 for (; *relto_suffix; ++relto_suffix)
182 if (*relto_suffix == '/')
183 printf ("%s", "/..");
186 printf ("%s", fname_suffix);
188 else
190 if (*fname_suffix)
191 printf ("%s", ++fname_suffix);
192 else
193 printf ("%c", '.');
196 putchar (use_nuls ? '\0' : '\n');
198 return true;
201 return false;
204 static bool
205 isdir (const char *path)
207 struct stat sb;
208 if (stat (path, &sb) != 0)
209 error (EXIT_FAILURE, errno, _("cannot stat %s"), quote (path));
210 return S_ISDIR (sb.st_mode);
213 static bool
214 process_path (const char *fname, int can_mode)
216 char *can_fname = realpath_canon (fname, can_mode);
217 if (!can_fname)
219 if (verbose)
220 error (0, errno, "%s", quote (fname));
221 return false;
224 if (!relpath (can_fname))
225 printf ("%s%c", can_fname, (use_nuls ? '\0' : '\n'));
227 free (can_fname);
229 return true;
233 main (int argc, char **argv)
235 bool ok = true;
236 int can_mode = CAN_ALL_BUT_LAST;
237 const char *relative_to = NULL;
238 const char *relative_base = NULL;
240 initialize_main (&argc, &argv);
241 set_program_name (argv[0]);
242 setlocale (LC_ALL, "");
243 bindtextdomain (PACKAGE, LOCALEDIR);
244 textdomain (PACKAGE);
246 atexit (close_stdout);
248 while (1)
250 int c = getopt_long (argc, argv, "eLmPqsz", longopts, NULL);
251 if (c == -1)
252 break;
253 switch (c)
255 case 'e':
256 can_mode &= ~CAN_MODE_MASK;
257 can_mode |= CAN_EXISTING;
258 break;
259 case 'm':
260 can_mode &= ~CAN_MODE_MASK;
261 can_mode |= CAN_MISSING;
262 break;
263 case 'L':
264 can_mode |= CAN_NOLINKS;
265 logical = true;
266 break;
267 case 's':
268 can_mode |= CAN_NOLINKS;
269 logical = false;
270 break;
271 case 'P':
272 can_mode &= ~CAN_NOLINKS;
273 logical = false;
274 break;
275 case 'q':
276 verbose = false;
277 break;
278 case 'z':
279 use_nuls = true;
280 break;
281 case RELATIVE_TO_OPTION:
282 relative_to = optarg;
283 break;
284 case RELATIVE_BASE_OPTION:
285 relative_base = optarg;
286 break;
287 case_GETOPT_HELP_CHAR;
288 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
289 default:
290 usage (EXIT_FAILURE);
294 if (optind >= argc)
296 error (0, 0, _("missing operand"));
297 usage (EXIT_FAILURE);
300 if (relative_base && !relative_to)
302 error (0, 0, _("--relative-base requires --relative-to"));
303 usage (EXIT_FAILURE);
306 bool need_dir = (can_mode & CAN_MODE_MASK) == CAN_EXISTING;
307 if (relative_to)
309 can_relative_to = realpath_canon (relative_to, can_mode);
310 if (!can_relative_to)
311 error (EXIT_FAILURE, errno, "%s", quote (relative_to));
312 if (need_dir && !isdir (can_relative_to))
313 error (EXIT_FAILURE, ENOTDIR, "%s", quote (relative_to));
315 if (relative_base)
317 can_relative_base = realpath_canon (relative_base, can_mode);
318 if (!can_relative_base)
319 error (EXIT_FAILURE, errno, "%s", quote (relative_base));
320 if (need_dir && !isdir (can_relative_base))
321 error (EXIT_FAILURE, ENOTDIR, "%s", quote (relative_base));
324 for (; optind < argc; ++optind)
325 ok &= process_path (argv[optind], can_mode);
327 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);