Hi,

the first patch adds the -L and -P options to pwd as specified by POSIX.
The test script again uses stat. This time in order to get inode numbers
of directories.

Felix
# HG changeset patch
# User Felix Janda <felix.ja...@posteo.de>
# Date 1356627399 -3600
# Node ID 592dab5e536c053ac8b8696f368045f76c8a30b9
# Parent  017b8fd3c9ac5a86dd849831622c4878fddebe5d
Add options -L and -P to pwd.

diff -r 017b8fd3c9ac -r 592dab5e536c toys/posix/pwd.c
--- a/toys/posix/pwd.c	Wed Dec 26 19:39:51 2012 -0600
+++ b/toys/posix/pwd.c	Thu Dec 27 17:56:39 2012 +0100
@@ -3,26 +3,34 @@
  * Copyright 2006 Rob Landley <r...@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);
 }
# HG changeset patch
# User Felix Janda <felix.ja...@posteo.de>
# Date 1356729021 -3600
# Node ID f5b0f21ef92f73e13c3415d8449be86d9c531186
# Parent  dbf0480c88f4895724d719738c7d75ffc9f6c957
Add some tests for pwd.

diff --git a/scripts/test/pwd.test b/scripts/test/pwd.test
new file mode 100755
--- /dev/null
+++ b/scripts/test/pwd.test
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+#TODO: Find better tests
+
+testing "pwd" "[ $(stat -c %i "$(pwd)") = $(stat -c %i .) ] && echo yes" \
+	"yes\n" "" ""
+testing "pwd -P" "[ $(stat -c %i "$(pwd -P)") = $(stat -c %i .) ] && echo yes" \
+	"yes\n" "" ""
+
+
+ln -s . sym
+cd sym
+testing "pwd" "[ $(stat -c %i "$(pwd)") = $(stat -c %i "$PWD") ] && echo yes" \
+	"yes\n" "" ""
+testing "pwd -P" "[ $(stat -c %i "$(pwd -P)") = $(stat -c %i "$PWD") ] || echo yes" \
+	"yes\n" "" ""
+cd ..
+rm sym
+
+export PWD=walrus
+testing "pwd (bad PWD)" "[ "$(pwd)" = "$(cd . ; pwd)" ] && echo yes" \
+	"yes\n" "" ""
_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to