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