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
36 static char sccsid[] = "@(#)dir.c 8.1 (Berkeley) 5/31/93";
38 static const char rcsid[] =
39 "$Id: dir.c,v 1.6 1997/02/22 14:01:42 peter Exp $";
43 #include <sys/param.h>
59 /* Directory management. */
61 static struct directory
63 static Char *dfollow __P((Char *));
64 static void printdirs __P((void));
65 static Char *dgoto __P((Char *));
66 static void dnewcwd __P((struct directory *));
67 static void dset __P((Char *));
69 struct directory dhead; /* "head" of loop */
70 int printd; /* force name to be printed */
72 static int dirflag = 0;
75 * dinit - initialize current working directory
84 char path[MAXPATHLEN];
85 static char *emsg = "csh: Trying to start from \"%s\"\n";
87 /* Don't believe the login shell home, because it may be a symlink */
88 tcp = getwd(path); /* see ngetwd.c for System V version */
89 if (tcp == NULL || *tcp == '\0') {
90 (void) fprintf(csherr, "csh: %s: %s\n", path, strerror(errno));
97 (void) fprintf(csherr, emsg, vis_str(hp));
102 (void) fprintf(csherr, emsg, "/");
103 if (chdir("/") == -1)
104 /* I am not even try to print an error message! */
110 struct stat swd, shp;
113 * See if $HOME is the working directory we got and use that
116 stat(tcp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
117 swd.st_dev == shp.st_dev && swd.st_ino == shp.st_ino)
123 * use PWD if we have it (for subshells)
125 if ((cwd = getenv("PWD")) != NULL) {
126 if (stat(cwd, &shp) != -1 && swd.st_dev == shp.st_dev &&
127 swd.st_ino == shp.st_ino)
130 cp = dcanon(SAVE(tcp), STRNULL);
134 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
135 dp->di_name = Strsave(cp);
137 dhead.di_next = dhead.di_prev = dp;
138 dp->di_next = dp->di_prev = &dhead;
148 * Don't call set() directly cause if the directory contains ` or
149 * other junk characters glob will fail.
151 Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
153 vec[0] = Strsave(dp);
155 setq(STRcwd, vec, &shvhed);
171 for (n++; *n != NULL && (*n)[0] == '-'; n++)
172 for (s = &((*n)[1]); *s; s++)
184 stderror(ERR_DIRUS, vis_str(**v), str);
191 * dodirs - list all directories in directory loop
202 stderror(ERR_DIRUS, "dirs", "");
209 struct directory *dp;
210 Char *s, *hp = value(STRhome);
221 if (dirflag & DIR_VERT) {
222 (void) fprintf(cshout, "%d\t", idx++);
225 if (!(dirflag & DIR_LONG) && hp != NULL && !eq(hp, STRslash) &&
226 (len = Strlen(hp), Strncmp(hp, dp->di_name, len) == 0) &&
227 (dp->di_name[len] == '\0' || dp->di_name[len] == '/'))
228 len = Strlen(s = (dp->di_name + len)) + 2;
230 len = Strlen(s = dp->di_name) + 1;
233 if ((dirflag & DIR_LINE) && cur >= 80 - 1 && len < 80) {
234 (void) fprintf(cshout, "\n");
237 (void) fprintf(cshout, s != dp->di_name ? "~%s%c" : "%s%c",
238 vis_str(s), (dirflag & DIR_VERT) ? '\n' : ' ');
239 } while ((dp = dp->di_prev) != dcwd);
240 if (!(dirflag & DIR_VERT))
241 (void) fprintf(cshout, "\n");
249 if (!eq(home, STRslash) && prefix(home, dir))
250 (void) fprintf(cshout, "~%s", vis_str(dir + Strlen(home)));
252 (void) fprintf(cshout, "%s", vis_str(dir));
258 struct directory *d = dcwd;
263 d->di_name = dcanon(d->di_name, STRNULL);
264 } while ((d = d->di_prev) != dcwd);
271 * If the name starts with . or .. then we might need to normalize
272 * it depending on the symbolic link flags
279 #define UC (unsigned char)
280 #define ISDOT(c) (UC(c)[0] == '.' && ((UC(c)[1] == '\0') || (UC(c)[1] == '/')))
281 #define ISDOTDOT(c) (UC(c)[0] == '.' && ISDOT(&((c)[1])))
283 if ((unsigned char) cp[0] == '/')
284 return (Strsave(cp));
286 if (adrof(STRignore_symlinks)) {
290 cwd = (Char *) xmalloc((size_t) ((Strlen(dcwd->di_name) + 3) *
292 (void) Strcpy(cwd, dcwd->di_name);
295 * Ignore . and count ..'s
302 else if (ISDOTDOT(cp)) {
312 if ((dp = Strrchr(cwd, '/'))) {
320 cwd[dotdot = Strlen(cwd)] = '/';
321 cwd[dotdot + 1] = '\0';
322 dp = Strspl(cwd, cp);
338 * dochngd - implement chdir command.
347 struct directory *dp;
349 skipargs(&v, " [<dir>]");
352 if ((cp = value(STRhome)) == NULL || *cp == 0)
353 stderror(ERR_NAME | ERR_NOHOMEDIR);
354 if (chdir(short2str(cp)) < 0)
355 stderror(ERR_NAME | ERR_CANTCHANGE);
358 else if (v[1] != NULL) {
359 stderror(ERR_NAME | ERR_TOOMANY);
363 else if ((dp = dfind(*v)) != 0) {
367 if (chdir(tmp = short2str(dp->di_name)) < 0)
368 stderror(ERR_SYSTEM, tmp, strerror(errno));
369 dcwd->di_prev->di_next = dcwd->di_next;
370 dcwd->di_next->di_prev = dcwd->di_prev;
377 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
380 dp->di_next = dcwd->di_next;
381 dp->di_prev = dcwd->di_prev;
382 dp->di_prev->di_next = dp;
383 dp->di_next->di_prev = dp;
398 for (p = dcwd->di_name; *p++;)
400 if ((cwdlen = p - dcwd->di_name - 1) == 1) /* root */
404 dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
405 for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
410 p--; /* don't add a / after root */
411 for (q = cp; (*p++ = *q++) != '\0';)
425 * dfollow - change to arg directory; fall back on cdpath if not valid
433 char ebuf[MAXPATHLEN];
436 cp = globone(cp, G_ERROR);
438 * if we are ignoring symlinks, try to fix relatives now.
441 if (chdir(short2str(dp)) >= 0) {
447 if (chdir(short2str(cp)) >= 0)
452 if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
453 && (c = adrof(STRcdpath))) {
456 Char buf[MAXPATHLEN];
458 for (cdp = c->vec; *cdp; cdp++) {
459 for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
462 for (p = cp; (*dp++ = *p++) != '\0';)
464 if (chdir(short2str(buf)) >= 0) {
473 if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
479 (void) strcpy(ebuf, short2str(cp));
481 stderror(ERR_SYSTEM, ebuf, strerror(serrno));
487 * dopushd - push new directory onto directory stack.
488 * with no arguments exchange top and second.
489 * with numeric argument (+n) bring it to top.
497 struct directory *dp;
499 skipargs(&v, " [<dir>|+<n>]");
504 if ((dp = dcwd->di_prev) == &dhead)
507 stderror(ERR_NAME | ERR_NODIR);
508 if (chdir(tmp = short2str(dp->di_name)) < 0)
509 stderror(ERR_SYSTEM, tmp, strerror(errno));
510 dp->di_prev->di_next = dp->di_next;
511 dp->di_next->di_prev = dp->di_prev;
512 dp->di_next = dcwd->di_next;
514 dcwd->di_next->di_prev = dp;
517 else if (v[1] != NULL) {
518 stderror(ERR_NAME | ERR_TOOMANY);
522 else if ((dp = dfind(*v)) != NULL) {
525 if (chdir(tmp = short2str(dp->di_name)) < 0)
526 stderror(ERR_SYSTEM, tmp, strerror(errno));
532 dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
536 dp->di_next = dcwd->di_next;
538 dp->di_next->di_prev = dp;
544 * dfind - find a directory if specified by numeric (+n) argument
546 static struct directory *
550 struct directory *dp;
556 for (ep = cp; Isdigit(*ep); ep++)
563 for (dp = dcwd; i != 0; i--) {
564 if ((dp = dp->di_prev) == &dhead)
567 stderror(ERR_NAME | ERR_DEEP);
573 * dopopd - pop a directory out of the directory stack
574 * with a numeric argument just discard it.
582 struct directory *dp, *p = NULL;
584 skipargs(&v, " [+<n>]");
588 else if (v[1] != NULL) {
589 stderror(ERR_NAME | ERR_TOOMANY);
593 else if ((dp = dfind(*v)) == 0)
594 stderror(ERR_NAME | ERR_BADDIR);
595 if (dp->di_prev == &dhead && dp->di_next == &dhead)
596 stderror(ERR_NAME | ERR_EMPTY);
600 if ((p = dp->di_prev) == &dhead)
602 if (chdir(tmp = short2str(p->di_name)) < 0)
603 stderror(ERR_SYSTEM, tmp, strerror(errno));
605 dp->di_prev->di_next = dp->di_next;
606 dp->di_next->di_prev = dp->di_prev;
616 * dfree - free the directory (or keep it if it still has ref count)
620 struct directory *dp;
623 if (dp->di_count != 0) {
624 dp->di_next = dp->di_prev = 0;
627 xfree((char *) dp->di_name);
633 * dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
634 * we are of course assuming that the file system is standardly
635 * constructed (always have ..'s, directories have links)
642 Char *p1, *p2; /* general purpose */
645 Char link[MAXPATHLEN];
646 char tlink[MAXPATHLEN];
651 * christos: if the path given does not start with a slash prepend cwd. If
652 * cwd does not start with a path or the result would be too long abort().
655 Char tmpdir[MAXPATHLEN];
658 if (p1 == NULL || *p1 != '/')
660 if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
662 (void) Strcpy(tmpdir, p1);
663 (void) Strcat(tmpdir, STRslash);
664 (void) Strcat(tmpdir, cp);
666 cp = p = Strsave(tmpdir);
669 while (*p) { /* for each component */
670 sp = p; /* save slash address */
671 while (*++p == '/') /* flush extra slashes */
674 for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
676 p = sp; /* save start of component */
678 while (*++p) /* find next slash or end of path */
685 if (*sp == '\0') /* if component is null */
686 if (--sp == cp) /* if path is one char (i.e. /) */
690 else if (sp[0] == '.' && sp[1] == 0) {
692 for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
699 else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
701 * We have something like "yyy/xxx/..", where "yyy" can be null or
702 * a path starting at /, and "xxx" is a single component. Before
703 * compressing "xxx/..", we want to expand "yyy/xxx", if it is a
706 *--sp = 0; /* form the pathname for readlink */
707 if (sp != cp && !adrof(STRignore_symlinks) &&
708 (cc = readlink(short2str(cp), tlink,
709 sizeof tlink)) >= 0) {
710 (void) Strcpy(link, str2short(tlink));
716 * Point p to the '/' in "/..", and restore the '/'.
726 * Relative path, expand it between the "yyy/" and the
727 * "/..". First, back sp up to the character past "yyy/".
734 * New length is "yyy/" + link + "/.." and rest
736 p1 = newcp = (Char *) xmalloc((size_t)
737 (((sp - cp) + cc + (p1 - p)) *
740 * Copy new path into newcp
742 for (p2 = cp; (*p1++ = *p2++) != '\0';)
744 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
746 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
749 * Restart canonicalization at expanded "/xxx".
751 p = sp - cp - 1 + newcp;
755 * New length is link + "/.." and rest
757 p1 = newcp = (Char *) xmalloc((size_t)
758 ((cc + (p1 - p)) * sizeof(Char)));
760 * Copy new path into newcp
762 for (p2 = link; (*p1++ = *p2++) != '\0';)
764 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
767 * Restart canonicalization at beginning
773 continue; /* canonicalize the link */
780 for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
789 else { /* normal dir name (not . or .. or nothing) */
791 if (sp != cp && adrof(STRchase_symlinks) &&
792 !adrof(STRignore_symlinks) &&
793 (cc = readlink(short2str(cp), tlink,
794 sizeof tlink)) >= 0) {
795 (void) Strcpy(link, str2short(tlink));
805 * point sp to p (rather than backing up).
816 * Relative path, expand it between the "yyy/" and the
817 * remainder. First, back sp up to the character past
825 * New length is "yyy/" + link + "/.." and rest
827 p1 = newcp = (Char *) xmalloc((size_t)
828 (((sp - cp) + cc + (p1 - p))
831 * Copy new path into newcp
833 for (p2 = cp; (*p1++ = *p2++) != '\0';)
835 for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
837 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
840 * Restart canonicalization at expanded "/xxx".
842 p = sp - cp - 1 + newcp;
846 * New length is link + the rest
848 p1 = newcp = (Char *) xmalloc((size_t)
849 ((cc + (p1 - p)) * sizeof(Char)));
851 * Copy new path into newcp
853 for (p2 = link; (*p1++ = *p2++) != '\0';)
855 for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
858 * Restart canonicalization at beginning
864 continue; /* canonicalize the link */
877 * See if we're not in a subdir of STRhome
879 if (p1 && *p1 == '/' &&
880 (Strncmp(p1, cp, cc) != 0 || (cp[cc] != '/' && cp[cc] != '\0'))) {
881 static ino_t home_ino = -1;
882 static dev_t home_dev = -1;
883 static Char *home_ptr = NULL;
887 * Get dev and ino of STRhome
889 if (home_ptr != p1 &&
890 stat(short2str(p1), &statbuf) != -1) {
891 home_dev = statbuf.st_dev;
892 home_ino = statbuf.st_ino;
896 * Start comparing dev & ino backwards
898 p2 = Strcpy(link, cp);
899 for (sp = NULL; *p2 && stat(short2str(p2), &statbuf) != -1;) {
900 if (statbuf.st_dev == home_dev &&
901 statbuf.st_ino == home_ino) {
905 if ((sp = Strrchr(p2, '/')) != NULL)
911 if (*p2 && sp == (Char *) -1) {
913 * Use STRhome to make '~' work
915 newcp = Strspl(p1, cp + Strlen(p2));
925 * dnewcwd - make a new directory in the loop the current one
929 struct directory *dp;
933 if (printd && !(adrof(STRpushdsilent)))