xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/ftp/glob.c (revision 473:9c6ea6da29de)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*473Sbw  *	Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  *	Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  *	University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  *	The Regents of the University of California
330Sstevel@tonic-gate  *	All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  *	University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  *	software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  *	contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * C-shell glob for random programs.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include "ftp_var.h"
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #ifndef NCARGS
490Sstevel@tonic-gate #define	NCARGS	5120
500Sstevel@tonic-gate #endif
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	QUOTE 0200
530Sstevel@tonic-gate #define	TRIM 0177
540Sstevel@tonic-gate #define	eq(a, b)	(strcmp(a, b) == 0)
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * According to the person who wrote the C shell "glob" code, a reasonable
580Sstevel@tonic-gate  * limit on number of arguments would seem to be the maximum number of
590Sstevel@tonic-gate  * characters in an arg list / 6.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * XXX:	With the new VM system, NCARGS has become enormous, making
620Sstevel@tonic-gate  *	it impractical to allocate arrays with NCARGS / 6 entries on
630Sstevel@tonic-gate  *	the stack.  The proper fix is to revamp code elsewhere (in
640Sstevel@tonic-gate  *	sh.dol.c and sh.glob.c) to use a different technique for handling
650Sstevel@tonic-gate  *	command line arguments.  In the meantime, we simply fall back
660Sstevel@tonic-gate  *	on using the old value of NCARGS.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate #ifdef	notyet
690Sstevel@tonic-gate #define	GAVSIZ	(NCARGS / 6)
700Sstevel@tonic-gate #else	/* notyet */
710Sstevel@tonic-gate #define	GAVSIZ	(10240 / 6)
720Sstevel@tonic-gate #endif	/* notyet */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static	char **gargv;		/* Pointer to the (stack) arglist */
750Sstevel@tonic-gate static	char **agargv;
760Sstevel@tonic-gate static	int agargv_size;
770Sstevel@tonic-gate static	long gargc;		/* Number args in gargv */
780Sstevel@tonic-gate static	short gflag;
790Sstevel@tonic-gate static char *strspl();
800Sstevel@tonic-gate static char *strend(char *cp);
810Sstevel@tonic-gate static char *strspl(char *cp, char *dp);
820Sstevel@tonic-gate static int tglob(char c);
830Sstevel@tonic-gate static char **copyblk(char **v);
840Sstevel@tonic-gate static void ginit(char **agargv);
850Sstevel@tonic-gate static void addpath(char c);
860Sstevel@tonic-gate static int any(int c, char *s);
870Sstevel@tonic-gate static void Gcat(char *s1, char *s2);
880Sstevel@tonic-gate static void collect(char *as);
890Sstevel@tonic-gate static void acollect(char *as);
900Sstevel@tonic-gate static void sort(void);
910Sstevel@tonic-gate static void expand(char *as);
920Sstevel@tonic-gate static void matchdir(char *pattern);
930Sstevel@tonic-gate static int execbrc(char *p, char *s);
940Sstevel@tonic-gate static int ftp_fnmatch(wchar_t t_ch, wchar_t t_fch, wchar_t t_lch);
950Sstevel@tonic-gate static int gethdir(char *home);
960Sstevel@tonic-gate static void xfree(char *cp);
970Sstevel@tonic-gate static void rscan(char **t, int (*f)(char));
980Sstevel@tonic-gate static int letter(char c);
990Sstevel@tonic-gate static int digit(char c);
1000Sstevel@tonic-gate static int match(char *s, char *p);
1010Sstevel@tonic-gate static int amatch(char *s, char *p);
1020Sstevel@tonic-gate static int blklen(char **av);
1030Sstevel@tonic-gate static char **blkcpy(char **oav, char **bv);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate static	int globcnt;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate static char	*globchars = "`{[*?";
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static	char *gpath, *gpathp, *lastgpathp;
1100Sstevel@tonic-gate static	int globbed;
1110Sstevel@tonic-gate static	char *entp;
1120Sstevel@tonic-gate static	char **sortbas;
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate char **
glob(char * v)1150Sstevel@tonic-gate glob(char *v)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	char agpath[FTPBUFSIZ];
1180Sstevel@tonic-gate 	char *vv[2];
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (agargv == NULL) {
1210Sstevel@tonic-gate 		agargv = (char **)malloc(GAVSIZ * sizeof (char *));
1220Sstevel@tonic-gate 		agargv_size = GAVSIZ;
1230Sstevel@tonic-gate 		if (agargv == NULL) {
1240Sstevel@tonic-gate 			globerr = "Arguments too long.";
1250Sstevel@tonic-gate 			return (0);
1260Sstevel@tonic-gate 		}
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 	vv[0] = v;
1290Sstevel@tonic-gate 	vv[1] = 0;
1300Sstevel@tonic-gate 	globerr = 0;
1310Sstevel@tonic-gate 	gflag = 0;
1320Sstevel@tonic-gate 	rscan(vv, tglob);
1330Sstevel@tonic-gate 	if (gflag == 0)
1340Sstevel@tonic-gate 		return (copyblk(vv));
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	gpath = agpath;
1370Sstevel@tonic-gate 	gpathp = gpath;
1380Sstevel@tonic-gate 	*gpathp = 0;
1390Sstevel@tonic-gate 	lastgpathp = &gpath[sizeof (agpath) - 2];
1400Sstevel@tonic-gate 	ginit(agargv);
1410Sstevel@tonic-gate 	globcnt = 0;
1420Sstevel@tonic-gate 	collect(v);
1430Sstevel@tonic-gate 	if (globcnt == 0 && (gflag&1)) {
1440Sstevel@tonic-gate 		blkfree(gargv);
1450Sstevel@tonic-gate 		if (gargv == agargv)
1460Sstevel@tonic-gate 			agargv = 0;
1470Sstevel@tonic-gate 		gargv = 0;
1480Sstevel@tonic-gate 		return (0);
1490Sstevel@tonic-gate 	} else
1500Sstevel@tonic-gate 		return (gargv = copyblk(gargv));
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate static void
ginit(char ** agargv)1540Sstevel@tonic-gate ginit(char **agargv)
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	agargv[0] = 0;
1580Sstevel@tonic-gate 	gargv = agargv;
1590Sstevel@tonic-gate 	sortbas = agargv;
1600Sstevel@tonic-gate 	gargc = 0;
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static void
collect(char * as)1640Sstevel@tonic-gate collect(char *as)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	if (eq(as, "{") || eq(as, "{}")) {
1670Sstevel@tonic-gate 		Gcat(as, "");
1680Sstevel@tonic-gate 		sort();
1690Sstevel@tonic-gate 	} else
1700Sstevel@tonic-gate 		acollect(as);
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate static void
acollect(char * as)1740Sstevel@tonic-gate acollect(char *as)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	register long ogargc = gargc;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	gpathp = gpath; *gpathp = 0; globbed = 0;
1790Sstevel@tonic-gate 	expand(as);
1800Sstevel@tonic-gate 	if (gargc != ogargc)
1810Sstevel@tonic-gate 		sort();
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static void
sort(void)1850Sstevel@tonic-gate sort(void)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	register char **p1, **p2, *c;
1880Sstevel@tonic-gate 	char **Gvp = &gargv[gargc];
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	p1 = sortbas;
1910Sstevel@tonic-gate 	while (p1 < Gvp-1) {
1920Sstevel@tonic-gate 		p2 = p1;
1930Sstevel@tonic-gate 		while (++p2 < Gvp)
1940Sstevel@tonic-gate 			if (strcmp(*p1, *p2) > 0)
1950Sstevel@tonic-gate 				c = *p1, *p1 = *p2, *p2 = c;
1960Sstevel@tonic-gate 		p1++;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 	sortbas = Gvp;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate static void
expand(char * as)2020Sstevel@tonic-gate expand(char *as)
2030Sstevel@tonic-gate {
2040Sstevel@tonic-gate 	register char *cs;
2050Sstevel@tonic-gate 	register char *sgpathp, *oldcs;
2060Sstevel@tonic-gate 	struct stat stb;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	sgpathp = gpathp;
2090Sstevel@tonic-gate 	cs = as;
2100Sstevel@tonic-gate 	if (*cs == '~' && gpathp == gpath) {
2110Sstevel@tonic-gate 		addpath('~');
2120Sstevel@tonic-gate 		cs++;
2130Sstevel@tonic-gate 		while (letter(*cs) || digit(*cs) || *cs == '-')
2140Sstevel@tonic-gate 			addpath(*cs++);
2150Sstevel@tonic-gate 		if (!*cs || *cs == '/') {
2160Sstevel@tonic-gate 			if (gpathp != gpath + 1) {
2170Sstevel@tonic-gate 				*gpathp = 0;
2180Sstevel@tonic-gate 				if (gethdir(gpath + 1))
2190Sstevel@tonic-gate 					globerr = "Unknown user name after ~";
2200Sstevel@tonic-gate 				(void) strcpy(gpath, gpath + 1);
2210Sstevel@tonic-gate 			} else
2220Sstevel@tonic-gate 				(void) strcpy(gpath, home);
2230Sstevel@tonic-gate 			gpathp = strend(gpath);
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 	while (!any(*cs, globchars)) {
2270Sstevel@tonic-gate 		if (*cs == 0) {
2280Sstevel@tonic-gate 			if (!globbed)
2290Sstevel@tonic-gate 				Gcat(gpath, "");
2300Sstevel@tonic-gate 			else if (stat(gpath, &stb) >= 0) {
2310Sstevel@tonic-gate 				Gcat(gpath, "");
2320Sstevel@tonic-gate 				globcnt++;
2330Sstevel@tonic-gate 			}
2340Sstevel@tonic-gate 			goto endit;
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 		addpath(*cs++);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	oldcs = cs;
2390Sstevel@tonic-gate 	while (cs > as && *cs != '/')
2400Sstevel@tonic-gate 		cs--, gpathp--;
2410Sstevel@tonic-gate 	if (*cs == '/')
2420Sstevel@tonic-gate 		cs++, gpathp++;
2430Sstevel@tonic-gate 	*gpathp = 0;
2440Sstevel@tonic-gate 	if (*oldcs == '{') {
2450Sstevel@tonic-gate 		(void) execbrc(cs, ((char *)0));
2460Sstevel@tonic-gate 		return;
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	matchdir(cs);
2490Sstevel@tonic-gate endit:
2500Sstevel@tonic-gate 	gpathp = sgpathp;
2510Sstevel@tonic-gate 	*gpathp = 0;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate static void
matchdir(char * pattern)2550Sstevel@tonic-gate matchdir(char *pattern)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	struct stat stb;
2580Sstevel@tonic-gate 	register struct dirent *dp;
2590Sstevel@tonic-gate 	DIR *dirp;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * BSD/SunOS open() system call maps a null pathname into
2630Sstevel@tonic-gate 	 * "." while System V does not.
2640Sstevel@tonic-gate 	 */
2650Sstevel@tonic-gate 	if (*gpath == (char)0) {
2660Sstevel@tonic-gate 		dirp = opendir(".");
2670Sstevel@tonic-gate 	} else
2680Sstevel@tonic-gate 		dirp = opendir(gpath);
2690Sstevel@tonic-gate 	if (dirp == NULL) {
2700Sstevel@tonic-gate 		if (globbed)
2710Sstevel@tonic-gate 			return;
2720Sstevel@tonic-gate 		goto patherr2;
2730Sstevel@tonic-gate 	}
2740Sstevel@tonic-gate 	if (fstat(dirp->dd_fd, &stb) < 0)
2750Sstevel@tonic-gate 		goto patherr1;
2760Sstevel@tonic-gate 	if (!S_ISDIR(stb.st_mode)) {
2770Sstevel@tonic-gate 		errno = ENOTDIR;
2780Sstevel@tonic-gate 		goto patherr1;
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
2810Sstevel@tonic-gate 		if (dp->d_ino == 0)
2820Sstevel@tonic-gate 			continue;
2830Sstevel@tonic-gate 		if (match(dp->d_name, pattern)) {
2840Sstevel@tonic-gate 			Gcat(gpath, dp->d_name);
2850Sstevel@tonic-gate 			globcnt++;
2860Sstevel@tonic-gate 		}
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	closedir(dirp);
2890Sstevel@tonic-gate 	return;
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate patherr1:
2920Sstevel@tonic-gate 	closedir(dirp);
2930Sstevel@tonic-gate patherr2:
2940Sstevel@tonic-gate 	globerr = "Bad directory components";
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate static int
execbrc(char * p,char * s)2980Sstevel@tonic-gate execbrc(char *p, char *s)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	char restbuf[FTPBUFSIZ + 2];
3010Sstevel@tonic-gate 	register char *pe, *pm, *pl;
3020Sstevel@tonic-gate 	int brclev = 0;
3030Sstevel@tonic-gate 	char *lm, savec, *sgpathp;
3040Sstevel@tonic-gate 	int	len;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	for (lm = restbuf; *p != '{'; *lm += len, p += len) {
3070Sstevel@tonic-gate 		if ((len = mblen(p, MB_CUR_MAX)) <= 0)
3080Sstevel@tonic-gate 			len = 1;
3090Sstevel@tonic-gate 		memcpy(lm, p, len);
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	for (pe = ++p; *pe; pe += len) {
3130Sstevel@tonic-gate 		if ((len = mblen(pe, MB_CUR_MAX)) <= 0)
3140Sstevel@tonic-gate 			len = 1;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 		switch (*pe) {
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 		case '{':
3190Sstevel@tonic-gate 			brclev++;
3200Sstevel@tonic-gate 			continue;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 		case '}':
3230Sstevel@tonic-gate 			if (brclev == 0)
3240Sstevel@tonic-gate 				goto pend;
3250Sstevel@tonic-gate 			brclev--;
3260Sstevel@tonic-gate 			continue;
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 		case '[':
3290Sstevel@tonic-gate 			for (pe++; *pe && *pe != ']'; pe += len) {
3300Sstevel@tonic-gate 				if ((len = mblen(pe, MB_CUR_MAX)) <= 0)
3310Sstevel@tonic-gate 					len = 1;
3320Sstevel@tonic-gate 			}
3330Sstevel@tonic-gate 			len = 1;
3340Sstevel@tonic-gate 			continue;
3350Sstevel@tonic-gate 		}
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate pend:
3380Sstevel@tonic-gate 	brclev = 0;
3390Sstevel@tonic-gate 	for (pl = pm = p; pm <= pe; pm += len) {
3400Sstevel@tonic-gate 		if ((len = mblen(pm, MB_CUR_MAX)) <= 0)
3410Sstevel@tonic-gate 			len = 1;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		switch (*pm & (QUOTE|TRIM)) {
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		case '{':
3460Sstevel@tonic-gate 			brclev++;
3470Sstevel@tonic-gate 			continue;
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 		case '}':
3500Sstevel@tonic-gate 			if (brclev) {
3510Sstevel@tonic-gate 				brclev--;
3520Sstevel@tonic-gate 				continue;
3530Sstevel@tonic-gate 			}
3540Sstevel@tonic-gate 			goto doit;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		case ','|QUOTE:
3570Sstevel@tonic-gate 		case ',':
3580Sstevel@tonic-gate 			if (brclev)
3590Sstevel@tonic-gate 				continue;
3600Sstevel@tonic-gate doit:
3610Sstevel@tonic-gate 			savec = *pm;
3620Sstevel@tonic-gate 			*pm = 0;
3630Sstevel@tonic-gate 			(void) strcpy(lm, pl);
3640Sstevel@tonic-gate 			(void) strcat(restbuf, pe + 1);
3650Sstevel@tonic-gate 			*pm = savec;
3660Sstevel@tonic-gate 			if (s == 0) {
3670Sstevel@tonic-gate 				sgpathp = gpathp;
3680Sstevel@tonic-gate 				expand(restbuf);
3690Sstevel@tonic-gate 				gpathp = sgpathp;
3700Sstevel@tonic-gate 				*gpathp = 0;
3710Sstevel@tonic-gate 			} else if (amatch(s, restbuf))
3720Sstevel@tonic-gate 				return (1);
3730Sstevel@tonic-gate 			sort();
3740Sstevel@tonic-gate 			pl = pm + 1;
3750Sstevel@tonic-gate 			if (brclev)
3760Sstevel@tonic-gate 				return (0);
3770Sstevel@tonic-gate 			continue;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		case '[':
3800Sstevel@tonic-gate 			for (pm++; *pm && *pm != ']'; pm += len) {
3810Sstevel@tonic-gate 				if ((len = mblen(pm, MB_CUR_MAX)) <= 0)
3820Sstevel@tonic-gate 					len = 1;
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 			len = 1;
3850Sstevel@tonic-gate 			if (!*pm)
3860Sstevel@tonic-gate 				pm--;
3870Sstevel@tonic-gate 			continue;
3880Sstevel@tonic-gate 		}
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 	if (brclev)
3910Sstevel@tonic-gate 		goto doit;
3920Sstevel@tonic-gate 	return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate static int
match(char * s,char * p)3960Sstevel@tonic-gate match(char *s, char *p)
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	register int c;
3990Sstevel@tonic-gate 	register char *sentp;
4000Sstevel@tonic-gate 	char sglobbed = globbed;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	if (*s == '.' && *p != '.')
4030Sstevel@tonic-gate 		return (0);
4040Sstevel@tonic-gate 	sentp = entp;
4050Sstevel@tonic-gate 	entp = s;
4060Sstevel@tonic-gate 	c = amatch(s, p);
4070Sstevel@tonic-gate 	entp = sentp;
4080Sstevel@tonic-gate 	globbed = sglobbed;
4090Sstevel@tonic-gate 	return (c);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate static int
amatch(char * s,char * p)4130Sstevel@tonic-gate amatch(char *s, char *p)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	wchar_t scc;
4160Sstevel@tonic-gate 	int ok;
4170Sstevel@tonic-gate 	wchar_t lc1, lc2;
4180Sstevel@tonic-gate 	char *sgpathp;
4190Sstevel@tonic-gate 	struct stat stb;
4200Sstevel@tonic-gate 	wchar_t c, cc;
4210Sstevel@tonic-gate 	int	len_s, len_p;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 	globbed = 1;
4240Sstevel@tonic-gate 	for (;;) {
4250Sstevel@tonic-gate 		if ((len_s = mbtowc(&scc, s, MB_CUR_MAX)) <= 0) {
4260Sstevel@tonic-gate 			scc = (unsigned char)*s;
4270Sstevel@tonic-gate 			len_s = 1;
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 		/* scc = *s++ & TRIM; */
4300Sstevel@tonic-gate 		s += len_s;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		if ((len_p = mbtowc(&c, p, MB_CUR_MAX)) <= 0) {
4330Sstevel@tonic-gate 			c = (unsigned char)*p;
4340Sstevel@tonic-gate 			len_p = 1;
4350Sstevel@tonic-gate 		}
4360Sstevel@tonic-gate 		p += len_p;
4370Sstevel@tonic-gate 		switch (c) {
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 		case '{':
4400Sstevel@tonic-gate 			return (execbrc(p - len_p, s - len_s));
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		case '[':
4430Sstevel@tonic-gate 			ok = 0;
4440Sstevel@tonic-gate 			lc1 = 0;
4450Sstevel@tonic-gate 			while ((cc = *p) != '\0') {
4460Sstevel@tonic-gate 				if ((len_p = mbtowc(&cc, p, MB_CUR_MAX)) <= 0) {
4470Sstevel@tonic-gate 					cc = (unsigned char)*p;
4480Sstevel@tonic-gate 					len_p = 1;
4490Sstevel@tonic-gate 				}
4500Sstevel@tonic-gate 				p += len_p;
4510Sstevel@tonic-gate 				if (cc == ']') {
4520Sstevel@tonic-gate 					if (ok)
4530Sstevel@tonic-gate 						break;
4540Sstevel@tonic-gate 					return (0);
4550Sstevel@tonic-gate 				}
4560Sstevel@tonic-gate 				if (cc == '-') {
4570Sstevel@tonic-gate 					if ((len_p = mbtowc(&lc2, p,
4580Sstevel@tonic-gate 					    MB_CUR_MAX)) <= 0) {
4590Sstevel@tonic-gate 						lc2 = (unsigned char)*p;
4600Sstevel@tonic-gate 						len_p = 1;
4610Sstevel@tonic-gate 					}
4620Sstevel@tonic-gate 					p += len_p;
4630Sstevel@tonic-gate 					if (ftp_fnmatch(scc, lc1, lc2))
4640Sstevel@tonic-gate 						ok++;
4650Sstevel@tonic-gate 				} else
4660Sstevel@tonic-gate 					if (scc == (lc1 = cc))
4670Sstevel@tonic-gate 						ok++;
4680Sstevel@tonic-gate 			}
4690Sstevel@tonic-gate 			if (cc == 0)
4700Sstevel@tonic-gate 				if (!ok)
4710Sstevel@tonic-gate 					return (0);
4720Sstevel@tonic-gate 			continue;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		case '*':
4750Sstevel@tonic-gate 			if (!*p)
4760Sstevel@tonic-gate 				return (1);
4770Sstevel@tonic-gate 			if (*p == '/') {
4780Sstevel@tonic-gate 				p++;
4790Sstevel@tonic-gate 				goto slash;
4800Sstevel@tonic-gate 			}
4810Sstevel@tonic-gate 			s -= len_s;
4820Sstevel@tonic-gate 			do {
4830Sstevel@tonic-gate 				if (amatch(s, p))
4840Sstevel@tonic-gate 					return (1);
4850Sstevel@tonic-gate 			} while (*s++);
4860Sstevel@tonic-gate 			return (0);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 		case 0:
4890Sstevel@tonic-gate 			return (scc == 0);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 		default:
4920Sstevel@tonic-gate 			if (c != scc)
4930Sstevel@tonic-gate 				return (0);
4940Sstevel@tonic-gate 			continue;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		case '?':
4970Sstevel@tonic-gate 			if (scc == 0)
4980Sstevel@tonic-gate 				return (0);
4990Sstevel@tonic-gate 			continue;
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate 		case '/':
5020Sstevel@tonic-gate 			if (scc)
5030Sstevel@tonic-gate 				return (0);
5040Sstevel@tonic-gate slash:
5050Sstevel@tonic-gate 			s = entp;
5060Sstevel@tonic-gate 			sgpathp = gpathp;
5070Sstevel@tonic-gate 			while (*s)
5080Sstevel@tonic-gate 				addpath(*s++);
5090Sstevel@tonic-gate 			addpath('/');
5100Sstevel@tonic-gate 			if (stat(gpath, &stb) == 0 && S_ISDIR(stb.st_mode))
5110Sstevel@tonic-gate 				if (*p == 0) {
5120Sstevel@tonic-gate 					Gcat(gpath, "");
5130Sstevel@tonic-gate 					globcnt++;
5140Sstevel@tonic-gate 				} else
5150Sstevel@tonic-gate 					expand(p);
5160Sstevel@tonic-gate 			gpathp = sgpathp;
5170Sstevel@tonic-gate 			*gpathp = 0;
5180Sstevel@tonic-gate 			return (0);
5190Sstevel@tonic-gate 		}
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate #ifdef notdef
5240Sstevel@tonic-gate static
Gmatch(s,p)5250Sstevel@tonic-gate Gmatch(s, p)
5260Sstevel@tonic-gate 	register char *s, *p;
5270Sstevel@tonic-gate {
5280Sstevel@tonic-gate 	register int scc;
5290Sstevel@tonic-gate 	int ok, lc;
5300Sstevel@tonic-gate 	int c, cc;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	for (;;) {
5330Sstevel@tonic-gate 		scc = *s++ & TRIM;
5340Sstevel@tonic-gate 		switch (c = *p++) {
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		case '[':
5370Sstevel@tonic-gate 			ok = 0;
5380Sstevel@tonic-gate 			lc = 077777;
5390Sstevel@tonic-gate 			while (cc = *p++) {
5400Sstevel@tonic-gate 				if (cc == ']') {
5410Sstevel@tonic-gate 					if (ok)
5420Sstevel@tonic-gate 						break;
5430Sstevel@tonic-gate 					return (0);
5440Sstevel@tonic-gate 				}
5450Sstevel@tonic-gate 				if (cc == '-') {
5460Sstevel@tonic-gate 					if (lc <= scc && scc <= *p++)
5470Sstevel@tonic-gate 						ok++;
5480Sstevel@tonic-gate 				} else
5490Sstevel@tonic-gate 					if (scc == (lc = cc))
5500Sstevel@tonic-gate 						ok++;
5510Sstevel@tonic-gate 			}
5520Sstevel@tonic-gate 			if (cc == 0)
5530Sstevel@tonic-gate 				if (ok)
5540Sstevel@tonic-gate 					p--;
5550Sstevel@tonic-gate 				else
5560Sstevel@tonic-gate 					return (0);
5570Sstevel@tonic-gate 			continue;
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		case '*':
5600Sstevel@tonic-gate 			if (!*p)
5610Sstevel@tonic-gate 				return (1);
5620Sstevel@tonic-gate 			for (s--; *s; s++)
5630Sstevel@tonic-gate 				if (Gmatch(s, p))
5640Sstevel@tonic-gate 					return (1);
5650Sstevel@tonic-gate 			return (0);
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 		case 0:
5680Sstevel@tonic-gate 			return (scc == 0);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 		default:
5710Sstevel@tonic-gate 			if ((c & TRIM) != scc)
5720Sstevel@tonic-gate 				return (0);
5730Sstevel@tonic-gate 			continue;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		case '?':
5760Sstevel@tonic-gate 			if (scc == 0)
5770Sstevel@tonic-gate 				return (0);
5780Sstevel@tonic-gate 			continue;
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate 		}
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate #endif
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate static void
Gcat(char * s1,char * s2)5860Sstevel@tonic-gate Gcat(char *s1, char *s2)
5870Sstevel@tonic-gate {
5880Sstevel@tonic-gate 	if (gargc >= agargv_size - 1) {
5890Sstevel@tonic-gate 		char **tmp;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 		if (globerr) {
5920Sstevel@tonic-gate 			return;
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 		tmp = (char **)realloc(agargv,
5950Sstevel@tonic-gate 		    (agargv_size + GAVSIZ) * sizeof (char *));
5960Sstevel@tonic-gate 		if (tmp == NULL) {
5970Sstevel@tonic-gate 			globerr = "Arguments too long";
5980Sstevel@tonic-gate 			return;
5990Sstevel@tonic-gate 		} else {
6000Sstevel@tonic-gate 			agargv = tmp;
6010Sstevel@tonic-gate 			agargv_size += GAVSIZ;
6020Sstevel@tonic-gate 		}
6030Sstevel@tonic-gate 		gargv = agargv;
6040Sstevel@tonic-gate 		sortbas = agargv;
6050Sstevel@tonic-gate 	}
6060Sstevel@tonic-gate 	gargc++;
6070Sstevel@tonic-gate 	gargv[gargc] = 0;
6080Sstevel@tonic-gate 	gargv[gargc - 1] = strspl(s1, s2);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate static void
addpath(char c)6120Sstevel@tonic-gate addpath(char c)
6130Sstevel@tonic-gate {
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	if (gpathp >= lastgpathp)
6160Sstevel@tonic-gate 		globerr = "Pathname too long";
6170Sstevel@tonic-gate 	else {
6180Sstevel@tonic-gate 		*gpathp++ = c;
6190Sstevel@tonic-gate 		*gpathp = 0;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate static void
rscan(char ** t,int (* f)(char))6240Sstevel@tonic-gate rscan(char **t, int (*f)(char))
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	register char *p, c;
6270Sstevel@tonic-gate 	int	len;
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	while (p = *t++) {
6300Sstevel@tonic-gate 		if (f == tglob)
6310Sstevel@tonic-gate 			if (*p == '~')
6320Sstevel@tonic-gate 				gflag |= 2;
6330Sstevel@tonic-gate 			else if (eq(p, "{") || eq(p, "{}"))
6340Sstevel@tonic-gate 				continue;
6350Sstevel@tonic-gate 		while ((c = *p) != '\0') {
6360Sstevel@tonic-gate 			(void) (*f)(c);
6370Sstevel@tonic-gate 			if ((len = mblen(p, MB_CUR_MAX)) <= 0)
6380Sstevel@tonic-gate 				len = 1;
6390Sstevel@tonic-gate 			p += len;
6400Sstevel@tonic-gate 		}
6410Sstevel@tonic-gate 	}
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate static int
tglob(char c)6450Sstevel@tonic-gate tglob(char c)
6460Sstevel@tonic-gate {
6470Sstevel@tonic-gate 	if (any(c, globchars))
6480Sstevel@tonic-gate 		gflag |= c == '{' ? 2 : 1;
6490Sstevel@tonic-gate 	return (c);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate static int
letter(char c)6530Sstevel@tonic-gate letter(char c)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate 	return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_');
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate static int
digit(char c)6590Sstevel@tonic-gate digit(char c)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	return (c >= '0' && c <= '9');
6620Sstevel@tonic-gate }
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate static int
any(int c,char * s)6650Sstevel@tonic-gate any(int c, char *s)
6660Sstevel@tonic-gate {
6670Sstevel@tonic-gate 	int	len;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	while (*s) {
6700Sstevel@tonic-gate 		if (*s == c)
6710Sstevel@tonic-gate 			return (1);
6720Sstevel@tonic-gate 		if ((len = mblen(s, MB_CUR_MAX)) <= 0)
6730Sstevel@tonic-gate 			len = 1;
6740Sstevel@tonic-gate 		s += len;
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 	return (0);
6770Sstevel@tonic-gate }
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate static int
blklen(char ** av)6800Sstevel@tonic-gate blklen(char **av)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	register int i = 0;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	while (*av++)
6850Sstevel@tonic-gate 		i++;
6860Sstevel@tonic-gate 	return (i);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate static char **
blkcpy(char ** oav,char ** bv)6900Sstevel@tonic-gate blkcpy(char **oav, char **bv)
6910Sstevel@tonic-gate {
6920Sstevel@tonic-gate 	register char **av = oav;
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 	while (*av++ = *bv++)
6950Sstevel@tonic-gate 		continue;
6960Sstevel@tonic-gate 	return (oav);
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate void
blkfree(char ** av0)7000Sstevel@tonic-gate blkfree(char **av0)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	register char **av = av0;
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	while (*av)
7050Sstevel@tonic-gate 		xfree(*av++);
7060Sstevel@tonic-gate 	free(av0);
7070Sstevel@tonic-gate }
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate static void
xfree(char * cp)7100Sstevel@tonic-gate xfree(char *cp)
7110Sstevel@tonic-gate {
7120Sstevel@tonic-gate 	extern char end[];
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (cp >= end && cp < (char *)&cp)
7150Sstevel@tonic-gate 		free(cp);
7160Sstevel@tonic-gate }
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate static char *
strspl(char * cp,char * dp)7190Sstevel@tonic-gate strspl(char *cp, char *dp)
7200Sstevel@tonic-gate {
7210Sstevel@tonic-gate 	register char *ep = malloc((unsigned)(strlen(cp) + strlen(dp) + 1));
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	if (ep == (char *)0)
7240Sstevel@tonic-gate 		fatal("Out of memory");
7250Sstevel@tonic-gate 	(void) strcpy(ep, cp);
7260Sstevel@tonic-gate 	(void) strcat(ep, dp);
7270Sstevel@tonic-gate 	return (ep);
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate static char **
copyblk(char ** v)7310Sstevel@tonic-gate copyblk(char **v)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	register char **nv = (char **)malloc((unsigned)((blklen(v) + 1) *
7340Sstevel@tonic-gate 	    sizeof (char **)));
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (nv == (char **)0)
7370Sstevel@tonic-gate 		fatal("Out of memory");
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	return (blkcpy(nv, v));
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate static char *
strend(char * cp)7430Sstevel@tonic-gate strend(char *cp)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	while (*cp)
7470Sstevel@tonic-gate 		cp++;
7480Sstevel@tonic-gate 	return (cp);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate  * Extract a home directory from the password file
7520Sstevel@tonic-gate  * The argument points to a buffer where the name of the
7530Sstevel@tonic-gate  * user whose home directory is sought is currently.
7540Sstevel@tonic-gate  * We write the home directory of the user back there.
7550Sstevel@tonic-gate  */
756*473Sbw static int
gethdir(char * home)7570Sstevel@tonic-gate gethdir(char *home)
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	register struct passwd *pp = getpwnam(home);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	if (!pp || home + strlen(pp->pw_dir) >= lastgpathp)
7620Sstevel@tonic-gate 		return (1);
7630Sstevel@tonic-gate 	(void) strcpy(home, pp->pw_dir);
7640Sstevel@tonic-gate 	return (0);
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate static int
ftp_fnmatch(wchar_t t_ch,wchar_t t_fch,wchar_t t_lch)7680Sstevel@tonic-gate ftp_fnmatch(wchar_t t_ch, wchar_t t_fch, wchar_t t_lch)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	char	t_char[MB_LEN_MAX + 1];
7710Sstevel@tonic-gate 	char	t_patan[MB_LEN_MAX * 2 + 8];
7720Sstevel@tonic-gate 	char	*p;
7730Sstevel@tonic-gate 	int	i;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	if ((t_ch == t_fch) || (t_ch == t_lch))
7760Sstevel@tonic-gate 		return (1);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	p = t_patan;
7790Sstevel@tonic-gate 	if ((i = wctomb(t_char, (wchar_t)t_ch)) <= 0)
7800Sstevel@tonic-gate 		return (0);
7810Sstevel@tonic-gate 	t_char[i] = 0;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	*p++ = '[';
7840Sstevel@tonic-gate 	if ((i = wctomb(p, (wchar_t)t_fch)) <= 0)
7850Sstevel@tonic-gate 		return (0);
7860Sstevel@tonic-gate 	p += i;
7870Sstevel@tonic-gate 	*p++ = '-';
7880Sstevel@tonic-gate 	if ((i = wctomb(p, (wchar_t)t_lch)) <= 0)
7890Sstevel@tonic-gate 		return (0);
7900Sstevel@tonic-gate 	p += i;
7910Sstevel@tonic-gate 	*p++ = ']';
7920Sstevel@tonic-gate 	*p = 0;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	if (fnmatch(t_patan, t_char, FNM_NOESCAPE))
7950Sstevel@tonic-gate 		return (0);
7960Sstevel@tonic-gate 	return (1);
7970Sstevel@tonic-gate }
798