xref: /onnv-gate/usr/src/lib/libc/port/regex/glob.c (revision 1219)
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  */
22*1219Sraf 
230Sstevel@tonic-gate /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * This code is MKS code ported to Solaris originally with minimum
320Sstevel@tonic-gate  * modifications so that upgrades from MKS would readily integrate.
330Sstevel@tonic-gate  * The MKS basis for this modification was:
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  *	$Id: glob.c 1.31 1994/04/07 22:50:43 mark
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * Additional modifications have been made to this code to make it
380Sstevel@tonic-gate  * 64-bit clean.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * glob, globfree -- POSIX.2 compatible file name expansion routines.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * Copyright 1985, 1991 by Mortice Kern Systems Inc.  All rights reserved.
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  * Written by Eric Gisin.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
49*1219Sraf #include "synonyms.h"
500Sstevel@tonic-gate #include <stdio.h>
510Sstevel@tonic-gate #include <unistd.h>
520Sstevel@tonic-gate #include <limits.h>
530Sstevel@tonic-gate #include <stdlib.h>
540Sstevel@tonic-gate #include <string.h>
550Sstevel@tonic-gate #include <dirent.h>
560Sstevel@tonic-gate #include <sys/stat.h>
570Sstevel@tonic-gate #include <glob.h>
580Sstevel@tonic-gate #include <errno.h>
590Sstevel@tonic-gate #include <fnmatch.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	GLOB__CHECK	0x80	/* stat generated paths */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	INITIAL	8		/* initial pathv allocation */
640Sstevel@tonic-gate #define	NULLCPP	((char **)0)	/* Null char ** */
65*1219Sraf #define	NAME_MAX	1024	/* something large */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static int	globit(size_t, const char *, glob_t *, int,
680Sstevel@tonic-gate 	int (*)(const char *, int), char **);
690Sstevel@tonic-gate static int	pstrcmp(const void *, const void *);
700Sstevel@tonic-gate static int	append(glob_t *, const char *);
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * Free all space consumed by glob.
740Sstevel@tonic-gate  */
750Sstevel@tonic-gate void
760Sstevel@tonic-gate globfree(glob_t *gp)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	size_t i;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (gp->gl_pathv == 0)
810Sstevel@tonic-gate 		return;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	for (i = gp->gl_offs; i < gp->gl_offs + gp->gl_pathc; ++i)
840Sstevel@tonic-gate 		free(gp->gl_pathv[i]);
850Sstevel@tonic-gate 	free((void *)gp->gl_pathv);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	gp->gl_pathc = 0;
880Sstevel@tonic-gate 	gp->gl_pathv = NULLCPP;
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Do filename expansion.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate int
950Sstevel@tonic-gate glob(const char *pattern, int flags,
960Sstevel@tonic-gate 	int (*errfn)(const char *, int), glob_t *gp)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate 	int rv;
990Sstevel@tonic-gate 	size_t i;
1000Sstevel@tonic-gate 	size_t ipathc;
1010Sstevel@tonic-gate 	char	*path;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	if ((flags & GLOB_DOOFFS) == 0)
1040Sstevel@tonic-gate 		gp->gl_offs = 0;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if (!(flags & GLOB_APPEND)) {
1070Sstevel@tonic-gate 		gp->gl_pathc = 0;
1080Sstevel@tonic-gate 		gp->gl_pathn = gp->gl_offs + INITIAL;
1090Sstevel@tonic-gate 		gp->gl_pathv = (char **)malloc(sizeof (char *) * gp->gl_pathn);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 		if (gp->gl_pathv == NULLCPP)
1120Sstevel@tonic-gate 			return (GLOB_NOSPACE);
1130Sstevel@tonic-gate 		gp->gl_pathp = gp->gl_pathv + gp->gl_offs;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 		for (i = 0; i < gp->gl_offs; ++i)
1160Sstevel@tonic-gate 			gp->gl_pathv[i] = NULL;
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if ((path = malloc(strlen(pattern)+1)) == NULL)
1200Sstevel@tonic-gate 		return (GLOB_NOSPACE);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	ipathc = gp->gl_pathc;
1230Sstevel@tonic-gate 	rv = globit(0, pattern, gp, flags, errfn, &path);
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (rv == GLOB_ABORTED) {
1260Sstevel@tonic-gate 		/*
1270Sstevel@tonic-gate 		 * User's error function returned non-zero, or GLOB_ERR was
1280Sstevel@tonic-gate 		 * set, and we encountered a directory we couldn't search.
1290Sstevel@tonic-gate 		 */
1300Sstevel@tonic-gate 		free(path);
1310Sstevel@tonic-gate 		return (GLOB_ABORTED);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	i = gp->gl_pathc - ipathc;
1350Sstevel@tonic-gate 	if (i >= 1 && !(flags & GLOB_NOSORT)) {
1360Sstevel@tonic-gate 		qsort((char *)(gp->gl_pathp+ipathc), i, sizeof (char *),
1370Sstevel@tonic-gate 			pstrcmp);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	if (i == 0) {
1400Sstevel@tonic-gate 		if (flags & GLOB_NOCHECK)
1410Sstevel@tonic-gate 			(void) append(gp, pattern);
1420Sstevel@tonic-gate 		else
1430Sstevel@tonic-gate 			rv = GLOB_NOMATCH;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	gp->gl_pathp[gp->gl_pathc] = NULL;
1460Sstevel@tonic-gate 	free(path);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	return (rv);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * Recursive routine to match glob pattern, and walk directories.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate int
1560Sstevel@tonic-gate globit(size_t dend, const char *sp, glob_t *gp, int flags,
1570Sstevel@tonic-gate 	int (*errfn)(const char *, int), char **path)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	size_t n;
1600Sstevel@tonic-gate 	size_t m;
1610Sstevel@tonic-gate 	ssize_t end = 0;	/* end of expanded directory */
1620Sstevel@tonic-gate 	char *pat = (char *)sp;	/* pattern component */
1630Sstevel@tonic-gate 	char *dp = (*path) + dend;
1640Sstevel@tonic-gate 	int expand = 0;		/* path has pattern */
1650Sstevel@tonic-gate 	char *cp;
1660Sstevel@tonic-gate 	struct stat64 sb;
1670Sstevel@tonic-gate 	DIR *dirp;
1680Sstevel@tonic-gate 	struct dirent64 *d;
1690Sstevel@tonic-gate 	int err;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	for (;;)
1720Sstevel@tonic-gate 		switch (*dp++ = *(unsigned char *)sp++) {
1730Sstevel@tonic-gate 		case '\0':	/* end of source path */
1740Sstevel@tonic-gate 			if (expand)
1750Sstevel@tonic-gate 				goto Expand;
1760Sstevel@tonic-gate 			else {
1770Sstevel@tonic-gate 				if (!(flags & GLOB_NOCHECK) ||
1780Sstevel@tonic-gate 				    flags & (GLOB__CHECK|GLOB_MARK))
1790Sstevel@tonic-gate 					if (stat64(*path, &sb) < 0) {
1800Sstevel@tonic-gate 						return (0);
1810Sstevel@tonic-gate 					}
1820Sstevel@tonic-gate 				if (flags & GLOB_MARK && S_ISDIR(sb.st_mode)) {
1830Sstevel@tonic-gate 					*dp = '\0';
1840Sstevel@tonic-gate 					*--dp = '/';
1850Sstevel@tonic-gate 				}
1860Sstevel@tonic-gate 				if (append(gp, *path) < 0) {
1870Sstevel@tonic-gate 					return (GLOB_NOSPACE);
1880Sstevel@tonic-gate 				}
1890Sstevel@tonic-gate 				return (0);
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 			/*NOTREACHED*/
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 		case '*':
1940Sstevel@tonic-gate 		case '?':
1950Sstevel@tonic-gate 		case '[':
1960Sstevel@tonic-gate 		case '\\':
1970Sstevel@tonic-gate 			++expand;
1980Sstevel@tonic-gate 			break;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 		case '/':
2010Sstevel@tonic-gate 			if (expand)
2020Sstevel@tonic-gate 				goto Expand;
2030Sstevel@tonic-gate 			end = dp - *path;
2040Sstevel@tonic-gate 			pat = (char *)sp;
2050Sstevel@tonic-gate 			break;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		Expand:
2080Sstevel@tonic-gate 			/* determine directory and open it */
2090Sstevel@tonic-gate 			(*path)[end] = '\0';
2100Sstevel@tonic-gate 			dirp = opendir(**path == '\0' ? "." : *path);
211*1219Sraf 			if (dirp == NULL) {
2120Sstevel@tonic-gate 				if (errfn != 0 && errfn(*path, errno) != 0 ||
2130Sstevel@tonic-gate 				    flags&GLOB_ERR) {
2140Sstevel@tonic-gate 					return (GLOB_ABORTED);
2150Sstevel@tonic-gate 				}
2160Sstevel@tonic-gate 				return (0);
2170Sstevel@tonic-gate 			}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 			/* extract pattern component */
2200Sstevel@tonic-gate 			n = sp - pat;
2210Sstevel@tonic-gate 			if ((cp = malloc(n)) == NULL) {
2220Sstevel@tonic-gate 				(void) closedir(dirp);
2230Sstevel@tonic-gate 				return (GLOB_NOSPACE);
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 			pat = memcpy(cp, pat, n);
2260Sstevel@tonic-gate 			pat[n-1] = '\0';
2270Sstevel@tonic-gate 			if (*--sp != '\0')
2280Sstevel@tonic-gate 				flags |= GLOB__CHECK;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 			/* expand path to max. expansion */
2310Sstevel@tonic-gate 			n = dp - *path;
2320Sstevel@tonic-gate 			*path = realloc(*path,
233*1219Sraf 				strlen(*path) + NAME_MAX + strlen(sp) + 1);
2340Sstevel@tonic-gate 			if (*path == NULL) {
2350Sstevel@tonic-gate 				(void) closedir(dirp);
2360Sstevel@tonic-gate 				free(pat);
2370Sstevel@tonic-gate 				return (GLOB_NOSPACE);
2380Sstevel@tonic-gate 			}
2390Sstevel@tonic-gate 			dp = (*path) + n;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate 			/* read directory and match entries */
2420Sstevel@tonic-gate 			err = 0;
243*1219Sraf 			while ((d = readdir64(dirp)) != NULL) {
2440Sstevel@tonic-gate 				cp = d->d_name;
2450Sstevel@tonic-gate 				if ((flags&GLOB_NOESCAPE)
2460Sstevel@tonic-gate 				    ? fnmatch(pat, cp, FNM_PERIOD|FNM_NOESCAPE)
2470Sstevel@tonic-gate 				    : fnmatch(pat, cp, FNM_PERIOD))
2480Sstevel@tonic-gate 					continue;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 				n = strlen(cp);
2510Sstevel@tonic-gate 				(void) memcpy((*path) + end, cp, n);
2520Sstevel@tonic-gate 				m = dp - *path;
2530Sstevel@tonic-gate 				err = globit(end+n, sp, gp, flags, errfn, path);
2540Sstevel@tonic-gate 				dp = (*path) + m;   /* globit can move path */
2550Sstevel@tonic-gate 				if (err != 0)
2560Sstevel@tonic-gate 					break;
2570Sstevel@tonic-gate 			}
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 			(void) closedir(dirp);
2600Sstevel@tonic-gate 			free(pat);
2610Sstevel@tonic-gate 			return (err);
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 		/* NOTREACHED */
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate  * Comparison routine for two name arguments, called by qsort.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate int
2700Sstevel@tonic-gate pstrcmp(const void *npp1, const void *npp2)
2710Sstevel@tonic-gate {
2720Sstevel@tonic-gate 	return (strcoll(*(char **)npp1, *(char **)npp2));
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate /*
2760Sstevel@tonic-gate  * Add a new matched filename to the glob_t structure, increasing the
2770Sstevel@tonic-gate  * size of that array, as required.
2780Sstevel@tonic-gate  */
2790Sstevel@tonic-gate int
2800Sstevel@tonic-gate append(glob_t *gp, const char *str)
2810Sstevel@tonic-gate {
2820Sstevel@tonic-gate 	char *cp;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if ((cp = malloc(strlen(str)+1)) == NULL)
2850Sstevel@tonic-gate 		return (GLOB_NOSPACE);
2860Sstevel@tonic-gate 	gp->gl_pathp[gp->gl_pathc++] = strcpy(cp, str);
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if ((gp->gl_pathc + gp->gl_offs) >= gp->gl_pathn) {
2890Sstevel@tonic-gate 		gp->gl_pathn *= 2;
2900Sstevel@tonic-gate 		gp->gl_pathv = (char **)realloc((void *)gp->gl_pathv,
2910Sstevel@tonic-gate 						gp->gl_pathn * sizeof (char *));
2920Sstevel@tonic-gate 		if (gp->gl_pathv == NULLCPP)
2930Sstevel@tonic-gate 			return (GLOB_NOSPACE);
2940Sstevel@tonic-gate 		gp->gl_pathp = gp->gl_pathv + gp->gl_offs;
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 	return (0);
2970Sstevel@tonic-gate }
298