147823Sbostic /*-
260765Sbostic * Copyright (c) 1980, 1991, 1993
360765Sbostic * The Regents of the University of California. All rights reserved.
447823Sbostic *
547823Sbostic * %sccs.include.redist.c%
621927Sdist */
721927Sdist
817517Sedward #ifndef lint
9*69126Schristos static char sccsid[] = "@(#)dir.c 8.2 (Berkeley) 04/29/95";
1047823Sbostic #endif /* not lint */
111288Sbill
1250028Sbostic #include <sys/param.h>
1350028Sbostic #include <sys/stat.h>
1450028Sbostic #include <errno.h>
1550028Sbostic #include <stdlib.h>
1650028Sbostic #include <string.h>
1750028Sbostic #include <unistd.h>
1850033Schristos #if __STDC__
1950033Schristos # include <stdarg.h>
2050033Schristos #else
2150033Schristos # include <varargs.h>
2250033Schristos #endif
2350028Sbostic
2450023Sbostic #include "csh.h"
2550023Sbostic #include "dir.h"
2650023Sbostic #include "extern.h"
271288Sbill
2850023Sbostic /* Directory management. */
2950023Sbostic
3051437Sleres static struct directory
3150024Schristos *dfind __P((Char *));
3251437Sleres static Char *dfollow __P((Char *));
3351437Sleres static void printdirs __P((void));
3451437Sleres static Char *dgoto __P((Char *));
3551437Sleres static void dnewcwd __P((struct directory *));
3651437Sleres static void dset __P((Char *));
371288Sbill
3849992Sbostic struct directory dhead; /* "head" of loop */
3949992Sbostic int printd; /* force name to be printed */
4049992Sbostic
4149992Sbostic static int dirflag = 0;
4249992Sbostic
431288Sbill /*
441288Sbill * dinit - initialize current working directory
451288Sbill */
4649992Sbostic void
dinit(hp)471288Sbill dinit(hp)
4849992Sbostic Char *hp;
491288Sbill {
5049992Sbostic register char *tcp;
5149992Sbostic register Char *cp;
5249992Sbostic register struct directory *dp;
5349992Sbostic char path[MAXPATHLEN];
5449992Sbostic static char *emsg = "csh: Trying to start from \"%s\"\n";
551288Sbill
5649992Sbostic /* Don't believe the login shell home, because it may be a symlink */
57*69126Schristos tcp = getcwd(path, MAXPATHLEN);
5849992Sbostic if (tcp == NULL || *tcp == '\0') {
59*69126Schristos (void) fprintf(csherr, "csh: %s\n", strerror(errno));
6049992Sbostic if (hp && *hp) {
6149992Sbostic tcp = short2str(hp);
6249992Sbostic if (chdir(tcp) == -1)
6350024Schristos cp = NULL;
6449992Sbostic else
651288Sbill cp = hp;
6651589Schristos (void) fprintf(csherr, emsg, vis_str(hp));
6749992Sbostic }
6849992Sbostic else
6950024Schristos cp = NULL;
7050024Schristos if (cp == NULL) {
7150439Schristos (void) fprintf(csherr, emsg, "/");
7249992Sbostic if (chdir("/") == -1)
7349992Sbostic /* I am not even try to print an error message! */
7449992Sbostic xexit(1);
7549992Sbostic cp = SAVE("/");
7649992Sbostic }
7749992Sbostic }
7849992Sbostic else {
7949992Sbostic struct stat swd, shp;
8049992Sbostic
8149992Sbostic /*
8249992Sbostic * See if $HOME is the working directory we got and use that
8349992Sbostic */
8449992Sbostic if (hp && *hp &&
8549992Sbostic stat(tcp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
8649992Sbostic swd.st_dev == shp.st_dev && swd.st_ino == shp.st_ino)
8749992Sbostic cp = hp;
8810703Ssam else {
8949992Sbostic char *cwd;
9049992Sbostic
9149992Sbostic /*
9249992Sbostic * use PWD if we have it (for subshells)
9349992Sbostic */
9460237Schristos if ((cwd = getenv("PWD")) != NULL) {
9549992Sbostic if (stat(cwd, &shp) != -1 && swd.st_dev == shp.st_dev &&
9649992Sbostic swd.st_ino == shp.st_ino)
9749992Sbostic tcp = cwd;
9849992Sbostic }
9951419Schristos cp = dcanon(SAVE(tcp), STRNULL);
10010703Ssam }
10149992Sbostic }
10249992Sbostic
10349992Sbostic dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
10449992Sbostic dp->di_name = Strsave(cp);
10549992Sbostic dp->di_count = 0;
10649992Sbostic dhead.di_next = dhead.di_prev = dp;
10749992Sbostic dp->di_next = dp->di_prev = &dhead;
10849992Sbostic printd = 0;
10949992Sbostic dnewcwd(dp);
1101288Sbill }
1111288Sbill
11249992Sbostic static void
dset(dp)11349992Sbostic dset(dp)
11449992Sbostic Char *dp;
11549992Sbostic {
11649992Sbostic /*
11749992Sbostic * Don't call set() directly cause if the directory contains ` or
11851437Sleres * other junk characters glob will fail.
11949992Sbostic */
12049992Sbostic register Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
12149992Sbostic
12249992Sbostic vec[0] = Strsave(dp);
12349992Sbostic vec[1] = 0;
12449992Sbostic setq(STRcwd, vec, &shvhed);
12549992Sbostic Setenv(STRPWD, dp);
12649992Sbostic }
12749992Sbostic
12849992Sbostic #define DIR_LONG 1
12949992Sbostic #define DIR_VERT 2
13049992Sbostic #define DIR_LINE 4
13149992Sbostic
13249992Sbostic static void
skipargs(v,str)13349992Sbostic skipargs(v, str)
13449992Sbostic Char ***v;
13549992Sbostic char *str;
13649992Sbostic {
13749992Sbostic Char **n = *v, *s;
13849992Sbostic
13949992Sbostic dirflag = 0;
14049992Sbostic for (n++; *n != NULL && (*n)[0] == '-'; n++)
14149992Sbostic for (s = &((*n)[1]); *s; s++)
14249992Sbostic switch (*s) {
14349992Sbostic case 'l':
14449992Sbostic dirflag |= DIR_LONG;
14549992Sbostic break;
14649992Sbostic case 'v':
14749992Sbostic dirflag |= DIR_VERT;
14849992Sbostic break;
14949992Sbostic case 'n':
15049992Sbostic dirflag |= DIR_LINE;
15149992Sbostic break;
15249992Sbostic default:
15351589Schristos stderror(ERR_DIRUS, vis_str(**v), str);
15449992Sbostic break;
15549992Sbostic }
15649992Sbostic *v = n;
15749992Sbostic }
15849992Sbostic
1591288Sbill /*
1601288Sbill * dodirs - list all directories in directory loop
1611288Sbill */
16249992Sbostic void
16350439Schristos /*ARGSUSED*/
dodirs(v,t)16450439Schristos dodirs(v, t)
16550439Schristos Char **v;
16650439Schristos struct command *t;
1671288Sbill {
16849992Sbostic skipargs(&v, "");
1691288Sbill
17049992Sbostic if (*v != NULL)
17149992Sbostic stderror(ERR_DIRUS, "dirs", "");
17249992Sbostic printdirs();
17349992Sbostic }
17449992Sbostic
17549992Sbostic static void
printdirs()17649992Sbostic printdirs()
17749992Sbostic {
17849992Sbostic register struct directory *dp;
17949992Sbostic Char *s, *hp = value(STRhome);
18049992Sbostic int idx, len, cur;
18149992Sbostic
18249992Sbostic if (*hp == '\0')
18349992Sbostic hp = NULL;
18449992Sbostic dp = dcwd;
18549992Sbostic idx = 0;
18649992Sbostic cur = 0;
18749992Sbostic do {
18849992Sbostic if (dp == &dhead)
18949992Sbostic continue;
19049992Sbostic if (dirflag & DIR_VERT) {
19150439Schristos (void) fprintf(cshout, "%d\t", idx++);
19249992Sbostic cur = 0;
19349992Sbostic }
19449992Sbostic if (!(dirflag & DIR_LONG) && hp != NULL && !eq(hp, STRslash) &&
19558560Sralph (len = Strlen(hp), Strncmp(hp, dp->di_name, len) == 0) &&
19652476Schristos (dp->di_name[len] == '\0' || dp->di_name[len] == '/'))
19752476Schristos len = Strlen(s = (dp->di_name + len)) + 2;
1981288Sbill else
19949992Sbostic len = Strlen(s = dp->di_name) + 1;
20049992Sbostic
20149992Sbostic cur += len;
20249992Sbostic if ((dirflag & DIR_LINE) && cur >= 80 - 1 && len < 80) {
20350439Schristos (void) fprintf(cshout, "\n");
20449992Sbostic cur = len;
20549992Sbostic }
20650439Schristos (void) fprintf(cshout, s != dp->di_name ? "~%s%c" : "%s%c",
20751589Schristos vis_str(s), (dirflag & DIR_VERT) ? '\n' : ' ');
20849992Sbostic } while ((dp = dp->di_prev) != dcwd);
20949992Sbostic if (!(dirflag & DIR_VERT))
21050439Schristos (void) fprintf(cshout, "\n");
2111288Sbill }
2121288Sbill
21349992Sbostic void
dtildepr(home,dir)2141288Sbill dtildepr(home, dir)
21549992Sbostic register Char *home, *dir;
2161288Sbill {
2171288Sbill
21849992Sbostic if (!eq(home, STRslash) && prefix(home, dir))
21951589Schristos (void) fprintf(cshout, "~%s", vis_str(dir + Strlen(home)));
22049992Sbostic else
22151589Schristos (void) fprintf(cshout, "%s", vis_str(dir));
2221288Sbill }
2231288Sbill
22449992Sbostic void
dtilde()22549992Sbostic dtilde()
22649992Sbostic {
22749992Sbostic struct directory *d = dcwd;
22849992Sbostic
22949992Sbostic do {
23049992Sbostic if (d == &dhead)
23149992Sbostic continue;
23249992Sbostic d->di_name = dcanon(d->di_name, STRNULL);
23349992Sbostic } while ((d = d->di_prev) != dcwd);
23449992Sbostic
23549992Sbostic dset(dcwd->di_name);
23649992Sbostic }
23749992Sbostic
23849992Sbostic
23949992Sbostic /* dnormalize():
24049992Sbostic * If the name starts with . or .. then we might need to normalize
24149992Sbostic * it depending on the symbolic link flags
24249992Sbostic */
24349992Sbostic Char *
dnormalize(cp)24449992Sbostic dnormalize(cp)
24549992Sbostic Char *cp;
24649992Sbostic {
24749992Sbostic
24849992Sbostic #define UC (unsigned char)
24949992Sbostic #define ISDOT(c) (UC(c)[0] == '.' && ((UC(c)[1] == '\0') || (UC(c)[1] == '/')))
25049992Sbostic #define ISDOTDOT(c) (UC(c)[0] == '.' && ISDOT(&((c)[1])))
25149992Sbostic
25249992Sbostic if ((unsigned char) cp[0] == '/')
25349992Sbostic return (Strsave(cp));
25449992Sbostic
25549992Sbostic if (adrof(STRignore_symlinks)) {
25649992Sbostic int dotdot = 0;
25749992Sbostic Char *dp, *cwd;
25849992Sbostic
25949992Sbostic cwd = (Char *) xmalloc((size_t) ((Strlen(dcwd->di_name) + 3) *
26049992Sbostic sizeof(Char)));
26149992Sbostic (void) Strcpy(cwd, dcwd->di_name);
26249992Sbostic
26349992Sbostic /*
26449992Sbostic * Ignore . and count ..'s
26549992Sbostic */
26649992Sbostic while (*cp) {
26749992Sbostic if (ISDOT(cp)) {
26849992Sbostic if (*++cp)
26949992Sbostic cp++;
27049992Sbostic }
27149992Sbostic else if (ISDOTDOT(cp)) {
27249992Sbostic dotdot++;
27349992Sbostic cp += 2;
27449992Sbostic if (*cp)
27549992Sbostic cp++;
27649992Sbostic }
27749992Sbostic else
27849992Sbostic break;
27949992Sbostic }
28049992Sbostic while (dotdot > 0)
28149992Sbostic if ((dp = Strrchr(cwd, '/'))) {
28249992Sbostic *dp = '\0';
28349992Sbostic dotdot--;
28449992Sbostic }
28549992Sbostic else
28649992Sbostic break;
28749992Sbostic
28849992Sbostic if (*cp) {
28949992Sbostic cwd[dotdot = Strlen(cwd)] = '/';
29049992Sbostic cwd[dotdot + 1] = '\0';
29149992Sbostic dp = Strspl(cwd, cp);
29249992Sbostic xfree((ptr_t) cwd);
29349992Sbostic return dp;
29449992Sbostic }
29549992Sbostic else {
29649992Sbostic if (!*cwd) {
29749992Sbostic cwd[0] = '/';
29849992Sbostic cwd[1] = '\0';
29949992Sbostic }
30049992Sbostic return cwd;
30149992Sbostic }
30249992Sbostic }
30349992Sbostic return Strsave(cp);
30449992Sbostic }
30549992Sbostic
3061288Sbill /*
3071288Sbill * dochngd - implement chdir command.
3081288Sbill */
30949992Sbostic void
31050439Schristos /*ARGSUSED*/
dochngd(v,t)31150439Schristos dochngd(v, t)
31250439Schristos Char **v;
31350439Schristos struct command *t;
3141288Sbill {
31549992Sbostic register Char *cp;
31649992Sbostic register struct directory *dp;
3171288Sbill
31849992Sbostic skipargs(&v, " [<dir>]");
31949992Sbostic printd = 0;
32049992Sbostic if (*v == NULL) {
32149992Sbostic if ((cp = value(STRhome)) == NULL || *cp == 0)
32249992Sbostic stderror(ERR_NAME | ERR_NOHOMEDIR);
32349992Sbostic if (chdir(short2str(cp)) < 0)
32449992Sbostic stderror(ERR_NAME | ERR_CANTCHANGE);
32549992Sbostic cp = Strsave(cp);
32649992Sbostic }
32749992Sbostic else if (v[1] != NULL) {
32849992Sbostic stderror(ERR_NAME | ERR_TOOMANY);
32949992Sbostic /* NOTREACHED */
33049992Sbostic return;
33149992Sbostic }
33249992Sbostic else if ((dp = dfind(*v)) != 0) {
33349992Sbostic char *tmp;
33449992Sbostic
33549992Sbostic printd = 1;
33649992Sbostic if (chdir(tmp = short2str(dp->di_name)) < 0)
33749992Sbostic stderror(ERR_SYSTEM, tmp, strerror(errno));
33849992Sbostic dcwd->di_prev->di_next = dcwd->di_next;
33949992Sbostic dcwd->di_next->di_prev = dcwd->di_prev;
3401288Sbill dfree(dcwd);
3411288Sbill dnewcwd(dp);
34249992Sbostic return;
34349992Sbostic }
34449992Sbostic else
34549992Sbostic cp = dfollow(*v);
34649992Sbostic dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
34749992Sbostic dp->di_name = cp;
34849992Sbostic dp->di_count = 0;
34949992Sbostic dp->di_next = dcwd->di_next;
35049992Sbostic dp->di_prev = dcwd->di_prev;
35149992Sbostic dp->di_prev->di_next = dp;
35249992Sbostic dp->di_next->di_prev = dp;
35349992Sbostic dfree(dcwd);
35449992Sbostic dnewcwd(dp);
3551288Sbill }
3561288Sbill
35749992Sbostic static Char *
dgoto(cp)35849992Sbostic dgoto(cp)
35949992Sbostic Char *cp;
36049992Sbostic {
36149992Sbostic Char *dp;
36249992Sbostic
36349992Sbostic if (*cp != '/') {
36449992Sbostic register Char *p, *q;
36549992Sbostic int cwdlen;
36649992Sbostic
36751437Sleres for (p = dcwd->di_name; *p++;)
36851437Sleres continue;
36949992Sbostic if ((cwdlen = p - dcwd->di_name - 1) == 1) /* root */
37049992Sbostic cwdlen = 0;
37151437Sleres for (p = cp; *p++;)
37251437Sleres continue;
37349992Sbostic dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
37460237Schristos for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
37551437Sleres continue;
37649992Sbostic if (cwdlen)
37749992Sbostic p[-1] = '/';
37849992Sbostic else
37949992Sbostic p--; /* don't add a / after root */
38060237Schristos for (q = cp; (*p++ = *q++) != '\0';)
38151437Sleres continue;
38249992Sbostic xfree((ptr_t) cp);
38349992Sbostic cp = dp;
38449992Sbostic dp += cwdlen;
38549992Sbostic }
38649992Sbostic else
38749992Sbostic dp = cp;
38849992Sbostic
38949992Sbostic cp = dcanon(cp, dp);
39049992Sbostic return cp;
39149992Sbostic }
39249992Sbostic
3931288Sbill /*
3941288Sbill * dfollow - change to arg directory; fall back on cdpath if not valid
3951288Sbill */
39649992Sbostic static Char *
dfollow(cp)3971288Sbill dfollow(cp)
39849992Sbostic register Char *cp;
3991288Sbill {
40049992Sbostic register Char *dp;
40149992Sbostic struct varent *c;
40249992Sbostic char ebuf[MAXPATHLEN];
40350161Schristos int serrno;
40419912Sedward
40549992Sbostic cp = globone(cp, G_ERROR);
40649992Sbostic /*
40749992Sbostic * if we are ignoring symlinks, try to fix relatives now.
40849992Sbostic */
40949992Sbostic dp = dnormalize(cp);
41049992Sbostic if (chdir(short2str(dp)) >= 0) {
41149992Sbostic xfree((ptr_t) cp);
41249992Sbostic return dgoto(dp);
41349992Sbostic }
41449992Sbostic else {
41549992Sbostic xfree((ptr_t) dp);
41649992Sbostic if (chdir(short2str(cp)) >= 0)
41749992Sbostic return dgoto(cp);
41850161Schristos serrno = errno;
41949992Sbostic }
42019912Sedward
42149992Sbostic if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
42249992Sbostic && (c = adrof(STRcdpath))) {
42349992Sbostic Char **cdp;
42449992Sbostic register Char *p;
42549992Sbostic Char buf[MAXPATHLEN];
42649992Sbostic
42749992Sbostic for (cdp = c->vec; *cdp; cdp++) {
42860237Schristos for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
42951437Sleres continue;
43049992Sbostic dp[-1] = '/';
43160237Schristos for (p = cp; (*dp++ = *p++) != '\0';)
43251437Sleres continue;
43349992Sbostic if (chdir(short2str(buf)) >= 0) {
43419912Sedward printd = 1;
43549992Sbostic xfree((ptr_t) cp);
43649992Sbostic cp = Strsave(buf);
43749992Sbostic return dgoto(cp);
43849992Sbostic }
4391288Sbill }
44049992Sbostic }
44149992Sbostic dp = value(cp);
44249992Sbostic if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
44349992Sbostic xfree((ptr_t) cp);
44449992Sbostic cp = Strsave(dp);
44549992Sbostic printd = 1;
44649992Sbostic return dgoto(cp);
44749992Sbostic }
44849992Sbostic (void) strcpy(ebuf, short2str(cp));
44949992Sbostic xfree((ptr_t) cp);
45050161Schristos stderror(ERR_SYSTEM, ebuf, strerror(serrno));
45149992Sbostic return (NULL);
45249992Sbostic }
4531288Sbill
45419912Sedward
4551288Sbill /*
4561288Sbill * dopushd - push new directory onto directory stack.
4571288Sbill * with no arguments exchange top and second.
4581288Sbill * with numeric argument (+n) bring it to top.
4591288Sbill */
46049992Sbostic void
46150439Schristos /*ARGSUSED*/
dopushd(v,t)46250439Schristos dopushd(v, t)
46350439Schristos Char **v;
46450439Schristos struct command *t;
4651288Sbill {
46649992Sbostic register struct directory *dp;
4671288Sbill
46849992Sbostic skipargs(&v, " [<dir>|+<n>]");
46949992Sbostic printd = 1;
47049992Sbostic if (*v == NULL) {
47149992Sbostic char *tmp;
4721288Sbill
47349992Sbostic if ((dp = dcwd->di_prev) == &dhead)
47449992Sbostic dp = dhead.di_prev;
47549992Sbostic if (dp == dcwd)
47649992Sbostic stderror(ERR_NAME | ERR_NODIR);
47749992Sbostic if (chdir(tmp = short2str(dp->di_name)) < 0)
47849992Sbostic stderror(ERR_SYSTEM, tmp, strerror(errno));
47949992Sbostic dp->di_prev->di_next = dp->di_next;
48049992Sbostic dp->di_next->di_prev = dp->di_prev;
48149992Sbostic dp->di_next = dcwd->di_next;
48249992Sbostic dp->di_prev = dcwd;
48349992Sbostic dcwd->di_next->di_prev = dp;
48449992Sbostic dcwd->di_next = dp;
48549992Sbostic }
48649992Sbostic else if (v[1] != NULL) {
48749992Sbostic stderror(ERR_NAME | ERR_TOOMANY);
48849992Sbostic /* NOTREACHED */
48949992Sbostic return;
49049992Sbostic }
49160237Schristos else if ((dp = dfind(*v)) != NULL) {
49249992Sbostic char *tmp;
49349992Sbostic
49449992Sbostic if (chdir(tmp = short2str(dp->di_name)) < 0)
49549992Sbostic stderror(ERR_SYSTEM, tmp, strerror(errno));
49649992Sbostic }
49749992Sbostic else {
49849992Sbostic register Char *ccp;
49949992Sbostic
50049992Sbostic ccp = dfollow(*v);
50149992Sbostic dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
50249992Sbostic dp->di_name = ccp;
50349992Sbostic dp->di_count = 0;
50449992Sbostic dp->di_prev = dcwd;
50549992Sbostic dp->di_next = dcwd->di_next;
50649992Sbostic dcwd->di_next = dp;
50749992Sbostic dp->di_next->di_prev = dp;
50849992Sbostic }
50949992Sbostic dnewcwd(dp);
5101288Sbill }
5111288Sbill
5121288Sbill /*
5131288Sbill * dfind - find a directory if specified by numeric (+n) argument
5141288Sbill */
51549992Sbostic static struct directory *
dfind(cp)5161288Sbill dfind(cp)
51749992Sbostic register Char *cp;
5181288Sbill {
51949992Sbostic register struct directory *dp;
52049992Sbostic register int i;
52149992Sbostic register Char *ep;
5221288Sbill
52349992Sbostic if (*cp++ != '+')
52449992Sbostic return (0);
52549992Sbostic for (ep = cp; Isdigit(*ep); ep++)
52649992Sbostic continue;
52749992Sbostic if (*ep)
52849992Sbostic return (0);
52949992Sbostic i = getn(cp);
53049992Sbostic if (i <= 0)
53149992Sbostic return (0);
53249992Sbostic for (dp = dcwd; i != 0; i--) {
53349992Sbostic if ((dp = dp->di_prev) == &dhead)
53449992Sbostic dp = dp->di_prev;
53549992Sbostic if (dp == dcwd)
53649992Sbostic stderror(ERR_NAME | ERR_DEEP);
53749992Sbostic }
53849992Sbostic return (dp);
5391288Sbill }
5401288Sbill
5411288Sbill /*
5421288Sbill * dopopd - pop a directory out of the directory stack
5431288Sbill * with a numeric argument just discard it.
5441288Sbill */
54549992Sbostic void
54650439Schristos /*ARGSUSED*/
dopopd(v,t)54750439Schristos dopopd(v, t)
54850439Schristos Char **v;
54950439Schristos struct command *t;
5501288Sbill {
55149992Sbostic register struct directory *dp, *p = NULL;
5521288Sbill
55349992Sbostic skipargs(&v, " [+<n>]");
55449992Sbostic printd = 1;
55549992Sbostic if (*v == NULL)
55649992Sbostic dp = dcwd;
55749992Sbostic else if (v[1] != NULL) {
55849992Sbostic stderror(ERR_NAME | ERR_TOOMANY);
55949992Sbostic /* NOTREACHED */
56049992Sbostic return;
56149992Sbostic }
56249992Sbostic else if ((dp = dfind(*v)) == 0)
56349992Sbostic stderror(ERR_NAME | ERR_BADDIR);
56449992Sbostic if (dp->di_prev == &dhead && dp->di_next == &dhead)
56549992Sbostic stderror(ERR_NAME | ERR_EMPTY);
56649992Sbostic if (dp == dcwd) {
56749992Sbostic char *tmp;
56849992Sbostic
56949992Sbostic if ((p = dp->di_prev) == &dhead)
57049992Sbostic p = dhead.di_prev;
57149992Sbostic if (chdir(tmp = short2str(p->di_name)) < 0)
57249992Sbostic stderror(ERR_SYSTEM, tmp, strerror(errno));
57349992Sbostic }
57449992Sbostic dp->di_prev->di_next = dp->di_next;
57549992Sbostic dp->di_next->di_prev = dp->di_prev;
57649992Sbostic if (dp == dcwd)
57749992Sbostic dnewcwd(p);
57849992Sbostic else {
57949992Sbostic printdirs();
58049992Sbostic }
58149992Sbostic dfree(dp);
5821288Sbill }
5831288Sbill
5841288Sbill /*
5851288Sbill * dfree - free the directory (or keep it if it still has ref count)
5861288Sbill */
58749992Sbostic void
dfree(dp)5881288Sbill dfree(dp)
58949992Sbostic register struct directory *dp;
5901288Sbill {
5911288Sbill
59249992Sbostic if (dp->di_count != 0) {
59349992Sbostic dp->di_next = dp->di_prev = 0;
59449992Sbostic }
59549992Sbostic else {
59649992Sbostic xfree((char *) dp->di_name);
59749992Sbostic xfree((ptr_t) dp);
59849992Sbostic }
5991288Sbill }
6001288Sbill
6011288Sbill /*
6021288Sbill * dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
6031288Sbill * we are of course assuming that the file system is standardly
6041288Sbill * constructed (always have ..'s, directories have links)
6051288Sbill */
60649992Sbostic Char *
dcanon(cp,p)60719912Sedward dcanon(cp, p)
60849992Sbostic register Char *cp, *p;
6091288Sbill {
61049992Sbostic register Char *sp;
61149992Sbostic register Char *p1, *p2; /* general purpose */
61249992Sbostic bool slash;
6131288Sbill
61449992Sbostic Char link[MAXPATHLEN];
61549992Sbostic char tlink[MAXPATHLEN];
61649992Sbostic int cc;
61749992Sbostic Char *newcp;
61819912Sedward
61949992Sbostic /*
62049992Sbostic * christos: if the path given does not start with a slash prepend cwd. If
62149992Sbostic * cwd does not start with a path or the result would be too long abort().
62249992Sbostic */
62349992Sbostic if (*cp != '/') {
62449992Sbostic Char tmpdir[MAXPATHLEN];
62549992Sbostic
62649992Sbostic p1 = value(STRcwd);
62750024Schristos if (p1 == NULL || *p1 != '/')
62849992Sbostic abort();
62949992Sbostic if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
63049992Sbostic abort();
63149992Sbostic (void) Strcpy(tmpdir, p1);
63249992Sbostic (void) Strcat(tmpdir, STRslash);
63349992Sbostic (void) Strcat(tmpdir, cp);
63449992Sbostic xfree((ptr_t) cp);
63549992Sbostic cp = p = Strsave(tmpdir);
63649992Sbostic }
63749992Sbostic
63849992Sbostic while (*p) { /* for each component */
63949992Sbostic sp = p; /* save slash address */
64049992Sbostic while (*++p == '/') /* flush extra slashes */
64151437Sleres continue;
64249992Sbostic if (p != ++sp)
64360237Schristos for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
64451437Sleres continue;
64549992Sbostic p = sp; /* save start of component */
64649992Sbostic slash = 0;
64749992Sbostic while (*++p) /* find next slash or end of path */
64849992Sbostic if (*p == '/') {
64949992Sbostic slash = 1;
65049992Sbostic *p = 0;
65149992Sbostic break;
65249992Sbostic }
65349992Sbostic
65449992Sbostic if (*sp == '\0') /* if component is null */
65549992Sbostic if (--sp == cp) /* if path is one char (i.e. /) */
65649992Sbostic break;
65749992Sbostic else
65849992Sbostic *sp = '\0';
65949992Sbostic else if (sp[0] == '.' && sp[1] == 0) {
66049992Sbostic if (slash) {
66160237Schristos for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
66251437Sleres continue;
66349992Sbostic p = --sp;
66449992Sbostic }
66549992Sbostic else if (--sp != cp)
66649992Sbostic *sp = '\0';
6671288Sbill }
66849992Sbostic else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
66949992Sbostic /*
67049992Sbostic * We have something like "yyy/xxx/..", where "yyy" can be null or
67149992Sbostic * a path starting at /, and "xxx" is a single component. Before
67249992Sbostic * compressing "xxx/..", we want to expand "yyy/xxx", if it is a
67349992Sbostic * symbolic link.
67449992Sbostic */
67549992Sbostic *--sp = 0; /* form the pathname for readlink */
67649992Sbostic if (sp != cp && !adrof(STRignore_symlinks) &&
67749992Sbostic (cc = readlink(short2str(cp), tlink,
67849992Sbostic sizeof tlink)) >= 0) {
67949992Sbostic (void) Strcpy(link, str2short(tlink));
68049992Sbostic link[cc] = '\0';
68149992Sbostic
68249992Sbostic if (slash)
68349992Sbostic *p = '/';
68449992Sbostic /*
68549992Sbostic * Point p to the '/' in "/..", and restore the '/'.
68649992Sbostic */
68749992Sbostic *(p = sp) = '/';
68849992Sbostic /*
68949992Sbostic * find length of p
69049992Sbostic */
69151437Sleres for (p1 = p; *p1++;)
69251437Sleres continue;
69349992Sbostic if (*link != '/') {
69449992Sbostic /*
69549992Sbostic * Relative path, expand it between the "yyy/" and the
69649992Sbostic * "/..". First, back sp up to the character past "yyy/".
69749992Sbostic */
69851437Sleres while (*--sp != '/')
69951437Sleres continue;
70049992Sbostic sp++;
70149992Sbostic *sp = 0;
70249992Sbostic /*
70349992Sbostic * New length is "yyy/" + link + "/.." and rest
70449992Sbostic */
70549992Sbostic p1 = newcp = (Char *) xmalloc((size_t)
70649992Sbostic (((sp - cp) + cc + (p1 - p)) *
70749992Sbostic sizeof(Char)));
70849992Sbostic /*
70949992Sbostic * Copy new path into newcp
71049992Sbostic */
71160237Schristos for (p2 = cp; (*p1++ = *p2++) != '\0';)
71251437Sleres continue;
71360237Schristos for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
71451437Sleres continue;
71560237Schristos for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
71651437Sleres continue;
71749992Sbostic /*
71849992Sbostic * Restart canonicalization at expanded "/xxx".
71949992Sbostic */
72049992Sbostic p = sp - cp - 1 + newcp;
72149992Sbostic }
72249992Sbostic else {
72349992Sbostic /*
72449992Sbostic * New length is link + "/.." and rest
72549992Sbostic */
72649992Sbostic p1 = newcp = (Char *) xmalloc((size_t)
72749992Sbostic ((cc + (p1 - p)) * sizeof(Char)));
72849992Sbostic /*
72949992Sbostic * Copy new path into newcp
73049992Sbostic */
73160237Schristos for (p2 = link; (*p1++ = *p2++) != '\0';)
73251437Sleres continue;
73360237Schristos for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
73451437Sleres continue;
73549992Sbostic /*
73649992Sbostic * Restart canonicalization at beginning
73749992Sbostic */
73849992Sbostic p = newcp;
73949992Sbostic }
74049992Sbostic xfree((ptr_t) cp);
74149992Sbostic cp = newcp;
74249992Sbostic continue; /* canonicalize the link */
74349992Sbostic }
74449992Sbostic *sp = '/';
74549992Sbostic if (sp != cp)
74651437Sleres while (*--sp != '/')
74751437Sleres continue;
74849992Sbostic if (slash) {
74960237Schristos for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
75051437Sleres continue;
75149992Sbostic p = sp;
75249992Sbostic }
75349992Sbostic else if (cp == sp)
75449992Sbostic *++sp = '\0';
75549992Sbostic else
75649992Sbostic *sp = '\0';
75749992Sbostic }
75849992Sbostic else { /* normal dir name (not . or .. or nothing) */
75949992Sbostic
76049992Sbostic if (sp != cp && adrof(STRchase_symlinks) &&
76149992Sbostic !adrof(STRignore_symlinks) &&
76249992Sbostic (cc = readlink(short2str(cp), tlink,
76349992Sbostic sizeof tlink)) >= 0) {
76449992Sbostic (void) Strcpy(link, str2short(tlink));
76549992Sbostic link[cc] = '\0';
76649992Sbostic
76749992Sbostic /*
76849992Sbostic * restore the '/'.
76949992Sbostic */
77049992Sbostic if (slash)
77149992Sbostic *p = '/';
77249992Sbostic
77349992Sbostic /*
77449992Sbostic * point sp to p (rather than backing up).
77549992Sbostic */
77649992Sbostic sp = p;
77749992Sbostic
77849992Sbostic /*
77949992Sbostic * find length of p
78049992Sbostic */
78151437Sleres for (p1 = p; *p1++;)
78251437Sleres continue;
78349992Sbostic if (*link != '/') {
78449992Sbostic /*
78549992Sbostic * Relative path, expand it between the "yyy/" and the
78649992Sbostic * remainder. First, back sp up to the character past
78749992Sbostic * "yyy/".
78849992Sbostic */
78951437Sleres while (*--sp != '/')
79051437Sleres continue;
79149992Sbostic sp++;
79249992Sbostic *sp = 0;
79349992Sbostic /*
79449992Sbostic * New length is "yyy/" + link + "/.." and rest
79549992Sbostic */
79649992Sbostic p1 = newcp = (Char *) xmalloc((size_t)
79749992Sbostic (((sp - cp) + cc + (p1 - p))
79849992Sbostic * sizeof(Char)));
79949992Sbostic /*
80049992Sbostic * Copy new path into newcp
80149992Sbostic */
80260237Schristos for (p2 = cp; (*p1++ = *p2++) != '\0';)
80351437Sleres continue;
80460237Schristos for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
80551437Sleres continue;
80660237Schristos for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
80751437Sleres continue;
80849992Sbostic /*
80949992Sbostic * Restart canonicalization at expanded "/xxx".
81049992Sbostic */
81149992Sbostic p = sp - cp - 1 + newcp;
81249992Sbostic }
81349992Sbostic else {
81449992Sbostic /*
81549992Sbostic * New length is link + the rest
81649992Sbostic */
81749992Sbostic p1 = newcp = (Char *) xmalloc((size_t)
81849992Sbostic ((cc + (p1 - p)) * sizeof(Char)));
81949992Sbostic /*
82049992Sbostic * Copy new path into newcp
82149992Sbostic */
82260237Schristos for (p2 = link; (*p1++ = *p2++) != '\0';)
82351437Sleres continue;
82460237Schristos for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
82551437Sleres continue;
82649992Sbostic /*
82749992Sbostic * Restart canonicalization at beginning
82849992Sbostic */
82949992Sbostic p = newcp;
83049992Sbostic }
83149992Sbostic xfree((ptr_t) cp);
83249992Sbostic cp = newcp;
83349992Sbostic continue; /* canonicalize the link */
83449992Sbostic }
83549992Sbostic if (slash)
83649992Sbostic *p = '/';
83749992Sbostic }
83849992Sbostic }
83949992Sbostic
84049992Sbostic /*
84149992Sbostic * fix home...
84249992Sbostic */
84349992Sbostic p1 = value(STRhome);
84449992Sbostic cc = Strlen(p1);
84549992Sbostic /*
84649992Sbostic * See if we're not in a subdir of STRhome
84749992Sbostic */
84850125Schristos if (p1 && *p1 == '/' &&
84949992Sbostic (Strncmp(p1, cp, cc) != 0 || (cp[cc] != '/' && cp[cc] != '\0'))) {
85049992Sbostic static ino_t home_ino = -1;
85149992Sbostic static dev_t home_dev = -1;
85250024Schristos static Char *home_ptr = NULL;
85349992Sbostic struct stat statbuf;
85449992Sbostic
85549992Sbostic /*
85649992Sbostic * Get dev and ino of STRhome
85749992Sbostic */
85849992Sbostic if (home_ptr != p1 &&
85949992Sbostic stat(short2str(p1), &statbuf) != -1) {
86049992Sbostic home_dev = statbuf.st_dev;
86149992Sbostic home_ino = statbuf.st_ino;
86249992Sbostic home_ptr = p1;
86349992Sbostic }
86449992Sbostic /*
86549992Sbostic * Start comparing dev & ino backwards
86649992Sbostic */
86749992Sbostic p2 = Strcpy(link, cp);
86850024Schristos for (sp = NULL; *p2 && stat(short2str(p2), &statbuf) != -1;) {
86949992Sbostic if (statbuf.st_dev == home_dev &&
87049992Sbostic statbuf.st_ino == home_ino) {
87149992Sbostic sp = (Char *) - 1;
87249992Sbostic break;
87349992Sbostic }
87460237Schristos if ((sp = Strrchr(p2, '/')) != NULL)
87549992Sbostic *sp = '\0';
87649992Sbostic }
87749992Sbostic /*
87849992Sbostic * See if we found it
87949992Sbostic */
88050024Schristos if (*p2 && sp == (Char *) -1) {
88149992Sbostic /*
88249992Sbostic * Use STRhome to make '~' work
88349992Sbostic */
88450747Schristos newcp = Strspl(p1, cp + Strlen(p2));
88549992Sbostic xfree((ptr_t) cp);
88649992Sbostic cp = newcp;
88749992Sbostic }
88849992Sbostic }
88949992Sbostic return cp;
8901288Sbill }
8911288Sbill
89249992Sbostic
8931288Sbill /*
8941288Sbill * dnewcwd - make a new directory in the loop the current one
8951288Sbill */
89649992Sbostic static void
dnewcwd(dp)8971288Sbill dnewcwd(dp)
89849992Sbostic register struct directory *dp;
8991288Sbill {
90049992Sbostic dcwd = dp;
90149992Sbostic dset(dcwd->di_name);
90249992Sbostic if (printd && !(adrof(STRpushdsilent)))
90349992Sbostic printdirs();
9041288Sbill }
905