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 */
819 #ifdef S_IFLNK /* if we have symlinks */
820 Char link[MAXPATHLEN];
821 char tlink[MAXPATHLEN];
827 * if the path given is too long truncate it!
829 if ((clen = Strlen(cp)) >= MAXPATHLEN)
830 cp[clen = MAXPATHLEN - 1] = '\0';
833 * christos: if the path given does not start with a slash prepend cwd. If
834 * cwd does not start with a slash or the result would be too long try to
837 if (!ABSOLUTEP(cp)) {
838 Char tmpdir[MAXPATHLEN];
842 if (p1 == STRNULL || !ABSOLUTEP(p1)) {
843 char *tmp = (char *)getcwd((char *)tmpdir, sizeof(tmpdir));
844 if (tmp == NULL || *tmp == '\0') {
845 xprintf("%s: %s\n", progname, strerror(errno));
846 set(STRcwd, SAVE("/"), VAR_READWRITE|VAR_NOGLOB);
848 set(STRcwd, SAVE(tmp), VAR_READWRITE|VAR_NOGLOB);
853 if (len + clen + 1 >= MAXPATHLEN)
854 cp[MAXPATHLEN - (len + 1)] = '\0';
855 (void) Strcpy(tmpdir, p1);
856 (void) Strcat(tmpdir, STRslash);
857 (void) Strcat(tmpdir, cp);
859 cp = p = Strsave(tmpdir);
863 slashslash = (cp[0] == '/' && cp[1] == '/');
866 while (*p) { /* for each component */
867 sp = p; /* save slash address */
868 while (*++p == '/') /* flush extra slashes */
871 for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
873 p = sp; /* save start of component */
876 while (*++p) /* find next slash or end of path */
884 if (&cp[1] == sp && sp[0] == '.' && sp[1] == '.' && sp[2] == '\0')
887 if (*sp == '\0') { /* if component is null */
888 if (--sp == cp) /* if path is one char (i.e. /) */
893 else if (sp[0] == '.' && sp[1] == 0) {
895 for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
904 else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
906 * We have something like "yyy/xxx/..", where "yyy" can be null or
907 * a path starting at /, and "xxx" is a single component. Before
908 * compressing "xxx/..", we want to expand "yyy/xxx", if it is a
911 *--sp = 0; /* form the pathname for readlink */
912 #ifdef S_IFLNK /* if we have symlinks */
913 if (sp != cp && /* symlinks != SYM_IGNORE && */
914 (cc = readlink(short2str(cp), tlink,
915 sizeof tlink)) >= 0) {
917 (void) Strncpy(link, str2short(tlink),
918 sizeof(link) / sizeof(Char));
919 link[sizeof(link) / sizeof(Char) - 1] = '\0';
924 * Point p to the '/' in "/..", and restore the '/'.
934 * Relative path, expand it between the "yyy/" and the
935 * "/..". First, back sp up to the character past "yyy/".
942 * New length is "yyy/" + link + "/.." and rest
944 p1 = newcp = (Char *) xmalloc((size_t)
945 (((sp - cp) + cc + (p1 - p)) *
948 * Copy new path into newcp
950 for (p2 = cp; (*p1++ = *p2++) != '\0';)
952 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
954 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
957 * Restart canonicalization at expanded "/xxx".
959 p = sp - cp - 1 + newcp;
963 * New length is link + "/.." and rest
965 p1 = newcp = (Char *) xmalloc((size_t)
966 ((cc + (p1 - p)) * sizeof(Char)));
968 * Copy new path into newcp
970 for (p2 = link; (*p1++ = *p2++) != '\0';)
972 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
975 * Restart canonicalization at beginning
982 slashslash = (cp[0] == '/' && cp[1] == '/');
984 continue; /* canonicalize the link */
992 for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
1001 else { /* normal dir name (not . or .. or nothing) */
1003 #ifdef S_IFLNK /* if we have symlinks */
1004 if (sp != cp && symlinks == SYM_CHASE &&
1005 (cc = readlink(short2str(cp), tlink,
1006 sizeof tlink)) >= 0) {
1008 (void) Strncpy(link, str2short(tlink),
1009 sizeof(link) / sizeof(Char));
1010 link[sizeof(link) / sizeof(Char) - 1] = '\0';
1019 * point sp to p (rather than backing up).
1026 for (p1 = p; *p1++;)
1030 * Relative path, expand it between the "yyy/" and the
1031 * remainder. First, back sp up to the character past
1034 while (*--sp != '/')
1039 * New length is "yyy/" + link + "/.." and rest
1041 p1 = newcp = (Char *) xmalloc((size_t)
1042 (((sp - cp) + cc + (p1 - p))
1045 * Copy new path into newcp
1047 for (p2 = cp; (*p1++ = *p2++) != '\0';)
1049 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
1051 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1054 * Restart canonicalization at expanded "/xxx".
1056 p = sp - cp - 1 + newcp;
1060 * New length is link + the rest
1062 p1 = newcp = (Char *) xmalloc((size_t)
1063 ((cc + (p1 - p)) * sizeof(Char)));
1065 * Copy new path into newcp
1067 for (p2 = link; (*p1++ = *p2++) != '\0';)
1069 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1072 * Restart canonicalization at beginning
1079 slashslash = (cp[0] == '/' && cp[1] == '/');
1081 continue; /* canonicalize the link */
1083 #endif /* S_IFLNK */
1093 p1 = varval(STRhome);
1094 cc = (int) Strlen(p1);
1096 * See if we're not in a subdir of STRhome
1098 if (p1 && *p1 == '/' && (Strncmp(p1, cp, (size_t) cc) != 0 ||
1099 (cp[cc] != '/' && cp[cc] != '\0'))) {
1100 static ino_t home_ino = (ino_t) -1;
1101 static dev_t home_dev = (dev_t) -1;
1102 static Char *home_ptr = NULL;
1103 struct stat statbuf;
1107 * Get dev and ino of STRhome
1109 if (home_ptr != p1 &&
1110 stat(short2str(p1), &statbuf) != -1) {
1111 home_dev = statbuf.st_dev;
1112 home_ino = statbuf.st_ino;
1116 * Start comparing dev & ino backwards
1118 p2 = Strncpy(link, cp, sizeof(link) / sizeof(Char));
1119 link[sizeof(link) / sizeof(Char) - 1] = '\0';
1121 while (*p2 && stat(short2str(p2), &statbuf) != -1) {
1122 if (DEV_DEV_COMPARE(statbuf.st_dev, home_dev) &&
1123 statbuf.st_ino == home_ino) {
1127 if ((sp = Strrchr(p2, '/')) != NULL)
1131 * See if we found it
1135 * Use STRhome to make '~' work
1137 newcp = Strspl(p1, cp + Strlen(p2));
1142 #endif /* S_IFLNK */
1147 p = (Char *) xmalloc((size_t) (Strlen(cp) + 2) * sizeof(Char));
1149 (void) Strcpy(&p[1], cp);
1154 if (cp[1] == '/' && cp[2] == '/')
1155 (void) Strcpy(&cp[1], &cp[2]);
1162 * dnewcwd - make a new directory in the loop the current one
1166 register struct directory *dp;
1171 if (adrof(STRdunique)) {
1172 struct directory *dn;
1174 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev)
1175 if (dn != dp && Strcmp(dn->di_name, dp->di_name) == 0) {
1176 dn->di_next->di_prev = dn->di_prev;
1177 dn->di_prev->di_next = dn->di_next;
1183 dset(dcwd->di_name);
1185 print = printd; /* if printd is set, print dirstack... */
1186 if (adrof(STRpushdsilent)) /* but pushdsilent overrides printd... */
1188 if (dflag & DIR_PRINT) /* but DIR_PRINT overrides pushdsilent... */
1190 if (bequiet) /* and bequiet overrides everything */
1194 cwd_cmd(); /* PWP: run the defined cwd command */
1202 struct directory *dn, *dp;
1204 if ((vp = adrof(STRdirstack)) == NULL)
1207 /* Free the whole stack */
1208 while ((dn = dhead.di_prev) != &dhead) {
1209 dn->di_next->di_prev = dn->di_prev;
1210 dn->di_prev->di_next = dn->di_next;
1215 /* thread the current working directory */
1216 dhead.di_prev = dhead.di_next = dcwd;
1217 dcwd->di_next = dcwd->di_prev = &dhead;
1219 /* put back the stack */
1220 for (cp = vp->vec; cp && *cp && **cp; cp++) {
1221 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
1222 dp->di_name = Strsave(*cp);
1225 dp->di_next = dcwd->di_next;
1227 dp->di_next->di_prev = dp;
1229 dgetstack(); /* Make $dirstack reflect the current state */
1237 struct directory *dn;
1239 if (adrof(STRdirstack) == NULL)
1242 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, i++)
1244 dbp = dblk = (Char**) xmalloc((size_t) (i + 1) * sizeof(Char *));
1245 for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, dbp++)
1246 *dbp = Strsave(dn->di_name);
1248 setq(STRdirstack, dblk, &shvhed, VAR_READWRITE);
1252 * getstakd - added by kfk 17 Jan 1984
1253 * Support routine for the stack hack. Finds nth directory in
1254 * the directory stack, or finds last directory in stack.
1261 struct directory *dp;
1264 if (cnt < 0) { /* < 0 ==> last dir requested. */
1278 (void) Strncpy(s, dp->di_name, BUFSIZE);
1279 s[BUFSIZE - 1] = '\0';
1284 * Karl Kleinpaste - 10 Feb 1984
1285 * Added dextract(), which is used in pushd +n.
1286 * Instead of just rotating the entire stack around, dextract()
1287 * lets the user have the nth dir extracted from its current
1288 * position, and pushes it onto the top.
1292 struct directory *dp;
1296 dp->di_next->di_prev = dp->di_prev;
1297 dp->di_prev->di_next = dp->di_next;
1298 dp->di_next = dcwd->di_next;
1300 dp->di_next->di_prev = dp;
1308 static Char *loaddirs_cmd[] = { STRsource, NULL, NULL };
1312 loaddirs_cmd[1] = fname;
1313 else if ((fname = varval(STRdirsfile)) != STRNULL)
1314 loaddirs_cmd[1] = fname;
1316 loaddirs_cmd[1] = STRtildotdirs;
1317 dosource(loaddirs_cmd, (struct command *)0);
1322 * create a file called ~/.cshdirs which has a sequence
1323 * of pushd commands which will restore the dir stack to
1324 * its state before exit/logout. remember that the order
1325 * is reversed in the file because we are pushing.
1333 int fp, ftmp, oldidfds;
1335 extern struct directory *dcwd;
1336 struct directory *dp;
1339 Char qname[MAXPATHLEN*2];
1341 if (fname == NULL && !def)
1344 if (fname == NULL) {
1345 if ((fname = varval(STRdirsfile)) == STRNULL)
1346 fname = Strspl(varval(STRhome), &STRtildotdirs[1]);
1348 fname = Strsave(fname);
1351 fname = globone(fname, G_ERROR);
1353 if ((fp = creat(short2str(fname), 0600)) == -1) {
1354 xfree((ptr_t) fname);
1358 if ((snum = varval(STRsavedirs)) == STRNULL)
1359 num = (unsigned int) ~0;
1361 num = (unsigned int) atoi(short2str(snum));
1375 xprintf("cd %S\n", quote_meta(qname, dp->di_name));
1378 xprintf("pushd %S\n", quote_meta(qname, dp->di_name));
1383 } while ((dp = dp->di_next) != dcwd->di_next);
1388 xfree((ptr_t) fname);