add copyright notices, SCCS headers, yank to 8.1
[unix-history] / usr / src / bin / sh / cd.c
index a9bba57..0a817a8 100644 (file)
@@ -1,6 +1,6 @@
 /*-
 /*-
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Kenneth Almquist.
  *
  * This code is derived from software contributed to Berkeley by
  * Kenneth Almquist.
@@ -9,7 +9,7 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)cd.c       5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)cd.c       8.1 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -44,13 +44,9 @@ STATIC char *getcomponent();
 
 
 char *curdir;                  /* current working directory */
 
 
 char *curdir;                  /* current working directory */
+char *prevdir;                 /* previous working directory */
 STATIC char *cdcomppath;
 
 STATIC char *cdcomppath;
 
-#if UDIR
-extern int didudir;            /* set if /u/logname expanded */
-#endif
-
-
 int
 cdcmd(argc, argv)  char **argv; {
        char *dest;
 int
 cdcmd(argc, argv)  char **argv; {
        char *dest;
@@ -58,16 +54,32 @@ cdcmd(argc, argv)  char **argv; {
        char *p;
        struct stat statb;
        char *padvance();
        char *p;
        struct stat statb;
        char *padvance();
+       int print = 0;
 
 
-       if ((dest = argv[1]) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
+       nextopt(nullstr);
+       if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
                error("HOME not set");
                error("HOME not set");
+       if (dest[0] == '-' && dest[1] == '\0') {
+               dest = prevdir ? prevdir : curdir;
+               print = 1;
+       }
        if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
                path = nullstr;
        while ((p = padvance(&path, dest)) != NULL) {
                if (stat(p, &statb) >= 0
        if (*dest == '/' || (path = bltinlookup("CDPATH", 1)) == NULL)
                path = nullstr;
        while ((p = padvance(&path, dest)) != NULL) {
                if (stat(p, &statb) >= 0
-                && (statb.st_mode & S_IFMT) == S_IFDIR
-                && docd(p, strcmp(p, dest)) >= 0)
-                       return 0;
+                && (statb.st_mode & S_IFMT) == S_IFDIR) {
+                       if (!print) {
+                               /*
+                                * XXX - rethink
+                                */
+                               if (p[0] == '.' && p[1] == '/')
+                                       p += 2;
+                               print = strcmp(p, dest);
+                       }
+                       if (docd(p, print) >= 0)
+                               return 0;
+
+               }
        }
        error("can't cd to %s", dest);
 }
        }
        error("can't cd to %s", dest);
 }
@@ -88,10 +100,6 @@ STATIC int
 docd(dest, print)
        char *dest;
        {
 docd(dest, print)
        char *dest;
        {
-#if UDIR
-       if (didudir)
-               print = 1;
-#endif
        INTOFF;
        if (chdir(dest) < 0) {
                INTON;
        INTOFF;
        if (chdir(dest) < 0) {
                INTON;
@@ -121,10 +129,6 @@ docd(dest, print)
        int i;
 
        TRACE(("docd(\"%s\", %d) called\n", dest, print));
        int i;
 
        TRACE(("docd(\"%s\", %d) called\n", dest, print));
-#if UDIR
-       if (didudir)
-               print = 1;
-#endif
 
 top:
        cdcomppath = dest;
 
 top:
        cdcomppath = dest;
@@ -267,9 +271,12 @@ updatepwd(dir)
        if (new == stackblock())
                STPUTC('/', new);
        STACKSTRNUL(new);
        if (new == stackblock())
                STPUTC('/', new);
        STACKSTRNUL(new);
-       if (curdir)
-               ckfree(curdir);
+       INTOFF;
+       if (prevdir)
+               ckfree(prevdir);
+       prevdir = curdir;
        curdir = savestr(stackblock());
        curdir = savestr(stackblock());
+       INTON;
 }
 
 
 }