# HG changeset patch # User Felix Janda # Date 1356627399 -3600 # Node ID adab8adf5f6d18717a0404076cce039658d3a807 # Parent edde4d30e98c8c0760baf72128e888a4c7900504 Add options -L and -P to pwd. diff -r edde4d30e98c -r adab8adf5f6d toys/posix/pwd.c --- a/toys/posix/pwd.c Sat Dec 29 03:18:34 2012 -0600 +++ b/toys/posix/pwd.c Thu Dec 27 17:56:39 2012 +0100 @@ -3,26 +3,34 @@ * Copyright 2006 Rob Landley * * See http://opengroup.org/onlinepubs/9699919799/utilities/echo.html - * - * TODO: add -L -P -USE_PWD(NEWTOY(pwd, NULL, TOYFLAG_BIN)) +USE_PWD(NEWTOY(pwd, ">0LP[!LP]", TOYFLAG_BIN)) config PWD bool "pwd" default y help - usage: pwd + usage: pwd [-L|-P] The print working directory command prints the current directory. + + -P Avoid all symlinks + -L Use the value of the environment variable "PWD" if valid + + The option "-L" is implied by default. */ +#define FOR_pwd #include "toys.h" void pwd_main(void) { - char *pwd = xgetcwd(); + char *pwd = xgetcwd(), *env_pwd; + struct stat st[2]; - xprintf("%s\n", pwd); + if (!(toys.optflags & FLAG_P) && (env_pwd = getenv("PWD")) && + !stat(pwd, &st[0]) && !stat(env_pwd, &st[1]) && + (st[0].st_ino == st[1].st_ino)) xprintf("%s\n", env_pwd); + else xprintf("%s\n", pwd); if (CFG_TOYBOX_FREE) free(pwd); }