/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Take a list of names and expand any macros, etc.
* wh = E_VARS if expanding variables.
* wh = E_SHELL if expanding shell characters.
* wh = E_TILDE if expanding `~'.
* or any of these or'ed together.
*
* Major portions of this were snarfed from csh/sh.glob.c.
*/
struct namelist *
expand(struct namelist *list, int wh)
{
struct namelist *nl, *prev;
int n;
char pathbuf[BUFSIZ];
char *argvbuf[GAVSIZ];
/*
* If there are any Shell meta characters in the name,
* expand into a list, after searching directory
*/
static void
expsh(char *s)
{
char *cp;
char *spathp, *oldcp;
struct stat stb;
spathp = pathp;
cp = s;
while (!any(*cp, shchars)) {
if (*cp == '\0') {
if (!expany || stat(path, &stb) >= 0) {
if (which & E_TILDE)
Cat(path, "");
else
Cat(tilde, tpathp);
}
goto endit;
}
addpath(*cp++);
}
oldcp = cp;
while (cp > s && *cp != '/')
cp--, pathp--;
if (*cp == '/')
cp++, pathp++;
*pathp = '\0';
if (*oldcp == '{') {
execbrc(cp, NULL);
return;
}
matchdir(cp);
endit:
pathp = spathp;
*pathp = '\0';
}
static void
matchdir(char *pattern)
{
struct stat stb;
struct dirent *dp;
DIR *dirp;
dirp = opendir(path);
if (dirp == NULL) {
if (expany)
return;
goto patherr2;
}
if (fstat(dirp->dd_fd, &stb) < 0)
goto patherr1;
if (!S_ISDIR(stb.st_mode)) {
errno = ENOTDIR;
goto patherr1;
}
while ((dp = readdir(dirp)) != NULL)
if (match(dp->d_name, pattern)) {
if (which & E_TILDE)
Cat(path, dp->d_name);
else {
strcpy(pathp, dp->d_name);
Cat(tilde, tpathp);
*pathp = '\0';
}
}
closedir(dirp);
return;
case '[':
ok = 0;
lc = 077777;
while ((cc = *p++) != 0) {
if (cc == ']') {
if (ok)
break;
return (0);
}
if (cc == '-') {
if (lc <= scc && scc <= *p++)
ok++;
} else
if (scc == (lc = cc))
ok++;
}
if (cc == 0) {
yyerror("Missing ']'");
return (0);
}
continue;
case '*':
if (!*p)
return (1);
if (*p == '/') {
p++;
goto slash;
}
for (s--; *s; s++)
if (amatch(s, p))
return (1);
return (0);
case '\0':
return (scc == '\0');
default:
if ((c & TRIM) != scc)
return (0);
continue;
case '?':
if (scc == '\0')
return (0);
continue;
case '/':
if (scc)
return (0);
slash:
s = entp;
spathp = pathp;
while (*s)
addpath(*s++);
addpath('/');
if (stat(path, &stb) == 0 && S_ISDIR(stb.st_mode)) {
if (*p == '\0') {
if (which & E_TILDE)
Cat(path, "");
else
Cat(tilde, tpathp);
} else
expsh(p);
}
pathp = spathp;
*pathp = '\0';
return (0);
}
}
}
static void
Cat(const char *s1, const char *s2)
{
int len = strlen(s1) + strlen(s2) + 1;
char *s;
nleft -= len;
if (nleft <= 0 || ++eargc >= GAVSIZ)
yyerror("Arguments too long");
eargv[eargc] = 0;
eargv[eargc - 1] = s = malloc(len);
if (s == NULL)
fatal("ran out of memory\n");
while ((*s++ = *s1++ & TRIM) != 0)
;
s--;
while ((*s++ = *s2++ & TRIM) != 0)
;
}
static void
addpath(int c)
{
if (pathp >= lastpathp)
yyerror("Pathname too long");
else {
*pathp++ = c & TRIM;
*pathp = '\0';
}
}
/*
* Expand file names beginning with `~' into the
* user's home directory path name. Return a pointer in buf to the
* part corresponding to `file'.
*/
char *
exptilde(char *expbuf, char *file)
{
char *s1, *s2, *s3;
extern char homedir[];
if (*file != '~') {
strcpy(expbuf, file);
return(expbuf);
}
if (*++file == '\0') {
s2 = homedir;
s3 = NULL;
} else if (*file == '/') {
s2 = homedir;
s3 = file;
} else {
s3 = file;
while (*s3 && *s3 != '/')
s3++;
if (*s3 == '/')
*s3 = '\0';
else
s3 = NULL;
if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
if ((pw = getpwnam(file)) == NULL) {
error("%s: unknown user name\n", file);
if (s3 != NULL)
*s3 = '/';
return(NULL);
}
}
if (s3 != NULL)
*s3 = '/';
s2 = pw->pw_dir;
}
for (s1 = expbuf; (*s1++ = *s2++) != 0; )
;
s2 = --s1;
if (s3 != NULL) {
s2++;
while ((*s1++ = *s3++) != 0)
;
}
return(s2);
}