Home | History | Annotate | Line # | Download | only in rdist
expand.c revision 1.1
      1  1.1  cgd /*
      2  1.1  cgd  * Copyright (c) 1983 Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.1  cgd static char sccsid[] = "@(#)expand.c	5.6 (Berkeley) 6/1/90";
     36  1.1  cgd #endif /* not lint */
     37  1.1  cgd 
     38  1.1  cgd #include "defs.h"
     39  1.1  cgd 
     40  1.1  cgd #define	GAVSIZ	NCARGS / 6
     41  1.1  cgd #define LC '{'
     42  1.1  cgd #define RC '}'
     43  1.1  cgd 
     44  1.1  cgd static char	shchars[] = "${[*?";
     45  1.1  cgd 
     46  1.1  cgd int	which;		/* bit mask of types to expand */
     47  1.1  cgd int	eargc;		/* expanded arg count */
     48  1.1  cgd char	**eargv;	/* expanded arg vectors */
     49  1.1  cgd char	*path;
     50  1.1  cgd char	*pathp;
     51  1.1  cgd char	*lastpathp;
     52  1.1  cgd char	*tilde;		/* "~user" if not expanding tilde, else "" */
     53  1.1  cgd char	*tpathp;
     54  1.1  cgd int	nleft;
     55  1.1  cgd 
     56  1.1  cgd int	expany;		/* any expansions done? */
     57  1.1  cgd char	*entp;
     58  1.1  cgd char	**sortbase;
     59  1.1  cgd 
     60  1.1  cgd char	*index();
     61  1.1  cgd int	argcmp();
     62  1.1  cgd 
     63  1.1  cgd #define sort()	qsort((char *)sortbase, &eargv[eargc] - sortbase, \
     64  1.1  cgd 		      sizeof(*sortbase), argcmp), sortbase = &eargv[eargc]
     65  1.1  cgd 
     66  1.1  cgd /*
     67  1.1  cgd  * Take a list of names and expand any macros, etc.
     68  1.1  cgd  * wh = E_VARS if expanding variables.
     69  1.1  cgd  * wh = E_SHELL if expanding shell characters.
     70  1.1  cgd  * wh = E_TILDE if expanding `~'.
     71  1.1  cgd  * or any of these or'ed together.
     72  1.1  cgd  *
     73  1.1  cgd  * Major portions of this were snarfed from csh/sh.glob.c.
     74  1.1  cgd  */
     75  1.1  cgd struct namelist *
     76  1.1  cgd expand(list, wh)
     77  1.1  cgd 	struct namelist *list;
     78  1.1  cgd 	int wh;
     79  1.1  cgd {
     80  1.1  cgd 	register struct namelist *nl, *prev;
     81  1.1  cgd 	register int n;
     82  1.1  cgd 	char pathbuf[BUFSIZ];
     83  1.1  cgd 	char *argvbuf[GAVSIZ];
     84  1.1  cgd 
     85  1.1  cgd 	if (debug) {
     86  1.1  cgd 		printf("expand(%x, %d)\nlist = ", list, wh);
     87  1.1  cgd 		prnames(list);
     88  1.1  cgd 	}
     89  1.1  cgd 
     90  1.1  cgd 	if (wh == 0) {
     91  1.1  cgd 		register char *cp;
     92  1.1  cgd 
     93  1.1  cgd 		for (nl = list; nl != NULL; nl = nl->n_next)
     94  1.1  cgd 			for (cp = nl->n_name; *cp; cp++)
     95  1.1  cgd 				*cp = *cp & TRIM;
     96  1.1  cgd 		return(list);
     97  1.1  cgd 	}
     98  1.1  cgd 
     99  1.1  cgd 	which = wh;
    100  1.1  cgd 	path = tpathp = pathp = pathbuf;
    101  1.1  cgd 	*pathp = '\0';
    102  1.1  cgd 	lastpathp = &path[sizeof pathbuf - 2];
    103  1.1  cgd 	tilde = "";
    104  1.1  cgd 	eargc = 0;
    105  1.1  cgd 	eargv = sortbase = argvbuf;
    106  1.1  cgd 	*eargv = 0;
    107  1.1  cgd 	nleft = NCARGS - 4;
    108  1.1  cgd 	/*
    109  1.1  cgd 	 * Walk the name list and expand names into eargv[];
    110  1.1  cgd 	 */
    111  1.1  cgd 	for (nl = list; nl != NULL; nl = nl->n_next)
    112  1.1  cgd 		expstr(nl->n_name);
    113  1.1  cgd 	/*
    114  1.1  cgd 	 * Take expanded list of names from eargv[] and build a new list.
    115  1.1  cgd 	 */
    116  1.1  cgd 	list = prev = NULL;
    117  1.1  cgd 	for (n = 0; n < eargc; n++) {
    118  1.1  cgd 		nl = makenl(NULL);
    119  1.1  cgd 		nl->n_name = eargv[n];
    120  1.1  cgd 		if (prev == NULL)
    121  1.1  cgd 			list = prev = nl;
    122  1.1  cgd 		else {
    123  1.1  cgd 			prev->n_next = nl;
    124  1.1  cgd 			prev = nl;
    125  1.1  cgd 		}
    126  1.1  cgd 	}
    127  1.1  cgd 	if (debug) {
    128  1.1  cgd 		printf("expanded list = ");
    129  1.1  cgd 		prnames(list);
    130  1.1  cgd 	}
    131  1.1  cgd 	return(list);
    132  1.1  cgd }
    133  1.1  cgd 
    134  1.1  cgd expstr(s)
    135  1.1  cgd 	char *s;
    136  1.1  cgd {
    137  1.1  cgd 	register char *cp, *cp1;
    138  1.1  cgd 	register struct namelist *tp;
    139  1.1  cgd 	char *tail;
    140  1.1  cgd 	char buf[BUFSIZ];
    141  1.1  cgd 	int savec, oeargc;
    142  1.1  cgd 	extern char homedir[];
    143  1.1  cgd 
    144  1.1  cgd 	if (s == NULL || *s == '\0')
    145  1.1  cgd 		return;
    146  1.1  cgd 
    147  1.1  cgd 	if ((which & E_VARS) && (cp = index(s, '$')) != NULL) {
    148  1.1  cgd 		*cp++ = '\0';
    149  1.1  cgd 		if (*cp == '\0') {
    150  1.1  cgd 			yyerror("no variable name after '$'");
    151  1.1  cgd 			return;
    152  1.1  cgd 		}
    153  1.1  cgd 		if (*cp == LC) {
    154  1.1  cgd 			cp++;
    155  1.1  cgd 			if ((tail = index(cp, RC)) == NULL) {
    156  1.1  cgd 				yyerror("unmatched '{'");
    157  1.1  cgd 				return;
    158  1.1  cgd 			}
    159  1.1  cgd 			*tail++ = savec = '\0';
    160  1.1  cgd 			if (*cp == '\0') {
    161  1.1  cgd 				yyerror("no variable name after '$'");
    162  1.1  cgd 				return;
    163  1.1  cgd 			}
    164  1.1  cgd 		} else {
    165  1.1  cgd 			tail = cp + 1;
    166  1.1  cgd 			savec = *tail;
    167  1.1  cgd 			*tail = '\0';
    168  1.1  cgd 		}
    169  1.1  cgd 		tp = lookup(cp, NULL, 0);
    170  1.1  cgd 		if (savec != '\0')
    171  1.1  cgd 			*tail = savec;
    172  1.1  cgd 		if (tp != NULL) {
    173  1.1  cgd 			for (; tp != NULL; tp = tp->n_next) {
    174  1.1  cgd 				sprintf(buf, "%s%s%s", s, tp->n_name, tail);
    175  1.1  cgd 				expstr(buf);
    176  1.1  cgd 			}
    177  1.1  cgd 			return;
    178  1.1  cgd 		}
    179  1.1  cgd 		sprintf(buf, "%s%s", s, tail);
    180  1.1  cgd 		expstr(buf);
    181  1.1  cgd 		return;
    182  1.1  cgd 	}
    183  1.1  cgd 	if ((which & ~E_VARS) == 0 || !strcmp(s, "{") || !strcmp(s, "{}")) {
    184  1.1  cgd 		Cat(s, "");
    185  1.1  cgd 		sort();
    186  1.1  cgd 		return;
    187  1.1  cgd 	}
    188  1.1  cgd 	if (*s == '~') {
    189  1.1  cgd 		cp = ++s;
    190  1.1  cgd 		if (*cp == '\0' || *cp == '/') {
    191  1.1  cgd 			tilde = "~";
    192  1.1  cgd 			cp1 = homedir;
    193  1.1  cgd 		} else {
    194  1.1  cgd 			tilde = cp1 = buf;
    195  1.1  cgd 			*cp1++ = '~';
    196  1.1  cgd 			do
    197  1.1  cgd 				*cp1++ = *cp++;
    198  1.1  cgd 			while (*cp && *cp != '/');
    199  1.1  cgd 			*cp1 = '\0';
    200  1.1  cgd 			if (pw == NULL || strcmp(pw->pw_name, buf+1) != 0) {
    201  1.1  cgd 				if ((pw = getpwnam(buf+1)) == NULL) {
    202  1.1  cgd 					strcat(buf, ": unknown user name");
    203  1.1  cgd 					yyerror(buf+1);
    204  1.1  cgd 					return;
    205  1.1  cgd 				}
    206  1.1  cgd 			}
    207  1.1  cgd 			cp1 = pw->pw_dir;
    208  1.1  cgd 			s = cp;
    209  1.1  cgd 		}
    210  1.1  cgd 		for (cp = path; *cp++ = *cp1++; )
    211  1.1  cgd 			;
    212  1.1  cgd 		tpathp = pathp = cp - 1;
    213  1.1  cgd 	} else {
    214  1.1  cgd 		tpathp = pathp = path;
    215  1.1  cgd 		tilde = "";
    216  1.1  cgd 	}
    217  1.1  cgd 	*pathp = '\0';
    218  1.1  cgd 	if (!(which & E_SHELL)) {
    219  1.1  cgd 		if (which & E_TILDE)
    220  1.1  cgd 			Cat(path, s);
    221  1.1  cgd 		else
    222  1.1  cgd 			Cat(tilde, s);
    223  1.1  cgd 		sort();
    224  1.1  cgd 		return;
    225  1.1  cgd 	}
    226  1.1  cgd 	oeargc = eargc;
    227  1.1  cgd 	expany = 0;
    228  1.1  cgd 	expsh(s);
    229  1.1  cgd 	if (eargc == oeargc)
    230  1.1  cgd 		Cat(s, "");		/* "nonomatch" is set */
    231  1.1  cgd 	sort();
    232  1.1  cgd }
    233  1.1  cgd 
    234  1.1  cgd static
    235  1.1  cgd argcmp(a1, a2)
    236  1.1  cgd 	char **a1, **a2;
    237  1.1  cgd {
    238  1.1  cgd 
    239  1.1  cgd 	return (strcmp(*a1, *a2));
    240  1.1  cgd }
    241  1.1  cgd 
    242  1.1  cgd /*
    243  1.1  cgd  * If there are any Shell meta characters in the name,
    244  1.1  cgd  * expand into a list, after searching directory
    245  1.1  cgd  */
    246  1.1  cgd expsh(s)
    247  1.1  cgd 	char *s;
    248  1.1  cgd {
    249  1.1  cgd 	register char *cp;
    250  1.1  cgd 	register char *spathp, *oldcp;
    251  1.1  cgd 	struct stat stb;
    252  1.1  cgd 
    253  1.1  cgd 	spathp = pathp;
    254  1.1  cgd 	cp = s;
    255  1.1  cgd 	while (!any(*cp, shchars)) {
    256  1.1  cgd 		if (*cp == '\0') {
    257  1.1  cgd 			if (!expany || stat(path, &stb) >= 0) {
    258  1.1  cgd 				if (which & E_TILDE)
    259  1.1  cgd 					Cat(path, "");
    260  1.1  cgd 				else
    261  1.1  cgd 					Cat(tilde, tpathp);
    262  1.1  cgd 			}
    263  1.1  cgd 			goto endit;
    264  1.1  cgd 		}
    265  1.1  cgd 		addpath(*cp++);
    266  1.1  cgd 	}
    267  1.1  cgd 	oldcp = cp;
    268  1.1  cgd 	while (cp > s && *cp != '/')
    269  1.1  cgd 		cp--, pathp--;
    270  1.1  cgd 	if (*cp == '/')
    271  1.1  cgd 		cp++, pathp++;
    272  1.1  cgd 	*pathp = '\0';
    273  1.1  cgd 	if (*oldcp == '{') {
    274  1.1  cgd 		execbrc(cp, NULL);
    275  1.1  cgd 		return;
    276  1.1  cgd 	}
    277  1.1  cgd 	matchdir(cp);
    278  1.1  cgd endit:
    279  1.1  cgd 	pathp = spathp;
    280  1.1  cgd 	*pathp = '\0';
    281  1.1  cgd }
    282  1.1  cgd 
    283  1.1  cgd matchdir(pattern)
    284  1.1  cgd 	char *pattern;
    285  1.1  cgd {
    286  1.1  cgd 	struct stat stb;
    287  1.1  cgd 	register struct direct *dp;
    288  1.1  cgd 	DIR *dirp;
    289  1.1  cgd 
    290  1.1  cgd 	dirp = opendir(path);
    291  1.1  cgd 	if (dirp == NULL) {
    292  1.1  cgd 		if (expany)
    293  1.1  cgd 			return;
    294  1.1  cgd 		goto patherr2;
    295  1.1  cgd 	}
    296  1.1  cgd 	if (fstat(dirp->dd_fd, &stb) < 0)
    297  1.1  cgd 		goto patherr1;
    298  1.1  cgd 	if (!ISDIR(stb.st_mode)) {
    299  1.1  cgd 		errno = ENOTDIR;
    300  1.1  cgd 		goto patherr1;
    301  1.1  cgd 	}
    302  1.1  cgd 	while ((dp = readdir(dirp)) != NULL)
    303  1.1  cgd 		if (match(dp->d_name, pattern)) {
    304  1.1  cgd 			if (which & E_TILDE)
    305  1.1  cgd 				Cat(path, dp->d_name);
    306  1.1  cgd 			else {
    307  1.1  cgd 				strcpy(pathp, dp->d_name);
    308  1.1  cgd 				Cat(tilde, tpathp);
    309  1.1  cgd 				*pathp = '\0';
    310  1.1  cgd 			}
    311  1.1  cgd 		}
    312  1.1  cgd 	closedir(dirp);
    313  1.1  cgd 	return;
    314  1.1  cgd 
    315  1.1  cgd patherr1:
    316  1.1  cgd 	closedir(dirp);
    317  1.1  cgd patherr2:
    318  1.1  cgd 	strcat(path, ": ");
    319  1.1  cgd 	strcat(path, strerror(errno));
    320  1.1  cgd 	yyerror(path);
    321  1.1  cgd }
    322  1.1  cgd 
    323  1.1  cgd execbrc(p, s)
    324  1.1  cgd 	char *p, *s;
    325  1.1  cgd {
    326  1.1  cgd 	char restbuf[BUFSIZ + 2];
    327  1.1  cgd 	register char *pe, *pm, *pl;
    328  1.1  cgd 	int brclev = 0;
    329  1.1  cgd 	char *lm, savec, *spathp;
    330  1.1  cgd 
    331  1.1  cgd 	for (lm = restbuf; *p != '{'; *lm++ = *p++)
    332  1.1  cgd 		continue;
    333  1.1  cgd 	for (pe = ++p; *pe; pe++)
    334  1.1  cgd 		switch (*pe) {
    335  1.1  cgd 
    336  1.1  cgd 		case '{':
    337  1.1  cgd 			brclev++;
    338  1.1  cgd 			continue;
    339  1.1  cgd 
    340  1.1  cgd 		case '}':
    341  1.1  cgd 			if (brclev == 0)
    342  1.1  cgd 				goto pend;
    343  1.1  cgd 			brclev--;
    344  1.1  cgd 			continue;
    345  1.1  cgd 
    346  1.1  cgd 		case '[':
    347  1.1  cgd 			for (pe++; *pe && *pe != ']'; pe++)
    348  1.1  cgd 				continue;
    349  1.1  cgd 			if (!*pe)
    350  1.1  cgd 				yyerror("Missing ']'");
    351  1.1  cgd 			continue;
    352  1.1  cgd 		}
    353  1.1  cgd pend:
    354  1.1  cgd 	if (brclev || !*pe) {
    355  1.1  cgd 		yyerror("Missing '}'");
    356  1.1  cgd 		return (0);
    357  1.1  cgd 	}
    358  1.1  cgd 	for (pl = pm = p; pm <= pe; pm++)
    359  1.1  cgd 		switch (*pm & (QUOTE|TRIM)) {
    360  1.1  cgd 
    361  1.1  cgd 		case '{':
    362  1.1  cgd 			brclev++;
    363  1.1  cgd 			continue;
    364  1.1  cgd 
    365  1.1  cgd 		case '}':
    366  1.1  cgd 			if (brclev) {
    367  1.1  cgd 				brclev--;
    368  1.1  cgd 				continue;
    369  1.1  cgd 			}
    370  1.1  cgd 			goto doit;
    371  1.1  cgd 
    372  1.1  cgd 		case ',':
    373  1.1  cgd 			if (brclev)
    374  1.1  cgd 				continue;
    375  1.1  cgd doit:
    376  1.1  cgd 			savec = *pm;
    377  1.1  cgd 			*pm = 0;
    378  1.1  cgd 			strcpy(lm, pl);
    379  1.1  cgd 			strcat(restbuf, pe + 1);
    380  1.1  cgd 			*pm = savec;
    381  1.1  cgd 			if (s == 0) {
    382  1.1  cgd 				spathp = pathp;
    383  1.1  cgd 				expsh(restbuf);
    384  1.1  cgd 				pathp = spathp;
    385  1.1  cgd 				*pathp = 0;
    386  1.1  cgd 			} else if (amatch(s, restbuf))
    387  1.1  cgd 				return (1);
    388  1.1  cgd 			sort();
    389  1.1  cgd 			pl = pm + 1;
    390  1.1  cgd 			continue;
    391  1.1  cgd 
    392  1.1  cgd 		case '[':
    393  1.1  cgd 			for (pm++; *pm && *pm != ']'; pm++)
    394  1.1  cgd 				continue;
    395  1.1  cgd 			if (!*pm)
    396  1.1  cgd 				yyerror("Missing ']'");
    397  1.1  cgd 			continue;
    398  1.1  cgd 		}
    399  1.1  cgd 	return (0);
    400  1.1  cgd }
    401  1.1  cgd 
    402  1.1  cgd match(s, p)
    403  1.1  cgd 	char *s, *p;
    404  1.1  cgd {
    405  1.1  cgd 	register int c;
    406  1.1  cgd 	register char *sentp;
    407  1.1  cgd 	char sexpany = expany;
    408  1.1  cgd 
    409  1.1  cgd 	if (*s == '.' && *p != '.')
    410  1.1  cgd 		return (0);
    411  1.1  cgd 	sentp = entp;
    412  1.1  cgd 	entp = s;
    413  1.1  cgd 	c = amatch(s, p);
    414  1.1  cgd 	entp = sentp;
    415  1.1  cgd 	expany = sexpany;
    416  1.1  cgd 	return (c);
    417  1.1  cgd }
    418  1.1  cgd 
    419  1.1  cgd amatch(s, p)
    420  1.1  cgd 	register char *s, *p;
    421  1.1  cgd {
    422  1.1  cgd 	register int scc;
    423  1.1  cgd 	int ok, lc;
    424  1.1  cgd 	char *spathp;
    425  1.1  cgd 	struct stat stb;
    426  1.1  cgd 	int c, cc;
    427  1.1  cgd 
    428  1.1  cgd 	expany = 1;
    429  1.1  cgd 	for (;;) {
    430  1.1  cgd 		scc = *s++ & TRIM;
    431  1.1  cgd 		switch (c = *p++) {
    432  1.1  cgd 
    433  1.1  cgd 		case '{':
    434  1.1  cgd 			return (execbrc(p - 1, s - 1));
    435  1.1  cgd 
    436  1.1  cgd 		case '[':
    437  1.1  cgd 			ok = 0;
    438  1.1  cgd 			lc = 077777;
    439  1.1  cgd 			while (cc = *p++) {
    440  1.1  cgd 				if (cc == ']') {
    441  1.1  cgd 					if (ok)
    442  1.1  cgd 						break;
    443  1.1  cgd 					return (0);
    444  1.1  cgd 				}
    445  1.1  cgd 				if (cc == '-') {
    446  1.1  cgd 					if (lc <= scc && scc <= *p++)
    447  1.1  cgd 						ok++;
    448  1.1  cgd 				} else
    449  1.1  cgd 					if (scc == (lc = cc))
    450  1.1  cgd 						ok++;
    451  1.1  cgd 			}
    452  1.1  cgd 			if (cc == 0) {
    453  1.1  cgd 				yyerror("Missing ']'");
    454  1.1  cgd 				return (0);
    455  1.1  cgd 			}
    456  1.1  cgd 			continue;
    457  1.1  cgd 
    458  1.1  cgd 		case '*':
    459  1.1  cgd 			if (!*p)
    460  1.1  cgd 				return (1);
    461  1.1  cgd 			if (*p == '/') {
    462  1.1  cgd 				p++;
    463  1.1  cgd 				goto slash;
    464  1.1  cgd 			}
    465  1.1  cgd 			for (s--; *s; s++)
    466  1.1  cgd 				if (amatch(s, p))
    467  1.1  cgd 					return (1);
    468  1.1  cgd 			return (0);
    469  1.1  cgd 
    470  1.1  cgd 		case '\0':
    471  1.1  cgd 			return (scc == '\0');
    472  1.1  cgd 
    473  1.1  cgd 		default:
    474  1.1  cgd 			if ((c & TRIM) != scc)
    475  1.1  cgd 				return (0);
    476  1.1  cgd 			continue;
    477  1.1  cgd 
    478  1.1  cgd 		case '?':
    479  1.1  cgd 			if (scc == '\0')
    480  1.1  cgd 				return (0);
    481  1.1  cgd 			continue;
    482  1.1  cgd 
    483  1.1  cgd 		case '/':
    484  1.1  cgd 			if (scc)
    485  1.1  cgd 				return (0);
    486  1.1  cgd slash:
    487  1.1  cgd 			s = entp;
    488  1.1  cgd 			spathp = pathp;
    489  1.1  cgd 			while (*s)
    490  1.1  cgd 				addpath(*s++);
    491  1.1  cgd 			addpath('/');
    492  1.1  cgd 			if (stat(path, &stb) == 0 && ISDIR(stb.st_mode))
    493  1.1  cgd 				if (*p == '\0') {
    494  1.1  cgd 					if (which & E_TILDE)
    495  1.1  cgd 						Cat(path, "");
    496  1.1  cgd 					else
    497  1.1  cgd 						Cat(tilde, tpathp);
    498  1.1  cgd 				} else
    499  1.1  cgd 					expsh(p);
    500  1.1  cgd 			pathp = spathp;
    501  1.1  cgd 			*pathp = '\0';
    502  1.1  cgd 			return (0);
    503  1.1  cgd 		}
    504  1.1  cgd 	}
    505  1.1  cgd }
    506  1.1  cgd 
    507  1.1  cgd smatch(s, p)
    508  1.1  cgd 	register char *s, *p;
    509  1.1  cgd {
    510  1.1  cgd 	register int scc;
    511  1.1  cgd 	int ok, lc;
    512  1.1  cgd 	int c, cc;
    513  1.1  cgd 
    514  1.1  cgd 	for (;;) {
    515  1.1  cgd 		scc = *s++ & TRIM;
    516  1.1  cgd 		switch (c = *p++) {
    517  1.1  cgd 
    518  1.1  cgd 		case '[':
    519  1.1  cgd 			ok = 0;
    520  1.1  cgd 			lc = 077777;
    521  1.1  cgd 			while (cc = *p++) {
    522  1.1  cgd 				if (cc == ']') {
    523  1.1  cgd 					if (ok)
    524  1.1  cgd 						break;
    525  1.1  cgd 					return (0);
    526  1.1  cgd 				}
    527  1.1  cgd 				if (cc == '-') {
    528  1.1  cgd 					if (lc <= scc && scc <= *p++)
    529  1.1  cgd 						ok++;
    530  1.1  cgd 				} else
    531  1.1  cgd 					if (scc == (lc = cc))
    532  1.1  cgd 						ok++;
    533  1.1  cgd 			}
    534  1.1  cgd 			if (cc == 0) {
    535  1.1  cgd 				yyerror("Missing ']'");
    536  1.1  cgd 				return (0);
    537  1.1  cgd 			}
    538  1.1  cgd 			continue;
    539  1.1  cgd 
    540  1.1  cgd 		case '*':
    541  1.1  cgd 			if (!*p)
    542  1.1  cgd 				return (1);
    543  1.1  cgd 			for (s--; *s; s++)
    544  1.1  cgd 				if (smatch(s, p))
    545  1.1  cgd 					return (1);
    546  1.1  cgd 			return (0);
    547  1.1  cgd 
    548  1.1  cgd 		case '\0':
    549  1.1  cgd 			return (scc == '\0');
    550  1.1  cgd 
    551  1.1  cgd 		default:
    552  1.1  cgd 			if ((c & TRIM) != scc)
    553  1.1  cgd 				return (0);
    554  1.1  cgd 			continue;
    555  1.1  cgd 
    556  1.1  cgd 		case '?':
    557  1.1  cgd 			if (scc == 0)
    558  1.1  cgd 				return (0);
    559  1.1  cgd 			continue;
    560  1.1  cgd 
    561  1.1  cgd 		}
    562  1.1  cgd 	}
    563  1.1  cgd }
    564  1.1  cgd 
    565  1.1  cgd Cat(s1, s2)
    566  1.1  cgd 	register char *s1, *s2;
    567  1.1  cgd {
    568  1.1  cgd 	int len = strlen(s1) + strlen(s2) + 1;
    569  1.1  cgd 	register char *s;
    570  1.1  cgd 
    571  1.1  cgd 	nleft -= len;
    572  1.1  cgd 	if (nleft <= 0 || ++eargc >= GAVSIZ)
    573  1.1  cgd 		yyerror("Arguments too long");
    574  1.1  cgd 	eargv[eargc] = 0;
    575  1.1  cgd 	eargv[eargc - 1] = s = malloc(len);
    576  1.1  cgd 	if (s == NULL)
    577  1.1  cgd 		fatal("ran out of memory\n");
    578  1.1  cgd 	while (*s++ = *s1++ & TRIM)
    579  1.1  cgd 		;
    580  1.1  cgd 	s--;
    581  1.1  cgd 	while (*s++ = *s2++ & TRIM)
    582  1.1  cgd 		;
    583  1.1  cgd }
    584  1.1  cgd 
    585  1.1  cgd addpath(c)
    586  1.1  cgd 	char c;
    587  1.1  cgd {
    588  1.1  cgd 
    589  1.1  cgd 	if (pathp >= lastpathp)
    590  1.1  cgd 		yyerror("Pathname too long");
    591  1.1  cgd 	else {
    592  1.1  cgd 		*pathp++ = c & TRIM;
    593  1.1  cgd 		*pathp = '\0';
    594  1.1  cgd 	}
    595  1.1  cgd }
    596  1.1  cgd 
    597  1.1  cgd /*
    598  1.1  cgd  * Expand file names beginning with `~' into the
    599  1.1  cgd  * user's home directory path name. Return a pointer in buf to the
    600  1.1  cgd  * part corresponding to `file'.
    601  1.1  cgd  */
    602  1.1  cgd char *
    603  1.1  cgd exptilde(buf, file)
    604  1.1  cgd 	char buf[];
    605  1.1  cgd 	register char *file;
    606  1.1  cgd {
    607  1.1  cgd 	register char *s1, *s2, *s3;
    608  1.1  cgd 	extern char homedir[];
    609  1.1  cgd 
    610  1.1  cgd 	if (*file != '~') {
    611  1.1  cgd 		strcpy(buf, file);
    612  1.1  cgd 		return(buf);
    613  1.1  cgd 	}
    614  1.1  cgd 	if (*++file == '\0') {
    615  1.1  cgd 		s2 = homedir;
    616  1.1  cgd 		s3 = NULL;
    617  1.1  cgd 	} else if (*file == '/') {
    618  1.1  cgd 		s2 = homedir;
    619  1.1  cgd 		s3 = file;
    620  1.1  cgd 	} else {
    621  1.1  cgd 		s3 = file;
    622  1.1  cgd 		while (*s3 && *s3 != '/')
    623  1.1  cgd 			s3++;
    624  1.1  cgd 		if (*s3 == '/')
    625  1.1  cgd 			*s3 = '\0';
    626  1.1  cgd 		else
    627  1.1  cgd 			s3 = NULL;
    628  1.1  cgd 		if (pw == NULL || strcmp(pw->pw_name, file) != 0) {
    629  1.1  cgd 			if ((pw = getpwnam(file)) == NULL) {
    630  1.1  cgd 				error("%s: unknown user name\n", file);
    631  1.1  cgd 				if (s3 != NULL)
    632  1.1  cgd 					*s3 = '/';
    633  1.1  cgd 				return(NULL);
    634  1.1  cgd 			}
    635  1.1  cgd 		}
    636  1.1  cgd 		if (s3 != NULL)
    637  1.1  cgd 			*s3 = '/';
    638  1.1  cgd 		s2 = pw->pw_dir;
    639  1.1  cgd 	}
    640  1.1  cgd 	for (s1 = buf; *s1++ = *s2++; )
    641  1.1  cgd 		;
    642  1.1  cgd 	s2 = --s1;
    643  1.1  cgd 	if (s3 != NULL) {
    644  1.1  cgd 		s2++;
    645  1.1  cgd 		while (*s1++ = *s3++)
    646  1.1  cgd 			;
    647  1.1  cgd 	}
    648  1.1  cgd 	return(s2);
    649  1.1  cgd }
    650