xref: /onnv-gate/usr/src/cmd/logadm/glob.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
25*0Sstevel@tonic-gate  *
26*0Sstevel@tonic-gate  * logadm/glob.c -- globbing routines
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  * these routines support two kinds of globs.  first, the
29*0Sstevel@tonic-gate  * usual kind of filename globbing, like:
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * 	*.c
32*0Sstevel@tonic-gate  * 	/var/log/syslog.?
33*0Sstevel@tonic-gate  * 	log[0-9]*file
34*0Sstevel@tonic-gate  * 	/var/apache/logs/x*{access,error}_log
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * this is basically the same syntax that csh supports for globs and
37*0Sstevel@tonic-gate  * is provided by the routine glob_glob() which takes a filename and
38*0Sstevel@tonic-gate  * returns a list of filenames that match the glob.
39*0Sstevel@tonic-gate  *
40*0Sstevel@tonic-gate  * the second type is something called a "reglob" which is a pathname
41*0Sstevel@tonic-gate  * where the components are regular expressions as described in regex(3c).
42*0Sstevel@tonic-gate  * some examples:
43*0Sstevel@tonic-gate  *
44*0Sstevel@tonic-gate  * 	.*\.c
45*0Sstevel@tonic-gate  * 	/var/log/syslog\..
46*0Sstevel@tonic-gate  * 	log[0-9].*file
47*0Sstevel@tonic-gate  * 	/var/log/syslog\.([0-9]+)$0
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * the last example uses the ()$n form to assign a numeric extension
50*0Sstevel@tonic-gate  * on a filename to the "n" value kept by the fn routines with each
51*0Sstevel@tonic-gate  * filename (see fn_setn() in fn.c).  logadm uses this mechanism to
52*0Sstevel@tonic-gate  * correctly sort lognames when templates containing $n are used.
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  * the routine glob_reglob() is used to expand reglobs.  glob_glob()
55*0Sstevel@tonic-gate  * is implemented by expanding the curly braces, converting the globs
56*0Sstevel@tonic-gate  * to reglobs, and then passing the work to glob_reglob().
57*0Sstevel@tonic-gate  *
58*0Sstevel@tonic-gate  * finally, since expanding globs and reglobs requires doing a stat(2)
59*0Sstevel@tonic-gate  * on the files, we store the resulting stat information in the filename
60*0Sstevel@tonic-gate  * struct (see fn_setstat() in fn.c).
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * the glob(3c) routines are not used here since they don't support
63*0Sstevel@tonic-gate  * braces, and don't support the more powerful reglobs required by logadm.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate #include <stdio.h>
69*0Sstevel@tonic-gate #include <libintl.h>
70*0Sstevel@tonic-gate #include <stdlib.h>
71*0Sstevel@tonic-gate #include <libgen.h>
72*0Sstevel@tonic-gate #include <strings.h>
73*0Sstevel@tonic-gate #include <sys/types.h>
74*0Sstevel@tonic-gate #include <sys/param.h>
75*0Sstevel@tonic-gate #include <sys/stat.h>
76*0Sstevel@tonic-gate #include <dirent.h>
77*0Sstevel@tonic-gate #include "err.h"
78*0Sstevel@tonic-gate #include "fn.h"
79*0Sstevel@tonic-gate #include "glob.h"
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /* forward declarations for functions used internally by this module */
82*0Sstevel@tonic-gate static struct fn_list *glob_debrace(struct fn *fnp);
83*0Sstevel@tonic-gate static struct fn_list *glob_reglob_list(struct fn_list *fnlp);
84*0Sstevel@tonic-gate static boolean_t glob_magic(struct fn *fnp);
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /* expand curly braces (like file{one,two,three}name) */
87*0Sstevel@tonic-gate static struct fn_list *
88*0Sstevel@tonic-gate glob_debrace(struct fn *fnp)
89*0Sstevel@tonic-gate {
90*0Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
91*0Sstevel@tonic-gate 	struct fn_list *newret;
92*0Sstevel@tonic-gate 	char *sp = fn_s(fnp);
93*0Sstevel@tonic-gate 	char *left;
94*0Sstevel@tonic-gate 	char *right;
95*0Sstevel@tonic-gate 	char *comma;
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	/* start with an empty string in the list */
98*0Sstevel@tonic-gate 	fn_list_adds(ret, "");
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	/* while braces remain... */
101*0Sstevel@tonic-gate 	while ((left = strchr(sp, '{')) != NULL)
102*0Sstevel@tonic-gate 		if ((right = strchr(left, '}')) == NULL) {
103*0Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "Missing }");
104*0Sstevel@tonic-gate 			fn_list_free(ret);
105*0Sstevel@tonic-gate 			return (NULL);
106*0Sstevel@tonic-gate 		} else {
107*0Sstevel@tonic-gate 			/* stuff before "left" is finished */
108*0Sstevel@tonic-gate 			fn_list_appendrange(ret, sp, left);
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 			/* stuff after "right" still need processing */
111*0Sstevel@tonic-gate 			sp = right + 1;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 			if (left + 1 == right)
114*0Sstevel@tonic-gate 				continue;	/* just an empty {} */
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 			/* stuff between "left" and "right" is comma-sep list */
117*0Sstevel@tonic-gate 			left++;
118*0Sstevel@tonic-gate 			newret = fn_list_new(NULL);
119*0Sstevel@tonic-gate 			while ((comma = strchr(left, ',')) != NULL) {
120*0Sstevel@tonic-gate 				struct fn_list *dup = fn_list_dup(ret);
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate 				/* stuff from left to comma is one variant */
123*0Sstevel@tonic-gate 				fn_list_appendrange(dup, left, comma);
124*0Sstevel@tonic-gate 				fn_list_addfn_list(newret, dup);
125*0Sstevel@tonic-gate 				left = comma + 1;
126*0Sstevel@tonic-gate 			}
127*0Sstevel@tonic-gate 			/* what's left is the last item in the list */
128*0Sstevel@tonic-gate 			fn_list_appendrange(ret, left, right);
129*0Sstevel@tonic-gate 			fn_list_addfn_list(newret, ret);
130*0Sstevel@tonic-gate 			ret = newret;
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	/* anything remaining in "s" is finished */
134*0Sstevel@tonic-gate 	fn_list_appendrange(ret, sp, &sp[strlen(sp)]);
135*0Sstevel@tonic-gate 	return (ret);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate /* return true if filename contains any "magic" characters (*,?,[) */
139*0Sstevel@tonic-gate static boolean_t
140*0Sstevel@tonic-gate glob_magic(struct fn *fnp)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	char *s = fn_s(fnp);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	for (; *s; s++)
145*0Sstevel@tonic-gate 		if (*s == '*' ||
146*0Sstevel@tonic-gate 		    *s == '?' ||
147*0Sstevel@tonic-gate 		    *s == '[')
148*0Sstevel@tonic-gate 			return (B_TRUE);
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	return (B_FALSE);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate /*
154*0Sstevel@tonic-gate  * glob_glob -- given a filename glob, return the list of matching filenames
155*0Sstevel@tonic-gate  *
156*0Sstevel@tonic-gate  * fn_setn() and fn_setstat() are called to set the "n" and stat information
157*0Sstevel@tonic-gate  * for the resulting filenames.
158*0Sstevel@tonic-gate  */
159*0Sstevel@tonic-gate struct fn_list *
160*0Sstevel@tonic-gate glob_glob(struct fn *fnp)
161*0Sstevel@tonic-gate {
162*0Sstevel@tonic-gate 	struct fn_list *tmplist = glob_debrace(fnp);
163*0Sstevel@tonic-gate 	struct fn_list *ret;
164*0Sstevel@tonic-gate 	struct fn *nextfnp;
165*0Sstevel@tonic-gate 	struct fn *newfnp;
166*0Sstevel@tonic-gate 	int magic = 0;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	/* debracing produced NULL list? */
169*0Sstevel@tonic-gate 	if (tmplist == NULL)
170*0Sstevel@tonic-gate 		return (NULL);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	/* see if anything in list contains magic characters */
173*0Sstevel@tonic-gate 	fn_list_rewind(tmplist);
174*0Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(tmplist)) != NULL)
175*0Sstevel@tonic-gate 		if (glob_magic(nextfnp)) {
176*0Sstevel@tonic-gate 			magic = 1;
177*0Sstevel@tonic-gate 			break;
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	if (!magic)
181*0Sstevel@tonic-gate 		return (tmplist);	/* no globs to expand */
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	/* foreach name in the list, call glob_glob() to expand it */
184*0Sstevel@tonic-gate 	fn_list_rewind(tmplist);
185*0Sstevel@tonic-gate 	ret = fn_list_new(NULL);
186*0Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(tmplist)) != NULL) {
187*0Sstevel@tonic-gate 		newfnp = glob_to_reglob(nextfnp);
188*0Sstevel@tonic-gate 		fn_list_addfn(ret, newfnp);
189*0Sstevel@tonic-gate 	}
190*0Sstevel@tonic-gate 	fn_list_free(tmplist);
191*0Sstevel@tonic-gate 	tmplist = ret;
192*0Sstevel@tonic-gate 	ret = glob_reglob_list(tmplist);
193*0Sstevel@tonic-gate 	fn_list_free(tmplist);
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	return (ret);
196*0Sstevel@tonic-gate }
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate /*
199*0Sstevel@tonic-gate  * glob_glob_list -- given a list of filename globs, return all matches
200*0Sstevel@tonic-gate  */
201*0Sstevel@tonic-gate struct fn_list *
202*0Sstevel@tonic-gate glob_glob_list(struct fn_list *fnlp)
203*0Sstevel@tonic-gate {
204*0Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
205*0Sstevel@tonic-gate 	struct fn *fnp;
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 	fn_list_rewind(fnlp);
208*0Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
209*0Sstevel@tonic-gate 		fn_list_addfn_list(ret, glob_glob(fnp));
210*0Sstevel@tonic-gate 	return (ret);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate /*
214*0Sstevel@tonic-gate  * glob_reglob -- given a filename reglob, return a list of matching filenames
215*0Sstevel@tonic-gate  *
216*0Sstevel@tonic-gate  * this routine does all the hard work in this module.
217*0Sstevel@tonic-gate  */
218*0Sstevel@tonic-gate struct fn_list *
219*0Sstevel@tonic-gate glob_reglob(struct fn *fnp)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
222*0Sstevel@tonic-gate 	struct fn_list *newret;
223*0Sstevel@tonic-gate 	struct fn *nextfnp;
224*0Sstevel@tonic-gate 	char *mys = STRDUP(fn_s(fnp));
225*0Sstevel@tonic-gate 	char *sp = mys;
226*0Sstevel@tonic-gate 	char *slash;
227*0Sstevel@tonic-gate 	int skipdotfiles;
228*0Sstevel@tonic-gate 	char *re;
229*0Sstevel@tonic-gate 	char ret0[MAXPATHLEN];
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	/* start with the initial directory in the list */
232*0Sstevel@tonic-gate 	if (*sp == '/') {
233*0Sstevel@tonic-gate 		fn_list_adds(ret, "/");
234*0Sstevel@tonic-gate 		while (*sp == '/')
235*0Sstevel@tonic-gate 			sp++;
236*0Sstevel@tonic-gate 	} else
237*0Sstevel@tonic-gate 		fn_list_adds(ret, "./");
238*0Sstevel@tonic-gate 
239*0Sstevel@tonic-gate 	/* while components remain... */
240*0Sstevel@tonic-gate 	do {
241*0Sstevel@tonic-gate 		if ((slash = strchr(sp, '/')) != NULL) {
242*0Sstevel@tonic-gate 			*slash++ = '\0';
243*0Sstevel@tonic-gate 			/* skip superfluous slashes */
244*0Sstevel@tonic-gate 			while (*slash == '/')
245*0Sstevel@tonic-gate 				slash++;
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 		/* dot files are skipped unless a dot was specifically given */
249*0Sstevel@tonic-gate 		if (sp[0] == '\\' && sp[1] == '.')
250*0Sstevel@tonic-gate 			skipdotfiles = 0;
251*0Sstevel@tonic-gate 		else
252*0Sstevel@tonic-gate 			skipdotfiles = 1;
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate 		/* compile the regex */
255*0Sstevel@tonic-gate 		if ((re = regcmp("^", sp, "$", (char *)0)) == NULL)
256*0Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "regcmp failed on <%s>", sp);
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 		/* apply regex to every filename we've matched so far */
259*0Sstevel@tonic-gate 		newret = fn_list_new(NULL);
260*0Sstevel@tonic-gate 		fn_list_rewind(ret);
261*0Sstevel@tonic-gate 		while ((nextfnp = fn_list_next(ret)) != NULL) {
262*0Sstevel@tonic-gate 			DIR *dirp;
263*0Sstevel@tonic-gate 			struct dirent *dp;
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 			/* go through directory looking for matches */
266*0Sstevel@tonic-gate 			if ((dirp = opendir(fn_s(nextfnp))) == NULL)
267*0Sstevel@tonic-gate 				continue;
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 			while ((dp = readdir(dirp)) != NULL) {
270*0Sstevel@tonic-gate 				if (skipdotfiles && dp->d_name[0] == '.')
271*0Sstevel@tonic-gate 					continue;
272*0Sstevel@tonic-gate 				*ret0 = '\0';
273*0Sstevel@tonic-gate 				if (regex(re, dp->d_name, ret0)) {
274*0Sstevel@tonic-gate 					struct fn *matchfnp = fn_dup(nextfnp);
275*0Sstevel@tonic-gate 					struct stat stbuf;
276*0Sstevel@tonic-gate 					int n;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 					fn_puts(matchfnp, dp->d_name);
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 					if (stat(fn_s(matchfnp), &stbuf) < 0) {
281*0Sstevel@tonic-gate 						fn_free(matchfnp);
282*0Sstevel@tonic-gate 						continue;
283*0Sstevel@tonic-gate 					}
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 					/* skip non-dirs if more components */
286*0Sstevel@tonic-gate 					if (slash &&
287*0Sstevel@tonic-gate 					    (stbuf.st_mode & S_IFMT) !=
288*0Sstevel@tonic-gate 					    S_IFDIR) {
289*0Sstevel@tonic-gate 						fn_free(matchfnp);
290*0Sstevel@tonic-gate 						continue;
291*0Sstevel@tonic-gate 					}
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 					/*
294*0Sstevel@tonic-gate 					 * component matched, fill in "n"
295*0Sstevel@tonic-gate 					 * value, stat information, and
296*0Sstevel@tonic-gate 					 * append component to directory
297*0Sstevel@tonic-gate 					 * name just searched.
298*0Sstevel@tonic-gate 					 */
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 					if (*ret0)
301*0Sstevel@tonic-gate 						n = atoi(ret0);
302*0Sstevel@tonic-gate 					else
303*0Sstevel@tonic-gate 						n = -1;
304*0Sstevel@tonic-gate 					fn_setn(matchfnp, n);
305*0Sstevel@tonic-gate 					fn_setstat(matchfnp, &stbuf);
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 					if (slash)
308*0Sstevel@tonic-gate 						fn_putc(matchfnp, '/');
309*0Sstevel@tonic-gate 
310*0Sstevel@tonic-gate 					fn_list_addfn(newret, matchfnp);
311*0Sstevel@tonic-gate 				}
312*0Sstevel@tonic-gate 			}
313*0Sstevel@tonic-gate 			(void) closedir(dirp);
314*0Sstevel@tonic-gate 		}
315*0Sstevel@tonic-gate 		fn_list_free(ret);
316*0Sstevel@tonic-gate 		ret = newret;
317*0Sstevel@tonic-gate 		sp = slash;
318*0Sstevel@tonic-gate 	} while (slash);
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	FREE(mys);
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	return (ret);
323*0Sstevel@tonic-gate }
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /* reglob a list of filenames */
326*0Sstevel@tonic-gate static struct fn_list *
327*0Sstevel@tonic-gate glob_reglob_list(struct fn_list *fnlp)
328*0Sstevel@tonic-gate {
329*0Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
330*0Sstevel@tonic-gate 	struct fn *fnp;
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 	fn_list_rewind(fnlp);
333*0Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
334*0Sstevel@tonic-gate 		fn_list_addfn_list(ret, glob_reglob(fnp));
335*0Sstevel@tonic-gate 	return (ret);
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate /*
339*0Sstevel@tonic-gate  * glob_to_reglob -- convert a glob (*, ?, etc) to a reglob (.*, ., etc.)
340*0Sstevel@tonic-gate  */
341*0Sstevel@tonic-gate struct fn *
342*0Sstevel@tonic-gate glob_to_reglob(struct fn *fnp)
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate 	int c;
345*0Sstevel@tonic-gate 	struct fn *ret = fn_new(NULL);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	fn_rewind(fnp);
348*0Sstevel@tonic-gate 	while ((c = fn_getc(fnp)) != '\0')
349*0Sstevel@tonic-gate 		switch (c) {
350*0Sstevel@tonic-gate 		case '.':
351*0Sstevel@tonic-gate 		case '(':
352*0Sstevel@tonic-gate 		case ')':
353*0Sstevel@tonic-gate 		case '^':
354*0Sstevel@tonic-gate 		case '+':
355*0Sstevel@tonic-gate 		case '{':
356*0Sstevel@tonic-gate 		case '}':
357*0Sstevel@tonic-gate 		case '$':
358*0Sstevel@tonic-gate 			/* magic characters need backslash */
359*0Sstevel@tonic-gate 			fn_putc(ret, '\\');
360*0Sstevel@tonic-gate 			fn_putc(ret, c);
361*0Sstevel@tonic-gate 			break;
362*0Sstevel@tonic-gate 		case '?':
363*0Sstevel@tonic-gate 			/* change '?' to a single dot */
364*0Sstevel@tonic-gate 			fn_putc(ret, '.');
365*0Sstevel@tonic-gate 			break;
366*0Sstevel@tonic-gate 		case '*':
367*0Sstevel@tonic-gate 			/* change '*' to ".*" */
368*0Sstevel@tonic-gate 			fn_putc(ret, '.');
369*0Sstevel@tonic-gate 			fn_putc(ret, '*');
370*0Sstevel@tonic-gate 			break;
371*0Sstevel@tonic-gate 		default:
372*0Sstevel@tonic-gate 			fn_putc(ret, c);
373*0Sstevel@tonic-gate 		}
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	return (ret);
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate #ifdef	TESTMODULE
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate /*
381*0Sstevel@tonic-gate  * test main for glob module, usage: a.out [-r] [pattern...]
382*0Sstevel@tonic-gate  *	-r means the patterns are reglobs instead of globs
383*0Sstevel@tonic-gate  */
384*0Sstevel@tonic-gate main(int argc, char *argv[])
385*0Sstevel@tonic-gate {
386*0Sstevel@tonic-gate 	int i;
387*0Sstevel@tonic-gate 	int reglobs = 0;
388*0Sstevel@tonic-gate 	struct fn *argfnp = fn_new(NULL);
389*0Sstevel@tonic-gate 	struct fn *fnp;
390*0Sstevel@tonic-gate 	struct fn_list *fnlp;
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 	err_init(argv[0]);
393*0Sstevel@tonic-gate 	setbuf(stdout, NULL);
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
396*0Sstevel@tonic-gate 		if (strcmp(argv[i], "-r") == 0) {
397*0Sstevel@tonic-gate 			reglobs = 1;
398*0Sstevel@tonic-gate 			continue;
399*0Sstevel@tonic-gate 		}
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 		if (SETJMP) {
402*0Sstevel@tonic-gate 			printf("    skipped due to errors\n");
403*0Sstevel@tonic-gate 			continue;
404*0Sstevel@tonic-gate 		} else {
405*0Sstevel@tonic-gate 			printf("<%s>:\n", argv[i]);
406*0Sstevel@tonic-gate 			fn_renew(argfnp, argv[i]);
407*0Sstevel@tonic-gate 			if (reglobs)
408*0Sstevel@tonic-gate 				fnlp = glob_reglob(argfnp);
409*0Sstevel@tonic-gate 			else
410*0Sstevel@tonic-gate 				fnlp = glob_glob(argfnp);
411*0Sstevel@tonic-gate 		}
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate 		fn_list_rewind(fnlp);
414*0Sstevel@tonic-gate 		while ((fnp = fn_list_next(fnlp)) != NULL)
415*0Sstevel@tonic-gate 			printf("    <%s>\n", fn_s(fnp));
416*0Sstevel@tonic-gate 
417*0Sstevel@tonic-gate 		printf("total size: %d\n", fn_list_totalsize(fnlp));
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 		while ((fnp = fn_list_popoldest(fnlp)) != NULL) {
420*0Sstevel@tonic-gate 			printf("    oldest <%s>\n", fn_s(fnp));
421*0Sstevel@tonic-gate 			fn_free(fnp);
422*0Sstevel@tonic-gate 		}
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate 		fn_list_free(fnlp);
425*0Sstevel@tonic-gate 	}
426*0Sstevel@tonic-gate 	fn_free(argfnp);
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	err_done(0);
429*0Sstevel@tonic-gate }
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate #endif	/* TESTMODULE */
432