busybox/coreutils/realpath.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
   4 *
   5 * Now does proper error checking on output and returns a failure exit code
   6 * if one or more paths cannot be resolved.
   7 *
   8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   9 */
  10//config:config REALPATH
  11//config:       bool "realpath (1.6 kb)"
  12//config:       default y
  13//config:       help
  14//config:       Return the canonicalized absolute pathname.
  15//config:       This isn't provided by GNU shellutils, but where else does it belong.
  16
  17//applet:IF_REALPATH(APPLET_NOFORK(realpath, realpath, BB_DIR_USR_BIN, BB_SUID_DROP, realpath))
  18
  19//kbuild:lib-$(CONFIG_REALPATH) += realpath.o
  20
  21/* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
  22
  23//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
  28#include "libbb.h"
  29
  30int realpath_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  31int realpath_main(int argc UNUSED_PARAM, char **argv)
  32{
  33        int retval = EXIT_SUCCESS;
  34
  35        if (!*++argv) {
  36                bb_show_usage();
  37        }
  38
  39        do {
  40                /* NOFORK: only one alloc is allowed; must free */
  41                char *resolved_path = xmalloc_realpath_coreutils(*argv);
  42                if (resolved_path != NULL) {
  43                        puts(resolved_path);
  44                        free(resolved_path);
  45                } else {
  46                        retval = EXIT_FAILURE;
  47                        bb_simple_perror_msg(*argv);
  48                }
  49        } while (*++argv);
  50
  51        fflush_stdout_and_exit(retval);
  52}
  53