1 /* vi: set sw=4 ts=4: */
3 * Mini pwd implementation for busybox
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>.
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config: bool "pwd (3.4 kb)"
13 //config: pwd is used to print the current directory.
15 //applet:IF_PWD(APPLET_NOFORK(pwd, pwd, BB_DIR_BIN, BB_SUID_DROP, pwd))
17 //kbuild:lib-$(CONFIG_PWD) += pwd.o
19 //usage:#define pwd_trivial_usage
21 //usage:#define pwd_full_usage "\n\n"
22 //usage: "Print the full filename of the current working directory"
24 //usage:#define pwd_example_usage
30 static int logical_getcwd(void)
38 if (!wd || wd[0] != '/')
43 /* doing strstr(p, "/.") by hand is smaller and faster... */
48 /* we found "/.", skip to next char */
51 p++; /* we found "/.." */
52 if (*p == '\0' || *p == '/')
53 return 0; /* "/./" or "/../" component: bad */
56 if (stat(wd, &st1) != 0)
58 if (stat(".", &st2) != 0)
60 if (st1.st_ino != st2.st_ino)
62 if (st1.st_dev != st2.st_dev)
69 int pwd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
70 int pwd_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
75 /* TODO: assume -L if $POSIXLY_CORRECT? (coreutils does that)
77 * POSIX requires a default of -L, but most scripts expect -P
79 unsigned opt = getopt32(argv, "LP");
80 if ((opt & 1) && logical_getcwd())
84 buf = xrealloc_getcwd_or_warn(NULL);