1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.dir.c,v 3.85 2016/04/08 16:10:52 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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 RCSID("$tcsh: sh.dir.c,v 3.85 2016/04/08 16:10:52 christos Exp $")
39 * C Shell - directory management
42 static Char *agetcwd (void);
43 static void dstart (const char *);
44 static struct directory *dfind (Char *);
45 static Char *dfollow (Char *, int);
46 static void printdirs (int);
47 static Char *dgoto (Char *);
48 static void dnewcwd (struct directory *, int);
49 static void dset (Char *);
50 static void dextract (struct directory *);
51 static int skipargs (Char ***, const char *,
53 static void dgetstack (void);
55 static struct directory dhead INIT_ZERO_STRUCT; /* "head" of loop */
56 static int printd; /* force name to be printed */
58 int bequiet = 0; /* do not print dir stack -strike */
69 while (getcwd(buf, len) == NULL) {
79 buf = xrealloc(buf, len);
91 dstart(const char *from)
93 xprintf(CGETS(12, 1, "%s: Trying to start from \"%s\"\n"), progname, from);
97 * dinit - initialize current working directory
103 struct directory *dp;
105 /* Don't believe the login shell home, because it may be a symlink */
108 xprintf("%s: %s\n", progname, strerror(errno));
110 char *xcp = short2str(hp);
112 if (chdir(xcp) == -1)
121 if (chdir("/") == -1)
122 /* I am not even try to print an error message! */
129 struct stat swd, shp;
132 swd_ok = stat(short2str(tcp), &swd) == 0;
134 * See if $HOME is the working directory we got and use that
136 if (swd_ok && hp && *hp && stat(short2str(hp), &shp) != -1 &&
137 DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
138 swd.st_ino == shp.st_ino)
144 * use PWD if we have it (for subshells)
146 if (swd_ok && (cwd = getenv("PWD")) != NULL) {
147 if (stat(cwd, &shp) != -1 &&
148 DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
149 swd.st_ino == shp.st_ino) {
151 cleanup_push(tcp, xfree);
154 cleanup_push(tcp, xfree);
155 cp = dcanon(tcp, STRNULL);
160 cleanup_push(tcp, xfree);
161 cp = dcanon(tcp, STRNULL);
167 dp = xcalloc(sizeof(struct directory), 1);
170 dhead.di_next = dhead.di_prev = dp;
171 dp->di_next = dp->di_prev = &dhead;
174 setcopy(STRdirstack, dp->di_name, VAR_READWRITE|VAR_NOGLOB);
181 * Don't call set() directly cause if the directory contains ` or
182 * other junk characters glob will fail.
184 setcopy(STRowd, varval(STRcwd), VAR_READWRITE|VAR_NOGLOB);
185 setcopy(STRcwd, dp, VAR_READWRITE|VAR_NOGLOB);
189 #define DIR_PRINT 0x01 /* -p */
190 #define DIR_LONG 0x02 /* -l */
191 #define DIR_VERT 0x04 /* -v */
192 #define DIR_LINE 0x08 /* -n */
193 #define DIR_SAVE 0x10 /* -S */
194 #define DIR_LOAD 0x20 /* -L */
195 #define DIR_CLEAR 0x40 /* -c */
196 #define DIR_OLD 0x80 /* - */
199 skipargs(Char ***v, const char *dstr, const char *str)
203 int dflag = 0, loop = 1;
204 for (n++; loop && *n != NULL && (*n)[0] == '-'; n++)
205 if (*(s = &((*n)[1])) == '\0') /* test for bare "-" argument */
207 else if ((*n)[1] == '-' && (*n)[2] == '\0') { /* test for -- */
212 while (*s != '\0') /* examine flags */ {
213 if ((p = strchr(dstr, *s++)) != NULL)
214 dflag |= (1 << (p - dstr));
216 stderror(ERR_DIRUS, short2str(**v), dstr, str);
219 if (*n && (dflag & DIR_OLD))
220 stderror(ERR_DIRUS, short2str(**v), dstr, str);
222 /* make -l, -v, and -n imply -p */
223 if (dflag & (DIR_LONG|DIR_VERT|DIR_LINE))
229 * dodirs - list all directories in directory loop
233 dodirs(Char **v, struct command *c)
235 static const char flags[] = "plvnSLc";
236 int dflag = skipargs(&v, flags, "");
239 if ((dflag & DIR_CLEAR) != 0) {
240 struct directory *dp, *fdp;
241 for (dp = dcwd->di_next; dp != dcwd; ) {
247 dhead.di_next = dhead.di_prev = dp;
248 dp->di_next = dp->di_prev = &dhead;
250 if ((dflag & DIR_LOAD) != 0)
252 else if ((dflag & DIR_SAVE) != 0)
255 if (*v && (dflag & (DIR_SAVE|DIR_LOAD)))
258 if (*v != NULL || (dflag & DIR_OLD))
259 stderror(ERR_DIRUS, "dirs", flags, "");
260 if ((dflag & (DIR_CLEAR|DIR_LOAD|DIR_SAVE)) == 0 || (dflag & DIR_PRINT))
267 struct directory *dp;
277 if (dflag & DIR_VERT) {
278 xprintf("%d\t", idx++);
283 if (!(dflag & DIR_LONG) && (user = getusername(&s)) != NULL)
284 len = (int) (Strlen(user) + Strlen(s) + 2);
286 len = (int) (Strlen(s) + 1);
289 if ((dflag & DIR_LINE) && cur >= TermH - 1 && len < TermH) {
294 xprintf("~%S", user);
295 xprintf("%-S%c", s, (dflag & DIR_VERT) ? '\n' : ' ');
296 } while ((dp = dp->di_prev) != dcwd);
297 if (!(dflag & DIR_VERT))
305 if ((user = getusername(&dir)) != NULL)
306 xprintf("~%-S%S", user, dir);
314 struct directory *d = dcwd;
319 d->di_name = dcanon(d->di_name, STRNULL);
320 } while ((d = d->di_prev) != dcwd);
327 * The path will be normalized if it
329 * 2) or starts with "../",
330 * 3) or ends with "/..",
331 * 4) or contains the string "/../",
332 * then it will be normalized, unless those strings are quoted.
333 * Otherwise, a copy is made and sent back.
336 dnormalize(const Char *cp, int expnd)
339 /* return true if dp is of the form "../xxx" or "/../xxx" */
340 #define IS_DOTDOT(sp, p) (ISDOTDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
341 #define IS_DOT(sp, p) (ISDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
345 struct Strbuf buf = Strbuf_INIT;
348 const Char *start = cp;
349 # ifdef HAVE_SLASHSLASH
351 # endif /* HAVE_SLASHSLASH */
354 * count the number of "../xxx" or "xxx/../xxx" in the path
356 for ( ; *cp && *(cp + 1); cp++)
357 if (IS_DOTDOT(start, cp))
361 * if none, we are done.
364 return (Strsave(start));
369 * We disable this test because:
370 * cd /tmp; mkdir dir1 dir2; cd dir2; ln -s /tmp/dir1; cd dir1;
371 * echo ../../dir1 does not expand. We had enabled this before
372 * because it was bothering people with expansions in compilation
373 * lines like -I../../foo. Maybe we need some kind of finer grain
376 * If the path doesn't exist, we are done too.
378 if (lstat(short2str(start), &sb) != 0 && errno == ENOENT)
379 return (Strsave(start));
382 cwd = xmalloc((Strlen(dcwd->di_name) + 3) * sizeof(Char));
383 (void) Strcpy(cwd, dcwd->di_name);
386 * If the path starts with a slash, we are not relative to
387 * the current working directory.
389 if (ABSOLUTEP(start))
391 # ifdef HAVE_SLASHSLASH
392 slashslash = cwd[0] == '/' && cwd[1] == '/';
393 # endif /* HAVE_SLASHSLASH */
396 * Ignore . and count ..'s
403 if (IS_DOT(start, cp)) {
407 else if (IS_DOTDOT(start, cp)) {
409 break; /* finish analyzing .././../xxx/[..] */
416 Strbuf_append1(&buf, *cp++);
418 Strbuf_terminate(&buf);
420 if ((dp = Strrchr(cwd, '/')) != NULL) {
421 # ifdef HAVE_SLASHSLASH
424 # endif /* HAVE_SLASHSLASH */
431 if (!*cwd) { /* too many ..'s, starts with "/" */
433 # ifdef HAVE_SLASHSLASH
435 * Only append another slash, if already the former cwd
436 * was in a double-slash path.
438 cwd[1] = slashslash ? '/' : '\0';
440 # else /* !HAVE_SLASHSLASH */
442 # endif /* HAVE_SLASHSLASH */
444 # ifdef HAVE_SLASHSLASH
445 else if (slashslash && cwd[1] == '\0') {
449 # endif /* HAVE_SLASHSLASH */
455 if (TRM(cwd[i - 1]) != '/') {
459 dp = Strspl(cwd, TRM(buf.s[0]) == '/' ? &buf.s[1] : buf.s);
463 if (TRM(cwd[i]) == '/')
466 /* Reduction of ".." following the stuff we collected in buf
467 * only makes sense if the directory item in buf really exists.
468 * Avoid reduction of "-I../.." (typical compiler call) to ""
469 * or "/usr/nonexistant/../bin" to "/usr/bin":
473 if (0 != stat(short2str(cwd), &exists)) {
476 return Strsave(start);
479 } while (*cp != '\0');
489 * dochngd - implement chdir command.
493 dochngd(Char **v, struct command *c)
496 struct directory *dp;
497 int dflag = skipargs(&v, "plvn", "[-|<dir>]");
501 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
505 stderror(ERR_NAME | ERR_TOOFEW);
506 else if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
507 stderror(ERR_NAME | ERR_NOHOMEDIR);
508 if (chdir(short2str(cp)) < 0)
509 stderror(ERR_NAME | ERR_CANTCHANGE);
512 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
513 stderror(ERR_NAME | ERR_TOOMANY);
517 else if ((dp = dfind(cp)) != 0) {
521 if (chdir(tmp = short2str(dp->di_name)) < 0)
522 stderror(ERR_SYSTEM, tmp, strerror(errno));
523 dcwd->di_prev->di_next = dcwd->di_next;
524 dcwd->di_next->di_prev = dcwd->di_prev;
530 if ((cp = dfollow(cp, dflag & DIR_OLD)) == NULL)
532 dp = xcalloc(sizeof(struct directory), 1);
535 dp->di_next = dcwd->di_next;
536 dp->di_prev = dcwd->di_prev;
537 dp->di_prev->di_next = dp;
538 dp->di_next->di_prev = dp;
553 cwdlen = Strlen(dcwd->di_name);
554 if (cwdlen == 1) /* root */
556 dp = xmalloc((cwdlen + Strlen(cp) + 2) * sizeof(Char));
557 for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
562 p--; /* don't add a / after root */
571 #if defined(WINNT_NATIVE)
573 #elif defined(__CYGWIN__)
574 if (ABSOLUTEP(cp) && cp[1] == ':') { /* Only DOS paths are treated that way */
577 cleanup_push(cp, xfree);
578 ret = dcanon(cp, dp);
582 #else /* !WINNT_NATIVE */
583 cleanup_push(cp, xfree);
584 ret = dcanon(cp, dp);
587 #endif /* WINNT_NATIVE */
592 * dfollow - change to arg directory; fall back on cdpath if not valid
595 dfollow(Char *cp, int old)
601 cp = old ? Strsave(cp) : globone(cp, G_ERROR);
602 cleanup_push(cp, xfree);
604 if (Strchr(cp, '`')) {
606 if (chdir(dptr = short2str(cp)) < 0)
607 stderror(ERR_SYSTEM, dptr, strerror(errno));
609 cleanup_push(dp, xfree);
615 stderror(ERR_SYSTEM, dptr, strerror(errno));
620 * if we are ignoring symlinks, try to fix relatives now.
621 * if we are expading symlinks, it should be done by now.
623 dp = dnormalize(cp, symlinks == SYM_IGNORE);
624 if (chdir(short2str(dp)) >= 0) {
630 if (chdir(short2str(cp)) >= 0) {
635 else if (errno != ENOENT && errno != ENOTDIR) {
639 stderror(ERR_SYSTEM, short2str(cp), strerror(err));
644 if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
645 && (c = adrof(STRcdpath)) && c->vec != NULL) {
646 struct Strbuf buf = Strbuf_INIT;
649 for (cdp = c->vec; *cdp; cdp++) {
650 size_t len = Strlen(*cdp);
653 Strbuf_append(&buf, *cdp);
654 if ((*cdp)[len - 1] != '/')
655 Strbuf_append1(&buf, '/');
657 Strbuf_append(&buf, cp);
658 Strbuf_terminate(&buf);
660 * We always want to fix the directory here
661 * If we are normalizing symlinks
663 dp = dnormalize(buf.s, symlinks == SYM_IGNORE ||
664 symlinks == SYM_EXPAND);
665 if (chdir(short2str(dp)) >= 0) {
671 else if (chdir(short2str(cp)) >= 0) {
684 if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
691 * on login source of ~/.cshdirs, errors are eaten. the dir stack is all
692 * directories we could get to.
695 stderror(ERR_SYSTEM, short2str(cp), strerror(serrno));
702 * dopushd - push new directory onto directory stack.
703 * with no arguments exchange top and second.
704 * with numeric argument (+n) bring it to top.
708 dopushd(Char **v, struct command *c)
710 struct directory *dp;
712 int dflag = skipargs(&v, "plvn", " [-|<dir>|+<n>]");
716 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
719 if (adrof(STRpushdtohome)) {
720 if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
721 stderror(ERR_NAME | ERR_NOHOMEDIR);
722 if (chdir(short2str(cp)) < 0)
723 stderror(ERR_NAME | ERR_CANTCHANGE);
724 if ((cp = dfollow(cp, dflag & DIR_OLD)) == NULL)
726 dp = xcalloc(sizeof(struct directory), 1);
730 dp->di_next = dcwd->di_next;
732 dp->di_next->di_prev = dp;
737 if ((dp = dcwd->di_prev) == &dhead)
740 stderror(ERR_NAME | ERR_NODIR);
741 if (chdir(tmp = short2str(dp->di_name)) < 0)
742 stderror(ERR_SYSTEM, tmp, strerror(errno));
743 dp->di_prev->di_next = dp->di_next;
744 dp->di_next->di_prev = dp->di_prev;
745 dp->di_next = dcwd->di_next;
747 dcwd->di_next->di_prev = dp;
751 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
752 stderror(ERR_NAME | ERR_TOOMANY);
756 else if ((dp = dfind(cp)) != NULL) {
759 if (chdir(tmp = short2str(dp->di_name)) < 0)
760 stderror(ERR_SYSTEM, tmp, strerror(errno));
762 * kfk - 10 Feb 1984 - added new "extraction style" pushd +n
764 if (adrof(STRdextract))
770 if ((ccp = dfollow(cp, dflag & DIR_OLD)) == NULL)
772 dp = xcalloc(sizeof(struct directory), 1);
776 dp->di_next = dcwd->di_next;
778 dp->di_next->di_prev = dp;
784 * dfind - find a directory if specified by numeric (+n) argument
786 static struct directory *
789 struct directory *dp;
795 for (ep = cp; Isdigit(*ep); ep++)
802 for (dp = dcwd; i != 0; i--) {
803 if ((dp = dp->di_prev) == &dhead)
806 stderror(ERR_NAME | ERR_DEEP);
812 * dopopd - pop a directory out of the directory stack
813 * with a numeric argument just discard it.
817 dopopd(Char **v, struct command *c)
820 struct directory *dp, *p = NULL;
821 int dflag = skipargs(&v, "plvn", " [-|+<n>]");
825 cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
829 else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
830 stderror(ERR_NAME | ERR_TOOMANY);
834 else if ((dp = dfind(cp)) == 0)
835 stderror(ERR_NAME | ERR_BADDIR);
836 if (dp->di_prev == &dhead && dp->di_next == &dhead)
837 stderror(ERR_NAME | ERR_EMPTY);
841 if ((p = dp->di_prev) == &dhead)
843 if (chdir(tmp = short2str(p->di_name)) < 0)
844 stderror(ERR_SYSTEM, tmp, strerror(errno));
846 dp->di_prev->di_next = dp->di_next;
847 dp->di_next->di_prev = dp->di_prev;
858 * dfree - free the directory (or keep it if it still has ref count)
861 dfree(struct directory *dp)
864 if (dp->di_count != 0) {
865 dp->di_next = dp->di_prev = 0;
874 * dcanon - 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(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;
1364 struct directory *dp;
1367 struct Strbuf qname = Strbuf_INIT;
1369 if (fname == NULL && !def)
1372 if (fname == NULL) {
1373 if ((fname = varval(STRdirsfile)) == STRNULL)
1374 fname = Strspl(varval(STRhome), &STRtildotdirs[1]);
1376 fname = Strsave(fname);
1379 fname = globone(fname, G_ERROR);
1380 cleanup_push(fname, xfree);
1382 if ((fp = xcreat(short2str(fname), 0600)) == -1) {
1383 cleanup_until(fname);
1387 if ((snum = varval(STRsavedirs)) == STRNULL || snum[0] == '\0')
1388 num = (unsigned int) ~0;
1390 num = (unsigned int) atoi(short2str(snum));
1397 cleanup_push(&qname, Strbuf_cleanup);
1405 xprintf("cd %S\n", quote_meta(&qname, dp->di_name));
1408 xprintf("pushd %S\n", quote_meta(&qname, dp->di_name));
1413 } while ((dp = dp->di_next) != dcwd->di_next);
1418 cleanup_until(fname);