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