1 /* $Header: /src/pub/tcsh/sh.dir.c,v 3.54 2000/11/11 23:03:36 christos Exp $ */
3 * sh.dir.c: Directory manipulation functions
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 RCSID("$Id: sh.dir.c,v 3.54 2000/11/11 23:03:36 christos Exp $")
42 * C Shell - directory management
45 static void dstart __P((const char *));
46 static struct directory *dfind __P((Char *));
47 static Char *dfollow __P((Char *));
48 static void printdirs __P((int));
49 static Char *dgoto __P((Char *));
50 static void dnewcwd __P((struct directory *, int));
51 static void dset __P((Char *));
52 static void dextract __P((struct directory *));
53 static int skipargs __P((Char ***, char *, char *));
54 static void dgetstack __P((void));
56 static struct directory dhead INIT_ZERO_STRUCT; /* "head" of loop */
57 static int printd; /* force name to be printed */
59 int bequiet = 0; /* do not print dir stack -strike */
65 xprintf(CGETS(12, 1, "%s: Trying to start from \"%s\"\n"), progname, from);
69 * dinit - initialize current working directory
77 register struct directory *dp;
78 char path[MAXPATHLEN];
80 /* Don't believe the login shell home, because it may be a symlink */
81 tcp = (char *) getcwd(path, sizeof(path));
82 if (tcp == NULL || *tcp == '\0') {
83 xprintf("%s: %s\n", progname, strerror(errno));
97 /* I am not even try to print an error message! */
104 struct stat swd, shp;
107 * See if $HOME is the working directory we got and use that
110 stat(tcp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
111 DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
112 swd.st_ino == shp.st_ino)
118 * use PWD if we have it (for subshells)
120 if ((cwd = getenv("PWD")) != NULL) {
121 if (stat(cwd, &shp) != -1 &&
122 DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
123 swd.st_ino == shp.st_ino)
126 cp = dcanon(SAVE(tcp), STRNULL);
129 cp = dcanon(SAVE(tcp), STRNULL);
133 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
136 dhead.di_next = dhead.di_prev = dp;
137 dp->di_next = dp->di_prev = &dhead;
140 set(STRdirstack, Strsave(dp->di_name), VAR_READWRITE|VAR_NOGLOB);
148 * Don't call set() directly cause if the directory contains ` or
149 * other junk characters glob will fail.
151 set(STRowd, Strsave(varval(STRcwd)), VAR_READWRITE|VAR_NOGLOB);
152 set(STRcwd, Strsave(dp), VAR_READWRITE|VAR_NOGLOB);
157 #define DIR_PRINT 0x01 /* -p */
158 #define DIR_LONG 0x02 /* -l */
159 #define DIR_VERT 0x04 /* -v */
160 #define DIR_LINE 0x08 /* -n */
161 #define DIR_SAVE 0x10 /* -S */
162 #define DIR_LOAD 0x20 /* -L */
163 #define DIR_CLEAR 0x40 /* -c */
164 #define DIR_OLD 0x80 /* - */
167 skipargs(v, dstr, str)
174 int dflag = 0, loop = 1;
175 for (n++; loop && *n != NULL && (*n)[0] == '-'; n++)
176 if (*(s = &((*n)[1])) == '\0') /* test for bare "-" argument */
180 while (loop && *s != '\0') /* examine flags */
182 if ((p = strchr(dstr, *s++)) != NULL)
183 dflag |= (1 << (p - dstr));
185 stderror(ERR_DIRUS, short2str(**v), dstr, str);
186 loop = 0; /* break from both loops */
191 if (*n && (dflag & DIR_OLD))
192 stderror(ERR_DIRUS, short2str(**v), dstr, str);
194 /* make -l, -v, and -n imply -p */
195 if (dflag & (DIR_LONG|DIR_VERT|DIR_LINE))
201 * dodirs - list all directories in directory loop
209 static char flags[] = "plvnSLc";
210 int dflag = skipargs(&v, flags, "");
213 if ((dflag & DIR_CLEAR) != 0) {
214 struct directory *dp, *fdp;
215 for (dp = dcwd->di_next; dp != dcwd; ) {
221 dhead.di_next = dhead.di_prev = dp;
222 dp->di_next = dp->di_prev = &dhead;
224 if ((dflag & DIR_LOAD) != 0)
226 else if ((dflag & DIR_SAVE) != 0)
229 if (*v && (dflag & (DIR_SAVE|DIR_LOAD)))
232 if (*v != NULL || (dflag & DIR_OLD))
233 stderror(ERR_DIRUS, "dirs", flags, "");
234 if ((dflag & (DIR_CLEAR|DIR_LOAD|DIR_SAVE)) == 0 || (dflag & DIR_PRINT))
242 register struct directory *dp;
253 if (dflag & DIR_VERT) {
254 xprintf("%d\t", idx++);
259 if (!(dflag & DIR_LONG) && (user = getusername(&s)) != NULL)
260 len = (int) (Strlen(user) + Strlen(s) + 2);
262 len = (int) (Strlen(s) + 1);
265 if ((dflag & DIR_LINE) && cur >= T_Cols - 1 && len < T_Cols) {
270 xprintf("~%S", user);
271 xprintf("%S%c", s, (dflag & DIR_VERT) ? '\n' : ' ');
272 } while ((dp = dp->di_prev) != dcwd);
273 if (!(dflag & DIR_VERT))
282 if ((user = getusername(&dir)) != NULL)
283 xprintf("~%S%S", user, dir);
291 struct directory *d = dcwd;
296 d->di_name = dcanon(d->di_name, STRNULL);
297 } while ((d = d->di_prev) != dcwd);
304 * The path will be normalized if it
306 * 2) or starts with "../",
307 * 3) or ends with "/..",
308 * 4) or contains the string "/../",
309 * then it will be normalized, unless those strings are quoted.
310 * Otherwise, a copy is made and sent back.
318 /* return true if dp is of the form "../xxx" or "/../xxx" */
319 #define IS_DOTDOT(sp, p) (ISDOTDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
320 #define IS_DOT(sp, p) (ISDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
325 Char *dp, *cwd, *start = cp, buf[MAXPATHLEN];
331 * count the number of "../xxx" or "xxx/../xxx" in the path
333 for (dp=start; *dp && *(dp+1); dp++)
334 if (IS_DOTDOT(start, dp))
337 * if none, we are done.
340 return (Strsave(cp));
342 cwd = (Char *) xmalloc((size_t) (((int) Strlen(dcwd->di_name) + 3) *
344 (void) Strcpy(cwd, dcwd->di_name);
347 * If the path starts with a slash, we are not relative to
348 * the current working directory.
350 if (ABSOLUTEP(start))
353 slashslash = cwd[0] == '/' && cwd[1] == '/';
357 * Ignore . and count ..'s
364 if (IS_DOT(start, cp)) {
368 else if (IS_DOTDOT(start, cp)) {
370 break; /* finish analyzing .././../xxx/[..] */
381 if ((dp = Strrchr(cwd, '/')) != NULL) {
392 if (!*cwd) { /* too many ..'s, starts with "/" */
402 else if (slashslash && cwd[1] == '\0') {
409 if ((TRM(cwd[(dotdot = (int) Strlen(cwd)) - 1])) != '/')
412 dp = Strspl(cwd, TRM(buf[0]) == '/' ? &buf[1] : buf);
415 if ((TRM(cwd[(dotdot = (int) Strlen(cwd)) - 1])) == '/')
416 cwd[--dotdot] = '\0';
429 * dochngd - implement chdir command.
438 register struct directory *dp;
439 int dflag = skipargs(&v, "plvn", "[-|<dir>]");
443 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
446 if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
447 stderror(ERR_NAME | ERR_NOHOMEDIR);
448 if (chdir(short2str(cp)) < 0)
449 stderror(ERR_NAME | ERR_CANTCHANGE);
452 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
453 stderror(ERR_NAME | ERR_TOOMANY);
457 else if ((dp = dfind(cp)) != 0) {
461 if (chdir(tmp = short2str(dp->di_name)) < 0)
462 stderror(ERR_SYSTEM, tmp, strerror(errno));
463 dcwd->di_prev->di_next = dcwd->di_next;
464 dcwd->di_next->di_prev = dcwd->di_prev;
470 if ((cp = dfollow(cp)) == NULL)
472 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
475 dp->di_next = dcwd->di_next;
476 dp->di_prev = dcwd->di_prev;
477 dp->di_prev->di_next = dp;
478 dp->di_next->di_prev = dp;
491 register Char *p, *q;
494 for (p = dcwd->di_name; *p++;)
496 if ((cwdlen = (int) (p - dcwd->di_name - 1)) == 1) /* root */
500 dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
501 for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
506 p--; /* don't add a / after root */
507 for (q = cp; (*p++ = *q++) != '\0';)
517 cp = SAVE(getcwd(NULL, 0));
518 #else /* !WINNT_NATIVE */
520 #endif /* WINNT_NATIVE */
525 * dfollow - change to arg directory; fall back on cdpath if not valid
533 char ebuf[MAXPATHLEN];
536 cp = globone(cp, G_ERROR);
538 if (Strchr(cp, '`')) {
540 if (chdir(dptr = short2str(cp)) < 0)
541 stderror(ERR_SYSTEM, dptr, strerror(errno));
542 else if ((ptr = getcwd(ebuf, sizeof(ebuf))) && *ptr != '\0') {
544 cp = Strsave(str2short(ptr));
548 stderror(ERR_SYSTEM, dptr, ebuf);
552 (void) strncpy(ebuf, short2str(cp), MAXPATHLEN);
553 ebuf[MAXPATHLEN-1] = '\0';
555 * if we are ignoring symlinks, try to fix relatives now.
556 * if we are expading symlinks, it should be done by now.
558 dp = dnormalize(cp, symlinks == SYM_IGNORE);
559 if (chdir(short2str(dp)) >= 0) {
565 if (chdir(short2str(cp)) >= 0)
567 else if (errno != ENOENT && errno != ENOTDIR)
568 stderror(ERR_SYSTEM, ebuf, strerror(errno));
572 if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
573 && (c = adrof(STRcdpath))) {
576 Char buf[MAXPATHLEN];
578 for (cdp = c->vec; *cdp; cdp++) {
579 for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
582 for (p = cp; (*dp++ = *p++) != '\0';)
585 * We always want to fix the directory here
586 * If we are normalizing symlinks
588 dp = dnormalize(buf, symlinks == SYM_IGNORE ||
589 symlinks == SYM_EXPAND);
590 if (chdir(short2str(dp)) >= 0) {
595 else if (chdir(short2str(cp)) >= 0) {
603 if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
611 * on login source of ~/.cshdirs, errors are eaten. the dir stack is all
612 * directories we could get to.
615 stderror(ERR_SYSTEM, ebuf, strerror(serrno));
624 * dopushd - push new directory onto directory stack.
625 * with no arguments exchange top and second.
626 * with numeric argument (+n) bring it to top.
634 register struct directory *dp;
636 int dflag = skipargs(&v, "plvn", " [-|<dir>|+<n>]");
640 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
643 if (adrof(STRpushdtohome)) {
644 if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
645 stderror(ERR_NAME | ERR_NOHOMEDIR);
646 if (chdir(short2str(cp)) < 0)
647 stderror(ERR_NAME | ERR_CANTCHANGE);
648 cp = Strsave(cp); /* hmmm... PWP */
649 if ((cp = dfollow(cp)) == NULL)
651 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
655 dp->di_next = dcwd->di_next;
657 dp->di_next->di_prev = dp;
662 if ((dp = dcwd->di_prev) == &dhead)
665 stderror(ERR_NAME | ERR_NODIR);
666 if (chdir(tmp = short2str(dp->di_name)) < 0)
667 stderror(ERR_SYSTEM, tmp, strerror(errno));
668 dp->di_prev->di_next = dp->di_next;
669 dp->di_next->di_prev = dp->di_prev;
670 dp->di_next = dcwd->di_next;
672 dcwd->di_next->di_prev = dp;
676 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
677 stderror(ERR_NAME | ERR_TOOMANY);
681 else if ((dp = dfind(cp)) != NULL) {
684 if (chdir(tmp = short2str(dp->di_name)) < 0)
685 stderror(ERR_SYSTEM, tmp, strerror(errno));
687 * kfk - 10 Feb 1984 - added new "extraction style" pushd +n
689 if (adrof(STRdextract))
695 if ((ccp = dfollow(cp)) == NULL)
697 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
701 dp->di_next = dcwd->di_next;
703 dp->di_next->di_prev = dp;
709 * dfind - find a directory if specified by numeric (+n) argument
711 static struct directory *
715 register struct directory *dp;
721 for (ep = cp; Isdigit(*ep); ep++)
728 for (dp = dcwd; i != 0; i--) {
729 if ((dp = dp->di_prev) == &dhead)
732 stderror(ERR_NAME | ERR_DEEP);
738 * dopopd - pop a directory out of the directory stack
739 * with a numeric argument just discard it.
748 register struct directory *dp, *p = NULL;
749 int dflag = skipargs(&v, "plvn", " [-|+<n>]");
753 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
757 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
758 stderror(ERR_NAME | ERR_TOOMANY);
762 else if ((dp = dfind(cp)) == 0)
763 stderror(ERR_NAME | ERR_BADDIR);
764 if (dp->di_prev == &dhead && dp->di_next == &dhead)
765 stderror(ERR_NAME | ERR_EMPTY);
769 if ((p = dp->di_prev) == &dhead)
771 if (chdir(tmp = short2str(p->di_name)) < 0)
772 stderror(ERR_SYSTEM, tmp, strerror(errno));
774 dp->di_prev->di_next = dp->di_next;
775 dp->di_next->di_prev = dp->di_prev;
786 * dfree - free the directory (or keep it if it still has ref count)
790 register struct directory *dp;
793 if (dp->di_count != 0) {
794 dp->di_next = dp->di_prev = 0;
797 xfree((ptr_t) dp->di_name);
803 * dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
804 * we are of course assuming that the file system is standardly
805 * constructed (always have ..'s, directories have links)
809 register Char *cp, *p;
812 register Char *p1, *p2; /* general purpose */
818 #ifdef S_IFLNK /* if we have symlinks */
819 Char link[MAXPATHLEN];
820 char tlink[MAXPATHLEN];
826 * kim: if the path given is too long abort().
828 if (Strlen(cp) >= MAXPATHLEN)
832 * christos: if the path given does not start with a slash prepend cwd. If
833 * cwd does not start with a slash or the result would be too long abort().
835 if (!ABSOLUTEP(cp)) {
836 Char tmpdir[MAXPATHLEN];
839 if (p1 == STRNULL || !ABSOLUTEP(p1))
841 if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
843 (void) Strcpy(tmpdir, p1);
844 (void) Strcat(tmpdir, STRslash);
845 (void) Strcat(tmpdir, cp);
847 cp = p = Strsave(tmpdir);
856 slashslash = (cp[0] == '/' && cp[1] == '/');
859 while (*p) { /* for each component */
860 sp = p; /* save slash address */
861 while (*++p == '/') /* flush extra slashes */
864 for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
866 p = sp; /* save start of component */
869 while (*++p) /* find next slash or end of path */
877 if (&cp[1] == sp && sp[0] == '.' && sp[1] == '.' && sp[2] == '\0')
880 if (*sp == '\0') { /* if component is null */
881 if (--sp == cp) /* if path is one char (i.e. /) */
886 else if (sp[0] == '.' && sp[1] == 0) {
888 for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
897 else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
899 * We have something like "yyy/xxx/..", where "yyy" can be null or
900 * a path starting at /, and "xxx" is a single component. Before
901 * compressing "xxx/..", we want to expand "yyy/xxx", if it is a
904 *--sp = 0; /* form the pathname for readlink */
905 #ifdef S_IFLNK /* if we have symlinks */
906 if (sp != cp && /* symlinks != SYM_IGNORE && */
907 (cc = readlink(short2str(cp), tlink,
908 sizeof tlink)) >= 0) {
910 (void) Strncpy(link, str2short(tlink),
911 sizeof(link) / sizeof(Char));
912 link[sizeof(link) / sizeof(Char) - 1] = '\0';
917 * Point p to the '/' in "/..", and restore the '/'.
927 * Relative path, expand it between the "yyy/" and the
928 * "/..". First, back sp up to the character past "yyy/".
935 * New length is "yyy/" + link + "/.." and rest
937 p1 = newcp = (Char *) xmalloc((size_t)
938 (((sp - cp) + cc + (p1 - p)) *
941 * Copy new path into newcp
943 for (p2 = cp; (*p1++ = *p2++) != '\0';)
945 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
947 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
950 * Restart canonicalization at expanded "/xxx".
952 p = sp - cp - 1 + newcp;
956 * New length is link + "/.." and rest
958 p1 = newcp = (Char *) xmalloc((size_t)
959 ((cc + (p1 - p)) * sizeof(Char)));
961 * Copy new path into newcp
963 for (p2 = link; (*p1++ = *p2++) != '\0';)
965 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
968 * Restart canonicalization at beginning
975 slashslash = (cp[0] == '/' && cp[1] == '/');
977 continue; /* canonicalize the link */
985 for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
994 else { /* normal dir name (not . or .. or nothing) */
996 #ifdef S_IFLNK /* if we have symlinks */
997 if (sp != cp && symlinks == SYM_CHASE &&
998 (cc = readlink(short2str(cp), tlink,
999 sizeof tlink)) >= 0) {
1001 (void) Strncpy(link, str2short(tlink),
1002 sizeof(link) / sizeof(Char));
1003 link[sizeof(link) / sizeof(Char) - 1] = '\0';
1012 * point sp to p (rather than backing up).
1019 for (p1 = p; *p1++;)
1023 * Relative path, expand it between the "yyy/" and the
1024 * remainder. First, back sp up to the character past
1027 while (*--sp != '/')
1032 * New length is "yyy/" + link + "/.." and rest
1034 p1 = newcp = (Char *) xmalloc((size_t)
1035 (((sp - cp) + cc + (p1 - p))
1038 * Copy new path into newcp
1040 for (p2 = cp; (*p1++ = *p2++) != '\0';)
1042 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
1044 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1047 * Restart canonicalization at expanded "/xxx".
1049 p = sp - cp - 1 + newcp;
1053 * New length is link + the rest
1055 p1 = newcp = (Char *) xmalloc((size_t)
1056 ((cc + (p1 - p)) * sizeof(Char)));
1058 * Copy new path into newcp
1060 for (p2 = link; (*p1++ = *p2++) != '\0';)
1062 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1065 * Restart canonicalization at beginning
1072 slashslash = (cp[0] == '/' && cp[1] == '/');
1074 continue; /* canonicalize the link */
1076 #endif /* S_IFLNK */
1086 p1 = varval(STRhome);
1087 cc = (int) Strlen(p1);
1089 * See if we're not in a subdir of STRhome
1091 if (p1 && *p1 == '/' && (Strncmp(p1, cp, (size_t) cc) != 0 ||
1092 (cp[cc] != '/' && cp[cc] != '\0'))) {
1093 static ino_t home_ino = (ino_t) -1;
1094 static dev_t home_dev = (dev_t) -1;
1095 static Char *home_ptr = NULL;
1096 struct stat statbuf;
1100 * Get dev and ino of STRhome
1102 if (home_ptr != p1 &&
1103 stat(short2str(p1), &statbuf) != -1) {
1104 home_dev = statbuf.st_dev;
1105 home_ino = statbuf.st_ino;
1109 * Start comparing dev & ino backwards
1111 p2 = Strncpy(link, cp, sizeof(link) / sizeof(Char));
1112 link[sizeof(link) / sizeof(Char) - 1] = '\0';
1114 while (*p2 && stat(short2str(p2), &statbuf) != -1) {
1115 if (DEV_DEV_COMPARE(statbuf.st_dev, home_dev) &&
1116 statbuf.st_ino == home_ino) {
1120 if ((sp = Strrchr(p2, '/')) != NULL)
1124 * See if we found it
1128 * Use STRhome to make '~' work
1130 newcp = Strspl(p1, cp + Strlen(p2));
1135 #endif /* S_IFLNK */
1140 p = (Char *) xmalloc((size_t) (Strlen(cp) + 2) * sizeof(Char));
1142 (void) Strcpy(&p[1], cp);
1147 if (cp[1] == '/' && cp[2] == '/')
1148 (void) Strcpy(&cp[1], &cp[2]);
1155 * dnewcwd - make a new directory in the loop the current one
1159 register struct directory *dp;
1164 if (adrof(STRdunique)) {
1165 struct directory *dn;
1167 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev)
1168 if (dn != dp && Strcmp(dn->di_name, dp->di_name) == 0) {
1169 dn->di_next->di_prev = dn->di_prev;
1170 dn->di_prev->di_next = dn->di_next;
1176 dset(dcwd->di_name);
1178 print = printd; /* if printd is set, print dirstack... */
1179 if (adrof(STRpushdsilent)) /* but pushdsilent overrides printd... */
1181 if (dflag & DIR_PRINT) /* but DIR_PRINT overrides pushdsilent... */
1183 if (bequiet) /* and bequiet overrides everything */
1187 cwd_cmd(); /* PWP: run the defined cwd command */
1195 struct directory *dn, *dp;
1197 if ((vp = adrof(STRdirstack)) == NULL)
1200 /* Free the whole stack */
1201 while ((dn = dhead.di_prev) != &dhead) {
1202 dn->di_next->di_prev = dn->di_prev;
1203 dn->di_prev->di_next = dn->di_next;
1208 /* thread the current working directory */
1209 dhead.di_prev = dhead.di_next = dcwd;
1210 dcwd->di_next = dcwd->di_prev = &dhead;
1212 /* put back the stack */
1213 for (cp = vp->vec; cp && *cp && **cp; cp++) {
1214 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
1215 dp->di_name = Strsave(*cp);
1218 dp->di_next = dcwd->di_next;
1220 dp->di_next->di_prev = dp;
1222 dgetstack(); /* Make $dirstack reflect the current state */
1230 struct directory *dn;
1232 if (adrof(STRdirstack) == NULL)
1235 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, i++)
1237 dbp = dblk = (Char**) xmalloc((size_t) (i + 1) * sizeof(Char *));
1238 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, dbp++)
1239 *dbp = Strsave(dn->di_name);
1241 setq(STRdirstack, dblk, &shvhed, VAR_READWRITE);
1245 * getstakd - added by kfk 17 Jan 1984
1246 * Support routine for the stack hack. Finds nth directory in
1247 * the directory stack, or finds last directory in stack.
1254 struct directory *dp;
1257 if (cnt < 0) { /* < 0 ==> last dir requested. */
1271 (void) Strncpy(s, dp->di_name, BUFSIZE);
1272 s[BUFSIZE - 1] = '\0';
1277 * Karl Kleinpaste - 10 Feb 1984
1278 * Added dextract(), which is used in pushd +n.
1279 * Instead of just rotating the entire stack around, dextract()
1280 * lets the user have the nth dir extracted from its current
1281 * position, and pushes it onto the top.
1285 struct directory *dp;
1289 dp->di_next->di_prev = dp->di_prev;
1290 dp->di_prev->di_next = dp->di_next;
1291 dp->di_next = dcwd->di_next;
1293 dp->di_next->di_prev = dp;
1301 static Char *loaddirs_cmd[] = { STRsource, NULL, NULL };
1305 loaddirs_cmd[1] = fname;
1306 else if ((fname = varval(STRdirsfile)) != STRNULL)
1307 loaddirs_cmd[1] = fname;
1309 loaddirs_cmd[1] = STRtildotdirs;
1310 dosource(loaddirs_cmd, (struct command *)0);
1315 * create a file called ~/.cshdirs which has a sequence
1316 * of pushd commands which will restore the dir stack to
1317 * its state before exit/logout. remember that the order
1318 * is reversed in the file because we are pushing.
1326 int fp, ftmp, oldidfds;
1328 extern struct directory *dcwd;
1329 struct directory *dp;
1332 Char qname[MAXPATHLEN*2];
1334 if (fname == NULL && !def)
1337 if (fname == NULL) {
1338 if ((fname = varval(STRdirsfile)) == STRNULL)
1339 fname = Strspl(varval(STRhome), &STRtildotdirs[1]);
1341 fname = Strsave(fname);
1344 fname = globone(fname, G_ERROR);
1346 if ((fp = creat(short2str(fname), 0600)) == -1) {
1347 xfree((ptr_t) fname);
1351 if ((snum = varval(STRsavedirs)) == STRNULL)
1352 num = (unsigned int) ~0;
1354 num = (unsigned int) atoi(short2str(snum));
1368 xprintf("cd %S\n", quote_meta(qname, dp->di_name));
1371 xprintf("pushd %S\n", quote_meta(qname, dp->di_name));
1376 } while ((dp = dp->di_next) != dcwd->di_next);
1381 xfree((ptr_t) fname);