2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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
37 static char sccsid[] = "@(#)dir.c 8.1 (Berkeley) 5/31/93";
40 #include <sys/param.h>
56 /* Directory management. */
58 static struct directory
60 static Char *dfollow __P((Char *));
61 static void printdirs __P((void));
62 static Char *dgoto __P((Char *));
63 static void dnewcwd __P((struct directory *));
64 static void dset __P((Char *));
66 struct directory dhead; /* "head" of loop */
67 int printd; /* force name to be printed */
69 static int dirflag = 0;
72 * dinit - initialize current working directory
80 register struct directory *dp;
81 char path[MAXPATHLEN];
82 static char *emsg = "csh: Trying to start from \"%s\"\n";
84 /* Don't believe the login shell home, because it may be a symlink */
85 tcp = getwd(path); /* see ngetwd.c for System V version */
86 if (tcp == NULL || *tcp == '\0') {
87 (void) fprintf(csherr, "csh: %s: %s\n", path, strerror(errno));
94 (void) fprintf(csherr, emsg, vis_str(hp));
99 (void) fprintf(csherr, emsg, "/");
100 if (chdir("/") == -1)
101 /* I am not even try to print an error message! */
107 struct stat swd, shp;
110 * See if $HOME is the working directory we got and use that
113 stat(tcp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
114 swd.st_dev == shp.st_dev && swd.st_ino == shp.st_ino)
120 * use PWD if we have it (for subshells)
122 if ((cwd = getenv("PWD")) != NULL) {
123 if (stat(cwd, &shp) != -1 && swd.st_dev == shp.st_dev &&
124 swd.st_ino == shp.st_ino)
127 cp = dcanon(SAVE(tcp), STRNULL);
131 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
132 dp->di_name = Strsave(cp);
134 dhead.di_next = dhead.di_prev = dp;
135 dp->di_next = dp->di_prev = &dhead;
145 * Don't call set() directly cause if the directory contains ` or
146 * other junk characters glob will fail.
148 register Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
150 vec[0] = Strsave(dp);
152 setq(STRcwd, vec, &shvhed);
168 for (n++; *n != NULL && (*n)[0] == '-'; n++)
169 for (s = &((*n)[1]); *s; s++)
181 stderror(ERR_DIRUS, vis_str(**v), str);
188 * dodirs - list all directories in directory loop
199 stderror(ERR_DIRUS, "dirs", "");
206 register struct directory *dp;
207 Char *s, *hp = value(STRhome);
218 if (dirflag & DIR_VERT) {
219 (void) fprintf(cshout, "%d\t", idx++);
222 if (!(dirflag & DIR_LONG) && hp != NULL && !eq(hp, STRslash) &&
223 (len = Strlen(hp), Strncmp(hp, dp->di_name, len) == 0) &&
224 (dp->di_name[len] == '\0' || dp->di_name[len] == '/'))
225 len = Strlen(s = (dp->di_name + len)) + 2;
227 len = Strlen(s = dp->di_name) + 1;
230 if ((dirflag & DIR_LINE) && cur >= 80 - 1 && len < 80) {
231 (void) fprintf(cshout, "\n");
234 (void) fprintf(cshout, s != dp->di_name ? "~%s%c" : "%s%c",
235 vis_str(s), (dirflag & DIR_VERT) ? '\n' : ' ');
236 } while ((dp = dp->di_prev) != dcwd);
237 if (!(dirflag & DIR_VERT))
238 (void) fprintf(cshout, "\n");
243 register Char *home, *dir;
246 if (!eq(home, STRslash) && prefix(home, dir))
247 (void) fprintf(cshout, "~%s", vis_str(dir + Strlen(home)));
249 (void) fprintf(cshout, "%s", vis_str(dir));
255 struct directory *d = dcwd;
260 d->di_name = dcanon(d->di_name, STRNULL);
261 } while ((d = d->di_prev) != dcwd);
268 * If the name starts with . or .. then we might need to normalize
269 * it depending on the symbolic link flags
276 #define UC (unsigned char)
277 #define ISDOT(c) (UC(c)[0] == '.' && ((UC(c)[1] == '\0') || (UC(c)[1] == '/')))
278 #define ISDOTDOT(c) (UC(c)[0] == '.' && ISDOT(&((c)[1])))
280 if ((unsigned char) cp[0] == '/')
281 return (Strsave(cp));
283 if (adrof(STRignore_symlinks)) {
287 cwd = (Char *) xmalloc((size_t) ((Strlen(dcwd->di_name) + 3) *
289 (void) Strcpy(cwd, dcwd->di_name);
292 * Ignore . and count ..'s
299 else if (ISDOTDOT(cp)) {
309 if ((dp = Strrchr(cwd, '/'))) {
317 cwd[dotdot = Strlen(cwd)] = '/';
318 cwd[dotdot + 1] = '\0';
319 dp = Strspl(cwd, cp);
335 * dochngd - implement chdir command.
344 register struct directory *dp;
346 skipargs(&v, " [<dir>]");
349 if ((cp = value(STRhome)) == NULL || *cp == 0)
350 stderror(ERR_NAME | ERR_NOHOMEDIR);
351 if (chdir(short2str(cp)) < 0)
352 stderror(ERR_NAME | ERR_CANTCHANGE);
355 else if (v[1] != NULL) {
356 stderror(ERR_NAME | ERR_TOOMANY);
360 else if ((dp = dfind(*v)) != 0) {
364 if (chdir(tmp = short2str(dp->di_name)) < 0)
365 stderror(ERR_SYSTEM, tmp, strerror(errno));
366 dcwd->di_prev->di_next = dcwd->di_next;
367 dcwd->di_next->di_prev = dcwd->di_prev;
374 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
377 dp->di_next = dcwd->di_next;
378 dp->di_prev = dcwd->di_prev;
379 dp->di_prev->di_next = dp;
380 dp->di_next->di_prev = dp;
392 register Char *p, *q;
395 for (p = dcwd->di_name; *p++;)
397 if ((cwdlen = p - dcwd->di_name - 1) == 1) /* root */
401 dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
402 for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
407 p--; /* don't add a / after root */
408 for (q = cp; (*p++ = *q++) != '\0';)
422 * dfollow - change to arg directory; fall back on cdpath if not valid
430 char ebuf[MAXPATHLEN];
433 cp = globone(cp, G_ERROR);
435 * if we are ignoring symlinks, try to fix relatives now.
438 if (chdir(short2str(dp)) >= 0) {
444 if (chdir(short2str(cp)) >= 0)
449 if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
450 && (c = adrof(STRcdpath))) {
453 Char buf[MAXPATHLEN];
455 for (cdp = c->vec; *cdp; cdp++) {
456 for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
459 for (p = cp; (*dp++ = *p++) != '\0';)
461 if (chdir(short2str(buf)) >= 0) {
470 if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
476 (void) strcpy(ebuf, short2str(cp));
478 stderror(ERR_SYSTEM, ebuf, strerror(serrno));
484 * dopushd - push new directory onto directory stack.
485 * with no arguments exchange top and second.
486 * with numeric argument (+n) bring it to top.
494 register struct directory *dp;
496 skipargs(&v, " [<dir>|+<n>]");
501 if ((dp = dcwd->di_prev) == &dhead)
504 stderror(ERR_NAME | ERR_NODIR);
505 if (chdir(tmp = short2str(dp->di_name)) < 0)
506 stderror(ERR_SYSTEM, tmp, strerror(errno));
507 dp->di_prev->di_next = dp->di_next;
508 dp->di_next->di_prev = dp->di_prev;
509 dp->di_next = dcwd->di_next;
511 dcwd->di_next->di_prev = dp;
514 else if (v[1] != NULL) {
515 stderror(ERR_NAME | ERR_TOOMANY);
519 else if ((dp = dfind(*v)) != NULL) {
522 if (chdir(tmp = short2str(dp->di_name)) < 0)
523 stderror(ERR_SYSTEM, tmp, strerror(errno));
529 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
533 dp->di_next = dcwd->di_next;
535 dp->di_next->di_prev = dp;
541 * dfind - find a directory if specified by numeric (+n) argument
543 static struct directory *
547 register struct directory *dp;
553 for (ep = cp; Isdigit(*ep); ep++)
560 for (dp = dcwd; i != 0; i--) {
561 if ((dp = dp->di_prev) == &dhead)
564 stderror(ERR_NAME | ERR_DEEP);
570 * dopopd - pop a directory out of the directory stack
571 * with a numeric argument just discard it.
579 register struct directory *dp, *p = NULL;
581 skipargs(&v, " [+<n>]");
585 else if (v[1] != NULL) {
586 stderror(ERR_NAME | ERR_TOOMANY);
590 else if ((dp = dfind(*v)) == 0)
591 stderror(ERR_NAME | ERR_BADDIR);
592 if (dp->di_prev == &dhead && dp->di_next == &dhead)
593 stderror(ERR_NAME | ERR_EMPTY);
597 if ((p = dp->di_prev) == &dhead)
599 if (chdir(tmp = short2str(p->di_name)) < 0)
600 stderror(ERR_SYSTEM, tmp, strerror(errno));
602 dp->di_prev->di_next = dp->di_next;
603 dp->di_next->di_prev = dp->di_prev;
613 * dfree - free the directory (or keep it if it still has ref count)
617 register struct directory *dp;
620 if (dp->di_count != 0) {
621 dp->di_next = dp->di_prev = 0;
624 xfree((char *) dp->di_name);
630 * dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
631 * we are of course assuming that the file system is standardly
632 * constructed (always have ..'s, directories have links)
636 register Char *cp, *p;
639 register Char *p1, *p2; /* general purpose */
642 Char link[MAXPATHLEN];
643 char tlink[MAXPATHLEN];
648 * christos: if the path given does not start with a slash prepend cwd. If
649 * cwd does not start with a path or the result would be too long abort().
652 Char tmpdir[MAXPATHLEN];
655 if (p1 == NULL || *p1 != '/')
657 if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
659 (void) Strcpy(tmpdir, p1);
660 (void) Strcat(tmpdir, STRslash);
661 (void) Strcat(tmpdir, cp);
663 cp = p = Strsave(tmpdir);
666 while (*p) { /* for each component */
667 sp = p; /* save slash address */
668 while (*++p == '/') /* flush extra slashes */
671 for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
673 p = sp; /* save start of component */
675 while (*++p) /* find next slash or end of path */
682 if (*sp == '\0') /* if component is null */
683 if (--sp == cp) /* if path is one char (i.e. /) */
687 else if (sp[0] == '.' && sp[1] == 0) {
689 for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
696 else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
698 * We have something like "yyy/xxx/..", where "yyy" can be null or
699 * a path starting at /, and "xxx" is a single component. Before
700 * compressing "xxx/..", we want to expand "yyy/xxx", if it is a
703 *--sp = 0; /* form the pathname for readlink */
704 if (sp != cp && !adrof(STRignore_symlinks) &&
705 (cc = readlink(short2str(cp), tlink,
706 sizeof tlink)) >= 0) {
707 (void) Strcpy(link, str2short(tlink));
713 * Point p to the '/' in "/..", and restore the '/'.
723 * Relative path, expand it between the "yyy/" and the
724 * "/..". First, back sp up to the character past "yyy/".
731 * New length is "yyy/" + link + "/.." and rest
733 p1 = newcp = (Char *) xmalloc((size_t)
734 (((sp - cp) + cc + (p1 - p)) *
737 * Copy new path into newcp
739 for (p2 = cp; (*p1++ = *p2++) != '\0';)
741 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
743 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
746 * Restart canonicalization at expanded "/xxx".
748 p = sp - cp - 1 + newcp;
752 * New length is link + "/.." and rest
754 p1 = newcp = (Char *) xmalloc((size_t)
755 ((cc + (p1 - p)) * sizeof(Char)));
757 * Copy new path into newcp
759 for (p2 = link; (*p1++ = *p2++) != '\0';)
761 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
764 * Restart canonicalization at beginning
770 continue; /* canonicalize the link */
777 for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
786 else { /* normal dir name (not . or .. or nothing) */
788 if (sp != cp && adrof(STRchase_symlinks) &&
789 !adrof(STRignore_symlinks) &&
790 (cc = readlink(short2str(cp), tlink,
791 sizeof tlink)) >= 0) {
792 (void) Strcpy(link, str2short(tlink));
802 * point sp to p (rather than backing up).
813 * Relative path, expand it between the "yyy/" and the
814 * remainder. First, back sp up to the character past
822 * New length is "yyy/" + link + "/.." and rest
824 p1 = newcp = (Char *) xmalloc((size_t)
825 (((sp - cp) + cc + (p1 - p))
828 * Copy new path into newcp
830 for (p2 = cp; (*p1++ = *p2++) != '\0';)
832 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
834 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
837 * Restart canonicalization at expanded "/xxx".
839 p = sp - cp - 1 + newcp;
843 * New length is link + the rest
845 p1 = newcp = (Char *) xmalloc((size_t)
846 ((cc + (p1 - p)) * sizeof(Char)));
848 * Copy new path into newcp
850 for (p2 = link; (*p1++ = *p2++) != '\0';)
852 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
855 * Restart canonicalization at beginning
861 continue; /* canonicalize the link */
874 * See if we're not in a subdir of STRhome
876 if (p1 && *p1 == '/' &&
877 (Strncmp(p1, cp, cc) != 0 || (cp[cc] != '/' && cp[cc] != '\0'))) {
878 static ino_t home_ino = -1;
879 static dev_t home_dev = -1;
880 static Char *home_ptr = NULL;
884 * Get dev and ino of STRhome
886 if (home_ptr != p1 &&
887 stat(short2str(p1), &statbuf) != -1) {
888 home_dev = statbuf.st_dev;
889 home_ino = statbuf.st_ino;
893 * Start comparing dev & ino backwards
895 p2 = Strcpy(link, cp);
896 for (sp = NULL; *p2 && stat(short2str(p2), &statbuf) != -1;) {
897 if (statbuf.st_dev == home_dev &&
898 statbuf.st_ino == home_ino) {
902 if ((sp = Strrchr(p2, '/')) != NULL)
908 if (*p2 && sp == (Char *) -1) {
910 * Use STRhome to make '~' work
912 newcp = Strspl(p1, cp + Strlen(p2));
922 * dnewcwd - make a new directory in the loop the current one
926 register struct directory *dp;
930 if (printd && !(adrof(STRpushdsilent)))