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