blob: 28906ba55493b48ce128b3d97a6c3e535b7dcca8 [file] [log] [blame]
Bernhard Reutner-Fischerc7801c42006-05-19 18:35:03 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath62fae302002-12-10 00:14:33 +00002
Manuel Novoa III cad53642003-03-19 09:13:01 +00003/* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
4
5/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
6 *
7 * Now does proper error checking on output and returns a failure exit code
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00008 * if one or more paths cannot be resolved.
Bernhard Reutner-Fischerc7801c42006-05-19 18:35:03 +00009 *
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Manuel Novoa III cad53642003-03-19 09:13:01 +000011 */
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Glenn L McGrath62fae302002-12-10 00:14:33 +000014
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000015int realpath_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000016int realpath_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath62fae302002-12-10 00:14:33 +000017{
Manuel Novoa III cad53642003-03-19 09:13:01 +000018 int retval = EXIT_SUCCESS;
19
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +000020#if PATH_MAX > (BUFSIZ+1)
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000021 RESERVE_CONFIG_BUFFER(resolved_path, PATH_MAX);
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +000022# define resolved_path_MUST_FREE 1
23#else
24#define resolved_path bb_common_bufsiz1
25# define resolved_path_MUST_FREE 0
26#endif
Glenn L McGrath62fae302002-12-10 00:14:33 +000027
Denis Vlasenko1d426652008-03-17 09:09:09 +000028 if (!*++argv) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 bb_show_usage();
Glenn L McGrath62fae302002-12-10 00:14:33 +000030 }
31
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000032 do {
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000033 if (realpath(*argv, resolved_path) != NULL) {
Glenn L McGrath62fae302002-12-10 00:14:33 +000034 puts(resolved_path);
35 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 retval = EXIT_FAILURE;
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000037 bb_simple_perror_msg(*argv);
Glenn L McGrath62fae302002-12-10 00:14:33 +000038 }
Denis Vlasenko1d426652008-03-17 09:09:09 +000039 } while (*++argv);
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000040
"Vladimir N. Oleynik"a2eec602005-10-15 13:45:32 +000041#if ENABLE_FEATURE_CLEAN_UP && resolved_path_MUST_FREE
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000042 RELEASE_CONFIG_BUFFER(resolved_path);
Manuel Novoa III cad53642003-03-19 09:13:01 +000043#endif
Glenn L McGrath62fae302002-12-10 00:14:33 +000044
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000045 fflush_stdout_and_exit(retval);
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000046}