2 * sh.dir.c: Directory manipulation functions
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * C Shell - directory management
39 static Char *agetcwd (void);
40 static void dstart (const char *);
41 static struct directory *dfind (Char *);
42 static Char *dfollow (Char *, int);
43 static void printdirs (int);
44 static Char *dgoto (Char *);
45 static void dnewcwd (struct directory *, int);
46 static void dset (Char *);
47 static void dextract (struct directory *);
48 static int skipargs (Char ***, const char *,
50 static void dgetstack (void);
51 static Char *dcanon_internal(Char *, Char *);
53 static struct directory dhead INIT_ZERO_STRUCT; /* "head" of loop */
54 static int printd; /* force name to be printed */
56 int bequiet = 0; /* do not print dir stack -strike */
67 while (getcwd(buf, len) == NULL) {
77 buf = xrealloc(buf, len);
89 dstart(const char *from)
91 xprintf(CGETS(12, 1, "%s: Trying to start from \"%s\"\n"), progname, from);
95 * dinit - initialize current working directory
101 struct directory *dp;
103 /* Don't believe the login shell home, because it may be a symlink */
106 xprintf("%s: %s\n", progname, strerror(errno));
108 char *xcp = short2str(hp);
110 if (chdir(xcp) == -1)
119 if (chdir("/") == -1)
120 /* I am not even try to print an error message! */
127 struct stat swd, shp;
130 swd_ok = stat(short2str(tcp), &swd) == 0;
132 * See if $HOME is the working directory we got and use that
134 if (swd_ok && hp && *hp && stat(short2str(hp), &shp) != -1 &&
135 DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
136 swd.st_ino == shp.st_ino)
142 * use PWD if we have it (for subshells)
144 if (swd_ok && (cwd = getenv("PWD")) != NULL) {
145 if (stat(cwd, &shp) != -1 &&
146 DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
147 swd.st_ino == shp.st_ino) {
149 cleanup_push(tcp, xfree);
152 cp = dcanon(tcp, STRNULL);
155 cp = dcanon(tcp, STRNULL);
159 dp = xcalloc(sizeof(struct directory), 1);
162 dhead.di_next = dhead.di_prev = dp;
163 dp->di_next = dp->di_prev = &dhead;
166 setcopy(STRdirstack, dp->di_name, VAR_READWRITE|VAR_NOGLOB);
173 * Don't call set() directly cause if the directory contains ` or
174 * other junk characters glob will fail.
176 setcopy(STRowd, varval(STRcwd), VAR_READWRITE|VAR_NOGLOB);
177 setcopy(STRcwd, dp, VAR_READWRITE|VAR_NOGLOB);
181 #define DIR_PRINT 0x01 /* -p */
182 #define DIR_LONG 0x02 /* -l */
183 #define DIR_VERT 0x04 /* -v */
184 #define DIR_LINE 0x08 /* -n */
185 #define DIR_SAVE 0x10 /* -S */
186 #define DIR_LOAD 0x20 /* -L */
187 #define DIR_CLEAR 0x40 /* -c */
188 #define DIR_OLD 0x80 /* - */
191 skipargs(Char ***v, const char *dstr, const char *str)
195 int dflag = 0, loop = 1;
196 for (n++; loop && *n != NULL && (*n)[0] == '-'; n++)
197 if (*(s = &((*n)[1])) == '\0') /* test for bare "-" argument */
199 else if ((*n)[1] == '-' && (*n)[2] == '\0') { /* test for -- */
204 while (*s != '\0') /* examine flags */ {
205 if ((p = strchr(dstr, *s++)) != NULL)
206 dflag |= (1 << (p - dstr));
208 stderror(ERR_DIRUS, short2str(**v), dstr, str);
211 if (*n && (dflag & DIR_OLD))
212 stderror(ERR_DIRUS, short2str(**v), dstr, str);
214 /* make -l, -v, and -n imply -p */
215 if (dflag & (DIR_LONG|DIR_VERT|DIR_LINE))
221 * dodirs - list all directories in directory loop
225 dodirs(Char **v, struct command *c)
227 static const char flags[] = "plvnSLc";
228 int dflag = skipargs(&v, flags, "");
231 if ((dflag & DIR_CLEAR) != 0) {
232 struct directory *dp, *fdp;
233 for (dp = dcwd->di_next; dp != dcwd; ) {
239 dhead.di_next = dhead.di_prev = dp;
240 dp->di_next = dp->di_prev = &dhead;
242 if ((dflag & DIR_LOAD) != 0)
244 else if ((dflag & DIR_SAVE) != 0)
247 if (*v && (dflag & (DIR_SAVE|DIR_LOAD)))
250 if (*v != NULL || (dflag & DIR_OLD))
251 stderror(ERR_DIRUS, "dirs", flags, "");
252 if ((dflag & (DIR_CLEAR|DIR_LOAD|DIR_SAVE)) == 0 || (dflag & DIR_PRINT))
259 struct directory *dp;
269 if (dflag & DIR_VERT) {
270 xprintf("%d\t", idx++);
275 if (!(dflag & DIR_LONG) && (user = getusername(&s)) != NULL)
276 len = (int) (Strlen(user) + Strlen(s) + 2);
278 len = (int) (Strlen(s) + 1);
281 if ((dflag & DIR_LINE) && cur >= TermH - 1 && len < TermH) {
286 xprintf("~%S", user);
287 xprintf("%-S%c", s, (dflag & DIR_VERT) ? '\n' : ' ');
288 } while ((dp = dp->di_prev) != dcwd);
289 if (!(dflag & DIR_VERT))
297 if ((user = getusername(&dir)) != NULL)
298 xprintf("~%-S%S", user, dir);
306 struct directory *d = dcwd;
311 d->di_name = dcanon_internal(d->di_name, STRNULL);
312 } while ((d = d->di_prev) != dcwd);
319 * The path will be normalized if it
321 * 2) or starts with "../",
322 * 3) or ends with "/..",
323 * 4) or contains the string "/../",
324 * then it will be normalized, unless those strings are quoted.
325 * Otherwise, a copy is made and sent back.
328 dnormalize(const Char *cp, int expnd)
331 /* return true if dp is of the form "../xxx" or "/../xxx" */
332 #define IS_DOTDOT(sp, p) (ISDOTDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
333 #define IS_DOT(sp, p) (ISDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
337 struct Strbuf buf = Strbuf_INIT;
340 const Char *start = cp;
341 # ifdef HAVE_SLASHSLASH
343 # endif /* HAVE_SLASHSLASH */
346 * count the number of "../xxx" or "xxx/../xxx" in the path
348 for ( ; *cp && *(cp + 1); cp++)
349 if (IS_DOTDOT(start, cp))
353 * if none, we are done.
356 return (Strsave(start));
361 * We disable this test because:
362 * cd /tmp; mkdir dir1 dir2; cd dir2; ln -s /tmp/dir1; cd dir1;
363 * echo ../../dir1 does not expand. We had enabled this before
364 * because it was bothering people with expansions in compilation
365 * lines like -I../../foo. Maybe we need some kind of finer grain
368 * If the path doesn't exist, we are done too.
370 if (lstat(short2str(start), &sb) != 0 && errno == ENOENT)
371 return (Strsave(start));
374 cwd = xmalloc((Strlen(dcwd->di_name) + 3) * sizeof(Char));
375 (void) Strcpy(cwd, dcwd->di_name);
378 * If the path starts with a slash, we are not relative to
379 * the current working directory.
381 if (ABSOLUTEP(start))
383 # ifdef HAVE_SLASHSLASH
384 slashslash = cwd[0] == '/' && cwd[1] == '/';
385 # endif /* HAVE_SLASHSLASH */
388 * Ignore . and count ..'s
395 if (IS_DOT(start, cp)) {
399 else if (IS_DOTDOT(start, cp)) {
401 break; /* finish analyzing .././../xxx/[..] */
408 Strbuf_append1(&buf, *cp++);
410 Strbuf_terminate(&buf);
412 if ((dp = Strrchr(cwd, '/')) != NULL) {
413 # ifdef HAVE_SLASHSLASH
416 # endif /* HAVE_SLASHSLASH */
423 if (!*cwd) { /* too many ..'s, starts with "/" */
425 # ifdef HAVE_SLASHSLASH
427 * Only append another slash, if already the former cwd
428 * was in a double-slash path.
430 cwd[1] = slashslash ? '/' : '\0';
432 # else /* !HAVE_SLASHSLASH */
434 # endif /* HAVE_SLASHSLASH */
436 # ifdef HAVE_SLASHSLASH
437 else if (slashslash && cwd[1] == '\0') {
441 # endif /* HAVE_SLASHSLASH */
447 if (TRM(cwd[i - 1]) != '/') {
451 dp = Strspl(cwd, TRM(buf.s[0]) == '/' ? &buf.s[1] : buf.s);
455 if (TRM(cwd[i]) == '/')
458 /* Reduction of ".." following the stuff we collected in buf
459 * only makes sense if the directory item in buf really exists.
460 * Avoid reduction of "-I../.." (typical compiler call) to ""
461 * or "/usr/nonexistant/../bin" to "/usr/bin":
465 if (0 != stat(short2str(cwd), &exists)) {
468 return Strsave(start);
471 } while (*cp != '\0');
481 * dochngd - implement chdir command.
485 dochngd(Char **v, struct command *c)
488 struct directory *dp;
489 int dflag = skipargs(&v, "plvn", "[-|<dir>]");
493 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
497 stderror(ERR_NAME | ERR_TOOFEW);
498 else if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
499 stderror(ERR_NAME | ERR_NOHOMEDIR);
500 if (chdir(short2str(cp)) < 0)
501 stderror(ERR_NAME | ERR_CANTCHANGE);
504 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
505 stderror(ERR_NAME | ERR_TOOMANY);
509 else if ((dp = dfind(cp)) != 0) {
513 if (chdir(tmp = short2str(dp->di_name)) < 0)
514 stderror(ERR_SYSTEM, tmp, strerror(errno));
515 dcwd->di_prev->di_next = dcwd->di_next;
516 dcwd->di_next->di_prev = dcwd->di_prev;
522 if ((cp = dfollow(cp, dflag & DIR_OLD)) == NULL)
524 dp = xcalloc(sizeof(struct directory), 1);
527 dp->di_next = dcwd->di_next;
528 dp->di_prev = dcwd->di_prev;
529 dp->di_prev->di_next = dp;
530 dp->di_next->di_prev = dp;
545 cwdlen = Strlen(dcwd->di_name);
546 if (cwdlen == 1) /* root */
548 dp = xmalloc((cwdlen + Strlen(cp) + 2) * sizeof(Char));
549 for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
554 p--; /* don't add a / after root */
563 #if defined(WINNT_NATIVE)
565 #elif defined(__CYGWIN__)
566 if (ABSOLUTEP(cp) && cp[1] == ':') { /* Only DOS paths are treated that way */
569 ret = dcanon(cp, dp);
571 #else /* !WINNT_NATIVE */
572 ret = dcanon(cp, dp);
573 #endif /* WINNT_NATIVE */
578 * dfollow - change to arg directory; fall back on cdpath if not valid
581 dfollow(Char *cp, int old)
587 cp = old ? Strsave(cp) : globone(cp, G_ERROR);
588 cleanup_push(cp, xfree);
590 if (Strchr(cp, '`')) {
592 if (chdir(dptr = short2str(cp)) < 0)
593 stderror(ERR_SYSTEM, dptr, strerror(errno));
595 cleanup_push(dp, xfree);
601 stderror(ERR_SYSTEM, dptr, strerror(errno));
606 * if we are ignoring symlinks, try to fix relatives now.
607 * if we are expading symlinks, it should be done by now.
609 dp = dnormalize(cp, symlinks == SYM_IGNORE);
610 if (chdir(short2str(dp)) >= 0) {
616 if (chdir(short2str(cp)) >= 0) {
621 else if (errno != ENOENT && errno != ENOTDIR) {
625 stderror(ERR_SYSTEM, short2str(cp), strerror(err));
630 if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
631 && (c = adrof(STRcdpath)) && c->vec != NULL) {
632 struct Strbuf buf = Strbuf_INIT;
635 for (cdp = c->vec; *cdp; cdp++) {
636 size_t len = Strlen(*cdp);
639 Strbuf_append(&buf, *cdp);
640 if ((*cdp)[len - 1] != '/')
641 Strbuf_append1(&buf, '/');
643 Strbuf_append(&buf, cp);
644 Strbuf_terminate(&buf);
646 * We always want to fix the directory here
647 * If we are normalizing symlinks
649 dp = dnormalize(buf.s, symlinks == SYM_IGNORE ||
650 symlinks == SYM_EXPAND);
651 if (chdir(short2str(dp)) >= 0) {
657 else if (chdir(short2str(cp)) >= 0) {
670 if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
677 * on login source of ~/.cshdirs, errors are eaten. the dir stack is all
678 * directories we could get to.
681 stderror(ERR_SYSTEM, short2str(cp), strerror(serrno));
688 * dopushd - push new directory onto directory stack.
689 * with no arguments exchange top and second.
690 * with numeric argument (+n) bring it to top.
694 dopushd(Char **v, struct command *c)
696 struct directory *dp;
698 int dflag = skipargs(&v, "plvn", " [-|<dir>|+<n>]");
702 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
705 if (adrof(STRpushdtohome)) {
706 if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
707 stderror(ERR_NAME | ERR_NOHOMEDIR);
708 if (chdir(short2str(cp)) < 0)
709 stderror(ERR_NAME | ERR_CANTCHANGE);
710 if ((cp = dfollow(cp, dflag & DIR_OLD)) == NULL)
712 dp = xcalloc(sizeof(struct directory), 1);
716 dp->di_next = dcwd->di_next;
718 dp->di_next->di_prev = dp;
723 if ((dp = dcwd->di_prev) == &dhead)
726 stderror(ERR_NAME | ERR_NODIR);
727 if (chdir(tmp = short2str(dp->di_name)) < 0)
728 stderror(ERR_SYSTEM, tmp, strerror(errno));
729 dp->di_prev->di_next = dp->di_next;
730 dp->di_next->di_prev = dp->di_prev;
731 dp->di_next = dcwd->di_next;
733 dcwd->di_next->di_prev = dp;
737 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
738 stderror(ERR_NAME | ERR_TOOMANY);
742 else if ((dp = dfind(cp)) != NULL) {
745 if (chdir(tmp = short2str(dp->di_name)) < 0)
746 stderror(ERR_SYSTEM, tmp, strerror(errno));
748 * kfk - 10 Feb 1984 - added new "extraction style" pushd +n
750 if (adrof(STRdextract))
756 if ((ccp = dfollow(cp, dflag & DIR_OLD)) == NULL)
758 dp = xcalloc(sizeof(struct directory), 1);
762 dp->di_next = dcwd->di_next;
764 dp->di_next->di_prev = dp;
770 * dfind - find a directory if specified by numeric (+n) argument
772 static struct directory *
775 struct directory *dp;
781 for (ep = cp; Isdigit(*ep); ep++)
788 for (dp = dcwd; i != 0; i--) {
789 if ((dp = dp->di_prev) == &dhead)
792 stderror(ERR_NAME | ERR_DEEP);
798 * dopopd - pop a directory out of the directory stack
799 * with a numeric argument just discard it.
803 dopopd(Char **v, struct command *c)
806 struct directory *dp, *p = NULL;
807 int dflag = skipargs(&v, "plvn", " [-|+<n>]");
811 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
815 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
816 stderror(ERR_NAME | ERR_TOOMANY);
820 else if ((dp = dfind(cp)) == 0)
821 stderror(ERR_NAME | ERR_BADDIR);
822 if (dp->di_prev == &dhead && dp->di_next == &dhead)
823 stderror(ERR_NAME | ERR_EMPTY);
827 if ((p = dp->di_prev) == &dhead)
829 if (chdir(tmp = short2str(p->di_name)) < 0)
830 stderror(ERR_SYSTEM, tmp, strerror(errno));
832 dp->di_prev->di_next = dp->di_next;
833 dp->di_next->di_prev = dp->di_prev;
844 * dfree - free the directory (or keep it if it still has ref count)
847 dfree(struct directory *dp)
850 if (dp->di_count != 0) {
851 dp->di_next = dp->di_prev = 0;
860 * dcanon - a safe version of dcanon_internal that arranges for cleanup
863 dcanon(Char *cp, Char *p)
865 cleanup_push(cp, xfree);
866 p = dcanon_internal(cp, p);
867 // coverity[use_after_free] we use the pointer as a marker
874 * dcanon_internal - canonicalize the pathname, removing excess ./ and ../ etc.
875 * we are of course assuming that the file system is standardly
876 * constructed (always have ..'s, directories have links)
879 dcanon_internal(Char *cp, Char *p)
882 Char *p1, *p2; /* general purpose */
884 #ifdef HAVE_SLASHSLASH
886 #endif /* HAVE_SLASHSLASH */
889 #ifdef S_IFLNK /* if we have symlinks */
898 * christos: if the path given does not start with a slash prepend cwd. If
899 * cwd does not start with a slash or the result would be too long try to
902 if (!ABSOLUTEP(cp)) {
907 if (p1 == STRNULL || !ABSOLUTEP(p1)) {
908 Char *new_cwd = agetcwd();
910 if (new_cwd == NULL) {
911 xprintf("%s: %s\n", progname, strerror(errno));
912 setcopy(STRcwd, str2short("/"), VAR_READWRITE|VAR_NOGLOB);
915 setv(STRcwd, new_cwd, VAR_READWRITE|VAR_NOGLOB);
919 tmpdir = xmalloc((len + clen + 2) * sizeof (*tmpdir));
920 (void) Strcpy(tmpdir, p1);
921 (void) Strcat(tmpdir, STRslash);
922 (void) Strcat(tmpdir, cp);
927 #ifdef HAVE_SLASHSLASH
928 slashslash = (cp[0] == '/' && cp[1] == '/');
929 #endif /* HAVE_SLASHSLASH */
931 while (*p) { /* for each component */
932 sp = p; /* save slash address */
933 while (*++p == '/') /* flush extra slashes */
936 for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
938 p = sp; /* save start of component */
941 while (*++p) /* find next slash or end of path */
948 #ifdef HAVE_SLASHSLASH
949 if (&cp[1] == sp && sp[0] == '.' && sp[1] == '.' && sp[2] == '\0')
951 #endif /* HAVE_SLASHSLASH */
952 if (*sp == '\0') { /* if component is null */
953 if (--sp == cp) /* if path is one char (i.e. /) */
958 else if (sp[0] == '.' && sp[1] == 0) {
960 for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
969 else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
971 * We have something like "yyy/xxx/..", where "yyy" can be null or
972 * a path starting at /, and "xxx" is a single component. Before
973 * compressing "xxx/..", we want to expand "yyy/xxx", if it is a
976 *--sp = 0; /* form the pathname for readlink */
977 #ifdef S_IFLNK /* if we have symlinks */
978 if (sp != cp && /* symlinks != SYM_IGNORE && */
979 (tlink = areadlink(short2str(cp))) != NULL) {
980 mlink = str2short(tlink);
986 * Point p to the '/' in "/..", and restore the '/'.
991 * Relative path, expand it between the "yyy/" and the
992 * "/..". First, back sp up to the character past "yyy/".
999 * New length is "yyy/" + mlink + "/.." and rest
1001 p1 = newcp = xmalloc(((sp - cp) + Strlen(mlink) +
1002 Strlen(p) + 1) * sizeof(Char));
1004 * Copy new path into newcp
1006 for (p2 = cp; (*p1++ = *p2++) != '\0';)
1008 for (p1--, p2 = mlink; (*p1++ = *p2++) != '\0';)
1010 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1013 * Restart canonicalization at expanded "/xxx".
1015 p = sp - cp - 1 + newcp;
1018 newcp = Strspl(mlink, p);
1020 * Restart canonicalization at beginning
1026 #ifdef HAVE_SLASHSLASH
1027 slashslash = (cp[0] == '/' && cp[1] == '/');
1028 #endif /* HAVE_SLASHSLASH */
1029 continue; /* canonicalize the link */
1031 #endif /* S_IFLNK */
1034 while (*--sp != '/')
1037 for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
1046 else { /* normal dir name (not . or .. or nothing) */
1048 #ifdef S_IFLNK /* if we have symlinks */
1049 if (sp != cp && symlinks == SYM_CHASE &&
1050 (tlink = areadlink(short2str(cp))) != NULL) {
1051 mlink = str2short(tlink);
1061 * point sp to p (rather than backing up).
1065 if (*mlink != '/') {
1067 * Relative path, expand it between the "yyy/" and the
1068 * remainder. First, back sp up to the character past
1071 while (*--sp != '/')
1076 * New length is "yyy/" + mlink + "/.." and rest
1078 p1 = newcp = xmalloc(((sp - cp) + Strlen(mlink) +
1079 Strlen(p) + 1) * sizeof(Char));
1081 * Copy new path into newcp
1083 for (p2 = cp; (*p1++ = *p2++) != '\0';)
1085 for (p1--, p2 = mlink; (*p1++ = *p2++) != '\0';)
1087 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1090 * Restart canonicalization at expanded "/xxx".
1092 p = sp - cp - 1 + newcp;
1095 newcp = Strspl(mlink, p);
1097 * Restart canonicalization at beginning
1103 #ifdef HAVE_SLASHSLASH
1104 slashslash = (cp[0] == '/' && cp[1] == '/');
1105 #endif /* HAVE_SLASHSLASH */
1106 continue; /* canonicalize the mlink */
1108 #endif /* S_IFLNK */
1118 p1 = varval(STRhome);
1121 * See if we're not in a subdir of STRhome
1123 if (p1 && *p1 == '/' && (Strncmp(p1, cp, cc) != 0 ||
1124 (cp[cc] != '/' && cp[cc] != '\0'))) {
1125 static ino_t home_ino = (ino_t) -1;
1126 static dev_t home_dev = (dev_t) -1;
1127 static Char *home_ptr = NULL;
1128 struct stat statbuf;
1133 * Get dev and ino of STRhome
1135 if (home_ptr != p1 &&
1136 stat(short2str(p1), &statbuf) != -1) {
1137 home_dev = statbuf.st_dev;
1138 home_ino = statbuf.st_ino;
1142 * Start comparing dev & ino backwards
1144 p2 = copy = Strsave(cp);
1146 while (*p2 && stat(short2str(p2), &statbuf) != -1) {
1147 if (DEV_DEV_COMPARE(statbuf.st_dev, home_dev) &&
1148 statbuf.st_ino == home_ino) {
1152 if ((sp = Strrchr(p2, '/')) != NULL)
1156 * See if we found it
1160 * Use STRhome to make '~' work
1162 newcp = Strspl(p1, cp + Strlen(p2));
1168 #endif /* S_IFLNK */
1170 #ifdef HAVE_SLASHSLASH
1173 p = xmalloc((Strlen(cp) + 2) * sizeof(Char));
1175 (void) Strcpy(&p[1], cp);
1180 if (cp[1] == '/' && cp[2] == '/') {
1181 for (p1 = &cp[1], p2 = &cp[2]; (*p1++ = *p2++) != '\0';)
1184 #endif /* HAVE_SLASHSLASH */
1190 * dnewcwd - make a new directory in the loop the current one
1193 dnewcwd(struct directory *dp, int dflag)
1197 if (adrof(STRdunique)) {
1198 struct directory *dn;
1200 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev)
1201 if (dn != dp && Strcmp(dn->di_name, dp->di_name) == 0) {
1202 dn->di_next->di_prev = dn->di_prev;
1203 dn->di_prev->di_next = dn->di_next;
1209 dset(dcwd->di_name);
1211 print = printd; /* if printd is set, print dirstack... */
1212 if (adrof(STRpushdsilent)) /* but pushdsilent overrides printd... */
1214 if (dflag & DIR_PRINT) /* but DIR_PRINT overrides pushdsilent... */
1216 if (bequiet) /* and bequiet overrides everything */
1220 cwd_cmd(); /* PWP: run the defined cwd command */
1228 struct directory *dn, *dp;
1230 if ((vp = adrof(STRdirstack)) == NULL || vp->vec == NULL)
1233 /* Free the whole stack */
1234 while ((dn = dhead.di_prev) != &dhead) {
1235 dn->di_next->di_prev = dn->di_prev;
1236 dn->di_prev->di_next = dn->di_next;
1241 /* thread the current working directory */
1242 dhead.di_prev = dhead.di_next = dcwd;
1243 dcwd->di_next = dcwd->di_prev = &dhead;
1245 /* put back the stack */
1246 for (cp = vp->vec; cp && *cp && **cp; cp++) {
1247 dp = xcalloc(sizeof(struct directory), 1);
1248 dp->di_name = Strsave(*cp);
1251 dp->di_next = dcwd->di_next;
1253 dp->di_next->di_prev = dp;
1255 dgetstack(); /* Make $dirstack reflect the current state */
1263 struct directory *dn;
1265 if (adrof(STRdirstack) == NULL)
1268 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, i++)
1270 dbp = dblk = xmalloc((i + 1) * sizeof(Char *));
1271 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, dbp++)
1272 *dbp = Strsave(dn->di_name);
1274 cleanup_push(dblk, blk_cleanup);
1275 setq(STRdirstack, dblk, &shvhed, VAR_READWRITE);
1276 cleanup_ignore(dblk);
1277 cleanup_until(dblk);
1281 * getstakd - added by kfk 17 Jan 1984
1282 * Support routine for the stack hack. Finds nth directory in
1283 * the directory stack, or finds last directory in stack.
1288 struct directory *dp;
1291 if (cnt < 0) { /* < 0 ==> last dir requested. */
1309 * Karl Kleinpaste - 10 Feb 1984
1310 * Added dextract(), which is used in pushd +n.
1311 * Instead of just rotating the entire stack around, dextract()
1312 * lets the user have the nth dir extracted from its current
1313 * position, and pushes it onto the top.
1316 dextract(struct directory *dp)
1320 dp->di_next->di_prev = dp->di_prev;
1321 dp->di_prev->di_next = dp->di_next;
1322 dp->di_next = dcwd->di_next;
1324 dp->di_next->di_prev = dp;
1329 bequiet_cleanup(void *dummy)
1336 loaddirs(Char *fname)
1338 static Char *loaddirs_cmd[] = { STRsource, NULL, NULL };
1341 cleanup_push(&bequiet, bequiet_cleanup);
1343 loaddirs_cmd[1] = fname;
1344 else if ((fname = varval(STRdirsfile)) != STRNULL)
1345 loaddirs_cmd[1] = fname;
1347 loaddirs_cmd[1] = STRtildotdirs;
1348 dosource(loaddirs_cmd, NULL);
1349 cleanup_until(&bequiet);
1353 * create a file called ~/.cshdirs which has a sequence
1354 * of pushd commands which will restore the dir stack to
1355 * its state before exit/logout. remember that the order
1356 * is reversed in the file because we are pushing.
1360 recdirs(Char *fname, int def)
1362 int fp, ftmp, oldidfds, ophup_disabled;
1364 struct directory *dp;
1367 struct Strbuf qname = Strbuf_INIT;
1369 if (fname == NULL && !def)
1372 ophup_disabled = phup_disabled;
1374 if (fname == NULL) {
1375 if ((fname = varval(STRdirsfile)) == STRNULL)
1376 fname = Strspl(varval(STRhome), &STRtildotdirs[1]);
1378 fname = Strsave(fname);
1381 fname = globone(fname, G_ERROR);
1382 cleanup_push(fname, xfree);
1384 if ((fp = xcreat(short2str(fname), 0600)) == -1) {
1385 cleanup_until(fname);
1386 phup_disabled = ophup_disabled;
1390 if ((snum = varval(STRsavedirs)) == STRNULL || snum[0] == '\0')
1391 num = (unsigned int) ~0;
1393 num = (unsigned int) atoi(short2str(snum));
1400 cleanup_push(&qname, Strbuf_cleanup);
1408 xprintf("cd %S\n", quote_meta(&qname, dp->di_name));
1411 xprintf("pushd %S\n", quote_meta(&qname, dp->di_name));
1416 } while ((dp = dp->di_next) != dcwd->di_next);
1421 cleanup_until(fname);
1422 phup_disabled = ophup_disabled;