ls: fix possible incorrect exit status when recursing directories
[coreutils.git] / src / realpath.c
blob292c8f3b769ed101abc40dcb44a8de1ec4a370d1
1 /* realpath - print the resolved path
2 Copyright (C) 2011-2013 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"
28 #include "relpath.h"
30 /* The official name of this program (e.g., no 'g' prefix). */
31 #define PROGRAM_NAME "realpath"
33 #define AUTHORS proper_name_utf8 ("Padraig Brady", "P\303\241draig Brady")
35 enum
37 RELATIVE_TO_OPTION = CHAR_MAX + 1,
38 RELATIVE_BASE_OPTION
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;
47 static struct option const longopts[] =
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' /* FIXME: deprecate in 2013 or so */},
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}
64 void
65 usage (int status)
67 if (status != EXIT_SUCCESS)
68 emit_try_help ();
69 else
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 components of the path need exist\n\
80 -L, --logical resolve '..' components before symlinks\n\
81 -P, --physical resolve symlinks as encountered (default)\n\
82 -q, --quiet suppress most error messages\n\
83 --relative-to=FILE print the resolved path relative to FILE\n\
84 --relative-base=FILE print absolute paths unless paths below FILE\n\
85 -s, --strip, --no-symlinks don't expand symlinks\n\
86 -z, --zero separate output with NUL rather than newline\n\
87 \n\
88 "), stdout);
89 fputs (HELP_OPTION_DESCRIPTION, stdout);
90 fputs (VERSION_OPTION_DESCRIPTION, stdout);
91 emit_ancillary_info ();
93 exit (status);
96 /* A wrapper around canonicalize_filename_mode(),
97 to call it twice when in LOGICAL mode. */
98 static char *
99 realpath_canon (const char *fname, int can_mode)
101 char *can_fname = canonicalize_filename_mode (fname, can_mode);
102 if (logical && can_fname) /* canonicalize again to resolve symlinks. */
104 can_mode &= ~CAN_NOLINKS;
105 char *can_fname2 = canonicalize_filename_mode (can_fname, can_mode);
106 free (can_fname);
107 return can_fname2;
109 return can_fname;
112 /* Test whether canonical prefix is parent or match of path. */
113 static bool _GL_ATTRIBUTE_PURE
114 path_prefix (const char *prefix, const char *path)
116 /* We already know prefix[0] and path[0] are '/'. */
117 prefix++;
118 path++;
120 /* '/' is the prefix of everything except '//' (since we know '//'
121 is only present after canonicalization if it is distinct). */
122 if (!*prefix)
123 return *path != '/';
125 /* Likewise, '//' is a prefix of any double-slash path. */
126 if (*prefix == '/' && !prefix[1])
127 return *path == '/';
129 /* Any other prefix has a non-slash portion. */
130 while (*prefix && *path)
132 if (*prefix != *path)
133 break;
134 prefix++;
135 path++;
137 return (!*prefix && (*path == '/' || !*path));
140 static bool
141 isdir (const char *path)
143 struct stat sb;
144 if (stat (path, &sb) != 0)
145 error (EXIT_FAILURE, errno, _("cannot stat %s"), quote (path));
146 return S_ISDIR (sb.st_mode);
149 static bool
150 process_path (const char *fname, int can_mode)
152 char *can_fname = realpath_canon (fname, can_mode);
153 if (!can_fname)
155 if (verbose)
156 error (0, errno, "%s", quote (fname));
157 return false;
160 if (!can_relative_to
161 || (can_relative_base && !path_prefix (can_relative_base, can_fname))
162 || (can_relative_to && !relpath (can_fname, can_relative_to, NULL, 0)))
163 fputs (can_fname, stdout);
165 putchar (use_nuls ? '\0' : '\n');
167 free (can_fname);
169 return true;
173 main (int argc, char **argv)
175 bool ok = true;
176 int can_mode = CAN_ALL_BUT_LAST;
177 const char *relative_to = NULL;
178 const char *relative_base = NULL;
180 initialize_main (&argc, &argv);
181 set_program_name (argv[0]);
182 setlocale (LC_ALL, "");
183 bindtextdomain (PACKAGE, LOCALEDIR);
184 textdomain (PACKAGE);
186 atexit (close_stdout);
188 while (1)
190 int c = getopt_long (argc, argv, "eLmPqsz", longopts, NULL);
191 if (c == -1)
192 break;
193 switch (c)
195 case 'e':
196 can_mode &= ~CAN_MODE_MASK;
197 can_mode |= CAN_EXISTING;
198 break;
199 case 'm':
200 can_mode &= ~CAN_MODE_MASK;
201 can_mode |= CAN_MISSING;
202 break;
203 case 'L':
204 can_mode |= CAN_NOLINKS;
205 logical = true;
206 break;
207 case 's':
208 can_mode |= CAN_NOLINKS;
209 logical = false;
210 break;
211 case 'P':
212 can_mode &= ~CAN_NOLINKS;
213 logical = false;
214 break;
215 case 'q':
216 verbose = false;
217 break;
218 case 'z':
219 use_nuls = true;
220 break;
221 case RELATIVE_TO_OPTION:
222 relative_to = optarg;
223 break;
224 case RELATIVE_BASE_OPTION:
225 relative_base = optarg;
226 break;
227 case_GETOPT_HELP_CHAR;
228 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
229 default:
230 usage (EXIT_FAILURE);
234 if (optind >= argc)
236 error (0, 0, _("missing operand"));
237 usage (EXIT_FAILURE);
240 if (relative_base && !relative_to)
241 relative_to = relative_base;
243 bool need_dir = (can_mode & CAN_MODE_MASK) == CAN_EXISTING;
244 if (relative_to)
246 can_relative_to = realpath_canon (relative_to, can_mode);
247 if (!can_relative_to)
248 error (EXIT_FAILURE, errno, "%s", quote (relative_to));
249 if (need_dir && !isdir (can_relative_to))
250 error (EXIT_FAILURE, ENOTDIR, "%s", quote (relative_to));
252 if (relative_base == relative_to)
253 can_relative_base = can_relative_to;
254 else if (relative_base)
256 char *base = realpath_canon (relative_base, can_mode);
257 if (!base)
258 error (EXIT_FAILURE, errno, "%s", quote (relative_base));
259 if (need_dir && !isdir (base))
260 error (EXIT_FAILURE, ENOTDIR, "%s", quote (relative_base));
261 /* --relative-to is a no-op if it does not have --relative-base
262 as a prefix */
263 if (path_prefix (base, can_relative_to))
264 can_relative_base = base;
265 else
267 free (base);
268 can_relative_base = can_relative_to;
269 can_relative_to = NULL;
273 for (; optind < argc; ++optind)
274 ok &= process_path (argv[optind], can_mode);
276 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);