aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Janda <felix.janda@posteo.de>2012-12-27 17:56:39 +0100
committerFelix Janda <felix.janda@posteo.de>2012-12-27 17:56:39 +0100
commit3bb115d6d65559450bb8c6663971317153ce1abe (patch)
tree00e3478f5a1c5e25a1e4816a1c8f4e834061d698
parent8abf095265341f1db12abb1497b5c92b683c6890 (diff)
downloadtoybox-3bb115d6d65559450bb8c6663971317153ce1abe.tar.gz
Add options -L and -P to pwd.
-rw-r--r--toys/posix/pwd.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/toys/posix/pwd.c b/toys/posix/pwd.c
index f70f9098..5c543e87 100644
--- a/toys/posix/pwd.c
+++ b/toys/posix/pwd.c
@@ -3,26 +3,34 @@
* Copyright 2006 Rob Landley <rob@landley.net>
*
* 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);
}