Home | History | Annotate | Line # | Download | only in csh
dir.c revision 1.13
      1 /*	$NetBSD: dir.c,v 1.13 1998/07/28 02:23:38 mycroft Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1980, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)dir.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: dir.c,v 1.13 1998/07/28 02:23:38 mycroft Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <errno.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 #if __STDC__
     52 # include <stdarg.h>
     53 #else
     54 # include <varargs.h>
     55 #endif
     56 
     57 #include "csh.h"
     58 #include "dir.h"
     59 #include "extern.h"
     60 
     61 /* Directory management. */
     62 
     63 static struct directory
     64 		*dfind __P((Char *));
     65 static Char	*dfollow __P((Char *));
     66 static void	 printdirs __P((void));
     67 static Char	*dgoto __P((Char *));
     68 static void 	 skipargs __P((Char ***, char *));
     69 static void	 dnewcwd __P((struct directory *));
     70 static void	 dset __P((Char *));
     71 
     72 struct directory dhead;		/* "head" of loop */
     73 int     printd;			/* force name to be printed */
     74 
     75 static int dirflag = 0;
     76 
     77 /*
     78  * dinit - initialize current working directory
     79  */
     80 void
     81 dinit(hp)
     82     Char   *hp;
     83 {
     84     const char *ecp;
     85     Char *cp;
     86     struct directory *dp;
     87     char    path[MAXPATHLEN];
     88     static char *emsg = "csh: Trying to start from \"%s\"\n";
     89 
     90     /* Don't believe the login shell home, because it may be a symlink */
     91     ecp = getcwd(path, MAXPATHLEN);
     92     if (ecp == NULL || *ecp == '\0') {
     93 	(void) fprintf(csherr, "csh: %s\n", strerror(errno));
     94 	if (hp && *hp) {
     95 	    ecp = short2str(hp);
     96 	    if (chdir(ecp) == -1)
     97 		cp = NULL;
     98 	    else
     99 		cp = hp;
    100 	    (void) fprintf(csherr, emsg, vis_str(hp));
    101 	}
    102 	else
    103 	    cp = NULL;
    104 	if (cp == NULL) {
    105 	    (void) fprintf(csherr, emsg, "/");
    106 	    if (chdir("/") == -1) {
    107 		/* I am not even try to print an error message! */
    108 		xexit(1);
    109 		/* NOTREACHED */
    110 	    }
    111 	    cp = SAVE("/");
    112 	}
    113     }
    114     else {
    115 	struct stat swd, shp;
    116 
    117 	/*
    118 	 * See if $HOME is the working directory we got and use that
    119 	 */
    120 	if (hp && *hp &&
    121 	    stat(ecp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
    122 	    swd.st_dev == shp.st_dev && swd.st_ino == shp.st_ino)
    123 	    cp = hp;
    124 	else {
    125 	    const char *cwd;
    126 
    127 	    /*
    128 	     * use PWD if we have it (for subshells)
    129 	     */
    130 	    if ((cwd = getenv("PWD")) != NULL) {
    131 		if (stat(cwd, &shp) != -1 && swd.st_dev == shp.st_dev &&
    132 		    swd.st_ino == shp.st_ino)
    133 		    ecp = cwd;
    134 	    }
    135 	    cp = dcanon(SAVE(ecp), STRNULL);
    136 	}
    137     }
    138 
    139     dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
    140     dp->di_name = Strsave(cp);
    141     dp->di_count = 0;
    142     dhead.di_next = dhead.di_prev = dp;
    143     dp->di_next = dp->di_prev = &dhead;
    144     printd = 0;
    145     dnewcwd(dp);
    146 }
    147 
    148 static void
    149 dset(dp)
    150 Char *dp;
    151 {
    152     /*
    153      * Don't call set() directly cause if the directory contains ` or
    154      * other junk characters glob will fail.
    155      */
    156     Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
    157 
    158     vec[0] = Strsave(dp);
    159     vec[1] = 0;
    160     setq(STRcwd, vec, &shvhed);
    161     Setenv(STRPWD, dp);
    162 }
    163 
    164 #define DIR_LONG 1
    165 #define DIR_VERT 2
    166 #define DIR_LINE 4
    167 
    168 static void
    169 skipargs(v, str)
    170     Char ***v;
    171     char   *str;
    172 {
    173     Char  **n = *v, *s;
    174 
    175     dirflag = 0;
    176     for (n++; *n != NULL && (*n)[0] == '-'; n++)
    177 	for (s = &((*n)[1]); *s; s++)
    178 	    switch (*s) {
    179 	    case 'l':
    180 		dirflag |= DIR_LONG;
    181 		break;
    182 	    case 'v':
    183 		dirflag |= DIR_VERT;
    184 		break;
    185 	    case 'n':
    186 		dirflag |= DIR_LINE;
    187 		break;
    188 	    default:
    189 		stderror(ERR_DIRUS, vis_str(**v), str);
    190 		/* NOTREACHED */
    191 	    }
    192     *v = n;
    193 }
    194 
    195 /*
    196  * dodirs - list all directories in directory loop
    197  */
    198 void
    199 /*ARGSUSED*/
    200 dodirs(v, t)
    201     Char **v;
    202     struct command *t;
    203 {
    204     skipargs(&v, "");
    205 
    206     if (*v != NULL) {
    207 	stderror(ERR_DIRUS, "dirs", "");
    208 	/* NOTREACHED */
    209     }
    210     printdirs();
    211 }
    212 
    213 static void
    214 printdirs()
    215 {
    216     struct directory *dp;
    217     Char   *s, *hp = value(STRhome);
    218     int     idx, len, cur;
    219 
    220     if (*hp == '\0')
    221 	hp = NULL;
    222     dp = dcwd;
    223     idx = 0;
    224     cur = 0;
    225     do {
    226 	if (dp == &dhead)
    227 	    continue;
    228 	if (dirflag & DIR_VERT) {
    229 	    (void) fprintf(cshout, "%d\t", idx++);
    230 	    cur = 0;
    231 	}
    232 	if (!(dirflag & DIR_LONG) && hp != NULL && !eq(hp, STRslash) &&
    233 	    (len = Strlen(hp), Strncmp(hp, dp->di_name, len) == 0) &&
    234 	    (dp->di_name[len] == '\0' || dp->di_name[len] == '/'))
    235 	    len = Strlen(s = (dp->di_name + len)) + 2;
    236 	else
    237 	    len = Strlen(s = dp->di_name) + 1;
    238 
    239 	cur += len;
    240 	if ((dirflag & DIR_LINE) && cur >= 80 - 1 && len < 80) {
    241 	    (void) fprintf(cshout, "\n");
    242 	    cur = len;
    243 	}
    244 	(void) fprintf(cshout, s != dp->di_name ? "~%s%c" : "%s%c",
    245 		vis_str(s), (dirflag & DIR_VERT) ? '\n' : ' ');
    246     } while ((dp = dp->di_prev) != dcwd);
    247     if (!(dirflag & DIR_VERT))
    248 	(void) fprintf(cshout, "\n");
    249 }
    250 
    251 void
    252 dtildepr(home, dir)
    253     Char *home, *dir;
    254 {
    255 
    256     if (!eq(home, STRslash) && prefix(home, dir))
    257 	(void) fprintf(cshout, "~%s", vis_str(dir + Strlen(home)));
    258     else
    259 	(void) fprintf(cshout, "%s", vis_str(dir));
    260 }
    261 
    262 void
    263 dtilde()
    264 {
    265     struct directory *d = dcwd;
    266 
    267     do {
    268 	if (d == &dhead)
    269 	    continue;
    270 	d->di_name = dcanon(d->di_name, STRNULL);
    271     } while ((d = d->di_prev) != dcwd);
    272 
    273     dset(dcwd->di_name);
    274 }
    275 
    276 
    277 /* dnormalize():
    278  *	If the name starts with . or .. then we might need to normalize
    279  *	it depending on the symbolic link flags
    280  */
    281 Char   *
    282 dnormalize(cp)
    283     Char   *cp;
    284 {
    285 
    286 #define UC (unsigned char)
    287 #define ISDOT(c) (UC(c)[0] == '.' && ((UC(c)[1] == '\0') || (UC(c)[1] == '/')))
    288 #define ISDOTDOT(c) (UC(c)[0] == '.' && ISDOT(&((c)[1])))
    289 
    290     if ((unsigned char) cp[0] == '/')
    291 	return (Strsave(cp));
    292 
    293     if (adrof(STRignore_symlinks)) {
    294 	int     dotdot = 0;
    295 	Char   *dp, *cwd;
    296 
    297 	cwd = (Char *) xmalloc((size_t) ((Strlen(dcwd->di_name) + 3) *
    298 					 sizeof(Char)));
    299 	(void) Strcpy(cwd, dcwd->di_name);
    300 
    301 	/*
    302 	 * Ignore . and count ..'s
    303 	 */
    304 	while (*cp) {
    305 	    if (ISDOT(cp)) {
    306 		if (*++cp)
    307 		    cp++;
    308 	    }
    309 	    else if (ISDOTDOT(cp)) {
    310 		dotdot++;
    311 		cp += 2;
    312 		if (*cp)
    313 		    cp++;
    314 	    }
    315 	    else
    316 		break;
    317 	}
    318 	while (dotdot > 0) {
    319 	    dp = Strrchr(cwd, '/');
    320 	    if (dp) {
    321 		*dp = '\0';
    322 		dotdot--;
    323 	    }
    324 	    else
    325 		break;
    326 	}
    327 
    328 	if (*cp) {
    329 	    cwd[dotdot = Strlen(cwd)] = '/';
    330 	    cwd[dotdot + 1] = '\0';
    331 	    dp = Strspl(cwd, cp);
    332 	    xfree((ptr_t) cwd);
    333 	    return dp;
    334 	}
    335 	else {
    336 	    if (!*cwd) {
    337 		cwd[0] = '/';
    338 		cwd[1] = '\0';
    339 	    }
    340 	    return cwd;
    341 	}
    342     }
    343     return Strsave(cp);
    344 }
    345 
    346 /*
    347  * dochngd - implement chdir command.
    348  */
    349 void
    350 /*ARGSUSED*/
    351 dochngd(v, t)
    352     Char **v;
    353     struct command *t;
    354 {
    355     Char *cp;
    356     struct directory *dp;
    357 
    358     skipargs(&v, " [<dir>]");
    359     printd = 0;
    360     if (*v == NULL) {
    361 	if ((cp = value(STRhome)) == NULL || *cp == 0) {
    362 	    stderror(ERR_NAME | ERR_NOHOMEDIR);
    363 	    /* NOTREACHED */
    364 	}
    365 	if (chdir(short2str(cp)) < 0) {
    366 	    stderror(ERR_NAME | ERR_CANTCHANGE);
    367 	    /* NOTREACHED */
    368 	}
    369 	cp = Strsave(cp);
    370     }
    371     else if (v[1] != NULL) {
    372 	stderror(ERR_NAME | ERR_TOOMANY);
    373 	/* NOTREACHED */
    374     }
    375     else if ((dp = dfind(*v)) != 0) {
    376 	char   *tmp;
    377 
    378 	printd = 1;
    379 	if (chdir(tmp = short2str(dp->di_name)) < 0) {
    380 	    stderror(ERR_SYSTEM, tmp, strerror(errno));
    381 	    /* NOTREACHED */
    382 	}
    383 	dcwd->di_prev->di_next = dcwd->di_next;
    384 	dcwd->di_next->di_prev = dcwd->di_prev;
    385 	dfree(dcwd);
    386 	dnewcwd(dp);
    387 	return;
    388     }
    389     else
    390 	cp = dfollow(*v);
    391     dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
    392     dp->di_name = cp;
    393     dp->di_count = 0;
    394     dp->di_next = dcwd->di_next;
    395     dp->di_prev = dcwd->di_prev;
    396     dp->di_prev->di_next = dp;
    397     dp->di_next->di_prev = dp;
    398     dfree(dcwd);
    399     dnewcwd(dp);
    400 }
    401 
    402 static Char *
    403 dgoto(cp)
    404     Char   *cp;
    405 {
    406     Char   *dp;
    407 
    408     if (*cp != '/') {
    409 	Char *p, *q;
    410 	int     cwdlen;
    411 
    412 	for (p = dcwd->di_name; *p++;)
    413 	    continue;
    414 	if ((cwdlen = p - dcwd->di_name - 1) == 1)	/* root */
    415 	    cwdlen = 0;
    416 	for (p = cp; *p++;)
    417 	    continue;
    418 	dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
    419 	for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
    420 	    continue;
    421 	if (cwdlen)
    422 	    p[-1] = '/';
    423 	else
    424 	    p--;		/* don't add a / after root */
    425 	for (q = cp; (*p++ = *q++) != '\0';)
    426 	    continue;
    427 	xfree((ptr_t) cp);
    428 	cp = dp;
    429 	dp += cwdlen;
    430     }
    431     else
    432 	dp = cp;
    433 
    434     cp = dcanon(cp, dp);
    435     return cp;
    436 }
    437 
    438 /*
    439  * dfollow - change to arg directory; fall back on cdpath if not valid
    440  */
    441 static Char *
    442 dfollow(cp)
    443     Char *cp;
    444 {
    445     Char *dp;
    446     struct varent *c;
    447     char    ebuf[MAXPATHLEN];
    448     int serrno;
    449 
    450     cp = globone(cp, G_ERROR);
    451     /*
    452      * if we are ignoring symlinks, try to fix relatives now.
    453      */
    454     dp = dnormalize(cp);
    455     if (chdir(short2str(dp)) >= 0) {
    456 	xfree((ptr_t) cp);
    457 	return dgoto(dp);
    458     }
    459     else {
    460 	xfree((ptr_t) dp);
    461 	if (chdir(short2str(cp)) >= 0)
    462 	    return dgoto(cp);
    463 	serrno = errno;
    464     }
    465 
    466     if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
    467 	&& (c = adrof(STRcdpath))) {
    468 	Char  **cdp;
    469 	Char *p;
    470 	Char    buf[MAXPATHLEN];
    471 
    472 	for (cdp = c->vec; *cdp; cdp++) {
    473 	    for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
    474 		continue;
    475 	    dp[-1] = '/';
    476 	    for (p = cp; (*dp++ = *p++) != '\0';)
    477 		continue;
    478 	    if (chdir(short2str(buf)) >= 0) {
    479 		printd = 1;
    480 		xfree((ptr_t) cp);
    481 		cp = Strsave(buf);
    482 		return dgoto(cp);
    483 	    }
    484 	}
    485     }
    486     dp = value(cp);
    487     if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
    488 	xfree((ptr_t) cp);
    489 	cp = Strsave(dp);
    490 	printd = 1;
    491 	return dgoto(cp);
    492     }
    493     (void) strcpy(ebuf, short2str(cp));
    494     xfree((ptr_t) cp);
    495     stderror(ERR_SYSTEM, ebuf, strerror(serrno));
    496     /* NOTREACHED */
    497 }
    498 
    499 
    500 /*
    501  * dopushd - push new directory onto directory stack.
    502  *	with no arguments exchange top and second.
    503  *	with numeric argument (+n) bring it to top.
    504  */
    505 void
    506 /*ARGSUSED*/
    507 dopushd(v, t)
    508     Char **v;
    509     struct command *t;
    510 {
    511     struct directory *dp;
    512 
    513     skipargs(&v, " [<dir>|+<n>]");
    514     printd = 1;
    515     if (*v == NULL) {
    516 	char   *tmp;
    517 
    518 	if ((dp = dcwd->di_prev) == &dhead)
    519 	    dp = dhead.di_prev;
    520 	if (dp == dcwd) {
    521 	    stderror(ERR_NAME | ERR_NODIR);
    522 	    /* NOTREACHED */
    523 	}
    524 	if (chdir(tmp = short2str(dp->di_name)) < 0)
    525 	    stderror(ERR_SYSTEM, tmp, strerror(errno));
    526 	dp->di_prev->di_next = dp->di_next;
    527 	dp->di_next->di_prev = dp->di_prev;
    528 	dp->di_next = dcwd->di_next;
    529 	dp->di_prev = dcwd;
    530 	dcwd->di_next->di_prev = dp;
    531 	dcwd->di_next = dp;
    532     }
    533     else if (v[1] != NULL) {
    534 	stderror(ERR_NAME | ERR_TOOMANY);
    535 	/* NOTREACHED */
    536     }
    537     else if ((dp = dfind(*v)) != NULL) {
    538 	char   *tmp;
    539 
    540 	if (chdir(tmp = short2str(dp->di_name)) < 0) {
    541 	    stderror(ERR_SYSTEM, tmp, strerror(errno));
    542 	    /* NOTREACHED */
    543 	}
    544     }
    545     else {
    546 	Char *ccp;
    547 
    548 	ccp = dfollow(*v);
    549 	dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
    550 	dp->di_name = ccp;
    551 	dp->di_count = 0;
    552 	dp->di_prev = dcwd;
    553 	dp->di_next = dcwd->di_next;
    554 	dcwd->di_next = dp;
    555 	dp->di_next->di_prev = dp;
    556     }
    557     dnewcwd(dp);
    558 }
    559 
    560 /*
    561  * dfind - find a directory if specified by numeric (+n) argument
    562  */
    563 static struct directory *
    564 dfind(cp)
    565     Char *cp;
    566 {
    567     struct directory *dp;
    568     int i;
    569     Char *ep;
    570 
    571     if (*cp++ != '+')
    572 	return (0);
    573     for (ep = cp; Isdigit(*ep); ep++)
    574 	continue;
    575     if (*ep)
    576 	return (0);
    577     i = getn(cp);
    578     if (i <= 0)
    579 	return (0);
    580     for (dp = dcwd; i != 0; i--) {
    581 	if ((dp = dp->di_prev) == &dhead)
    582 	    dp = dp->di_prev;
    583 	if (dp == dcwd) {
    584 	    stderror(ERR_NAME | ERR_DEEP);
    585 	    /* NOTREACHED */
    586 	}
    587     }
    588     return (dp);
    589 }
    590 
    591 /*
    592  * dopopd - pop a directory out of the directory stack
    593  *	with a numeric argument just discard it.
    594  */
    595 void
    596 /*ARGSUSED*/
    597 dopopd(v, t)
    598     Char **v;
    599     struct command *t;
    600 {
    601     struct directory *dp, *p = NULL;
    602 
    603     skipargs(&v, " [+<n>]");
    604     printd = 1;
    605     if (*v == NULL)
    606 	dp = dcwd;
    607     else if (v[1] != NULL) {
    608 	stderror(ERR_NAME | ERR_TOOMANY);
    609 	/* NOTREACHED */
    610     }
    611     else if ((dp = dfind(*v)) == 0) {
    612 	stderror(ERR_NAME | ERR_BADDIR);
    613 	/* NOTREACHED */
    614     }
    615     if (dp->di_prev == &dhead && dp->di_next == &dhead) {
    616 	stderror(ERR_NAME | ERR_EMPTY);
    617 	/* NOTREACHED */
    618     }
    619     if (dp == dcwd) {
    620 	char   *tmp;
    621 
    622 	if ((p = dp->di_prev) == &dhead)
    623 	    p = dhead.di_prev;
    624 	if (chdir(tmp = short2str(p->di_name)) < 0) {
    625 	    stderror(ERR_SYSTEM, tmp, strerror(errno));
    626 	    /* NOTREACHED */
    627 	}
    628     }
    629     dp->di_prev->di_next = dp->di_next;
    630     dp->di_next->di_prev = dp->di_prev;
    631     if (dp == dcwd)
    632 	dnewcwd(p);
    633     else {
    634 	printdirs();
    635     }
    636     dfree(dp);
    637 }
    638 
    639 /*
    640  * dfree - free the directory (or keep it if it still has ref count)
    641  */
    642 void
    643 dfree(dp)
    644     struct directory *dp;
    645 {
    646 
    647     if (dp->di_count != 0) {
    648 	dp->di_next = dp->di_prev = 0;
    649     }
    650     else {
    651 	xfree((char *) dp->di_name);
    652 	xfree((ptr_t) dp);
    653     }
    654 }
    655 
    656 /*
    657  * dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
    658  *	we are of course assuming that the file system is standardly
    659  *	constructed (always have ..'s, directories have links)
    660  */
    661 Char   *
    662 dcanon(cp, p)
    663     Char *cp, *p;
    664 {
    665     Char *sp;
    666     Char *p1, *p2;	/* general purpose */
    667     bool    slash;
    668 
    669     Char    link[MAXPATHLEN];
    670     char    tlink[MAXPATHLEN];
    671     int     cc;
    672     Char   *newcp;
    673 
    674     /*
    675      * christos: if the path given does not start with a slash prepend cwd. If
    676      * cwd does not start with a path or the result would be too long abort().
    677      */
    678     if (*cp != '/') {
    679 	Char    tmpdir[MAXPATHLEN];
    680 
    681 	p1 = value(STRcwd);
    682 	if (p1 == NULL || *p1 != '/')
    683 	    abort();
    684 	if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
    685 	    abort();
    686 	(void) Strcpy(tmpdir, p1);
    687 	(void) Strcat(tmpdir, STRslash);
    688 	(void) Strcat(tmpdir, cp);
    689 	xfree((ptr_t) cp);
    690 	cp = p = Strsave(tmpdir);
    691     }
    692 
    693     while (*p) {		/* for each component */
    694 	sp = p;			/* save slash address */
    695 	while (*++p == '/')	/* flush extra slashes */
    696 	    continue;
    697 	if (p != ++sp)
    698 	    for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
    699 		continue;
    700 	p = sp;			/* save start of component */
    701 	slash = 0;
    702 	while (*++p)		/* find next slash or end of path */
    703 	    if (*p == '/') {
    704 		slash = 1;
    705 		*p = 0;
    706 		break;
    707 	    }
    708 
    709 	if (*sp == '\0')	/* if component is null */
    710 	    if (--sp == cp)	/* if path is one char (i.e. /) */
    711 		break;
    712 	    else
    713 		*sp = '\0';
    714 	else if (sp[0] == '.' && sp[1] == 0) {
    715 	    if (slash) {
    716 		for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
    717 		    continue;
    718 		p = --sp;
    719 	    }
    720 	    else if (--sp != cp)
    721 		*sp = '\0';
    722 	}
    723 	else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
    724 	    /*
    725 	     * We have something like "yyy/xxx/..", where "yyy" can be null or
    726 	     * a path starting at /, and "xxx" is a single component. Before
    727 	     * compressing "xxx/..", we want to expand "yyy/xxx", if it is a
    728 	     * symbolic link.
    729 	     */
    730 	    *--sp = 0;		/* form the pathname for readlink */
    731 	    if (sp != cp && !adrof(STRignore_symlinks) &&
    732 		(cc = readlink(short2str(cp), tlink,
    733 			       sizeof tlink)) >= 0) {
    734 		(void) Strcpy(link, str2short(tlink));
    735 		link[cc] = '\0';
    736 
    737 		if (slash)
    738 		    *p = '/';
    739 		/*
    740 		 * Point p to the '/' in "/..", and restore the '/'.
    741 		 */
    742 		*(p = sp) = '/';
    743 		/*
    744 		 * find length of p
    745 		 */
    746 		for (p1 = p; *p1++;)
    747 		    continue;
    748 		if (*link != '/') {
    749 		    /*
    750 		     * Relative path, expand it between the "yyy/" and the
    751 		     * "/..". First, back sp up to the character past "yyy/".
    752 		     */
    753 		    while (*--sp != '/')
    754 			continue;
    755 		    sp++;
    756 		    *sp = 0;
    757 		    /*
    758 		     * New length is "yyy/" + link + "/.." and rest
    759 		     */
    760 		    p1 = newcp = (Char *) xmalloc((size_t)
    761 						(((sp - cp) + cc + (p1 - p)) *
    762 						 sizeof(Char)));
    763 		    /*
    764 		     * Copy new path into newcp
    765 		     */
    766 		    for (p2 = cp; (*p1++ = *p2++) != '\0';)
    767 			continue;
    768 		    for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
    769 			continue;
    770 		    for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
    771 			continue;
    772 		    /*
    773 		     * Restart canonicalization at expanded "/xxx".
    774 		     */
    775 		    p = sp - cp - 1 + newcp;
    776 		}
    777 		else {
    778 		    /*
    779 		     * New length is link + "/.." and rest
    780 		     */
    781 		    p1 = newcp = (Char *) xmalloc((size_t)
    782 					    ((cc + (p1 - p)) * sizeof(Char)));
    783 		    /*
    784 		     * Copy new path into newcp
    785 		     */
    786 		    for (p2 = link; (*p1++ = *p2++) != '\0';)
    787 			continue;
    788 		    for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
    789 			continue;
    790 		    /*
    791 		     * Restart canonicalization at beginning
    792 		     */
    793 		    p = newcp;
    794 		}
    795 		xfree((ptr_t) cp);
    796 		cp = newcp;
    797 		continue;	/* canonicalize the link */
    798 	    }
    799 	    *sp = '/';
    800 	    if (sp != cp)
    801 		while (*--sp != '/')
    802 		    continue;
    803 	    if (slash) {
    804 		for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
    805 		    continue;
    806 		p = sp;
    807 	    }
    808 	    else if (cp == sp)
    809 		*++sp = '\0';
    810 	    else
    811 		*sp = '\0';
    812 	}
    813 	else {			/* normal dir name (not . or .. or nothing) */
    814 
    815 	    if (sp != cp && adrof(STRchase_symlinks) &&
    816 		!adrof(STRignore_symlinks) &&
    817 		(cc = readlink(short2str(cp), tlink,
    818 			       sizeof tlink)) >= 0) {
    819 		(void) Strcpy(link, str2short(tlink));
    820 		link[cc] = '\0';
    821 
    822 		/*
    823 		 * restore the '/'.
    824 		 */
    825 		if (slash)
    826 		    *p = '/';
    827 
    828 		/*
    829 		 * point sp to p (rather than backing up).
    830 		 */
    831 		sp = p;
    832 
    833 		/*
    834 		 * find length of p
    835 		 */
    836 		for (p1 = p; *p1++;)
    837 		    continue;
    838 		if (*link != '/') {
    839 		    /*
    840 		     * Relative path, expand it between the "yyy/" and the
    841 		     * remainder. First, back sp up to the character past
    842 		     * "yyy/".
    843 		     */
    844 		    while (*--sp != '/')
    845 			continue;
    846 		    sp++;
    847 		    *sp = 0;
    848 		    /*
    849 		     * New length is "yyy/" + link + "/.." and rest
    850 		     */
    851 		    p1 = newcp = (Char *) xmalloc((size_t)
    852 						  (((sp - cp) + cc + (p1 - p))
    853 						   * sizeof(Char)));
    854 		    /*
    855 		     * Copy new path into newcp
    856 		     */
    857 		    for (p2 = cp; (*p1++ = *p2++) != '\0';)
    858 			continue;
    859 		    for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
    860 			continue;
    861 		    for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
    862 			continue;
    863 		    /*
    864 		     * Restart canonicalization at expanded "/xxx".
    865 		     */
    866 		    p = sp - cp - 1 + newcp;
    867 		}
    868 		else {
    869 		    /*
    870 		     * New length is link + the rest
    871 		     */
    872 		    p1 = newcp = (Char *) xmalloc((size_t)
    873 					    ((cc + (p1 - p)) * sizeof(Char)));
    874 		    /*
    875 		     * Copy new path into newcp
    876 		     */
    877 		    for (p2 = link; (*p1++ = *p2++) != '\0';)
    878 			continue;
    879 		    for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
    880 			continue;
    881 		    /*
    882 		     * Restart canonicalization at beginning
    883 		     */
    884 		    p = newcp;
    885 		}
    886 		xfree((ptr_t) cp);
    887 		cp = newcp;
    888 		continue;	/* canonicalize the link */
    889 	    }
    890 	    if (slash)
    891 		*p = '/';
    892 	}
    893     }
    894 
    895     /*
    896      * fix home...
    897      */
    898     p1 = value(STRhome);
    899     cc = Strlen(p1);
    900     /*
    901      * See if we're not in a subdir of STRhome
    902      */
    903     if (p1 && *p1 == '/' &&
    904 	(Strncmp(p1, cp, cc) != 0 || (cp[cc] != '/' && cp[cc] != '\0'))) {
    905 	static ino_t home_ino;
    906 	static dev_t home_dev = NODEV;
    907 	static Char *home_ptr = NULL;
    908 	struct stat statbuf;
    909 
    910 	/*
    911 	 * Get dev and ino of STRhome
    912 	 */
    913 	if (home_ptr != p1 &&
    914 	    stat(short2str(p1), &statbuf) != -1) {
    915 	    home_dev = statbuf.st_dev;
    916 	    home_ino = statbuf.st_ino;
    917 	    home_ptr = p1;
    918 	}
    919 	/*
    920 	 * Start comparing dev & ino backwards
    921 	 */
    922 	p2 = Strcpy(link, cp);
    923 	for (sp = NULL; *p2 && stat(short2str(p2), &statbuf) != -1;) {
    924 	    if (statbuf.st_dev == home_dev &&
    925 		statbuf.st_ino == home_ino) {
    926 		sp = (Char *) - 1;
    927 		break;
    928 	    }
    929 	    if ((sp = Strrchr(p2, '/')) != NULL)
    930 		*sp = '\0';
    931 	}
    932 	/*
    933 	 * See if we found it
    934 	 */
    935 	if (*p2 && sp == (Char *) -1) {
    936 	    /*
    937 	     * Use STRhome to make '~' work
    938 	     */
    939 	    newcp = Strspl(p1, cp + Strlen(p2));
    940 	    xfree((ptr_t) cp);
    941 	    cp = newcp;
    942 	}
    943     }
    944     return cp;
    945 }
    946 
    947 
    948 /*
    949  * dnewcwd - make a new directory in the loop the current one
    950  */
    951 static void
    952 dnewcwd(dp)
    953     struct directory *dp;
    954 {
    955     dcwd = dp;
    956     dset(dcwd->di_name);
    957     if (printd && !(adrof(STRpushdsilent)))
    958 	printdirs();
    959 }
    960