]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/csh/dir.c
This commit was generated by cvs2svn to compensate for changes in r32243,
[FreeBSD/FreeBSD.git] / bin / csh / dir.c
1 /*-
2  * Copyright (c) 1980, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)dir.c       8.1 (Berkeley) 5/31/93";
37 #else
38 static const char rcsid[] =
39         "$Id: dir.c,v 1.6 1997/02/22 14:01:42 peter Exp $";
40 #endif
41 #endif /* not lint */
42
43 #include <sys/param.h>
44 #include <sys/stat.h>
45 #include <errno.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #if __STDC__
50 # include <stdarg.h>
51 #else
52 # include <varargs.h>
53 #endif
54
55 #include "csh.h"
56 #include "dir.h"
57 #include "extern.h"
58
59 /* Directory management. */
60
61 static struct directory
62                 *dfind __P((Char *));
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 *));
68
69 struct directory dhead;         /* "head" of loop */
70 int     printd;                 /* force name to be printed */
71
72 static int dirflag = 0;
73
74 /*
75  * dinit - initialize current working directory
76  */
77 void
78 dinit(hp)
79     Char   *hp;
80 {
81     char *tcp;
82     Char *cp;
83     struct directory *dp;
84     char    path[MAXPATHLEN];
85     static char *emsg = "csh: Trying to start from \"%s\"\n";
86
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));
91         if (hp && *hp) {
92             tcp = short2str(hp);
93             if (chdir(tcp) == -1)
94                 cp = NULL;
95             else
96                 cp = hp;
97             (void) fprintf(csherr, emsg, vis_str(hp));
98         }
99         else
100             cp = NULL;
101         if (cp == NULL) {
102             (void) fprintf(csherr, emsg, "/");
103             if (chdir("/") == -1)
104                 /* I am not even try to print an error message! */
105                 xexit(1);
106             cp = SAVE("/");
107         }
108     }
109     else {
110         struct stat swd, shp;
111
112         /*
113          * See if $HOME is the working directory we got and use that
114          */
115         if (hp && *hp &&
116             stat(tcp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
117             swd.st_dev == shp.st_dev && swd.st_ino == shp.st_ino)
118             cp = hp;
119         else {
120             char   *cwd;
121
122             /*
123              * use PWD if we have it (for subshells)
124              */
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)
128                     tcp = cwd;
129             }
130             cp = dcanon(SAVE(tcp), STRNULL);
131         }
132     }
133
134     dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
135     dp->di_name = Strsave(cp);
136     dp->di_count = 0;
137     dhead.di_next = dhead.di_prev = dp;
138     dp->di_next = dp->di_prev = &dhead;
139     printd = 0;
140     dnewcwd(dp);
141 }
142
143 static void
144 dset(dp)
145 Char *dp;
146 {
147     /*
148      * Don't call set() directly cause if the directory contains ` or
149      * other junk characters glob will fail.
150      */
151     Char **vec = (Char **) xmalloc((size_t) (2 * sizeof(Char **)));
152
153     vec[0] = Strsave(dp);
154     vec[1] = 0;
155     setq(STRcwd, vec, &shvhed);
156     Setenv(STRPWD, dp);
157 }
158
159 #define DIR_LONG 1
160 #define DIR_VERT 2
161 #define DIR_LINE 4
162
163 static void
164 skipargs(v, str)
165     Char ***v;
166     char   *str;
167 {
168     Char  **n = *v, *s;
169
170     dirflag = 0;
171     for (n++; *n != NULL && (*n)[0] == '-'; n++)
172         for (s = &((*n)[1]); *s; s++)
173             switch (*s) {
174             case 'l':
175                 dirflag |= DIR_LONG;
176                 break;
177             case 'v':
178                 dirflag |= DIR_VERT;
179                 break;
180             case 'n':
181                 dirflag |= DIR_LINE;
182                 break;
183             default:
184                 stderror(ERR_DIRUS, vis_str(**v), str);
185                 break;
186             }
187     *v = n;
188 }
189
190 /*
191  * dodirs - list all directories in directory loop
192  */
193 void
194 /*ARGSUSED*/
195 dodirs(v, t)
196     Char **v;
197     struct command *t;
198 {
199     skipargs(&v, "");
200
201     if (*v != NULL)
202         stderror(ERR_DIRUS, "dirs", "");
203     printdirs();
204 }
205
206 static void
207 printdirs()
208 {
209     struct directory *dp;
210     Char   *s, *hp = value(STRhome);
211     int     idx, len, cur;
212
213     if (*hp == '\0')
214         hp = NULL;
215     dp = dcwd;
216     idx = 0;
217     cur = 0;
218     do {
219         if (dp == &dhead)
220             continue;
221         if (dirflag & DIR_VERT) {
222             (void) fprintf(cshout, "%d\t", idx++);
223             cur = 0;
224         }
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;
229         else
230             len = Strlen(s = dp->di_name) + 1;
231
232         cur += len;
233         if ((dirflag & DIR_LINE) && cur >= 80 - 1 && len < 80) {
234             (void) fprintf(cshout, "\n");
235             cur = len;
236         }
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");
242 }
243
244 void
245 dtildepr(home, dir)
246     Char *home, *dir;
247 {
248
249     if (!eq(home, STRslash) && prefix(home, dir))
250         (void) fprintf(cshout, "~%s", vis_str(dir + Strlen(home)));
251     else
252         (void) fprintf(cshout, "%s", vis_str(dir));
253 }
254
255 void
256 dtilde()
257 {
258     struct directory *d = dcwd;
259
260     do {
261         if (d == &dhead)
262             continue;
263         d->di_name = dcanon(d->di_name, STRNULL);
264     } while ((d = d->di_prev) != dcwd);
265
266     dset(dcwd->di_name);
267 }
268
269
270 /* dnormalize():
271  *      If the name starts with . or .. then we might need to normalize
272  *      it depending on the symbolic link flags
273  */
274 Char   *
275 dnormalize(cp)
276     Char   *cp;
277 {
278
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])))
282
283     if ((unsigned char) cp[0] == '/')
284         return (Strsave(cp));
285
286     if (adrof(STRignore_symlinks)) {
287         int     dotdot = 0;
288         Char   *dp, *cwd;
289
290         cwd = (Char *) xmalloc((size_t) ((Strlen(dcwd->di_name) + 3) *
291                                          sizeof(Char)));
292         (void) Strcpy(cwd, dcwd->di_name);
293
294         /*
295          * Ignore . and count ..'s
296          */
297         while (*cp) {
298             if (ISDOT(cp)) {
299                 if (*++cp)
300                     cp++;
301             }
302             else if (ISDOTDOT(cp)) {
303                 dotdot++;
304                 cp += 2;
305                 if (*cp)
306                     cp++;
307             }
308             else
309                 break;
310         }
311         while (dotdot > 0)
312             if ((dp = Strrchr(cwd, '/'))) {
313                 *dp = '\0';
314                 dotdot--;
315             }
316             else
317                 break;
318
319         if (*cp) {
320             cwd[dotdot = Strlen(cwd)] = '/';
321             cwd[dotdot + 1] = '\0';
322             dp = Strspl(cwd, cp);
323             xfree((ptr_t) cwd);
324             return dp;
325         }
326         else {
327             if (!*cwd) {
328                 cwd[0] = '/';
329                 cwd[1] = '\0';
330             }
331             return cwd;
332         }
333     }
334     return Strsave(cp);
335 }
336
337 /*
338  * dochngd - implement chdir command.
339  */
340 void
341 /*ARGSUSED*/
342 dochngd(v, t)
343     Char **v;
344     struct command *t;
345 {
346     Char *cp;
347     struct directory *dp;
348
349     skipargs(&v, " [<dir>]");
350     printd = 0;
351     if (*v == NULL) {
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);
356         cp = Strsave(cp);
357     }
358     else if (v[1] != NULL) {
359         stderror(ERR_NAME | ERR_TOOMANY);
360         /* NOTREACHED */
361         return;
362     }
363     else if ((dp = dfind(*v)) != 0) {
364         char   *tmp;
365
366         printd = 1;
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;
371         dfree(dcwd);
372         dnewcwd(dp);
373         return;
374     }
375     else
376         cp = dfollow(*v);
377     dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
378     dp->di_name = cp;
379     dp->di_count = 0;
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;
384     dfree(dcwd);
385     dnewcwd(dp);
386 }
387
388 static Char *
389 dgoto(cp)
390     Char   *cp;
391 {
392     Char   *dp;
393
394     if (*cp != '/') {
395         Char *p, *q;
396         int     cwdlen;
397
398         for (p = dcwd->di_name; *p++;)
399             continue;
400         if ((cwdlen = p - dcwd->di_name - 1) == 1)      /* root */
401             cwdlen = 0;
402         for (p = cp; *p++;)
403             continue;
404         dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
405         for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
406             continue;
407         if (cwdlen)
408             p[-1] = '/';
409         else
410             p--;                /* don't add a / after root */
411         for (q = cp; (*p++ = *q++) != '\0';)
412             continue;
413         xfree((ptr_t) cp);
414         cp = dp;
415         dp += cwdlen;
416     }
417     else
418         dp = cp;
419
420     cp = dcanon(cp, dp);
421     return cp;
422 }
423
424 /*
425  * dfollow - change to arg directory; fall back on cdpath if not valid
426  */
427 static Char *
428 dfollow(cp)
429     Char *cp;
430 {
431     Char *dp;
432     struct varent *c;
433     char    ebuf[MAXPATHLEN];
434     int serrno;
435
436     cp = globone(cp, G_ERROR);
437     /*
438      * if we are ignoring symlinks, try to fix relatives now.
439      */
440     dp = dnormalize(cp);
441     if (chdir(short2str(dp)) >= 0) {
442         xfree((ptr_t) cp);
443         return dgoto(dp);
444     }
445     else {
446         xfree((ptr_t) dp);
447         if (chdir(short2str(cp)) >= 0)
448             return dgoto(cp);
449         serrno = errno;
450     }
451
452     if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
453         && (c = adrof(STRcdpath))) {
454         Char  **cdp;
455         Char *p;
456         Char    buf[MAXPATHLEN];
457
458         for (cdp = c->vec; *cdp; cdp++) {
459             for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
460                 continue;
461             dp[-1] = '/';
462             for (p = cp; (*dp++ = *p++) != '\0';)
463                 continue;
464             if (chdir(short2str(buf)) >= 0) {
465                 printd = 1;
466                 xfree((ptr_t) cp);
467                 cp = Strsave(buf);
468                 return dgoto(cp);
469             }
470         }
471     }
472     dp = value(cp);
473     if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
474         xfree((ptr_t) cp);
475         cp = Strsave(dp);
476         printd = 1;
477         return dgoto(cp);
478     }
479     (void) strcpy(ebuf, short2str(cp));
480     xfree((ptr_t) cp);
481     stderror(ERR_SYSTEM, ebuf, strerror(serrno));
482     return (NULL);
483 }
484
485
486 /*
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.
490  */
491 void
492 /*ARGSUSED*/
493 dopushd(v, t)
494     Char **v;
495     struct command *t;
496 {
497     struct directory *dp;
498
499     skipargs(&v, " [<dir>|+<n>]");
500     printd = 1;
501     if (*v == NULL) {
502         char   *tmp;
503
504         if ((dp = dcwd->di_prev) == &dhead)
505             dp = dhead.di_prev;
506         if (dp == dcwd)
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;
513         dp->di_prev = dcwd;
514         dcwd->di_next->di_prev = dp;
515         dcwd->di_next = dp;
516     }
517     else if (v[1] != NULL) {
518         stderror(ERR_NAME | ERR_TOOMANY);
519         /* NOTREACHED */
520         return;
521     }
522     else if ((dp = dfind(*v)) != NULL) {
523         char   *tmp;
524
525         if (chdir(tmp = short2str(dp->di_name)) < 0)
526             stderror(ERR_SYSTEM, tmp, strerror(errno));
527     }
528     else {
529         Char *ccp;
530
531         ccp = dfollow(*v);
532         dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
533         dp->di_name = ccp;
534         dp->di_count = 0;
535         dp->di_prev = dcwd;
536         dp->di_next = dcwd->di_next;
537         dcwd->di_next = dp;
538         dp->di_next->di_prev = dp;
539     }
540     dnewcwd(dp);
541 }
542
543 /*
544  * dfind - find a directory if specified by numeric (+n) argument
545  */
546 static struct directory *
547 dfind(cp)
548     Char *cp;
549 {
550     struct directory *dp;
551     int i;
552     Char *ep;
553
554     if (*cp++ != '+')
555         return (0);
556     for (ep = cp; Isdigit(*ep); ep++)
557         continue;
558     if (*ep)
559         return (0);
560     i = getn(cp);
561     if (i <= 0)
562         return (0);
563     for (dp = dcwd; i != 0; i--) {
564         if ((dp = dp->di_prev) == &dhead)
565             dp = dp->di_prev;
566         if (dp == dcwd)
567             stderror(ERR_NAME | ERR_DEEP);
568     }
569     return (dp);
570 }
571
572 /*
573  * dopopd - pop a directory out of the directory stack
574  *      with a numeric argument just discard it.
575  */
576 void
577 /*ARGSUSED*/
578 dopopd(v, t)
579     Char **v;
580     struct command *t;
581 {
582     struct directory *dp, *p = NULL;
583
584     skipargs(&v, " [+<n>]");
585     printd = 1;
586     if (*v == NULL)
587         dp = dcwd;
588     else if (v[1] != NULL) {
589         stderror(ERR_NAME | ERR_TOOMANY);
590         /* NOTREACHED */
591         return;
592     }
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);
597     if (dp == dcwd) {
598         char   *tmp;
599
600         if ((p = dp->di_prev) == &dhead)
601             p = dhead.di_prev;
602         if (chdir(tmp = short2str(p->di_name)) < 0)
603             stderror(ERR_SYSTEM, tmp, strerror(errno));
604     }
605     dp->di_prev->di_next = dp->di_next;
606     dp->di_next->di_prev = dp->di_prev;
607     if (dp == dcwd)
608         dnewcwd(p);
609     else {
610         printdirs();
611     }
612     dfree(dp);
613 }
614
615 /*
616  * dfree - free the directory (or keep it if it still has ref count)
617  */
618 void
619 dfree(dp)
620     struct directory *dp;
621 {
622
623     if (dp->di_count != 0) {
624         dp->di_next = dp->di_prev = 0;
625     }
626     else {
627         xfree((char *) dp->di_name);
628         xfree((ptr_t) dp);
629     }
630 }
631
632 /*
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)
636  */
637 Char   *
638 dcanon(cp, p)
639     Char *cp, *p;
640 {
641     Char *sp;
642     Char *p1, *p2;      /* general purpose */
643     bool    slash;
644
645     Char    link[MAXPATHLEN];
646     char    tlink[MAXPATHLEN];
647     int     cc;
648     Char   *newcp;
649
650     /*
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().
653      */
654     if (*cp != '/') {
655         Char    tmpdir[MAXPATHLEN];
656
657         p1 = value(STRcwd);
658         if (p1 == NULL || *p1 != '/')
659             abort();
660         if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
661             abort();
662         (void) Strcpy(tmpdir, p1);
663         (void) Strcat(tmpdir, STRslash);
664         (void) Strcat(tmpdir, cp);
665         xfree((ptr_t) cp);
666         cp = p = Strsave(tmpdir);
667     }
668
669     while (*p) {                /* for each component */
670         sp = p;                 /* save slash address */
671         while (*++p == '/')     /* flush extra slashes */
672             continue;
673         if (p != ++sp)
674             for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
675                 continue;
676         p = sp;                 /* save start of component */
677         slash = 0;
678         while (*++p)            /* find next slash or end of path */
679             if (*p == '/') {
680                 slash = 1;
681                 *p = 0;
682                 break;
683             }
684
685         if (*sp == '\0')        /* if component is null */
686             if (--sp == cp)     /* if path is one char (i.e. /) */
687                 break;
688             else
689                 *sp = '\0';
690         else if (sp[0] == '.' && sp[1] == 0) {
691             if (slash) {
692                 for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
693                     continue;
694                 p = --sp;
695             }
696             else if (--sp != cp)
697                 *sp = '\0';
698         }
699         else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
700             /*
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
704              * symbolic link.
705              */
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));
711                 link[cc] = '\0';
712
713                 if (slash)
714                     *p = '/';
715                 /*
716                  * Point p to the '/' in "/..", and restore the '/'.
717                  */
718                 *(p = sp) = '/';
719                 /*
720                  * find length of p
721                  */
722                 for (p1 = p; *p1++;)
723                     continue;
724                 if (*link != '/') {
725                     /*
726                      * Relative path, expand it between the "yyy/" and the
727                      * "/..". First, back sp up to the character past "yyy/".
728                      */
729                     while (*--sp != '/')
730                         continue;
731                     sp++;
732                     *sp = 0;
733                     /*
734                      * New length is "yyy/" + link + "/.." and rest
735                      */
736                     p1 = newcp = (Char *) xmalloc((size_t)
737                                                 (((sp - cp) + cc + (p1 - p)) *
738                                                  sizeof(Char)));
739                     /*
740                      * Copy new path into newcp
741                      */
742                     for (p2 = cp; (*p1++ = *p2++) != '\0';)
743                         continue;
744                     for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
745                         continue;
746                     for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
747                         continue;
748                     /*
749                      * Restart canonicalization at expanded "/xxx".
750                      */
751                     p = sp - cp - 1 + newcp;
752                 }
753                 else {
754                     /*
755                      * New length is link + "/.." and rest
756                      */
757                     p1 = newcp = (Char *) xmalloc((size_t)
758                                             ((cc + (p1 - p)) * sizeof(Char)));
759                     /*
760                      * Copy new path into newcp
761                      */
762                     for (p2 = link; (*p1++ = *p2++) != '\0';)
763                         continue;
764                     for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
765                         continue;
766                     /*
767                      * Restart canonicalization at beginning
768                      */
769                     p = newcp;
770                 }
771                 xfree((ptr_t) cp);
772                 cp = newcp;
773                 continue;       /* canonicalize the link */
774             }
775             *sp = '/';
776             if (sp != cp)
777                 while (*--sp != '/')
778                     continue;
779             if (slash) {
780                 for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
781                     continue;
782                 p = sp;
783             }
784             else if (cp == sp)
785                 *++sp = '\0';
786             else
787                 *sp = '\0';
788         }
789         else {                  /* normal dir name (not . or .. or nothing) */
790
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));
796                 link[cc] = '\0';
797
798                 /*
799                  * restore the '/'.
800                  */
801                 if (slash)
802                     *p = '/';
803
804                 /*
805                  * point sp to p (rather than backing up).
806                  */
807                 sp = p;
808
809                 /*
810                  * find length of p
811                  */
812                 for (p1 = p; *p1++;)
813                     continue;
814                 if (*link != '/') {
815                     /*
816                      * Relative path, expand it between the "yyy/" and the
817                      * remainder. First, back sp up to the character past
818                      * "yyy/".
819                      */
820                     while (*--sp != '/')
821                         continue;
822                     sp++;
823                     *sp = 0;
824                     /*
825                      * New length is "yyy/" + link + "/.." and rest
826                      */
827                     p1 = newcp = (Char *) xmalloc((size_t)
828                                                   (((sp - cp) + cc + (p1 - p))
829                                                    * sizeof(Char)));
830                     /*
831                      * Copy new path into newcp
832                      */
833                     for (p2 = cp; (*p1++ = *p2++) != '\0';)
834                         continue;
835                     for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
836                         continue;
837                     for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
838                         continue;
839                     /*
840                      * Restart canonicalization at expanded "/xxx".
841                      */
842                     p = sp - cp - 1 + newcp;
843                 }
844                 else {
845                     /*
846                      * New length is link + the rest
847                      */
848                     p1 = newcp = (Char *) xmalloc((size_t)
849                                             ((cc + (p1 - p)) * sizeof(Char)));
850                     /*
851                      * Copy new path into newcp
852                      */
853                     for (p2 = link; (*p1++ = *p2++) != '\0';)
854                         continue;
855                     for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
856                         continue;
857                     /*
858                      * Restart canonicalization at beginning
859                      */
860                     p = newcp;
861                 }
862                 xfree((ptr_t) cp);
863                 cp = newcp;
864                 continue;       /* canonicalize the link */
865             }
866             if (slash)
867                 *p = '/';
868         }
869     }
870
871     /*
872      * fix home...
873      */
874     p1 = value(STRhome);
875     cc = Strlen(p1);
876     /*
877      * See if we're not in a subdir of STRhome
878      */
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;
884         struct stat statbuf;
885
886         /*
887          * Get dev and ino of STRhome
888          */
889         if (home_ptr != p1 &&
890             stat(short2str(p1), &statbuf) != -1) {
891             home_dev = statbuf.st_dev;
892             home_ino = statbuf.st_ino;
893             home_ptr = p1;
894         }
895         /*
896          * Start comparing dev & ino backwards
897          */
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) {
902                 sp = (Char *) - 1;
903                 break;
904             }
905             if ((sp = Strrchr(p2, '/')) != NULL)
906                 *sp = '\0';
907         }
908         /*
909          * See if we found it
910          */
911         if (*p2 && sp == (Char *) -1) {
912             /*
913              * Use STRhome to make '~' work
914              */
915             newcp = Strspl(p1, cp + Strlen(p2));
916             xfree((ptr_t) cp);
917             cp = newcp;
918         }
919     }
920     return cp;
921 }
922
923
924 /*
925  * dnewcwd - make a new directory in the loop the current one
926  */
927 static void
928 dnewcwd(dp)
929     struct directory *dp;
930 {
931     dcwd = dp;
932     dset(dcwd->di_name);
933     if (printd && !(adrof(STRpushdsilent)))
934         printdirs();
935 }