xref: /onnv-gate/usr/src/cmd/logadm/glob.c (revision 2397:34388daecb5f)
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
5*2397Sbasabi  * Common Development and Distribution License (the "License").
6*2397Sbasabi  * 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  */
210Sstevel@tonic-gate /*
22*2397Sbasabi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*2397Sbasabi  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * logadm/glob.c -- globbing routines
260Sstevel@tonic-gate  *
270Sstevel@tonic-gate  * these routines support two kinds of globs.  first, the
280Sstevel@tonic-gate  * usual kind of filename globbing, like:
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 	*.c
310Sstevel@tonic-gate  * 	/var/log/syslog.?
320Sstevel@tonic-gate  * 	log[0-9]*file
330Sstevel@tonic-gate  * 	/var/apache/logs/x*{access,error}_log
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * this is basically the same syntax that csh supports for globs and
360Sstevel@tonic-gate  * is provided by the routine glob_glob() which takes a filename and
370Sstevel@tonic-gate  * returns a list of filenames that match the glob.
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * the second type is something called a "reglob" which is a pathname
400Sstevel@tonic-gate  * where the components are regular expressions as described in regex(3c).
410Sstevel@tonic-gate  * some examples:
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * 	.*\.c
440Sstevel@tonic-gate  * 	/var/log/syslog\..
450Sstevel@tonic-gate  * 	log[0-9].*file
460Sstevel@tonic-gate  * 	/var/log/syslog\.([0-9]+)$0
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * the last example uses the ()$n form to assign a numeric extension
490Sstevel@tonic-gate  * on a filename to the "n" value kept by the fn routines with each
500Sstevel@tonic-gate  * filename (see fn_setn() in fn.c).  logadm uses this mechanism to
510Sstevel@tonic-gate  * correctly sort lognames when templates containing $n are used.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * the routine glob_reglob() is used to expand reglobs.  glob_glob()
540Sstevel@tonic-gate  * is implemented by expanding the curly braces, converting the globs
550Sstevel@tonic-gate  * to reglobs, and then passing the work to glob_reglob().
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  * finally, since expanding globs and reglobs requires doing a stat(2)
580Sstevel@tonic-gate  * on the files, we store the resulting stat information in the filename
590Sstevel@tonic-gate  * struct (see fn_setstat() in fn.c).
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * the glob(3c) routines are not used here since they don't support
620Sstevel@tonic-gate  * braces, and don't support the more powerful reglobs required by logadm.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <stdio.h>
680Sstevel@tonic-gate #include <libintl.h>
690Sstevel@tonic-gate #include <stdlib.h>
700Sstevel@tonic-gate #include <libgen.h>
710Sstevel@tonic-gate #include <strings.h>
720Sstevel@tonic-gate #include <sys/types.h>
730Sstevel@tonic-gate #include <sys/param.h>
740Sstevel@tonic-gate #include <sys/stat.h>
750Sstevel@tonic-gate #include <dirent.h>
760Sstevel@tonic-gate #include "err.h"
770Sstevel@tonic-gate #include "fn.h"
780Sstevel@tonic-gate #include "glob.h"
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* forward declarations for functions used internally by this module */
810Sstevel@tonic-gate static struct fn_list *glob_debrace(struct fn *fnp);
820Sstevel@tonic-gate static struct fn_list *glob_reglob_list(struct fn_list *fnlp);
830Sstevel@tonic-gate static boolean_t glob_magic(struct fn *fnp);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate /* expand curly braces (like file{one,two,three}name) */
860Sstevel@tonic-gate static struct fn_list *
glob_debrace(struct fn * fnp)870Sstevel@tonic-gate glob_debrace(struct fn *fnp)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
900Sstevel@tonic-gate 	struct fn_list *newret;
910Sstevel@tonic-gate 	char *sp = fn_s(fnp);
920Sstevel@tonic-gate 	char *left;
930Sstevel@tonic-gate 	char *right;
940Sstevel@tonic-gate 	char *comma;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/* start with an empty string in the list */
970Sstevel@tonic-gate 	fn_list_adds(ret, "");
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/* while braces remain... */
100*2397Sbasabi 	while (sp != NULL && (left = strchr(sp, '{')) != NULL)
1010Sstevel@tonic-gate 		if ((right = strchr(left, '}')) == NULL) {
1020Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "Missing }");
1030Sstevel@tonic-gate 			fn_list_free(ret);
1040Sstevel@tonic-gate 			return (NULL);
1050Sstevel@tonic-gate 		} else {
1060Sstevel@tonic-gate 			/* stuff before "left" is finished */
1070Sstevel@tonic-gate 			fn_list_appendrange(ret, sp, left);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 			/* stuff after "right" still need processing */
1100Sstevel@tonic-gate 			sp = right + 1;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 			if (left + 1 == right)
1130Sstevel@tonic-gate 				continue;	/* just an empty {} */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 			/* stuff between "left" and "right" is comma-sep list */
1160Sstevel@tonic-gate 			left++;
1170Sstevel@tonic-gate 			newret = fn_list_new(NULL);
1180Sstevel@tonic-gate 			while ((comma = strchr(left, ',')) != NULL) {
1190Sstevel@tonic-gate 				struct fn_list *dup = fn_list_dup(ret);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 				/* stuff from left to comma is one variant */
1220Sstevel@tonic-gate 				fn_list_appendrange(dup, left, comma);
1230Sstevel@tonic-gate 				fn_list_addfn_list(newret, dup);
1240Sstevel@tonic-gate 				left = comma + 1;
1250Sstevel@tonic-gate 			}
1260Sstevel@tonic-gate 			/* what's left is the last item in the list */
1270Sstevel@tonic-gate 			fn_list_appendrange(ret, left, right);
1280Sstevel@tonic-gate 			fn_list_addfn_list(newret, ret);
1290Sstevel@tonic-gate 			ret = newret;
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	/* anything remaining in "s" is finished */
1330Sstevel@tonic-gate 	fn_list_appendrange(ret, sp, &sp[strlen(sp)]);
1340Sstevel@tonic-gate 	return (ret);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /* return true if filename contains any "magic" characters (*,?,[) */
1380Sstevel@tonic-gate static boolean_t
glob_magic(struct fn * fnp)1390Sstevel@tonic-gate glob_magic(struct fn *fnp)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	char *s = fn_s(fnp);
1420Sstevel@tonic-gate 
143*2397Sbasabi 	for (; s != NULL && *s; s++)
1440Sstevel@tonic-gate 		if (*s == '*' ||
1450Sstevel@tonic-gate 		    *s == '?' ||
1460Sstevel@tonic-gate 		    *s == '[')
1470Sstevel@tonic-gate 			return (B_TRUE);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	return (B_FALSE);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * glob_glob -- given a filename glob, return the list of matching filenames
1540Sstevel@tonic-gate  *
1550Sstevel@tonic-gate  * fn_setn() and fn_setstat() are called to set the "n" and stat information
1560Sstevel@tonic-gate  * for the resulting filenames.
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate struct fn_list *
glob_glob(struct fn * fnp)1590Sstevel@tonic-gate glob_glob(struct fn *fnp)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	struct fn_list *tmplist = glob_debrace(fnp);
1620Sstevel@tonic-gate 	struct fn_list *ret;
1630Sstevel@tonic-gate 	struct fn *nextfnp;
1640Sstevel@tonic-gate 	struct fn *newfnp;
1650Sstevel@tonic-gate 	int magic = 0;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/* debracing produced NULL list? */
1680Sstevel@tonic-gate 	if (tmplist == NULL)
1690Sstevel@tonic-gate 		return (NULL);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 	/* see if anything in list contains magic characters */
1720Sstevel@tonic-gate 	fn_list_rewind(tmplist);
1730Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(tmplist)) != NULL)
1740Sstevel@tonic-gate 		if (glob_magic(nextfnp)) {
1750Sstevel@tonic-gate 			magic = 1;
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (!magic)
1800Sstevel@tonic-gate 		return (tmplist);	/* no globs to expand */
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/* foreach name in the list, call glob_glob() to expand it */
1830Sstevel@tonic-gate 	fn_list_rewind(tmplist);
1840Sstevel@tonic-gate 	ret = fn_list_new(NULL);
1850Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(tmplist)) != NULL) {
1860Sstevel@tonic-gate 		newfnp = glob_to_reglob(nextfnp);
1870Sstevel@tonic-gate 		fn_list_addfn(ret, newfnp);
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	fn_list_free(tmplist);
1900Sstevel@tonic-gate 	tmplist = ret;
1910Sstevel@tonic-gate 	ret = glob_reglob_list(tmplist);
1920Sstevel@tonic-gate 	fn_list_free(tmplist);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	return (ret);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate  * glob_glob_list -- given a list of filename globs, return all matches
1990Sstevel@tonic-gate  */
2000Sstevel@tonic-gate struct fn_list *
glob_glob_list(struct fn_list * fnlp)2010Sstevel@tonic-gate glob_glob_list(struct fn_list *fnlp)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
2040Sstevel@tonic-gate 	struct fn *fnp;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	fn_list_rewind(fnlp);
2070Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
2080Sstevel@tonic-gate 		fn_list_addfn_list(ret, glob_glob(fnp));
2090Sstevel@tonic-gate 	return (ret);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * glob_reglob -- given a filename reglob, return a list of matching filenames
2140Sstevel@tonic-gate  *
2150Sstevel@tonic-gate  * this routine does all the hard work in this module.
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate struct fn_list *
glob_reglob(struct fn * fnp)2180Sstevel@tonic-gate glob_reglob(struct fn *fnp)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
2210Sstevel@tonic-gate 	struct fn_list *newret;
2220Sstevel@tonic-gate 	struct fn *nextfnp;
2230Sstevel@tonic-gate 	char *mys = STRDUP(fn_s(fnp));
2240Sstevel@tonic-gate 	char *sp = mys;
2250Sstevel@tonic-gate 	char *slash;
2260Sstevel@tonic-gate 	int skipdotfiles;
2270Sstevel@tonic-gate 	char *re;
2280Sstevel@tonic-gate 	char ret0[MAXPATHLEN];
2290Sstevel@tonic-gate 
230*2397Sbasabi 
2310Sstevel@tonic-gate 	/* start with the initial directory in the list */
2320Sstevel@tonic-gate 	if (*sp == '/') {
2330Sstevel@tonic-gate 		fn_list_adds(ret, "/");
2340Sstevel@tonic-gate 		while (*sp == '/')
2350Sstevel@tonic-gate 			sp++;
2360Sstevel@tonic-gate 	} else
2370Sstevel@tonic-gate 		fn_list_adds(ret, "./");
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* while components remain... */
2400Sstevel@tonic-gate 	do {
2410Sstevel@tonic-gate 		if ((slash = strchr(sp, '/')) != NULL) {
2420Sstevel@tonic-gate 			*slash++ = '\0';
2430Sstevel@tonic-gate 			/* skip superfluous slashes */
2440Sstevel@tonic-gate 			while (*slash == '/')
2450Sstevel@tonic-gate 				slash++;
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 		/* dot files are skipped unless a dot was specifically given */
2490Sstevel@tonic-gate 		if (sp[0] == '\\' && sp[1] == '.')
2500Sstevel@tonic-gate 			skipdotfiles = 0;
2510Sstevel@tonic-gate 		else
2520Sstevel@tonic-gate 			skipdotfiles = 1;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		/* compile the regex */
2550Sstevel@tonic-gate 		if ((re = regcmp("^", sp, "$", (char *)0)) == NULL)
2560Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "regcmp failed on <%s>", sp);
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 		/* apply regex to every filename we've matched so far */
2590Sstevel@tonic-gate 		newret = fn_list_new(NULL);
2600Sstevel@tonic-gate 		fn_list_rewind(ret);
2610Sstevel@tonic-gate 		while ((nextfnp = fn_list_next(ret)) != NULL) {
2620Sstevel@tonic-gate 			DIR *dirp;
2630Sstevel@tonic-gate 			struct dirent *dp;
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 			/* go through directory looking for matches */
2660Sstevel@tonic-gate 			if ((dirp = opendir(fn_s(nextfnp))) == NULL)
2670Sstevel@tonic-gate 				continue;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 			while ((dp = readdir(dirp)) != NULL) {
2700Sstevel@tonic-gate 				if (skipdotfiles && dp->d_name[0] == '.')
2710Sstevel@tonic-gate 					continue;
2720Sstevel@tonic-gate 				*ret0 = '\0';
2730Sstevel@tonic-gate 				if (regex(re, dp->d_name, ret0)) {
2740Sstevel@tonic-gate 					struct fn *matchfnp = fn_dup(nextfnp);
2750Sstevel@tonic-gate 					struct stat stbuf;
2760Sstevel@tonic-gate 					int n;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 					fn_puts(matchfnp, dp->d_name);
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 					if (stat(fn_s(matchfnp), &stbuf) < 0) {
2810Sstevel@tonic-gate 						fn_free(matchfnp);
2820Sstevel@tonic-gate 						continue;
2830Sstevel@tonic-gate 					}
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate 					/* skip non-dirs if more components */
2860Sstevel@tonic-gate 					if (slash &&
2870Sstevel@tonic-gate 					    (stbuf.st_mode & S_IFMT) !=
2880Sstevel@tonic-gate 					    S_IFDIR) {
2890Sstevel@tonic-gate 						fn_free(matchfnp);
2900Sstevel@tonic-gate 						continue;
2910Sstevel@tonic-gate 					}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 					/*
2940Sstevel@tonic-gate 					 * component matched, fill in "n"
2950Sstevel@tonic-gate 					 * value, stat information, and
2960Sstevel@tonic-gate 					 * append component to directory
2970Sstevel@tonic-gate 					 * name just searched.
2980Sstevel@tonic-gate 					 */
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 					if (*ret0)
3010Sstevel@tonic-gate 						n = atoi(ret0);
3020Sstevel@tonic-gate 					else
3030Sstevel@tonic-gate 						n = -1;
3040Sstevel@tonic-gate 					fn_setn(matchfnp, n);
3050Sstevel@tonic-gate 					fn_setstat(matchfnp, &stbuf);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 					if (slash)
3080Sstevel@tonic-gate 						fn_putc(matchfnp, '/');
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 					fn_list_addfn(newret, matchfnp);
3110Sstevel@tonic-gate 				}
3120Sstevel@tonic-gate 			}
3130Sstevel@tonic-gate 			(void) closedir(dirp);
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 		fn_list_free(ret);
3160Sstevel@tonic-gate 		ret = newret;
3170Sstevel@tonic-gate 		sp = slash;
3180Sstevel@tonic-gate 	} while (slash);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	FREE(mys);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	return (ret);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate /* reglob a list of filenames */
3260Sstevel@tonic-gate static struct fn_list *
glob_reglob_list(struct fn_list * fnlp)3270Sstevel@tonic-gate glob_reglob_list(struct fn_list *fnlp)
3280Sstevel@tonic-gate {
3290Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
3300Sstevel@tonic-gate 	struct fn *fnp;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	fn_list_rewind(fnlp);
3330Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
3340Sstevel@tonic-gate 		fn_list_addfn_list(ret, glob_reglob(fnp));
3350Sstevel@tonic-gate 	return (ret);
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /*
3390Sstevel@tonic-gate  * glob_to_reglob -- convert a glob (*, ?, etc) to a reglob (.*, ., etc.)
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate struct fn *
glob_to_reglob(struct fn * fnp)3420Sstevel@tonic-gate glob_to_reglob(struct fn *fnp)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 	int c;
3450Sstevel@tonic-gate 	struct fn *ret = fn_new(NULL);
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	fn_rewind(fnp);
3480Sstevel@tonic-gate 	while ((c = fn_getc(fnp)) != '\0')
3490Sstevel@tonic-gate 		switch (c) {
3500Sstevel@tonic-gate 		case '.':
3510Sstevel@tonic-gate 		case '(':
3520Sstevel@tonic-gate 		case ')':
3530Sstevel@tonic-gate 		case '^':
3540Sstevel@tonic-gate 		case '+':
3550Sstevel@tonic-gate 		case '{':
3560Sstevel@tonic-gate 		case '}':
3570Sstevel@tonic-gate 		case '$':
3580Sstevel@tonic-gate 			/* magic characters need backslash */
3590Sstevel@tonic-gate 			fn_putc(ret, '\\');
3600Sstevel@tonic-gate 			fn_putc(ret, c);
3610Sstevel@tonic-gate 			break;
3620Sstevel@tonic-gate 		case '?':
3630Sstevel@tonic-gate 			/* change '?' to a single dot */
3640Sstevel@tonic-gate 			fn_putc(ret, '.');
3650Sstevel@tonic-gate 			break;
3660Sstevel@tonic-gate 		case '*':
3670Sstevel@tonic-gate 			/* change '*' to ".*" */
3680Sstevel@tonic-gate 			fn_putc(ret, '.');
3690Sstevel@tonic-gate 			fn_putc(ret, '*');
3700Sstevel@tonic-gate 			break;
3710Sstevel@tonic-gate 		default:
3720Sstevel@tonic-gate 			fn_putc(ret, c);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	return (ret);
3760Sstevel@tonic-gate }
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate #ifdef	TESTMODULE
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
3810Sstevel@tonic-gate  * test main for glob module, usage: a.out [-r] [pattern...]
3820Sstevel@tonic-gate  *	-r means the patterns are reglobs instead of globs
3830Sstevel@tonic-gate  */
384*2397Sbasabi int
main(int argc,char * argv[])3850Sstevel@tonic-gate main(int argc, char *argv[])
3860Sstevel@tonic-gate {
3870Sstevel@tonic-gate 	int i;
3880Sstevel@tonic-gate 	int reglobs = 0;
3890Sstevel@tonic-gate 	struct fn *argfnp = fn_new(NULL);
3900Sstevel@tonic-gate 	struct fn *fnp;
3910Sstevel@tonic-gate 	struct fn_list *fnlp;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	err_init(argv[0]);
3940Sstevel@tonic-gate 	setbuf(stdout, NULL);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
3970Sstevel@tonic-gate 		if (strcmp(argv[i], "-r") == 0) {
3980Sstevel@tonic-gate 			reglobs = 1;
3990Sstevel@tonic-gate 			continue;
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		if (SETJMP) {
4030Sstevel@tonic-gate 			printf("    skipped due to errors\n");
4040Sstevel@tonic-gate 			continue;
4050Sstevel@tonic-gate 		} else {
4060Sstevel@tonic-gate 			printf("<%s>:\n", argv[i]);
4070Sstevel@tonic-gate 			fn_renew(argfnp, argv[i]);
4080Sstevel@tonic-gate 			if (reglobs)
4090Sstevel@tonic-gate 				fnlp = glob_reglob(argfnp);
4100Sstevel@tonic-gate 			else
4110Sstevel@tonic-gate 				fnlp = glob_glob(argfnp);
4120Sstevel@tonic-gate 		}
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 		fn_list_rewind(fnlp);
4150Sstevel@tonic-gate 		while ((fnp = fn_list_next(fnlp)) != NULL)
4160Sstevel@tonic-gate 			printf("    <%s>\n", fn_s(fnp));
4170Sstevel@tonic-gate 
418*2397Sbasabi 		printf("total size: %lld\n", fn_list_totalsize(fnlp));
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		while ((fnp = fn_list_popoldest(fnlp)) != NULL) {
4210Sstevel@tonic-gate 			printf("    oldest <%s>\n", fn_s(fnp));
4220Sstevel@tonic-gate 			fn_free(fnp);
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 		fn_list_free(fnlp);
4260Sstevel@tonic-gate 	}
4270Sstevel@tonic-gate 	fn_free(argfnp);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	err_done(0);
430*2397Sbasabi 	/* NOTREACHED */
431*2397Sbasabi 	return (0);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate #endif	/* TESTMODULE */
435