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