2
* sh.dir.c: Directory manipulation functions
5
* Copyright (c) 1980, 1991 The Regents of the University of California.
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41
* C Shell - directory management
44
static void dstart __P((const char *));
45
static struct directory *dfind __P((Char *));
46
static Char *dfollow __P((Char *));
47
static void printdirs __P((int));
48
static Char *dgoto __P((Char *));
49
static void dnewcwd __P((struct directory *, int));
50
static void dset __P((Char *));
51
static void dextract __P((struct directory *));
52
static int skipargs __P((Char ***, char *, char *));
53
static void dgetstack __P((void));
55
static struct directory dhead INIT_ZERO_STRUCT; /* "head" of loop */
56
static int printd; /* force name to be printed */
58
int bequiet = 0; /* do not print dir stack -strike */
64
xprintf(CGETS(12, 1, "%s: Trying to start from \"%s\"\n"), progname, from);
68
* dinit - initialize current working directory
76
register struct directory *dp;
77
char path[MAXPATHLEN];
80
if ((tcp = (char *) getenv("PWD")) == NULL)
82
/* Don't believe the login shell home, because it may be a symlink */
83
tcp = (char *) getcwd(path, sizeof(path));
84
if (tcp == NULL || *tcp == '\0') {
85
xprintf("%s: %s\n", progname, strerror(errno));
99
/* I am not even try to print an error message! */
106
struct stat swd, shp;
109
* See if $HOME is the working directory we got and use that
112
stat(tcp, &swd) != -1 && stat(short2str(hp), &shp) != -1 &&
113
DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
114
swd.st_ino == shp.st_ino)
120
* use PWD if we have it (for subshells)
122
if ((cwd = getenv("PWD")) != NULL) {
123
if (stat(cwd, &shp) != -1 &&
124
DEV_DEV_COMPARE(swd.st_dev, shp.st_dev) &&
125
swd.st_ino == shp.st_ino)
128
cp = dcanon(SAVE(tcp), STRNULL);
131
cp = dcanon(SAVE(tcp), STRNULL);
135
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
138
dhead.di_next = dhead.di_prev = dp;
139
dp->di_next = dp->di_prev = &dhead;
142
set(STRdirstack, Strsave(dp->di_name), VAR_READWRITE|VAR_NOGLOB);
150
* Don't call set() directly cause if the directory contains ` or
151
* other junk characters glob will fail.
153
set(STRowd, Strsave(varval(STRcwd)), VAR_READWRITE|VAR_NOGLOB);
154
set(STRcwd, Strsave(dp), VAR_READWRITE|VAR_NOGLOB);
159
#define DIR_PRINT 0x01 /* -p */
160
#define DIR_LONG 0x02 /* -l */
161
#define DIR_VERT 0x04 /* -v */
162
#define DIR_LINE 0x08 /* -n */
163
#define DIR_SAVE 0x10 /* -S */
164
#define DIR_LOAD 0x20 /* -L */
165
#define DIR_CLEAR 0x40 /* -c */
166
#define DIR_OLD 0x80 /* - */
169
skipargs(v, dstr, str)
176
int dflag = 0, loop = 1;
177
for (n++; loop && *n != NULL && (*n)[0] == '-'; n++)
178
if (*(s = &((*n)[1])) == '\0') /* test for bare "-" argument */
182
while (loop && *s != '\0') /* examine flags */
184
if ((p = strchr(dstr, *s++)) != NULL)
185
dflag |= (1 << (p - dstr));
187
stderror(ERR_DIRUS, short2str(**v), dstr, str);
188
loop = 0; /* break from both loops */
193
if (*n && (dflag & DIR_OLD))
194
stderror(ERR_DIRUS, short2str(**v), dstr, str);
196
/* make -l, -v, and -n imply -p */
197
if (dflag & (DIR_LONG|DIR_VERT|DIR_LINE))
203
* dodirs - list all directories in directory loop
211
static char flags[] = "plvnSLc";
212
int dflag = skipargs(&v, flags, "");
215
if ((dflag & DIR_CLEAR) != 0) {
216
struct directory *dp, *fdp;
217
for (dp = dcwd->di_next; dp != dcwd; ) {
223
dhead.di_next = dhead.di_prev = dp;
224
dp->di_next = dp->di_prev = &dhead;
226
if ((dflag & DIR_LOAD) != 0)
228
else if ((dflag & DIR_SAVE) != 0)
231
if (*v && (dflag & (DIR_SAVE|DIR_LOAD)))
234
if (*v != NULL || (dflag & DIR_OLD))
235
stderror(ERR_DIRUS, "dirs", flags, "");
236
if ((dflag & (DIR_CLEAR|DIR_LOAD|DIR_SAVE)) == 0 || (dflag & DIR_PRINT))
244
register struct directory *dp;
255
if (dflag & DIR_VERT) {
256
xprintf("%d\t", idx++);
261
if (!(dflag & DIR_LONG) && (user = getusername(&s)) != NULL)
262
len = (int) (Strlen(user) + Strlen(s) + 2);
264
len = (int) (Strlen(s) + 1);
267
if ((dflag & DIR_LINE) && cur >= T_Cols - 1 && len < T_Cols) {
272
xprintf("~%S", user);
273
xprintf("%S%c", s, (dflag & DIR_VERT) ? '\n' : ' ');
274
} while ((dp = dp->di_prev) != dcwd);
275
if (!(dflag & DIR_VERT))
284
if ((user = getusername(&dir)) != NULL)
285
xprintf("~%S%S", user, dir);
293
struct directory *d = dcwd;
298
d->di_name = dcanon(d->di_name, STRNULL);
299
} while ((d = d->di_prev) != dcwd);
306
* The path will be normalized if it
308
* 2) or starts with "../",
309
* 3) or ends with "/..",
310
* 4) or contains the string "/../",
311
* then it will be normalized, unless those strings are quoted.
312
* Otherwise, a copy is made and sent back.
320
/* return true if dp is of the form "../xxx" or "/../xxx" */
321
#define IS_DOTDOT(sp, p) (ISDOTDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
322
#define IS_DOT(sp, p) (ISDOT(p) && ((p) == (sp) || *((p) - 1) == '/'))
327
Char *dp, *cwd, *start = cp, buf[MAXPATHLEN];
333
* count the number of "../xxx" or "xxx/../xxx" in the path
335
for (dp=start; *dp && *(dp+1); dp++)
336
if (IS_DOTDOT(start, dp))
339
* if none, we are done.
342
return (Strsave(cp));
344
cwd = (Char *) xmalloc((size_t) (((int) Strlen(dcwd->di_name) + 3) *
346
(void) Strcpy(cwd, dcwd->di_name);
349
* If the path starts with a slash, we are not relative to
350
* the current working directory.
352
if (ABSOLUTEP(start))
355
slashslash = cwd[0] == '/' && cwd[1] == '/';
359
* Ignore . and count ..'s
366
if (IS_DOT(start, cp)) {
370
else if (IS_DOTDOT(start, cp)) {
372
break; /* finish analyzing .././../xxx/[..] */
383
if ((dp = Strrchr(cwd, '/')) != NULL) {
394
if (!*cwd) { /* too many ..'s, starts with "/" */
404
else if (slashslash && cwd[1] == '\0') {
411
if ((TRM(cwd[(dotdot = (int) Strlen(cwd)) - 1])) != '/')
414
dp = Strspl(cwd, TRM(buf[0]) == '/' ? &buf[1] : buf);
417
if ((TRM(cwd[(dotdot = (int) Strlen(cwd)) - 1])) == '/')
418
cwd[--dotdot] = '\0';
431
* dochngd - implement chdir command.
440
register struct directory *dp;
441
int dflag = skipargs(&v, "plvn", "[-|<dir>]");
445
cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
448
if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
449
stderror(ERR_NAME | ERR_NOHOMEDIR);
450
if (chdir(short2str(cp)) < 0)
451
stderror(ERR_NAME | ERR_CANTCHANGE);
454
else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
455
stderror(ERR_NAME | ERR_TOOMANY);
459
else if ((dp = dfind(cp)) != 0) {
463
if (chdir(tmp = short2str(dp->di_name)) < 0)
464
stderror(ERR_SYSTEM, tmp, strerror(errno));
465
dcwd->di_prev->di_next = dcwd->di_next;
466
dcwd->di_next->di_prev = dcwd->di_prev;
472
if ((cp = dfollow(cp)) == NULL)
474
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
477
dp->di_next = dcwd->di_next;
478
dp->di_prev = dcwd->di_prev;
479
dp->di_prev->di_next = dp;
480
dp->di_next->di_prev = dp;
493
register Char *p, *q;
496
for (p = dcwd->di_name; *p++;)
498
if ((cwdlen = (int) (p - dcwd->di_name - 1)) == 1) /* root */
502
dp = (Char *) xmalloc((size_t)((cwdlen + (p - cp) + 1) * sizeof(Char)));
503
for (p = dp, q = dcwd->di_name; (*p++ = *q++) != '\0';)
508
p--; /* don't add a / after root */
509
for (q = cp; (*p++ = *q++) != '\0';)
519
cp = SAVE(getcwd(NULL, 0));
527
* dfollow - change to arg directory; fall back on cdpath if not valid
535
char ebuf[MAXPATHLEN];
538
cp = globone(cp, G_ERROR);
540
if (Strchr(cp, '`')) {
542
if (chdir(dptr = short2str(cp)) < 0)
543
stderror(ERR_SYSTEM, dptr, strerror(errno));
544
else if ((ptr = getcwd(ebuf, sizeof(ebuf))) && *ptr != '\0') {
546
cp = Strsave(str2short(ptr));
550
stderror(ERR_SYSTEM, dptr, ebuf);
554
(void) strncpy(ebuf, short2str(cp), MAXPATHLEN);
555
ebuf[MAXPATHLEN-1] = '\0';
557
* if we are ignoring symlinks, try to fix relatives now.
558
* if we are expading symlinks, it should be done by now.
560
dp = dnormalize(cp, symlinks == SYM_IGNORE);
561
if (chdir(short2str(dp)) >= 0) {
567
if (chdir(short2str(cp)) >= 0)
569
else if (errno != ENOENT && errno != ENOTDIR)
570
stderror(ERR_SYSTEM, ebuf, strerror(errno));
574
if (cp[0] != '/' && !prefix(STRdotsl, cp) && !prefix(STRdotdotsl, cp)
575
&& (c = adrof(STRcdpath))) {
578
Char buf[MAXPATHLEN];
580
for (cdp = c->vec; *cdp; cdp++) {
581
for (dp = buf, p = *cdp; (*dp++ = *p++) != '\0';)
584
for (p = cp; (*dp++ = *p++) != '\0';)
587
* We always want to fix the directory here
588
* If we are normalizing symlinks
590
dp = dnormalize(buf, symlinks == SYM_IGNORE ||
591
symlinks == SYM_EXPAND);
592
if (chdir(short2str(dp)) >= 0) {
597
else if (chdir(short2str(cp)) >= 0) {
605
if ((dp[0] == '/' || dp[0] == '.') && chdir(short2str(dp)) >= 0) {
613
* on login source of ~/.cshdirs, errors are eaten. the dir stack is all
614
* directories we could get to.
617
stderror(ERR_SYSTEM, ebuf, strerror(serrno));
626
* dopushd - push new directory onto directory stack.
627
* with no arguments exchange top and second.
628
* with numeric argument (+n) bring it to top.
636
register struct directory *dp;
638
int dflag = skipargs(&v, "plvn", " [-|<dir>|+<n>]");
642
cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
645
if (adrof(STRpushdtohome)) {
646
if ((cp = varval(STRhome)) == STRNULL || *cp == 0)
647
stderror(ERR_NAME | ERR_NOHOMEDIR);
648
if (chdir(short2str(cp)) < 0)
649
stderror(ERR_NAME | ERR_CANTCHANGE);
650
cp = Strsave(cp); /* hmmm... PWP */
651
if ((cp = dfollow(cp)) == NULL)
653
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
657
dp->di_next = dcwd->di_next;
659
dp->di_next->di_prev = dp;
664
if ((dp = dcwd->di_prev) == &dhead)
667
stderror(ERR_NAME | ERR_NODIR);
668
if (chdir(tmp = short2str(dp->di_name)) < 0)
669
stderror(ERR_SYSTEM, tmp, strerror(errno));
670
dp->di_prev->di_next = dp->di_next;
671
dp->di_next->di_prev = dp->di_prev;
672
dp->di_next = dcwd->di_next;
674
dcwd->di_next->di_prev = dp;
678
else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
679
stderror(ERR_NAME | ERR_TOOMANY);
683
else if ((dp = dfind(cp)) != NULL) {
686
if (chdir(tmp = short2str(dp->di_name)) < 0)
687
stderror(ERR_SYSTEM, tmp, strerror(errno));
689
* kfk - 10 Feb 1984 - added new "extraction style" pushd +n
691
if (adrof(STRdextract))
697
if ((ccp = dfollow(cp)) == NULL)
699
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
703
dp->di_next = dcwd->di_next;
705
dp->di_next->di_prev = dp;
711
* dfind - find a directory if specified by numeric (+n) argument
713
static struct directory *
717
register struct directory *dp;
723
for (ep = cp; Isdigit(*ep); ep++)
730
for (dp = dcwd; i != 0; i--) {
731
if ((dp = dp->di_prev) == &dhead)
734
stderror(ERR_NAME | ERR_DEEP);
740
* dopopd - pop a directory out of the directory stack
741
* with a numeric argument just discard it.
750
register struct directory *dp, *p = NULL;
751
int dflag = skipargs(&v, "plvn", " [-|+<n>]");
755
cp = (dflag & DIR_OLD) ? varval(STRowd) : *v;
759
else if ((dflag & DIR_OLD) == 0 && v[1] != NULL) {
760
stderror(ERR_NAME | ERR_TOOMANY);
764
else if ((dp = dfind(cp)) == 0)
765
stderror(ERR_NAME | ERR_BADDIR);
766
if (dp->di_prev == &dhead && dp->di_next == &dhead)
767
stderror(ERR_NAME | ERR_EMPTY);
771
if ((p = dp->di_prev) == &dhead)
773
if (chdir(tmp = short2str(p->di_name)) < 0)
774
stderror(ERR_SYSTEM, tmp, strerror(errno));
776
dp->di_prev->di_next = dp->di_next;
777
dp->di_next->di_prev = dp->di_prev;
788
* dfree - free the directory (or keep it if it still has ref count)
792
register struct directory *dp;
795
if (dp->di_count != 0) {
796
dp->di_next = dp->di_prev = 0;
799
xfree((ptr_t) dp->di_name);
805
* dcanon - canonicalize the pathname, removing excess ./ and ../ etc.
806
* we are of course assuming that the file system is standardly
807
* constructed (always have ..'s, directories have links)
811
register Char *cp, *p;
814
register Char *p1, *p2; /* general purpose */
820
#ifdef S_IFLNK /* if we have symlinks */
821
Char link[MAXPATHLEN];
822
char tlink[MAXPATHLEN];
828
* kim: if the path given is too long abort().
830
if (Strlen(cp) >= MAXPATHLEN)
834
* christos: if the path given does not start with a slash prepend cwd. If
835
* cwd does not start with a slash or the result would be too long abort().
837
if (!ABSOLUTEP(cp)) {
838
Char tmpdir[MAXPATHLEN];
841
if (p1 == STRNULL || !ABSOLUTEP(p1))
843
if (Strlen(p1) + Strlen(cp) + 1 >= MAXPATHLEN)
845
(void) Strcpy(tmpdir, p1);
846
(void) Strcat(tmpdir, STRslash);
847
(void) Strcat(tmpdir, cp);
849
cp = p = Strsave(tmpdir);
858
slashslash = (cp[0] == '/' && cp[1] == '/');
861
while (*p) { /* for each component */
862
sp = p; /* save slash address */
863
while (*++p == '/') /* flush extra slashes */
866
for (p1 = sp, p2 = p; (*p1++ = *p2++) != '\0';)
868
p = sp; /* save start of component */
871
while (*++p) /* find next slash or end of path */
879
if (&cp[1] == sp && sp[0] == '.' && sp[1] == '.' && sp[2] == '\0')
882
if (*sp == '\0') { /* if component is null */
883
if (--sp == cp) /* if path is one char (i.e. /) */
888
else if (sp[0] == '.' && sp[1] == 0) {
890
for (p1 = sp, p2 = p + 1; (*p1++ = *p2++) != '\0';)
899
else if (sp[0] == '.' && sp[1] == '.' && sp[2] == 0) {
901
* We have something like "yyy/xxx/..", where "yyy" can be null or
902
* a path starting at /, and "xxx" is a single component. Before
903
* compressing "xxx/..", we want to expand "yyy/xxx", if it is a
906
*--sp = 0; /* form the pathname for readlink */
907
#ifdef S_IFLNK /* if we have symlinks */
908
if (sp != cp && /* symlinks != SYM_IGNORE && */
909
(cc = readlink(short2str(cp), tlink,
910
sizeof tlink)) >= 0) {
912
(void) Strncpy(link, str2short(tlink),
913
sizeof(link) / sizeof(Char));
914
link[sizeof(link) / sizeof(Char) - 1] = '\0';
919
* Point p to the '/' in "/..", and restore the '/'.
929
* Relative path, expand it between the "yyy/" and the
930
* "/..". First, back sp up to the character past "yyy/".
937
* New length is "yyy/" + link + "/.." and rest
939
p1 = newcp = (Char *) xmalloc((size_t)
940
(((sp - cp) + cc + (p1 - p)) *
943
* Copy new path into newcp
945
for (p2 = cp; (*p1++ = *p2++) != '\0';)
947
for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
949
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
952
* Restart canonicalization at expanded "/xxx".
954
p = sp - cp - 1 + newcp;
958
* New length is link + "/.." and rest
960
p1 = newcp = (Char *) xmalloc((size_t)
961
((cc + (p1 - p)) * sizeof(Char)));
963
* Copy new path into newcp
965
for (p2 = link; (*p1++ = *p2++) != '\0';)
967
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
970
* Restart canonicalization at beginning
977
slashslash = (cp[0] == '/' && cp[1] == '/');
979
continue; /* canonicalize the link */
987
for (p1 = sp + 1, p2 = p + 1; (*p1++ = *p2++) != '\0';)
996
else { /* normal dir name (not . or .. or nothing) */
998
#ifdef S_IFLNK /* if we have symlinks */
999
if (sp != cp && symlinks == SYM_CHASE &&
1000
(cc = readlink(short2str(cp), tlink,
1001
sizeof tlink)) >= 0) {
1003
(void) Strncpy(link, str2short(tlink),
1004
sizeof(link) / sizeof(Char));
1005
link[sizeof(link) / sizeof(Char) - 1] = '\0';
1014
* point sp to p (rather than backing up).
1021
for (p1 = p; *p1++;)
1025
* Relative path, expand it between the "yyy/" and the
1026
* remainder. First, back sp up to the character past
1029
while (*--sp != '/')
1034
* New length is "yyy/" + link + "/.." and rest
1036
p1 = newcp = (Char *) xmalloc((size_t)
1037
(((sp - cp) + cc + (p1 - p))
1040
* Copy new path into newcp
1042
for (p2 = cp; (*p1++ = *p2++) != '\0';)
1044
for (p1--, p2 = link; (*p1++ = *p2++) != '\0';)
1046
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1049
* Restart canonicalization at expanded "/xxx".
1051
p = sp - cp - 1 + newcp;
1055
* New length is link + the rest
1057
p1 = newcp = (Char *) xmalloc((size_t)
1058
((cc + (p1 - p)) * sizeof(Char)));
1060
* Copy new path into newcp
1062
for (p2 = link; (*p1++ = *p2++) != '\0';)
1064
for (p1--, p2 = p; (*p1++ = *p2++) != '\0';)
1067
* Restart canonicalization at beginning
1074
slashslash = (cp[0] == '/' && cp[1] == '/');
1076
continue; /* canonicalize the link */
1078
#endif /* S_IFLNK */
1088
p1 = varval(STRhome);
1089
cc = (int) Strlen(p1);
1091
* See if we're not in a subdir of STRhome
1093
if (p1 && *p1 == '/' && (Strncmp(p1, cp, (size_t) cc) != 0 ||
1094
(cp[cc] != '/' && cp[cc] != '\0'))) {
1095
static ino_t home_ino = (ino_t) -1;
1096
static dev_t home_dev = (dev_t) -1;
1097
static Char *home_ptr = NULL;
1098
struct stat statbuf;
1102
* Get dev and ino of STRhome
1104
if (home_ptr != p1 &&
1105
stat(short2str(p1), &statbuf) != -1) {
1106
home_dev = statbuf.st_dev;
1107
home_ino = statbuf.st_ino;
1111
* Start comparing dev & ino backwards
1113
p2 = Strncpy(link, cp, sizeof(link) / sizeof(Char));
1114
link[sizeof(link) / sizeof(Char) - 1] = '\0';
1116
while (*p2 && stat(short2str(p2), &statbuf) != -1) {
1117
if (DEV_DEV_COMPARE(statbuf.st_dev, home_dev) &&
1118
statbuf.st_ino == home_ino) {
1122
if ((sp = Strrchr(p2, '/')) != NULL)
1126
* See if we found it
1130
* Use STRhome to make '~' work
1132
newcp = Strspl(p1, cp + Strlen(p2));
1137
#endif /* S_IFLNK */
1142
p = (Char *) xmalloc((size_t) (Strlen(cp) + 2) * sizeof(Char));
1144
(void) Strcpy(&p[1], cp);
1149
if (cp[1] == '/' && cp[2] == '/')
1150
(void) Strcpy(&cp[1], &cp[2]);
1157
* dnewcwd - make a new directory in the loop the current one
1161
register struct directory *dp;
1166
if (adrof(STRdunique)) {
1167
struct directory *dn;
1169
for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev)
1170
if (dn != dp && Strcmp(dn->di_name, dp->di_name) == 0) {
1171
dn->di_next->di_prev = dn->di_prev;
1172
dn->di_prev->di_next = dn->di_next;
1178
dset(dcwd->di_name);
1180
print = printd; /* if printd is set, print dirstack... */
1181
if (adrof(STRpushdsilent)) /* but pushdsilent overrides printd... */
1183
if (dflag & DIR_PRINT) /* but DIR_PRINT overrides pushdsilent... */
1185
if (bequiet) /* and bequiet overrides everything */
1189
cwd_cmd(); /* PWP: run the defined cwd command */
1197
struct directory *dn, *dp;
1199
if ((vp = adrof(STRdirstack)) == NULL)
1202
/* Free the whole stack */
1203
while ((dn = dhead.di_prev) != &dhead) {
1204
dn->di_next->di_prev = dn->di_prev;
1205
dn->di_prev->di_next = dn->di_next;
1210
/* thread the current working directory */
1211
dhead.di_prev = dhead.di_next = dcwd;
1212
dcwd->di_next = dcwd->di_prev = &dhead;
1214
/* put back the stack */
1215
for (cp = vp->vec; cp && *cp && **cp; cp++) {
1216
dp = (struct directory *) xcalloc(sizeof(struct directory), 1);
1217
dp->di_name = Strsave(*cp);
1220
dp->di_next = dcwd->di_next;
1222
dp->di_next->di_prev = dp;
1224
dgetstack(); /* Make $dirstack reflect the current state */
1232
struct directory *dn;
1234
if (adrof(STRdirstack) == NULL)
1237
for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, i++)
1239
dbp = dblk = (Char**) xmalloc((size_t) (i + 1) * sizeof(Char *));
1240
for (dn = dhead.di_prev; dn != &dhead; dn = dn->di_prev, dbp++)
1241
*dbp = Strsave(dn->di_name);
1243
setq(STRdirstack, dblk, &shvhed, VAR_READWRITE);
1247
* getstakd - added by kfk 17 Jan 1984
1248
* Support routine for the stack hack. Finds nth directory in
1249
* the directory stack, or finds last directory in stack.
1256
struct directory *dp;
1259
if (cnt < 0) { /* < 0 ==> last dir requested. */
1273
(void) Strcpy(s, dp->di_name);
1278
* Karl Kleinpaste - 10 Feb 1984
1279
* Added dextract(), which is used in pushd +n.
1280
* Instead of just rotating the entire stack around, dextract()
1281
* lets the user have the nth dir extracted from its current
1282
* position, and pushes it onto the top.
1286
struct directory *dp;
1290
dp->di_next->di_prev = dp->di_prev;
1291
dp->di_prev->di_next = dp->di_next;
1292
dp->di_next = dcwd->di_next;
1294
dp->di_next->di_prev = dp;
1302
static Char *loaddirs_cmd[] = { STRsource, NULL, NULL };
1306
loaddirs_cmd[1] = fname;
1307
else if ((fname = varval(STRdirsfile)) != STRNULL)
1308
loaddirs_cmd[1] = fname;
1310
loaddirs_cmd[1] = STRtildotdirs;
1311
dosource(loaddirs_cmd, (struct command *)0);
1316
* create a file called ~/.cshdirs which has a sequence
1317
* of pushd commands which will restore the dir stack to
1318
* its state before exit/logout. remember that the order
1319
* is reversed in the file because we are pushing.
1327
int fp, ftmp, oldidfds;
1329
extern struct directory *dcwd;
1330
struct directory *dp;
1333
Char qname[MAXPATHLEN*2];
1335
if (fname == NULL && !def)
1338
if (fname == NULL) {
1339
if ((fname = varval(STRdirsfile)) == STRNULL)
1340
fname = Strspl(varval(STRhome), &STRtildotdirs[1]);
1342
fname = Strsave(fname);
1345
fname = globone(fname, G_ERROR);
1347
if ((fp = creat(short2str(fname), 0600)) == -1) {
1348
xfree((ptr_t) fname);
1352
if ((snum = varval(STRsavedirs)) == STRNULL)
1353
num = (unsigned int) ~0;
1355
num = (unsigned int) atoi(short2str(snum));
1369
xprintf("cd %S\n", quote_meta(qname, dp->di_name));
1372
xprintf("pushd %S\n", quote_meta(qname, dp->di_name));
1377
} while ((dp = dp->di_next) != dcwd->di_next);
1382
xfree((ptr_t) fname);