busybox/coreutils/pwd.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * Mini pwd implementation for busybox
   4 *
   5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
   6 *
   7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
   8 */
   9//config:config PWD
  10//config:       bool "pwd (3.7 kb)"
  11//config:       default y
  12//config:       help
  13//config:       pwd is used to print the current directory.
  14
  15//applet:IF_PWD(APPLET_NOFORK(pwd, pwd, BB_DIR_BIN, BB_SUID_DROP, pwd))
  16
  17//kbuild:lib-$(CONFIG_PWD) += pwd.o
  18
  19//usage:#define pwd_trivial_usage
  20//usage:       ""
  21//usage:#define pwd_full_usage "\n\n"
  22//usage:       "Print the full filename of the current working directory"
  23//usage:
  24//usage:#define pwd_example_usage
  25//usage:       "$ pwd\n"
  26//usage:       "/root\n"
  27
  28#include "libbb.h"
  29
  30static int logical_getcwd(void)
  31{
  32        struct stat st1;
  33        struct stat st2;
  34        char *wd;
  35        char *p;
  36
  37        wd = getenv("PWD");
  38        if (!wd || wd[0] != '/')
  39                return 0;
  40
  41        p = wd;
  42        while (*p) {
  43                /* doing strstr(p, "/.") by hand is smaller and faster... */
  44                if (*p++ != '/')
  45                        continue;
  46                if (*p != '.')
  47                        continue;
  48                /* we found "/.", skip to next char */
  49                p++;
  50                if (*p == '.')
  51                        p++; /* we found "/.." */
  52                if (*p == '\0' || *p == '/')
  53                        return 0; /* "/./" or "/../" component: bad */
  54        }
  55
  56        if (stat(wd, &st1) != 0)
  57                return 0;
  58        if (stat(".", &st2) != 0)
  59                return 0;
  60        if (st1.st_ino != st2.st_ino)
  61                return 0;
  62        if (st1.st_dev != st2.st_dev)
  63                return 0;
  64
  65        puts(wd);
  66        return 1;
  67}
  68
  69int pwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  70int pwd_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  71{
  72        char *buf;
  73
  74        if (ENABLE_DESKTOP) {
  75                /* TODO: assume -L if $POSIXLY_CORRECT? (coreutils does that)
  76                 * Rationale:
  77                 * POSIX requires a default of -L, but most scripts expect -P
  78                 */
  79                unsigned opt = getopt32(argv, "LP");
  80                if ((opt & 1) && logical_getcwd())
  81                        return fflush_all();
  82        }
  83
  84        buf = xrealloc_getcwd_or_warn(NULL);
  85
  86        if (buf) {
  87                puts(buf);
  88                free(buf);
  89                return fflush_all();
  90        }
  91
  92        return EXIT_FAILURE;
  93}
  94