17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*740638c8Sbw  *	Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  *	Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  *	The Regents of the University of California
337c478bd9Sstevel@tonic-gate  *	All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  *	University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  *	software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  *	contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * C-shell glob for random programs.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include "ftp_var.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifndef NCARGS
477c478bd9Sstevel@tonic-gate #define	NCARGS	5120
487c478bd9Sstevel@tonic-gate #endif
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	QUOTE 0200
517c478bd9Sstevel@tonic-gate #define	TRIM 0177
527c478bd9Sstevel@tonic-gate #define	eq(a, b)	(strcmp(a, b) == 0)
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * According to the person who wrote the C shell "glob" code, a reasonable
567c478bd9Sstevel@tonic-gate  * limit on number of arguments would seem to be the maximum number of
577c478bd9Sstevel@tonic-gate  * characters in an arg list / 6.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * XXX:	With the new VM system, NCARGS has become enormous, making
607c478bd9Sstevel@tonic-gate  *	it impractical to allocate arrays with NCARGS / 6 entries on
617c478bd9Sstevel@tonic-gate  *	the stack.  The proper fix is to revamp code elsewhere (in
627c478bd9Sstevel@tonic-gate  *	sh.dol.c and sh.glob.c) to use a different technique for handling
637c478bd9Sstevel@tonic-gate  *	command line arguments.  In the meantime, we simply fall back
647c478bd9Sstevel@tonic-gate  *	on using the old value of NCARGS.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate #ifdef	notyet
677c478bd9Sstevel@tonic-gate #define	GAVSIZ	(NCARGS / 6)
687c478bd9Sstevel@tonic-gate #else	/* notyet */
697c478bd9Sstevel@tonic-gate #define	GAVSIZ	(10240 / 6)
707c478bd9Sstevel@tonic-gate #endif	/* notyet */
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static	char **gargv;		/* Pointer to the (stack) arglist */
737c478bd9Sstevel@tonic-gate static	char **agargv;
747c478bd9Sstevel@tonic-gate static	int agargv_size;
757c478bd9Sstevel@tonic-gate static	long gargc;		/* Number args in gargv */
767c478bd9Sstevel@tonic-gate static	short gflag;
777c478bd9Sstevel@tonic-gate static char *strspl();
787c478bd9Sstevel@tonic-gate static char *strend(char *cp);
797c478bd9Sstevel@tonic-gate static char *strspl(char *cp, char *dp);
807c478bd9Sstevel@tonic-gate static int tglob(char c);
817c478bd9Sstevel@tonic-gate static char **copyblk(char **v);
827c478bd9Sstevel@tonic-gate static void ginit(char **agargv);
837c478bd9Sstevel@tonic-gate static void addpath(char c);
847c478bd9Sstevel@tonic-gate static int any(int c, char *s);
857c478bd9Sstevel@tonic-gate static void Gcat(char *s1, char *s2);
867c478bd9Sstevel@tonic-gate static void collect(char *as);
877c478bd9Sstevel@tonic-gate static void acollect(char *as);
887c478bd9Sstevel@tonic-gate static void sort(void);
897c478bd9Sstevel@tonic-gate static void expand(char *as);
907c478bd9Sstevel@tonic-gate static void matchdir(char *pattern);
917c478bd9Sstevel@tonic-gate static int execbrc(char *p, char *s);
927c478bd9Sstevel@tonic-gate static int ftp_fnmatch(wchar_t t_ch, wchar_t t_fch, wchar_t t_lch);
937c478bd9Sstevel@tonic-gate static int gethdir(char *home);
947c478bd9Sstevel@tonic-gate static void xfree(char *cp);
957c478bd9Sstevel@tonic-gate static void rscan(char **t, int (*f)(char));
967c478bd9Sstevel@tonic-gate static int letter(char c);
977c478bd9Sstevel@tonic-gate static int digit(char c);
987c478bd9Sstevel@tonic-gate static int match(char *s, char *p);
997c478bd9Sstevel@tonic-gate static int amatch(char *s, char *p);
1007c478bd9Sstevel@tonic-gate static int blklen(char **av);
1017c478bd9Sstevel@tonic-gate static char **blkcpy(char **oav, char **bv);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate static	int globcnt;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static char	*globchars = "`{[*?";
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static	char *gpath, *gpathp, *lastgpathp;
1087c478bd9Sstevel@tonic-gate static	int globbed;
1097c478bd9Sstevel@tonic-gate static	char *entp;
1107c478bd9Sstevel@tonic-gate static	char **sortbas;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate char **
glob(char * v)1137c478bd9Sstevel@tonic-gate glob(char *v)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate 	char agpath[FTPBUFSIZ];
1167c478bd9Sstevel@tonic-gate 	char *vv[2];
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (agargv == NULL) {
1197c478bd9Sstevel@tonic-gate 		agargv = (char **)malloc(GAVSIZ * sizeof (char *));
1207c478bd9Sstevel@tonic-gate 		agargv_size = GAVSIZ;
1217c478bd9Sstevel@tonic-gate 		if (agargv == NULL) {
1227c478bd9Sstevel@tonic-gate 			globerr = "Arguments too long.";
1237c478bd9Sstevel@tonic-gate 			return (0);
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 	vv[0] = v;
1277c478bd9Sstevel@tonic-gate 	vv[1] = 0;
1287c478bd9Sstevel@tonic-gate 	globerr = 0;
1297c478bd9Sstevel@tonic-gate 	gflag = 0;
1307c478bd9Sstevel@tonic-gate 	rscan(vv, tglob);
1317c478bd9Sstevel@tonic-gate 	if (gflag == 0)
1327c478bd9Sstevel@tonic-gate 		return (copyblk(vv));
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	gpath = agpath;
1357c478bd9Sstevel@tonic-gate 	gpathp = gpath;
1367c478bd9Sstevel@tonic-gate 	*gpathp = 0;
1377c478bd9Sstevel@tonic-gate 	lastgpathp = &gpath[sizeof (agpath) - 2];
1387c478bd9Sstevel@tonic-gate 	ginit(agargv);
1397c478bd9Sstevel@tonic-gate 	globcnt = 0;
1407c478bd9Sstevel@tonic-gate 	collect(v);
1417c478bd9Sstevel@tonic-gate 	if (globcnt == 0 && (gflag&1)) {
1427c478bd9Sstevel@tonic-gate 		blkfree(gargv);
1437c478bd9Sstevel@tonic-gate 		if (gargv == agargv)
1447c478bd9Sstevel@tonic-gate 			agargv = 0;
1457c478bd9Sstevel@tonic-gate 		gargv = 0;
1467c478bd9Sstevel@tonic-gate 		return (0);
1477c478bd9Sstevel@tonic-gate 	} else
1487c478bd9Sstevel@tonic-gate 		return (gargv = copyblk(gargv));
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static void
ginit(char ** agargv)1527c478bd9Sstevel@tonic-gate ginit(char **agargv)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	agargv[0] = 0;
1567c478bd9Sstevel@tonic-gate 	gargv = agargv;
1577c478bd9Sstevel@tonic-gate 	sortbas = agargv;
1587c478bd9Sstevel@tonic-gate 	gargc = 0;
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate static void
collect(char * as)1627c478bd9Sstevel@tonic-gate collect(char *as)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	if (eq(as, "{") || eq(as, "{}")) {
1657c478bd9Sstevel@tonic-gate 		Gcat(as, "");
1667c478bd9Sstevel@tonic-gate 		sort();
1677c478bd9Sstevel@tonic-gate 	} else
1687c478bd9Sstevel@tonic-gate 		acollect(as);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate static void
acollect(char * as)1727c478bd9Sstevel@tonic-gate acollect(char *as)
1737c478bd9Sstevel@tonic-gate {
1747c478bd9Sstevel@tonic-gate 	register long ogargc = gargc;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	gpathp = gpath; *gpathp = 0; globbed = 0;
1777c478bd9Sstevel@tonic-gate 	expand(as);
1787c478bd9Sstevel@tonic-gate 	if (gargc != ogargc)
1797c478bd9Sstevel@tonic-gate 		sort();
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate static void
sort(void)1837c478bd9Sstevel@tonic-gate sort(void)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	register char **p1, **p2, *c;
1867c478bd9Sstevel@tonic-gate 	char **Gvp = &gargv[gargc];
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	p1 = sortbas;
1897c478bd9Sstevel@tonic-gate 	while (p1 < Gvp-1) {
1907c478bd9Sstevel@tonic-gate 		p2 = p1;
1917c478bd9Sstevel@tonic-gate 		while (++p2 < Gvp)
1927c478bd9Sstevel@tonic-gate 			if (strcmp(*p1, *p2) > 0)
1937c478bd9Sstevel@tonic-gate 				c = *p1, *p1 = *p2, *p2 = c;
1947c478bd9Sstevel@tonic-gate 		p1++;
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 	sortbas = Gvp;
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate static void
expand(char * as)2007c478bd9Sstevel@tonic-gate expand(char *as)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	register char *cs;
2037c478bd9Sstevel@tonic-gate 	register char *sgpathp, *oldcs;
2047c478bd9Sstevel@tonic-gate 	struct stat stb;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	sgpathp = gpathp;
2077c478bd9Sstevel@tonic-gate 	cs = as;
2087c478bd9Sstevel@tonic-gate 	if (*cs == '~' && gpathp == gpath) {
2097c478bd9Sstevel@tonic-gate 		addpath('~');
2107c478bd9Sstevel@tonic-gate 		cs++;
2117c478bd9Sstevel@tonic-gate 		while (letter(*cs) || digit(*cs) || *cs == '-')
2127c478bd9Sstevel@tonic-gate 			addpath(*cs++);
2137c478bd9Sstevel@tonic-gate 		if (!*cs || *cs == '/') {
2147c478bd9Sstevel@tonic-gate 			if (gpathp != gpath + 1) {
2157c478bd9Sstevel@tonic-gate 				*gpathp = 0;
2167c478bd9Sstevel@tonic-gate 				if (gethdir(gpath + 1))
2177c478bd9Sstevel@tonic-gate 					globerr = "Unknown user name after ~";
2187c478bd9Sstevel@tonic-gate 				(void) strcpy(gpath, gpath + 1);
2197c478bd9Sstevel@tonic-gate 			} else
2207c478bd9Sstevel@tonic-gate 				(void) strcpy(gpath, home);
2217c478bd9Sstevel@tonic-gate 			gpathp = strend(gpath);
2227c478bd9Sstevel@tonic-gate 		}
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	while (!any(*cs, globchars)) {
2257c478bd9Sstevel@tonic-gate 		if (*cs == 0) {
2267c478bd9Sstevel@tonic-gate 			if (!globbed)
2277c478bd9Sstevel@tonic-gate 				Gcat(gpath, "");
2287c478bd9Sstevel@tonic-gate 			else if (stat(gpath, &stb) >= 0) {
2297c478bd9Sstevel@tonic-gate 				Gcat(gpath, "");
2307c478bd9Sstevel@tonic-gate 				globcnt++;
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 			goto endit;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 		addpath(*cs++);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	oldcs = cs;
2377c478bd9Sstevel@tonic-gate 	while (cs > as && *cs != '/')
2387c478bd9Sstevel@tonic-gate 		cs--, gpathp--;
2397c478bd9Sstevel@tonic-gate 	if (*cs == '/')
2407c478bd9Sstevel@tonic-gate 		cs++, gpathp++;
2417c478bd9Sstevel@tonic-gate 	*gpathp = 0;
2427c478bd9Sstevel@tonic-gate 	if (*oldcs == '{') {
2437c478bd9Sstevel@tonic-gate 		(void) execbrc(cs, ((char *)0));
2447c478bd9Sstevel@tonic-gate 		return;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	matchdir(cs);
2477c478bd9Sstevel@tonic-gate endit:
2487c478bd9Sstevel@tonic-gate 	gpathp = sgpathp;
2497c478bd9Sstevel@tonic-gate 	*gpathp = 0;
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate static void
matchdir(char * pattern)2537c478bd9Sstevel@tonic-gate matchdir(char *pattern)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	struct stat stb;
2567c478bd9Sstevel@tonic-gate 	register struct dirent *dp;
2577c478bd9Sstevel@tonic-gate 	DIR *dirp;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * BSD/SunOS open() system call maps a null pathname into
2617c478bd9Sstevel@tonic-gate 	 * "." while System V does not.
2627c478bd9Sstevel@tonic-gate 	 */
2637c478bd9Sstevel@tonic-gate 	if (*gpath == (char)0) {
2647c478bd9Sstevel@tonic-gate 		dirp = opendir(".");
2657c478bd9Sstevel@tonic-gate 	} else
2667c478bd9Sstevel@tonic-gate 		dirp = opendir(gpath);
2677c478bd9Sstevel@tonic-gate 	if (dirp == NULL) {
2687c478bd9Sstevel@tonic-gate 		if (globbed)
2697c478bd9Sstevel@tonic-gate 			return;
2707c478bd9Sstevel@tonic-gate 		goto patherr2;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	if (fstat(dirp->dd_fd, &stb) < 0)
2737c478bd9Sstevel@tonic-gate 		goto patherr1;
2747c478bd9Sstevel@tonic-gate 	if (!S_ISDIR(stb.st_mode)) {
2757c478bd9Sstevel@tonic-gate 		errno = ENOTDIR;
2767c478bd9Sstevel@tonic-gate 		goto patherr1;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 	while ((dp = readdir(dirp)) != NULL) {
2797c478bd9Sstevel@tonic-gate 		if (dp->d_ino == 0)
2807c478bd9Sstevel@tonic-gate 			continue;
2817c478bd9Sstevel@tonic-gate 		if (match(dp->d_name, pattern)) {
2827c478bd9Sstevel@tonic-gate 			Gcat(gpath, dp->d_name);
2837c478bd9Sstevel@tonic-gate 			globcnt++;
2847c478bd9Sstevel@tonic-gate 		}
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	closedir(dirp);
2877c478bd9Sstevel@tonic-gate 	return;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate patherr1:
2907c478bd9Sstevel@tonic-gate 	closedir(dirp);
2917c478bd9Sstevel@tonic-gate patherr2:
2927c478bd9Sstevel@tonic-gate 	globerr = "Bad directory components";
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate static int
execbrc(char * p,char * s)2967c478bd9Sstevel@tonic-gate execbrc(char *p, char *s)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	char restbuf[FTPBUFSIZ + 2];
2997c478bd9Sstevel@tonic-gate 	register char *pe, *pm, *pl;
3007c478bd9Sstevel@tonic-gate 	int brclev = 0;
3017c478bd9Sstevel@tonic-gate 	char *lm, savec, *sgpathp;
3027c478bd9Sstevel@tonic-gate 	int	len;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	for (lm = restbuf; *p != '{'; *lm += len, p += len) {
3057c478bd9Sstevel@tonic-gate 		if ((len = mblen(p, MB_CUR_MAX)) <= 0)
3067c478bd9Sstevel@tonic-gate 			len = 1;
3077c478bd9Sstevel@tonic-gate 		memcpy(lm, p, len);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	for (pe = ++p; *pe; pe += len) {
3117c478bd9Sstevel@tonic-gate 		if ((len = mblen(pe, MB_CUR_MAX)) <= 0)
3127c478bd9Sstevel@tonic-gate 			len = 1;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		switch (*pe) {
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		case '{':
3177c478bd9Sstevel@tonic-gate 			brclev++;
3187c478bd9Sstevel@tonic-gate 			continue;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		case '}':
3217c478bd9Sstevel@tonic-gate 			if (brclev == 0)
3227c478bd9Sstevel@tonic-gate 				goto pend;
3237c478bd9Sstevel@tonic-gate 			brclev--;
3247c478bd9Sstevel@tonic-gate 			continue;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		case '[':
3277c478bd9Sstevel@tonic-gate 			for (pe++; *pe && *pe != ']'; pe += len) {
3287c478bd9Sstevel@tonic-gate 				if ((len = mblen(pe, MB_CUR_MAX)) <= 0)
3297c478bd9Sstevel@tonic-gate 					len = 1;
3307c478bd9Sstevel@tonic-gate 			}
3317c478bd9Sstevel@tonic-gate 			len = 1;
3327c478bd9Sstevel@tonic-gate 			continue;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate pend:
3367c478bd9Sstevel@tonic-gate 	brclev = 0;
3377c478bd9Sstevel@tonic-gate 	for (pl = pm = p; pm <= pe; pm += len) {
3387c478bd9Sstevel@tonic-gate 		if ((len = mblen(pm, MB_CUR_MAX)) <= 0)
3397c478bd9Sstevel@tonic-gate 			len = 1;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		switch (*pm & (QUOTE|TRIM)) {
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		case '{':
3447c478bd9Sstevel@tonic-gate 			brclev++;
3457c478bd9Sstevel@tonic-gate 			continue;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 		case '}':
3487c478bd9Sstevel@tonic-gate 			if (brclev) {
3497c478bd9Sstevel@tonic-gate 				brclev--;
3507c478bd9Sstevel@tonic-gate 				continue;
3517c478bd9Sstevel@tonic-gate 			}
3527c478bd9Sstevel@tonic-gate 			goto doit;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		case ','|QUOTE:
3557c478bd9Sstevel@tonic-gate 		case ',':
3567c478bd9Sstevel@tonic-gate 			if (brclev)
3577c478bd9Sstevel@tonic-gate 				continue;
3587c478bd9Sstevel@tonic-gate doit:
3597c478bd9Sstevel@tonic-gate 			savec = *pm;
3607c478bd9Sstevel@tonic-gate 			*pm = 0;
3617c478bd9Sstevel@tonic-gate 			(void) strcpy(lm, pl);
3627c478bd9Sstevel@tonic-gate 			(void) strcat(restbuf, pe + 1);
3637c478bd9Sstevel@tonic-gate 			*pm = savec;
3647c478bd9Sstevel@tonic-gate 			if (s == 0) {
3657c478bd9Sstevel@tonic-gate 				sgpathp = gpathp;
3667c478bd9Sstevel@tonic-gate 				expand(restbuf);
3677c478bd9Sstevel@tonic-gate 				gpathp = sgpathp;
3687c478bd9Sstevel@tonic-gate 				*gpathp = 0;
3697c478bd9Sstevel@tonic-gate 			} else if (amatch(s, restbuf))
3707c478bd9Sstevel@tonic-gate 				return (1);
3717c478bd9Sstevel@tonic-gate 			sort();
3727c478bd9Sstevel@tonic-gate 			pl = pm + 1;
3737c478bd9Sstevel@tonic-gate 			if (brclev)
3747c478bd9Sstevel@tonic-gate 				return (0);
3757c478bd9Sstevel@tonic-gate 			continue;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 		case '[':
3787c478bd9Sstevel@tonic-gate 			for (pm++; *pm && *pm != ']'; pm += len) {
3797c478bd9Sstevel@tonic-gate 				if ((len = mblen(pm, MB_CUR_MAX)) <= 0)
3807c478bd9Sstevel@tonic-gate 					len = 1;
3817c478bd9Sstevel@tonic-gate 			}
3827c478bd9Sstevel@tonic-gate 			len = 1;
3837c478bd9Sstevel@tonic-gate 			if (!*pm)
3847c478bd9Sstevel@tonic-gate 				pm--;
3857c478bd9Sstevel@tonic-gate 			continue;
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	if (brclev)
3897c478bd9Sstevel@tonic-gate 		goto doit;
3907c478bd9Sstevel@tonic-gate 	return (0);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate static int
match(char * s,char * p)3947c478bd9Sstevel@tonic-gate match(char *s, char *p)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	register int c;
3977c478bd9Sstevel@tonic-gate 	register char *sentp;
3987c478bd9Sstevel@tonic-gate 	char sglobbed = globbed;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (*s == '.' && *p != '.')
4017c478bd9Sstevel@tonic-gate 		return (0);
4027c478bd9Sstevel@tonic-gate 	sentp = entp;
4037c478bd9Sstevel@tonic-gate 	entp = s;
4047c478bd9Sstevel@tonic-gate 	c = amatch(s, p);
4057c478bd9Sstevel@tonic-gate 	entp = sentp;
4067c478bd9Sstevel@tonic-gate 	globbed = sglobbed;
4077c478bd9Sstevel@tonic-gate 	return (c);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate static int
amatch(char * s,char * p)4117c478bd9Sstevel@tonic-gate amatch(char *s, char *p)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	wchar_t scc;
4147c478bd9Sstevel@tonic-gate 	int ok;
4157c478bd9Sstevel@tonic-gate 	wchar_t lc1, lc2;
4167c478bd9Sstevel@tonic-gate 	char *sgpathp;
4177c478bd9Sstevel@tonic-gate 	struct stat stb;
4187c478bd9Sstevel@tonic-gate 	wchar_t c, cc;
4197c478bd9Sstevel@tonic-gate 	int	len_s, len_p;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	globbed = 1;
4227c478bd9Sstevel@tonic-gate 	for (;;) {
4237c478bd9Sstevel@tonic-gate 		if ((len_s = mbtowc(&scc, s, MB_CUR_MAX)) <= 0) {
4247c478bd9Sstevel@tonic-gate 			scc = (unsigned char)*s;
4257c478bd9Sstevel@tonic-gate 			len_s = 1;
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		/* scc = *s++ & TRIM; */
4287c478bd9Sstevel@tonic-gate 		s += len_s;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		if ((len_p = mbtowc(&c, p, MB_CUR_MAX)) <= 0) {
4317c478bd9Sstevel@tonic-gate 			c = (unsigned char)*p;
4327c478bd9Sstevel@tonic-gate 			len_p = 1;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 		p += len_p;
4357c478bd9Sstevel@tonic-gate 		switch (c) {
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		case '{':
4387c478bd9Sstevel@tonic-gate 			return (execbrc(p - len_p, s - len_s));
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		case '[':
4417c478bd9Sstevel@tonic-gate 			ok = 0;
4427c478bd9Sstevel@tonic-gate 			lc1 = 0;
4437c478bd9Sstevel@tonic-gate 			while ((cc = *p) != '\0') {
4447c478bd9Sstevel@tonic-gate 				if ((len_p = mbtowc(&cc, p, MB_CUR_MAX)) <= 0) {
4457c478bd9Sstevel@tonic-gate 					cc = (unsigned char)*p;
4467c478bd9Sstevel@tonic-gate 					len_p = 1;
4477c478bd9Sstevel@tonic-gate 				}
4487c478bd9Sstevel@tonic-gate 				p += len_p;
4497c478bd9Sstevel@tonic-gate 				if (cc == ']') {
4507c478bd9Sstevel@tonic-gate 					if (ok)
4517c478bd9Sstevel@tonic-gate 						break;
4527c478bd9Sstevel@tonic-gate 					return (0);
4537c478bd9Sstevel@tonic-gate 				}
4547c478bd9Sstevel@tonic-gate 				if (cc == '-') {
4557c478bd9Sstevel@tonic-gate 					if ((len_p = mbtowc(&lc2, p,
4567c478bd9Sstevel@tonic-gate 					    MB_CUR_MAX)) <= 0) {
4577c478bd9Sstevel@tonic-gate 						lc2 = (unsigned char)*p;
4587c478bd9Sstevel@tonic-gate 						len_p = 1;
4597c478bd9Sstevel@tonic-gate 					}
4607c478bd9Sstevel@tonic-gate 					p += len_p;
4617c478bd9Sstevel@tonic-gate 					if (ftp_fnmatch(scc, lc1, lc2))
4627c478bd9Sstevel@tonic-gate 						ok++;
4637c478bd9Sstevel@tonic-gate 				} else
4647c478bd9Sstevel@tonic-gate 					if (scc == (lc1 = cc))
4657c478bd9Sstevel@tonic-gate 						ok++;
4667c478bd9Sstevel@tonic-gate 			}
4677c478bd9Sstevel@tonic-gate 			if (cc == 0)
4687c478bd9Sstevel@tonic-gate 				if (!ok)
4697c478bd9Sstevel@tonic-gate 					return (0);
4707c478bd9Sstevel@tonic-gate 			continue;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		case '*':
4737c478bd9Sstevel@tonic-gate 			if (!*p)
4747c478bd9Sstevel@tonic-gate 				return (1);
4757c478bd9Sstevel@tonic-gate 			if (*p == '/') {
4767c478bd9Sstevel@tonic-gate 				p++;
4777c478bd9Sstevel@tonic-gate 				goto slash;
4787c478bd9Sstevel@tonic-gate 			}
4797c478bd9Sstevel@tonic-gate 			s -= len_s;
4807c478bd9Sstevel@tonic-gate 			do {
4817c478bd9Sstevel@tonic-gate 				if (amatch(s, p))
4827c478bd9Sstevel@tonic-gate 					return (1);
4837c478bd9Sstevel@tonic-gate 			} while (*s++);
4847c478bd9Sstevel@tonic-gate 			return (0);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		case 0:
4877c478bd9Sstevel@tonic-gate 			return (scc == 0);
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		default:
4907c478bd9Sstevel@tonic-gate 			if (c != scc)
4917c478bd9Sstevel@tonic-gate 				return (0);
4927c478bd9Sstevel@tonic-gate 			continue;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		case '?':
4957c478bd9Sstevel@tonic-gate 			if (scc == 0)
4967c478bd9Sstevel@tonic-gate 				return (0);
4977c478bd9Sstevel@tonic-gate 			continue;
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 		case '/':
5007c478bd9Sstevel@tonic-gate 			if (scc)
5017c478bd9Sstevel@tonic-gate 				return (0);
5027c478bd9Sstevel@tonic-gate slash:
5037c478bd9Sstevel@tonic-gate 			s = entp;
5047c478bd9Sstevel@tonic-gate 			sgpathp = gpathp;
5057c478bd9Sstevel@tonic-gate 			while (*s)
5067c478bd9Sstevel@tonic-gate 				addpath(*s++);
5077c478bd9Sstevel@tonic-gate 			addpath('/');
5087c478bd9Sstevel@tonic-gate 			if (stat(gpath, &stb) == 0 && S_ISDIR(stb.st_mode))
5097c478bd9Sstevel@tonic-gate 				if (*p == 0) {
5107c478bd9Sstevel@tonic-gate 					Gcat(gpath, "");
5117c478bd9Sstevel@tonic-gate 					globcnt++;
5127c478bd9Sstevel@tonic-gate 				} else
5137c478bd9Sstevel@tonic-gate 					expand(p);
5147c478bd9Sstevel@tonic-gate 			gpathp = sgpathp;
5157c478bd9Sstevel@tonic-gate 			*gpathp = 0;
5167c478bd9Sstevel@tonic-gate 			return (0);
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate #ifdef notdef
5227c478bd9Sstevel@tonic-gate static
Gmatch(s,p)5237c478bd9Sstevel@tonic-gate Gmatch(s, p)
5247c478bd9Sstevel@tonic-gate 	register char *s, *p;
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	register int scc;
5277c478bd9Sstevel@tonic-gate 	int ok, lc;
5287c478bd9Sstevel@tonic-gate 	int c, cc;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	for (;;) {
5317c478bd9Sstevel@tonic-gate 		scc = *s++ & TRIM;
5327c478bd9Sstevel@tonic-gate 		switch (c = *p++) {
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 		case '[':
5357c478bd9Sstevel@tonic-gate 			ok = 0;
5367c478bd9Sstevel@tonic-gate 			lc = 077777;
5377c478bd9Sstevel@tonic-gate 			while (cc = *p++) {
5387c478bd9Sstevel@tonic-gate 				if (cc == ']') {
5397c478bd9Sstevel@tonic-gate 					if (ok)
5407c478bd9Sstevel@tonic-gate 						break;
5417c478bd9Sstevel@tonic-gate 					return (0);
5427c478bd9Sstevel@tonic-gate 				}
5437c478bd9Sstevel@tonic-gate 				if (cc == '-') {
5447c478bd9Sstevel@tonic-gate 					if (lc <= scc && scc <= *p++)
5457c478bd9Sstevel@tonic-gate 						ok++;
5467c478bd9Sstevel@tonic-gate 				} else
5477c478bd9Sstevel@tonic-gate 					if (scc == (lc = cc))
5487c478bd9Sstevel@tonic-gate 						ok++;
5497c478bd9Sstevel@tonic-gate 			}
5507c478bd9Sstevel@tonic-gate 			if (cc == 0)
5517c478bd9Sstevel@tonic-gate 				if (ok)
5527c478bd9Sstevel@tonic-gate 					p--;
5537c478bd9Sstevel@tonic-gate 				else
5547c478bd9Sstevel@tonic-gate 					return (0);
5557c478bd9Sstevel@tonic-gate 			continue;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		case '*':
5587c478bd9Sstevel@tonic-gate 			if (!*p)
5597c478bd9Sstevel@tonic-gate 				return (1);
5607c478bd9Sstevel@tonic-gate 			for (s--; *s; s++)
5617c478bd9Sstevel@tonic-gate 				if (Gmatch(s, p))
5627c478bd9Sstevel@tonic-gate 					return (1);
5637c478bd9Sstevel@tonic-gate 			return (0);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		case 0:
5667c478bd9Sstevel@tonic-gate 			return (scc == 0);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		default:
5697c478bd9Sstevel@tonic-gate 			if ((c & TRIM) != scc)
5707c478bd9Sstevel@tonic-gate 				return (0);
5717c478bd9Sstevel@tonic-gate 			continue;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		case '?':
5747c478bd9Sstevel@tonic-gate 			if (scc == 0)
5757c478bd9Sstevel@tonic-gate 				return (0);
5767c478bd9Sstevel@tonic-gate 			continue;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate #endif
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate static void
Gcat(char * s1,char * s2)5847c478bd9Sstevel@tonic-gate Gcat(char *s1, char *s2)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	if (gargc >= agargv_size - 1) {
5877c478bd9Sstevel@tonic-gate 		char **tmp;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 		if (globerr) {
5907c478bd9Sstevel@tonic-gate 			return;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 		tmp = (char **)realloc(agargv,
5937c478bd9Sstevel@tonic-gate 		    (agargv_size + GAVSIZ) * sizeof (char *));
5947c478bd9Sstevel@tonic-gate 		if (tmp == NULL) {
5957c478bd9Sstevel@tonic-gate 			globerr = "Arguments too long";
5967c478bd9Sstevel@tonic-gate 			return;
5977c478bd9Sstevel@tonic-gate 		} else {
5987c478bd9Sstevel@tonic-gate 			agargv = tmp;
5997c478bd9Sstevel@tonic-gate 			agargv_size += GAVSIZ;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 		gargv = agargv;
6027c478bd9Sstevel@tonic-gate 		sortbas = agargv;
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	gargc++;
6057c478bd9Sstevel@tonic-gate 	gargv[gargc] = 0;
6067c478bd9Sstevel@tonic-gate 	gargv[gargc - 1] = strspl(s1, s2);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate static void
addpath(char c)6107c478bd9Sstevel@tonic-gate addpath(char c)
6117c478bd9Sstevel@tonic-gate {
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	if (gpathp >= lastgpathp)
6147c478bd9Sstevel@tonic-gate 		globerr = "Pathname too long";
6157c478bd9Sstevel@tonic-gate 	else {
6167c478bd9Sstevel@tonic-gate 		*gpathp++ = c;
6177c478bd9Sstevel@tonic-gate 		*gpathp = 0;
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate static void
rscan(char ** t,int (* f)(char))6227c478bd9Sstevel@tonic-gate rscan(char **t, int (*f)(char))
6237c478bd9Sstevel@tonic-gate {
6247c478bd9Sstevel@tonic-gate 	register char *p, c;
6257c478bd9Sstevel@tonic-gate 	int	len;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	while (p = *t++) {
6287c478bd9Sstevel@tonic-gate 		if (f == tglob)
6297c478bd9Sstevel@tonic-gate 			if (*p == '~')
6307c478bd9Sstevel@tonic-gate 				gflag |= 2;
6317c478bd9Sstevel@tonic-gate 			else if (eq(p, "{") || eq(p, "{}"))
6327c478bd9Sstevel@tonic-gate 				continue;
6337c478bd9Sstevel@tonic-gate 		while ((c = *p) != '\0') {
6347c478bd9Sstevel@tonic-gate 			(void) (*f)(c);
6357c478bd9Sstevel@tonic-gate 			if ((len = mblen(p, MB_CUR_MAX)) <= 0)
6367c478bd9Sstevel@tonic-gate 				len = 1;
6377c478bd9Sstevel@tonic-gate 			p += len;
6387c478bd9Sstevel@tonic-gate 		}
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate static int
tglob(char c)6437c478bd9Sstevel@tonic-gate tglob(char c)
6447c478bd9Sstevel@tonic-gate {
6457c478bd9Sstevel@tonic-gate 	if (any(c, globchars))
6467c478bd9Sstevel@tonic-gate 		gflag |= c == '{' ? 2 : 1;
6477c478bd9Sstevel@tonic-gate 	return (c);
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate static int
letter(char c)6517c478bd9Sstevel@tonic-gate letter(char c)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_');
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate static int
digit(char c)6577c478bd9Sstevel@tonic-gate digit(char c)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	return (c >= '0' && c <= '9');
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate static int
any(int c,char * s)6637c478bd9Sstevel@tonic-gate any(int c, char *s)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	int	len;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	while (*s) {
6687c478bd9Sstevel@tonic-gate 		if (*s == c)
6697c478bd9Sstevel@tonic-gate 			return (1);
6707c478bd9Sstevel@tonic-gate 		if ((len = mblen(s, MB_CUR_MAX)) <= 0)
6717c478bd9Sstevel@tonic-gate 			len = 1;
6727c478bd9Sstevel@tonic-gate 		s += len;
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	return (0);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate static int
blklen(char ** av)6787c478bd9Sstevel@tonic-gate blklen(char **av)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	register int i = 0;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	while (*av++)
6837c478bd9Sstevel@tonic-gate 		i++;
6847c478bd9Sstevel@tonic-gate 	return (i);
6857c478bd9Sstevel@tonic-gate }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate static char **
blkcpy(char ** oav,char ** bv)6887c478bd9Sstevel@tonic-gate blkcpy(char **oav, char **bv)
6897c478bd9Sstevel@tonic-gate {
6907c478bd9Sstevel@tonic-gate 	register char **av = oav;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	while (*av++ = *bv++)
6937c478bd9Sstevel@tonic-gate 		continue;
6947c478bd9Sstevel@tonic-gate 	return (oav);
6957c478bd9Sstevel@tonic-gate }
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate void
blkfree(char ** av0)6987c478bd9Sstevel@tonic-gate blkfree(char **av0)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	register char **av = av0;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	while (*av)
7037c478bd9Sstevel@tonic-gate 		xfree(*av++);
7047c478bd9Sstevel@tonic-gate 	free(av0);
7057c478bd9Sstevel@tonic-gate }
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate static void
xfree(char * cp)7087c478bd9Sstevel@tonic-gate xfree(char *cp)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate 	extern char end[];
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if (cp >= end && cp < (char *)&cp)
7137c478bd9Sstevel@tonic-gate 		free(cp);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate static char *
strspl(char * cp,char * dp)7177c478bd9Sstevel@tonic-gate strspl(char *cp, char *dp)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	register char *ep = malloc((unsigned)(strlen(cp) + strlen(dp) + 1));
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (ep == (char *)0)
7227c478bd9Sstevel@tonic-gate 		fatal("Out of memory");
7237c478bd9Sstevel@tonic-gate 	(void) strcpy(ep, cp);
7247c478bd9Sstevel@tonic-gate 	(void) strcat(ep, dp);
7257c478bd9Sstevel@tonic-gate 	return (ep);
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate static char **
copyblk(char ** v)7297c478bd9Sstevel@tonic-gate copyblk(char **v)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate 	register char **nv = (char **)malloc((unsigned)((blklen(v) + 1) *
7327c478bd9Sstevel@tonic-gate 	    sizeof (char **)));
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (nv == (char **)0)
7357c478bd9Sstevel@tonic-gate 		fatal("Out of memory");
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	return (blkcpy(nv, v));
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate static char *
strend(char * cp)7417c478bd9Sstevel@tonic-gate strend(char *cp)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	while (*cp)
7457c478bd9Sstevel@tonic-gate 		cp++;
7467c478bd9Sstevel@tonic-gate 	return (cp);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate /*
7497c478bd9Sstevel@tonic-gate  * Extract a home directory from the password file
7507c478bd9Sstevel@tonic-gate  * The argument points to a buffer where the name of the
7517c478bd9Sstevel@tonic-gate  * user whose home directory is sought is currently.
7527c478bd9Sstevel@tonic-gate  * We write the home directory of the user back there.
7537c478bd9Sstevel@tonic-gate  */
754*740638c8Sbw static int
gethdir(char * home)7557c478bd9Sstevel@tonic-gate gethdir(char *home)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 	register struct passwd *pp = getpwnam(home);
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (!pp || home + strlen(pp->pw_dir) >= lastgpathp)
7607c478bd9Sstevel@tonic-gate 		return (1);
7617c478bd9Sstevel@tonic-gate 	(void) strcpy(home, pp->pw_dir);
7627c478bd9Sstevel@tonic-gate 	return (0);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate static int
ftp_fnmatch(wchar_t t_ch,wchar_t t_fch,wchar_t t_lch)7667c478bd9Sstevel@tonic-gate ftp_fnmatch(wchar_t t_ch, wchar_t t_fch, wchar_t t_lch)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	char	t_char[MB_LEN_MAX + 1];
7697c478bd9Sstevel@tonic-gate 	char	t_patan[MB_LEN_MAX * 2 + 8];
7707c478bd9Sstevel@tonic-gate 	char	*p;
7717c478bd9Sstevel@tonic-gate 	int	i;
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if ((t_ch == t_fch) || (t_ch == t_lch))
7747c478bd9Sstevel@tonic-gate 		return (1);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	p = t_patan;
7777c478bd9Sstevel@tonic-gate 	if ((i = wctomb(t_char, (wchar_t)t_ch)) <= 0)
7787c478bd9Sstevel@tonic-gate 		return (0);
7797c478bd9Sstevel@tonic-gate 	t_char[i] = 0;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	*p++ = '[';
7827c478bd9Sstevel@tonic-gate 	if ((i = wctomb(p, (wchar_t)t_fch)) <= 0)
7837c478bd9Sstevel@tonic-gate 		return (0);
7847c478bd9Sstevel@tonic-gate 	p += i;
7857c478bd9Sstevel@tonic-gate 	*p++ = '-';
7867c478bd9Sstevel@tonic-gate 	if ((i = wctomb(p, (wchar_t)t_lch)) <= 0)
7877c478bd9Sstevel@tonic-gate 		return (0);
7887c478bd9Sstevel@tonic-gate 	p += i;
7897c478bd9Sstevel@tonic-gate 	*p++ = ']';
7907c478bd9Sstevel@tonic-gate 	*p = 0;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if (fnmatch(t_patan, t_char, FNM_NOESCAPE))
7937c478bd9Sstevel@tonic-gate 		return (0);
7947c478bd9Sstevel@tonic-gate 	return (1);
7957c478bd9Sstevel@tonic-gate }
796