summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/regex/glob.c
blob: 936c1e78d88e558a8f67166d59829612ca0f640e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * This code is MKS code ported to Solaris originally with minimum
 * modifications so that upgrades from MKS would readily integrate.
 * The MKS basis for this modification was:
 *
 *	$Id: glob.c 1.31 1994/04/07 22:50:43 mark
 *
 * Additional modifications have been made to this code to make it
 * 64-bit clean.
 */

/*
 * glob, globfree -- POSIX.2 compatible file name expansion routines.
 *
 * Copyright 1985, 1991 by Mortice Kern Systems Inc.  All rights reserved.
 *
 * Written by Eric Gisin.
 */

#include "synonyms.h"
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <glob.h>
#include <errno.h>
#include <fnmatch.h>

#define	GLOB__CHECK	0x80	/* stat generated paths */

#define	INITIAL	8		/* initial pathv allocation */
#define	NULLCPP	((char **)0)	/* Null char ** */
#define	NAME_MAX	1024	/* something large */

static int	globit(size_t, const char *, glob_t *, int,
	int (*)(const char *, int), char **);
static int	pstrcmp(const void *, const void *);
static int	append(glob_t *, const char *);

/*
 * Free all space consumed by glob.
 */
void
globfree(glob_t *gp)
{
	size_t i;

	if (gp->gl_pathv == 0)
		return;

	for (i = gp->gl_offs; i < gp->gl_offs + gp->gl_pathc; ++i)
		free(gp->gl_pathv[i]);
	free((void *)gp->gl_pathv);

	gp->gl_pathc = 0;
	gp->gl_pathv = NULLCPP;
}

/*
 * Do filename expansion.
 */
int
glob(const char *pattern, int flags,
	int (*errfn)(const char *, int), glob_t *gp)
{
	int rv;
	size_t i;
	size_t ipathc;
	char	*path;

	if ((flags & GLOB_DOOFFS) == 0)
		gp->gl_offs = 0;

	if (!(flags & GLOB_APPEND)) {
		gp->gl_pathc = 0;
		gp->gl_pathn = gp->gl_offs + INITIAL;
		gp->gl_pathv = (char **)malloc(sizeof (char *) * gp->gl_pathn);

		if (gp->gl_pathv == NULLCPP)
			return (GLOB_NOSPACE);
		gp->gl_pathp = gp->gl_pathv + gp->gl_offs;

		for (i = 0; i < gp->gl_offs; ++i)
			gp->gl_pathv[i] = NULL;
	}

	if ((path = malloc(strlen(pattern)+1)) == NULL)
		return (GLOB_NOSPACE);

	ipathc = gp->gl_pathc;
	rv = globit(0, pattern, gp, flags, errfn, &path);

	if (rv == GLOB_ABORTED) {
		/*
		 * User's error function returned non-zero, or GLOB_ERR was
		 * set, and we encountered a directory we couldn't search.
		 */
		free(path);
		return (GLOB_ABORTED);
	}

	i = gp->gl_pathc - ipathc;
	if (i >= 1 && !(flags & GLOB_NOSORT)) {
		qsort((char *)(gp->gl_pathp+ipathc), i, sizeof (char *),
			pstrcmp);
	}
	if (i == 0) {
		if (flags & GLOB_NOCHECK)
			(void) append(gp, pattern);
		else
			rv = GLOB_NOMATCH;
	}
	gp->gl_pathp[gp->gl_pathc] = NULL;
	free(path);

	return (rv);
}


/*
 * Recursive routine to match glob pattern, and walk directories.
 */
int
globit(size_t dend, const char *sp, glob_t *gp, int flags,
	int (*errfn)(const char *, int), char **path)
{
	size_t n;
	size_t m;
	ssize_t end = 0;	/* end of expanded directory */
	char *pat = (char *)sp;	/* pattern component */
	char *dp = (*path) + dend;
	int expand = 0;		/* path has pattern */
	char *cp;
	struct stat64 sb;
	DIR *dirp;
	struct dirent64 *d;
	int err;

	for (;;)
		switch (*dp++ = *(unsigned char *)sp++) {
		case '\0':	/* end of source path */
			if (expand)
				goto Expand;
			else {
				if (!(flags & GLOB_NOCHECK) ||
				    flags & (GLOB__CHECK|GLOB_MARK))
					if (stat64(*path, &sb) < 0) {
						return (0);
					}
				if (flags & GLOB_MARK && S_ISDIR(sb.st_mode)) {
					*dp = '\0';
					*--dp = '/';
				}
				if (append(gp, *path) < 0) {
					return (GLOB_NOSPACE);
				}
				return (0);
			}
			/*NOTREACHED*/

		case '*':
		case '?':
		case '[':
		case '\\':
			++expand;
			break;

		case '/':
			if (expand)
				goto Expand;
			end = dp - *path;
			pat = (char *)sp;
			break;

		Expand:
			/* determine directory and open it */
			(*path)[end] = '\0';
			dirp = opendir(**path == '\0' ? "." : *path);
			if (dirp == NULL) {
				if (errfn != 0 && errfn(*path, errno) != 0 ||
				    flags&GLOB_ERR) {
					return (GLOB_ABORTED);
				}
				return (0);
			}

			/* extract pattern component */
			n = sp - pat;
			if ((cp = malloc(n)) == NULL) {
				(void) closedir(dirp);
				return (GLOB_NOSPACE);
			}
			pat = memcpy(cp, pat, n);
			pat[n-1] = '\0';
			if (*--sp != '\0')
				flags |= GLOB__CHECK;

			/* expand path to max. expansion */
			n = dp - *path;
			*path = realloc(*path,
				strlen(*path) + NAME_MAX + strlen(sp) + 1);
			if (*path == NULL) {
				(void) closedir(dirp);
				free(pat);
				return (GLOB_NOSPACE);
			}
			dp = (*path) + n;

			/* read directory and match entries */
			err = 0;
			while ((d = readdir64(dirp)) != NULL) {
				cp = d->d_name;
				if ((flags&GLOB_NOESCAPE)
				    ? fnmatch(pat, cp, FNM_PERIOD|FNM_NOESCAPE)
				    : fnmatch(pat, cp, FNM_PERIOD))
					continue;

				n = strlen(cp);
				(void) memcpy((*path) + end, cp, n);
				m = dp - *path;
				err = globit(end+n, sp, gp, flags, errfn, path);
				dp = (*path) + m;   /* globit can move path */
				if (err != 0)
					break;
			}

			(void) closedir(dirp);
			free(pat);
			return (err);
		}
		/* NOTREACHED */
}

/*
 * Comparison routine for two name arguments, called by qsort.
 */
int
pstrcmp(const void *npp1, const void *npp2)
{
	return (strcoll(*(char **)npp1, *(char **)npp2));
}

/*
 * Add a new matched filename to the glob_t structure, increasing the
 * size of that array, as required.
 */
int
append(glob_t *gp, const char *str)
{
	char *cp;

	if ((cp = malloc(strlen(str)+1)) == NULL)
		return (GLOB_NOSPACE);
	gp->gl_pathp[gp->gl_pathc++] = strcpy(cp, str);

	if ((gp->gl_pathc + gp->gl_offs) >= gp->gl_pathn) {
		gp->gl_pathn *= 2;
		gp->gl_pathv = (char **)realloc((void *)gp->gl_pathv,
						gp->gl_pathn * sizeof (char *));
		if (gp->gl_pathv == NULLCPP)
			return (GLOB_NOSPACE);
		gp->gl_pathp = gp->gl_pathv + gp->gl_offs;
	}
	return (0);
}