sh: Correct criterion for using CDPATH in cd.
authorPeter Avalos <pavalos@dragonflybsd.org>
Sun, 21 Aug 2011 19:46:44 +0000 (12:46 -0700)
committerPeter Avalos <pavalos@dragonflybsd.org>
Sun, 21 Aug 2011 20:49:16 +0000 (13:49 -0700)
CDPATH should be ignored not only for pathnames starting with '/' but
also for pathnames whose first component is '.' or '..'.

Obtained-from:   FreeBSD 222381

bin/sh/cd.c
tools/regression/bin/sh/builtins/cd6.0 [new file with mode: 0644]
tools/regression/bin/sh/builtins/cd7.0 [new file with mode: 0644]

index fdbeff1..8396df6 100644 (file)
@@ -34,7 +34,7 @@
  * SUCH DAMAGE.
  *
  * @(#)cd.c    8.2 (Berkeley) 5/4/95
- * $FreeBSD: src/bin/sh/cd.c,v 1.47 2011/05/25 21:38:16 jilles Exp $
+ * $FreeBSD: src/bin/sh/cd.c,v 1.48 2011/05/27 20:01:46 jilles Exp $
  */
 
 #include <sys/types.h>
@@ -122,7 +122,10 @@ cdcmd(int argc, char **argv)
                else
                        dest = ".";
        }
-       if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
+       if (dest[0] == '/' ||
+           (dest[0] == '.' && (dest[1] == '/' || dest[1] == '\0')) ||
+           (dest[0] == '.' && dest[1] == '.' && (dest[2] == '/' || dest[2] == '\0')) ||
+           (path = bltinlookup("CDPATH", 1)) == NULL)
                path = nullstr;
        while ((p = padvance(&path, dest)) != NULL) {
                if (stat(p, &statb) >= 0 && S_ISDIR(statb.st_mode)) {
diff --git a/tools/regression/bin/sh/builtins/cd6.0 b/tools/regression/bin/sh/builtins/cd6.0
new file mode 100644 (file)
index 0000000..664f11d
--- /dev/null
@@ -0,0 +1,10 @@
+# $FreeBSD: src/tools/regression/bin/sh/builtins/cd6.0,v 1.1 2011/05/27 20:01:46 jilles Exp $
+
+set -e
+cd -P /bin
+d=$PWD
+CDPATH=/:
+cd -P .
+[ "$d" = "$PWD" ]
+cd -P ./
+[ "$d" = "$PWD" ]
diff --git a/tools/regression/bin/sh/builtins/cd7.0 b/tools/regression/bin/sh/builtins/cd7.0
new file mode 100644 (file)
index 0000000..0a9c9d8
--- /dev/null
@@ -0,0 +1,15 @@
+# $FreeBSD: src/tools/regression/bin/sh/builtins/cd7.0,v 1.1 2011/05/27 20:01:46 jilles Exp $
+
+set -e
+cd /usr/bin
+[ "$PWD" = /usr/bin ]
+CDPATH=/:
+cd .
+[ "$PWD" = /usr/bin ]
+cd ./
+[ "$PWD" = /usr/bin ]
+cd ..
+[ "$PWD" = /usr ]
+cd /usr/bin
+cd ../
+[ "$PWD" = /usr ]