xref: /illumos-gate/usr/src/cmd/logadm/glob.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*b493790cSbasabi  * Common Development and Distribution License (the "License").
6*b493790cSbasabi  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b493790cSbasabi  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*b493790cSbasabi  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * logadm/glob.c -- globbing routines
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * these routines support two kinds of globs.  first, the
287c478bd9Sstevel@tonic-gate  * usual kind of filename globbing, like:
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  * 	*.c
317c478bd9Sstevel@tonic-gate  * 	/var/log/syslog.?
327c478bd9Sstevel@tonic-gate  * 	log[0-9]*file
337c478bd9Sstevel@tonic-gate  * 	/var/apache/logs/x*{access,error}_log
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * this is basically the same syntax that csh supports for globs and
367c478bd9Sstevel@tonic-gate  * is provided by the routine glob_glob() which takes a filename and
377c478bd9Sstevel@tonic-gate  * returns a list of filenames that match the glob.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * the second type is something called a "reglob" which is a pathname
407c478bd9Sstevel@tonic-gate  * where the components are regular expressions as described in regex(3c).
417c478bd9Sstevel@tonic-gate  * some examples:
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * 	.*\.c
447c478bd9Sstevel@tonic-gate  * 	/var/log/syslog\..
457c478bd9Sstevel@tonic-gate  * 	log[0-9].*file
467c478bd9Sstevel@tonic-gate  * 	/var/log/syslog\.([0-9]+)$0
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * the last example uses the ()$n form to assign a numeric extension
497c478bd9Sstevel@tonic-gate  * on a filename to the "n" value kept by the fn routines with each
507c478bd9Sstevel@tonic-gate  * filename (see fn_setn() in fn.c).  logadm uses this mechanism to
517c478bd9Sstevel@tonic-gate  * correctly sort lognames when templates containing $n are used.
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * the routine glob_reglob() is used to expand reglobs.  glob_glob()
547c478bd9Sstevel@tonic-gate  * is implemented by expanding the curly braces, converting the globs
557c478bd9Sstevel@tonic-gate  * to reglobs, and then passing the work to glob_reglob().
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * finally, since expanding globs and reglobs requires doing a stat(2)
587c478bd9Sstevel@tonic-gate  * on the files, we store the resulting stat information in the filename
597c478bd9Sstevel@tonic-gate  * struct (see fn_setstat() in fn.c).
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * the glob(3c) routines are not used here since they don't support
627c478bd9Sstevel@tonic-gate  * braces, and don't support the more powerful reglobs required by logadm.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <stdio.h>
667c478bd9Sstevel@tonic-gate #include <libintl.h>
677c478bd9Sstevel@tonic-gate #include <stdlib.h>
687c478bd9Sstevel@tonic-gate #include <libgen.h>
697c478bd9Sstevel@tonic-gate #include <strings.h>
707c478bd9Sstevel@tonic-gate #include <sys/types.h>
717c478bd9Sstevel@tonic-gate #include <sys/param.h>
727c478bd9Sstevel@tonic-gate #include <sys/stat.h>
737c478bd9Sstevel@tonic-gate #include <dirent.h>
747c478bd9Sstevel@tonic-gate #include "err.h"
757c478bd9Sstevel@tonic-gate #include "fn.h"
767c478bd9Sstevel@tonic-gate #include "glob.h"
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /* forward declarations for functions used internally by this module */
797c478bd9Sstevel@tonic-gate static struct fn_list *glob_debrace(struct fn *fnp);
807c478bd9Sstevel@tonic-gate static struct fn_list *glob_reglob_list(struct fn_list *fnlp);
817c478bd9Sstevel@tonic-gate static boolean_t glob_magic(struct fn *fnp);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /* expand curly braces (like file{one,two,three}name) */
847c478bd9Sstevel@tonic-gate static struct fn_list *
glob_debrace(struct fn * fnp)857c478bd9Sstevel@tonic-gate glob_debrace(struct fn *fnp)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
887c478bd9Sstevel@tonic-gate 	struct fn_list *newret;
897c478bd9Sstevel@tonic-gate 	char *sp = fn_s(fnp);
907c478bd9Sstevel@tonic-gate 	char *left;
917c478bd9Sstevel@tonic-gate 	char *right;
927c478bd9Sstevel@tonic-gate 	char *comma;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	/* start with an empty string in the list */
957c478bd9Sstevel@tonic-gate 	fn_list_adds(ret, "");
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/* while braces remain... */
98*b493790cSbasabi 	while (sp != NULL && (left = strchr(sp, '{')) != NULL)
997c478bd9Sstevel@tonic-gate 		if ((right = strchr(left, '}')) == NULL) {
1007c478bd9Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "Missing }");
1017c478bd9Sstevel@tonic-gate 			fn_list_free(ret);
1027c478bd9Sstevel@tonic-gate 			return (NULL);
1037c478bd9Sstevel@tonic-gate 		} else {
1047c478bd9Sstevel@tonic-gate 			/* stuff before "left" is finished */
1057c478bd9Sstevel@tonic-gate 			fn_list_appendrange(ret, sp, left);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 			/* stuff after "right" still need processing */
1087c478bd9Sstevel@tonic-gate 			sp = right + 1;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 			if (left + 1 == right)
1117c478bd9Sstevel@tonic-gate 				continue;	/* just an empty {} */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 			/* stuff between "left" and "right" is comma-sep list */
1147c478bd9Sstevel@tonic-gate 			left++;
1157c478bd9Sstevel@tonic-gate 			newret = fn_list_new(NULL);
1167c478bd9Sstevel@tonic-gate 			while ((comma = strchr(left, ',')) != NULL) {
1177c478bd9Sstevel@tonic-gate 				struct fn_list *dup = fn_list_dup(ret);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 				/* stuff from left to comma is one variant */
1207c478bd9Sstevel@tonic-gate 				fn_list_appendrange(dup, left, comma);
1217c478bd9Sstevel@tonic-gate 				fn_list_addfn_list(newret, dup);
1227c478bd9Sstevel@tonic-gate 				left = comma + 1;
1237c478bd9Sstevel@tonic-gate 			}
1247c478bd9Sstevel@tonic-gate 			/* what's left is the last item in the list */
1257c478bd9Sstevel@tonic-gate 			fn_list_appendrange(ret, left, right);
1267c478bd9Sstevel@tonic-gate 			fn_list_addfn_list(newret, ret);
1277c478bd9Sstevel@tonic-gate 			ret = newret;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/* anything remaining in "s" is finished */
1317c478bd9Sstevel@tonic-gate 	fn_list_appendrange(ret, sp, &sp[strlen(sp)]);
1327c478bd9Sstevel@tonic-gate 	return (ret);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /* return true if filename contains any "magic" characters (*,?,[) */
1367c478bd9Sstevel@tonic-gate static boolean_t
glob_magic(struct fn * fnp)1377c478bd9Sstevel@tonic-gate glob_magic(struct fn *fnp)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate 	char *s = fn_s(fnp);
1407c478bd9Sstevel@tonic-gate 
141*b493790cSbasabi 	for (; s != NULL && *s; s++)
1427c478bd9Sstevel@tonic-gate 		if (*s == '*' ||
1437c478bd9Sstevel@tonic-gate 		    *s == '?' ||
1447c478bd9Sstevel@tonic-gate 		    *s == '[')
1457c478bd9Sstevel@tonic-gate 			return (B_TRUE);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	return (B_FALSE);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * glob_glob -- given a filename glob, return the list of matching filenames
1527c478bd9Sstevel@tonic-gate  *
1537c478bd9Sstevel@tonic-gate  * fn_setn() and fn_setstat() are called to set the "n" and stat information
1547c478bd9Sstevel@tonic-gate  * for the resulting filenames.
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate struct fn_list *
glob_glob(struct fn * fnp)1577c478bd9Sstevel@tonic-gate glob_glob(struct fn *fnp)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	struct fn_list *tmplist = glob_debrace(fnp);
1607c478bd9Sstevel@tonic-gate 	struct fn_list *ret;
1617c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
1627c478bd9Sstevel@tonic-gate 	struct fn *newfnp;
1637c478bd9Sstevel@tonic-gate 	int magic = 0;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	/* debracing produced NULL list? */
1667c478bd9Sstevel@tonic-gate 	if (tmplist == NULL)
1677c478bd9Sstevel@tonic-gate 		return (NULL);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* see if anything in list contains magic characters */
1707c478bd9Sstevel@tonic-gate 	fn_list_rewind(tmplist);
1717c478bd9Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(tmplist)) != NULL)
1727c478bd9Sstevel@tonic-gate 		if (glob_magic(nextfnp)) {
1737c478bd9Sstevel@tonic-gate 			magic = 1;
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 		}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (!magic)
1787c478bd9Sstevel@tonic-gate 		return (tmplist);	/* no globs to expand */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	/* foreach name in the list, call glob_glob() to expand it */
1817c478bd9Sstevel@tonic-gate 	fn_list_rewind(tmplist);
1827c478bd9Sstevel@tonic-gate 	ret = fn_list_new(NULL);
1837c478bd9Sstevel@tonic-gate 	while ((nextfnp = fn_list_next(tmplist)) != NULL) {
1847c478bd9Sstevel@tonic-gate 		newfnp = glob_to_reglob(nextfnp);
1857c478bd9Sstevel@tonic-gate 		fn_list_addfn(ret, newfnp);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 	fn_list_free(tmplist);
1887c478bd9Sstevel@tonic-gate 	tmplist = ret;
1897c478bd9Sstevel@tonic-gate 	ret = glob_reglob_list(tmplist);
1907c478bd9Sstevel@tonic-gate 	fn_list_free(tmplist);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	return (ret);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * glob_glob_list -- given a list of filename globs, return all matches
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate struct fn_list *
glob_glob_list(struct fn_list * fnlp)1997c478bd9Sstevel@tonic-gate glob_glob_list(struct fn_list *fnlp)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
2027c478bd9Sstevel@tonic-gate 	struct fn *fnp;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
2057c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
2067c478bd9Sstevel@tonic-gate 		fn_list_addfn_list(ret, glob_glob(fnp));
2077c478bd9Sstevel@tonic-gate 	return (ret);
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * glob_reglob -- given a filename reglob, return a list of matching filenames
2127c478bd9Sstevel@tonic-gate  *
2137c478bd9Sstevel@tonic-gate  * this routine does all the hard work in this module.
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate struct fn_list *
glob_reglob(struct fn * fnp)2167c478bd9Sstevel@tonic-gate glob_reglob(struct fn *fnp)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
2197c478bd9Sstevel@tonic-gate 	struct fn_list *newret;
2207c478bd9Sstevel@tonic-gate 	struct fn *nextfnp;
2217c478bd9Sstevel@tonic-gate 	char *mys = STRDUP(fn_s(fnp));
2227c478bd9Sstevel@tonic-gate 	char *sp = mys;
2237c478bd9Sstevel@tonic-gate 	char *slash;
2247c478bd9Sstevel@tonic-gate 	int skipdotfiles;
2257c478bd9Sstevel@tonic-gate 	char *re;
2267c478bd9Sstevel@tonic-gate 	char ret0[MAXPATHLEN];
2277c478bd9Sstevel@tonic-gate 
228*b493790cSbasabi 
2297c478bd9Sstevel@tonic-gate 	/* start with the initial directory in the list */
2307c478bd9Sstevel@tonic-gate 	if (*sp == '/') {
2317c478bd9Sstevel@tonic-gate 		fn_list_adds(ret, "/");
2327c478bd9Sstevel@tonic-gate 		while (*sp == '/')
2337c478bd9Sstevel@tonic-gate 			sp++;
2347c478bd9Sstevel@tonic-gate 	} else
2357c478bd9Sstevel@tonic-gate 		fn_list_adds(ret, "./");
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/* while components remain... */
2387c478bd9Sstevel@tonic-gate 	do {
2397c478bd9Sstevel@tonic-gate 		if ((slash = strchr(sp, '/')) != NULL) {
2407c478bd9Sstevel@tonic-gate 			*slash++ = '\0';
2417c478bd9Sstevel@tonic-gate 			/* skip superfluous slashes */
2427c478bd9Sstevel@tonic-gate 			while (*slash == '/')
2437c478bd9Sstevel@tonic-gate 				slash++;
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 		/* dot files are skipped unless a dot was specifically given */
2477c478bd9Sstevel@tonic-gate 		if (sp[0] == '\\' && sp[1] == '.')
2487c478bd9Sstevel@tonic-gate 			skipdotfiles = 0;
2497c478bd9Sstevel@tonic-gate 		else
2507c478bd9Sstevel@tonic-gate 			skipdotfiles = 1;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		/* compile the regex */
2537c478bd9Sstevel@tonic-gate 		if ((re = regcmp("^", sp, "$", (char *)0)) == NULL)
2547c478bd9Sstevel@tonic-gate 			err(EF_FILE|EF_JMP, "regcmp failed on <%s>", sp);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 		/* apply regex to every filename we've matched so far */
2577c478bd9Sstevel@tonic-gate 		newret = fn_list_new(NULL);
2587c478bd9Sstevel@tonic-gate 		fn_list_rewind(ret);
2597c478bd9Sstevel@tonic-gate 		while ((nextfnp = fn_list_next(ret)) != NULL) {
2607c478bd9Sstevel@tonic-gate 			DIR *dirp;
2617c478bd9Sstevel@tonic-gate 			struct dirent *dp;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 			/* go through directory looking for matches */
2647c478bd9Sstevel@tonic-gate 			if ((dirp = opendir(fn_s(nextfnp))) == NULL)
2657c478bd9Sstevel@tonic-gate 				continue;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			while ((dp = readdir(dirp)) != NULL) {
2687c478bd9Sstevel@tonic-gate 				if (skipdotfiles && dp->d_name[0] == '.')
2697c478bd9Sstevel@tonic-gate 					continue;
2707c478bd9Sstevel@tonic-gate 				*ret0 = '\0';
2717c478bd9Sstevel@tonic-gate 				if (regex(re, dp->d_name, ret0)) {
2727c478bd9Sstevel@tonic-gate 					struct fn *matchfnp = fn_dup(nextfnp);
2737c478bd9Sstevel@tonic-gate 					struct stat stbuf;
2747c478bd9Sstevel@tonic-gate 					int n;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 					fn_puts(matchfnp, dp->d_name);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 					if (stat(fn_s(matchfnp), &stbuf) < 0) {
2797c478bd9Sstevel@tonic-gate 						fn_free(matchfnp);
2807c478bd9Sstevel@tonic-gate 						continue;
2817c478bd9Sstevel@tonic-gate 					}
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 					/* skip non-dirs if more components */
2847c478bd9Sstevel@tonic-gate 					if (slash &&
2857c478bd9Sstevel@tonic-gate 					    (stbuf.st_mode & S_IFMT) !=
2867c478bd9Sstevel@tonic-gate 					    S_IFDIR) {
2877c478bd9Sstevel@tonic-gate 						fn_free(matchfnp);
2887c478bd9Sstevel@tonic-gate 						continue;
2897c478bd9Sstevel@tonic-gate 					}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 					/*
2927c478bd9Sstevel@tonic-gate 					 * component matched, fill in "n"
2937c478bd9Sstevel@tonic-gate 					 * value, stat information, and
2947c478bd9Sstevel@tonic-gate 					 * append component to directory
2957c478bd9Sstevel@tonic-gate 					 * name just searched.
2967c478bd9Sstevel@tonic-gate 					 */
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 					if (*ret0)
2997c478bd9Sstevel@tonic-gate 						n = atoi(ret0);
3007c478bd9Sstevel@tonic-gate 					else
3017c478bd9Sstevel@tonic-gate 						n = -1;
3027c478bd9Sstevel@tonic-gate 					fn_setn(matchfnp, n);
3037c478bd9Sstevel@tonic-gate 					fn_setstat(matchfnp, &stbuf);
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 					if (slash)
3067c478bd9Sstevel@tonic-gate 						fn_putc(matchfnp, '/');
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 					fn_list_addfn(newret, matchfnp);
3097c478bd9Sstevel@tonic-gate 				}
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 			(void) closedir(dirp);
3127c478bd9Sstevel@tonic-gate 		}
3137c478bd9Sstevel@tonic-gate 		fn_list_free(ret);
3147c478bd9Sstevel@tonic-gate 		ret = newret;
3157c478bd9Sstevel@tonic-gate 		sp = slash;
3167c478bd9Sstevel@tonic-gate 	} while (slash);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	FREE(mys);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return (ret);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /* reglob a list of filenames */
3247c478bd9Sstevel@tonic-gate static struct fn_list *
glob_reglob_list(struct fn_list * fnlp)3257c478bd9Sstevel@tonic-gate glob_reglob_list(struct fn_list *fnlp)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate 	struct fn_list *ret = fn_list_new(NULL);
3287c478bd9Sstevel@tonic-gate 	struct fn *fnp;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	fn_list_rewind(fnlp);
3317c478bd9Sstevel@tonic-gate 	while ((fnp = fn_list_next(fnlp)) != NULL)
3327c478bd9Sstevel@tonic-gate 		fn_list_addfn_list(ret, glob_reglob(fnp));
3337c478bd9Sstevel@tonic-gate 	return (ret);
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate /*
3377c478bd9Sstevel@tonic-gate  * glob_to_reglob -- convert a glob (*, ?, etc) to a reglob (.*, ., etc.)
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate struct fn *
glob_to_reglob(struct fn * fnp)3407c478bd9Sstevel@tonic-gate glob_to_reglob(struct fn *fnp)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	int c;
3437c478bd9Sstevel@tonic-gate 	struct fn *ret = fn_new(NULL);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	fn_rewind(fnp);
3467c478bd9Sstevel@tonic-gate 	while ((c = fn_getc(fnp)) != '\0')
3477c478bd9Sstevel@tonic-gate 		switch (c) {
3487c478bd9Sstevel@tonic-gate 		case '.':
3497c478bd9Sstevel@tonic-gate 		case '(':
3507c478bd9Sstevel@tonic-gate 		case ')':
3517c478bd9Sstevel@tonic-gate 		case '^':
3527c478bd9Sstevel@tonic-gate 		case '+':
3537c478bd9Sstevel@tonic-gate 		case '{':
3547c478bd9Sstevel@tonic-gate 		case '}':
3557c478bd9Sstevel@tonic-gate 		case '$':
3567c478bd9Sstevel@tonic-gate 			/* magic characters need backslash */
3577c478bd9Sstevel@tonic-gate 			fn_putc(ret, '\\');
3587c478bd9Sstevel@tonic-gate 			fn_putc(ret, c);
3597c478bd9Sstevel@tonic-gate 			break;
3607c478bd9Sstevel@tonic-gate 		case '?':
3617c478bd9Sstevel@tonic-gate 			/* change '?' to a single dot */
3627c478bd9Sstevel@tonic-gate 			fn_putc(ret, '.');
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		case '*':
3657c478bd9Sstevel@tonic-gate 			/* change '*' to ".*" */
3667c478bd9Sstevel@tonic-gate 			fn_putc(ret, '.');
3677c478bd9Sstevel@tonic-gate 			fn_putc(ret, '*');
3687c478bd9Sstevel@tonic-gate 			break;
3697c478bd9Sstevel@tonic-gate 		default:
3707c478bd9Sstevel@tonic-gate 			fn_putc(ret, c);
3717c478bd9Sstevel@tonic-gate 		}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	return (ret);
3747c478bd9Sstevel@tonic-gate }
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate #ifdef	TESTMODULE
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate /*
3797c478bd9Sstevel@tonic-gate  * test main for glob module, usage: a.out [-r] [pattern...]
3807c478bd9Sstevel@tonic-gate  *	-r means the patterns are reglobs instead of globs
3817c478bd9Sstevel@tonic-gate  */
382*b493790cSbasabi int
main(int argc,char * argv[])3837c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate 	int i;
3867c478bd9Sstevel@tonic-gate 	int reglobs = 0;
3877c478bd9Sstevel@tonic-gate 	struct fn *argfnp = fn_new(NULL);
3887c478bd9Sstevel@tonic-gate 	struct fn *fnp;
3897c478bd9Sstevel@tonic-gate 	struct fn_list *fnlp;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	err_init(argv[0]);
3927c478bd9Sstevel@tonic-gate 	setbuf(stdout, NULL);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
3957c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "-r") == 0) {
3967c478bd9Sstevel@tonic-gate 			reglobs = 1;
3977c478bd9Sstevel@tonic-gate 			continue;
3987c478bd9Sstevel@tonic-gate 		}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		if (SETJMP) {
4017c478bd9Sstevel@tonic-gate 			printf("    skipped due to errors\n");
4027c478bd9Sstevel@tonic-gate 			continue;
4037c478bd9Sstevel@tonic-gate 		} else {
4047c478bd9Sstevel@tonic-gate 			printf("<%s>:\n", argv[i]);
4057c478bd9Sstevel@tonic-gate 			fn_renew(argfnp, argv[i]);
4067c478bd9Sstevel@tonic-gate 			if (reglobs)
4077c478bd9Sstevel@tonic-gate 				fnlp = glob_reglob(argfnp);
4087c478bd9Sstevel@tonic-gate 			else
4097c478bd9Sstevel@tonic-gate 				fnlp = glob_glob(argfnp);
4107c478bd9Sstevel@tonic-gate 		}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 		fn_list_rewind(fnlp);
4137c478bd9Sstevel@tonic-gate 		while ((fnp = fn_list_next(fnlp)) != NULL)
4147c478bd9Sstevel@tonic-gate 			printf("    <%s>\n", fn_s(fnp));
4157c478bd9Sstevel@tonic-gate 
416*b493790cSbasabi 		printf("total size: %lld\n", fn_list_totalsize(fnlp));
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		while ((fnp = fn_list_popoldest(fnlp)) != NULL) {
4197c478bd9Sstevel@tonic-gate 			printf("    oldest <%s>\n", fn_s(fnp));
4207c478bd9Sstevel@tonic-gate 			fn_free(fnp);
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 		fn_list_free(fnlp);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	fn_free(argfnp);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	err_done(0);
428*b493790cSbasabi 	/* NOTREACHED */
429*b493790cSbasabi 	return (0);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate #endif	/* TESTMODULE */
433