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. */
22 #include <sys/types.h>
25 #include "canonicalize.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")
36 RELATIVE_TO_OPTION
= CHAR_MAX
+ 1,
40 static bool verbose
= true;
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
},
66 if (status
!= EXIT_SUCCESS
)
70 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name
);
72 Print the resolved absolute file name;\n\
73 all but the last component must exist\n\
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\
88 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
89 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
90 emit_ancillary_info ();
95 /* A wrapper around canonicalize_filename_mode(),
96 to call it twice when in LOGICAL mode. */
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
);
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
)
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
)
134 while (*path1
&& *path2
)
136 if (*path1
!= *path2
)
145 if (!*path1
&& !*path2
)
147 if (!*path1
&& *path2
== '/')
149 if (!*path2
&& *path1
== '/')
155 /* Output the relative representation if requested. */
157 relpath (const char *can_fname
)
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
))
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. */
180 for (; *relto_suffix
; ++relto_suffix
)
182 if (*relto_suffix
== '/')
183 printf ("%s", "/..");
186 printf ("%s", fname_suffix
);
191 printf ("%s", ++fname_suffix
);
196 putchar (use_nuls
? '\0' : '\n');
205 isdir (const char *path
)
208 if (stat (path
, &sb
) != 0)
209 error (EXIT_FAILURE
, errno
, _("cannot stat %s"), quote (path
));
210 return S_ISDIR (sb
.st_mode
);
214 process_path (const char *fname
, int can_mode
)
216 char *can_fname
= realpath_canon (fname
, can_mode
);
220 error (0, errno
, "%s", quote (fname
));
224 if (!relpath (can_fname
))
225 printf ("%s%c", can_fname
, (use_nuls
? '\0' : '\n'));
233 main (int argc
, char **argv
)
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
);
250 int c
= getopt_long (argc
, argv
, "eLmPqsz", longopts
, NULL
);
256 can_mode
&= ~CAN_MODE_MASK
;
257 can_mode
|= CAN_EXISTING
;
260 can_mode
&= ~CAN_MODE_MASK
;
261 can_mode
|= CAN_MISSING
;
264 can_mode
|= CAN_NOLINKS
;
268 can_mode
|= CAN_NOLINKS
;
272 can_mode
&= ~CAN_NOLINKS
;
281 case RELATIVE_TO_OPTION
:
282 relative_to
= optarg
;
284 case RELATIVE_BASE_OPTION
:
285 relative_base
= optarg
;
287 case_GETOPT_HELP_CHAR
;
288 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
290 usage (EXIT_FAILURE
);
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
;
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
));
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
);