static char *sccsid
= "@(#)expand.c 4.2 (Berkeley) 83/09/27";
char *path
, *pathp
, *lastpathp
;
int expany
; /* any expansions done? */
* Take a list of names and expand any macros, etc.
register struct block
*prev
, *bp
, *tp
;
for (prev
= NULL
, bp
= list
; bp
!= NULL
; prev
= bp
, bp
= bp
->b_next
) {
cp
= index(bp
->b_name
, '$');
if (cp
== NULL
|| cp
!= bp
->b_name
&& cp
[-1] == '\\')
fatal("no variable name after '$'");
if ((tail
= index(cp
, '}')) == NULL
)
fatal("no variable name after '$'");
tp
= lookup(cp
, NULL
, 0);
if ((tp
= tp
->b_args
) != NULL
) {
struct block
*first
= tp
;
makestr(tp
, bp
->b_name
, tail
);
while (tp
->b_next
!= NULL
) {
makestr(tp
, bp
->b_name
, tail
);
prev
->b_next
= tp
= bp
->b_next
;
lastpathp
= &path
[sizeof pathbuf
- 2];
argv
= sortbase
= argvbuf
;
for (bp
= list
; bp
!= NULL
; bp
= bp
->b_next
)
for (bp
= list
; bp
!= NULL
; bp
= tp
) {
for (n
= 0; n
< argc
; n
++) {
fatal("ran out of memory\n");
bp
->b_next
= bp
->b_args
= NULL
;
* Concat head, bp->b_name, and tail
n
= strlen(bp
->b_name
) + strlen(head
) + strlen(tail
) + 1;
fatal("ran out of memory");
sprintf(cp
, "%s%s%s", head
, bp
->b_name
, tail
);
* If there are any Shell meta characters in the name,
* expand into a list, after searching directory
register int oargc
= argc
;
if (!strcmp(s
, "{") || !strcmp(s
, "{}")) {
* Bubble sort any new entries
register char **p1
, **p2
, *c
;
if (strcmp(*p1
, *p2
) > 0)
c
= *p1
, *p1
= *p2
, *p2
= c
;
register char *spathp
, *oldcp
;
while (!any(*cp
, shchars
)) {
else if (stat(path
, &stb
) >= 0) {
while (cp
> s
&& *cp
!= '/')
register struct direct
*dp
;
if (fstat(dirp
->dd_fd
, &stb
) < 0)
if ((stb
.st_mode
& S_IFMT
) != S_IFDIR
) {
while ((dp
= readdir(dirp
)) != NULL
)
if (match(dp
->d_name
, pattern
)) {
fatal("%s: %s\n", path
, sys_errlist
[errno
]);
char restbuf
[BUFSIZ
+ 2];
register char *pe
, *pm
, *pl
;
char *lm
, savec
, *spathp
;
for (lm
= restbuf
; *p
!= '{'; *lm
++ = *p
++)
for (pe
= ++p
; *pe
; pe
++)
for (pe
++; *pe
&& *pe
!= ']'; pe
++)
for (pl
= pm
= p
; pm
<= pe
; pm
++)
switch (*pm
& (QUOTE
|TRIM
)) {
} else if (amatch(s
, restbuf
))
for (pm
++; *pm
&& *pm
!= ']'; pm
++)
if (*s
== '.' && *p
!= '.')
return (execbrc(p
- 1, s
- 1));
if (lc
<= scc
&& scc
<= *p
++)
if (stat(path
, &stb
) == 0 &&
(stb
.st_mode
& S_IFMT
) == S_IFDIR
)
if (lc
<= scc
&& scc
<= *p
++)
int len
= strlen(s1
) + strlen(s2
) + 1;
if (nleft
<= 0 || ++argc
>= GAVSIZ
)
fatal("Arguments too long\n");
argv
[argc
- 1] = s
= (char *) malloc(len
);
fatal("ran out of memory\n");
while (*s
++ = *s1
++ & TRIM
)
while (*s
++ = *s2
++ & TRIM
)
fatal("Pathname too long\n");
* Expand file names beginning with `~' into the
* user's home directory path name.
register char *s1
, *s2
, *s3
;
register struct passwd
*pw
;
if (*file
== '\0' || *file
== '/') {
for (s3
= file
; *s3
&& *s3
!= '/'; s3
++)
fatal("unknown user %s\n", file
);
for (s1
= buf
; *s1
++ = *s2
++; )