blob: 43923681c434151111d980e63e4e4b1f9249ae41 [file] [log] [blame]
Bernhard Reutner-Fischerc7801c42006-05-19 18:35:03 +00001/* vi: set sw=4 ts=4: */
Denys Vlasenkoa02a4e92017-10-05 15:19:25 +02002/*
3 * Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
Manuel Novoa III cad53642003-03-19 09:13:01 +00004 *
5 * Now does proper error checking on output and returns a failure exit code
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00006 * if one or more paths cannot be resolved.
Bernhard Reutner-Fischerc7801c42006-05-19 18:35:03 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Manuel Novoa III cad53642003-03-19 09:13:01 +00009 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010010//config:config REALPATH
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "realpath (1.1 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010012//config: default y
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: Return the canonicalized absolute pathname.
15//config: This isn't provided by GNU shellutils, but where else does it belong.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010016
Denys Vlasenko39194f02017-08-03 19:00:01 +020017//applet:IF_REALPATH(APPLET_NOFORK(realpath, realpath, BB_DIR_USR_BIN, BB_SUID_DROP, realpath))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010018
19//kbuild:lib-$(CONFIG_REALPATH) += realpath.o
20
21/* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000022
Pere Orga34425382011-03-31 14:43:25 +020023//usage:#define realpath_trivial_usage
24//usage: "FILE..."
25//usage:#define realpath_full_usage "\n\n"
26//usage: "Return the absolute pathnames of given FILE"
27
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Glenn L McGrath62fae302002-12-10 00:14:33 +000029
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000030int realpath_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000031int realpath_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath62fae302002-12-10 00:14:33 +000032{
Manuel Novoa III cad53642003-03-19 09:13:01 +000033 int retval = EXIT_SUCCESS;
34
Denis Vlasenko1d426652008-03-17 09:09:09 +000035 if (!*++argv) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000036 bb_show_usage();
Glenn L McGrath62fae302002-12-10 00:14:33 +000037 }
38
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000039 do {
Denys Vlasenko39194f02017-08-03 19:00:01 +020040 /* NOFORK: only one alloc is allowed; must free */
Denys Vlasenko74716212018-05-24 17:29:14 +020041 char *resolved_path = xmalloc_realpath_coreutils(*argv);
Denys Vlasenko8f65b0c2010-07-06 18:46:02 +020042 if (resolved_path != NULL) {
Glenn L McGrath62fae302002-12-10 00:14:33 +000043 puts(resolved_path);
Jérémie Koenigfbedacf2010-03-26 19:08:53 +010044 free(resolved_path);
Glenn L McGrath62fae302002-12-10 00:14:33 +000045 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 retval = EXIT_FAILURE;
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000047 bb_simple_perror_msg(*argv);
Glenn L McGrath62fae302002-12-10 00:14:33 +000048 }
Denis Vlasenko1d426652008-03-17 09:09:09 +000049 } while (*++argv);
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000050
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000051 fflush_stdout_and_exit(retval);
Glenn L McGrath7b4e89b2002-12-10 03:16:37 +000052}