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 #pragma ident "%Z%%M% %I% %E% SMI" 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #include <stdio.h> 687c478bd9Sstevel@tonic-gate #include <libintl.h> 697c478bd9Sstevel@tonic-gate #include <stdlib.h> 707c478bd9Sstevel@tonic-gate #include <libgen.h> 717c478bd9Sstevel@tonic-gate #include <strings.h> 727c478bd9Sstevel@tonic-gate #include <sys/types.h> 737c478bd9Sstevel@tonic-gate #include <sys/param.h> 747c478bd9Sstevel@tonic-gate #include <sys/stat.h> 757c478bd9Sstevel@tonic-gate #include <dirent.h> 767c478bd9Sstevel@tonic-gate #include "err.h" 777c478bd9Sstevel@tonic-gate #include "fn.h" 787c478bd9Sstevel@tonic-gate #include "glob.h" 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* forward declarations for functions used internally by this module */ 817c478bd9Sstevel@tonic-gate static struct fn_list *glob_debrace(struct fn *fnp); 827c478bd9Sstevel@tonic-gate static struct fn_list *glob_reglob_list(struct fn_list *fnlp); 837c478bd9Sstevel@tonic-gate static boolean_t glob_magic(struct fn *fnp); 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* expand curly braces (like file{one,two,three}name) */ 867c478bd9Sstevel@tonic-gate static struct fn_list * 877c478bd9Sstevel@tonic-gate glob_debrace(struct fn *fnp) 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate struct fn_list *ret = fn_list_new(NULL); 907c478bd9Sstevel@tonic-gate struct fn_list *newret; 917c478bd9Sstevel@tonic-gate char *sp = fn_s(fnp); 927c478bd9Sstevel@tonic-gate char *left; 937c478bd9Sstevel@tonic-gate char *right; 947c478bd9Sstevel@tonic-gate char *comma; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate /* start with an empty string in the list */ 977c478bd9Sstevel@tonic-gate fn_list_adds(ret, ""); 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* while braces remain... */ 100*b493790cSbasabi while (sp != NULL && (left = strchr(sp, '{')) != NULL) 1017c478bd9Sstevel@tonic-gate if ((right = strchr(left, '}')) == NULL) { 1027c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, "Missing }"); 1037c478bd9Sstevel@tonic-gate fn_list_free(ret); 1047c478bd9Sstevel@tonic-gate return (NULL); 1057c478bd9Sstevel@tonic-gate } else { 1067c478bd9Sstevel@tonic-gate /* stuff before "left" is finished */ 1077c478bd9Sstevel@tonic-gate fn_list_appendrange(ret, sp, left); 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* stuff after "right" still need processing */ 1107c478bd9Sstevel@tonic-gate sp = right + 1; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate if (left + 1 == right) 1137c478bd9Sstevel@tonic-gate continue; /* just an empty {} */ 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate /* stuff between "left" and "right" is comma-sep list */ 1167c478bd9Sstevel@tonic-gate left++; 1177c478bd9Sstevel@tonic-gate newret = fn_list_new(NULL); 1187c478bd9Sstevel@tonic-gate while ((comma = strchr(left, ',')) != NULL) { 1197c478bd9Sstevel@tonic-gate struct fn_list *dup = fn_list_dup(ret); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* stuff from left to comma is one variant */ 1227c478bd9Sstevel@tonic-gate fn_list_appendrange(dup, left, comma); 1237c478bd9Sstevel@tonic-gate fn_list_addfn_list(newret, dup); 1247c478bd9Sstevel@tonic-gate left = comma + 1; 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate /* what's left is the last item in the list */ 1277c478bd9Sstevel@tonic-gate fn_list_appendrange(ret, left, right); 1287c478bd9Sstevel@tonic-gate fn_list_addfn_list(newret, ret); 1297c478bd9Sstevel@tonic-gate ret = newret; 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* anything remaining in "s" is finished */ 1337c478bd9Sstevel@tonic-gate fn_list_appendrange(ret, sp, &sp[strlen(sp)]); 1347c478bd9Sstevel@tonic-gate return (ret); 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* return true if filename contains any "magic" characters (*,?,[) */ 1387c478bd9Sstevel@tonic-gate static boolean_t 1397c478bd9Sstevel@tonic-gate glob_magic(struct fn *fnp) 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate char *s = fn_s(fnp); 1427c478bd9Sstevel@tonic-gate 143*b493790cSbasabi for (; s != NULL && *s; s++) 1447c478bd9Sstevel@tonic-gate if (*s == '*' || 1457c478bd9Sstevel@tonic-gate *s == '?' || 1467c478bd9Sstevel@tonic-gate *s == '[') 1477c478bd9Sstevel@tonic-gate return (B_TRUE); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate return (B_FALSE); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * glob_glob -- given a filename glob, return the list of matching filenames 1547c478bd9Sstevel@tonic-gate * 1557c478bd9Sstevel@tonic-gate * fn_setn() and fn_setstat() are called to set the "n" and stat information 1567c478bd9Sstevel@tonic-gate * for the resulting filenames. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate struct fn_list * 1597c478bd9Sstevel@tonic-gate glob_glob(struct fn *fnp) 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate struct fn_list *tmplist = glob_debrace(fnp); 1627c478bd9Sstevel@tonic-gate struct fn_list *ret; 1637c478bd9Sstevel@tonic-gate struct fn *nextfnp; 1647c478bd9Sstevel@tonic-gate struct fn *newfnp; 1657c478bd9Sstevel@tonic-gate int magic = 0; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* debracing produced NULL list? */ 1687c478bd9Sstevel@tonic-gate if (tmplist == NULL) 1697c478bd9Sstevel@tonic-gate return (NULL); 1707c478bd9Sstevel@tonic-gate 1717c478bd9Sstevel@tonic-gate /* see if anything in list contains magic characters */ 1727c478bd9Sstevel@tonic-gate fn_list_rewind(tmplist); 1737c478bd9Sstevel@tonic-gate while ((nextfnp = fn_list_next(tmplist)) != NULL) 1747c478bd9Sstevel@tonic-gate if (glob_magic(nextfnp)) { 1757c478bd9Sstevel@tonic-gate magic = 1; 1767c478bd9Sstevel@tonic-gate break; 1777c478bd9Sstevel@tonic-gate } 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate if (!magic) 1807c478bd9Sstevel@tonic-gate return (tmplist); /* no globs to expand */ 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate /* foreach name in the list, call glob_glob() to expand it */ 1837c478bd9Sstevel@tonic-gate fn_list_rewind(tmplist); 1847c478bd9Sstevel@tonic-gate ret = fn_list_new(NULL); 1857c478bd9Sstevel@tonic-gate while ((nextfnp = fn_list_next(tmplist)) != NULL) { 1867c478bd9Sstevel@tonic-gate newfnp = glob_to_reglob(nextfnp); 1877c478bd9Sstevel@tonic-gate fn_list_addfn(ret, newfnp); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate fn_list_free(tmplist); 1907c478bd9Sstevel@tonic-gate tmplist = ret; 1917c478bd9Sstevel@tonic-gate ret = glob_reglob_list(tmplist); 1927c478bd9Sstevel@tonic-gate fn_list_free(tmplist); 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate return (ret); 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* 1987c478bd9Sstevel@tonic-gate * glob_glob_list -- given a list of filename globs, return all matches 1997c478bd9Sstevel@tonic-gate */ 2007c478bd9Sstevel@tonic-gate struct fn_list * 2017c478bd9Sstevel@tonic-gate glob_glob_list(struct fn_list *fnlp) 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate struct fn_list *ret = fn_list_new(NULL); 2047c478bd9Sstevel@tonic-gate struct fn *fnp; 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 2077c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 2087c478bd9Sstevel@tonic-gate fn_list_addfn_list(ret, glob_glob(fnp)); 2097c478bd9Sstevel@tonic-gate return (ret); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /* 2137c478bd9Sstevel@tonic-gate * glob_reglob -- given a filename reglob, return a list of matching filenames 2147c478bd9Sstevel@tonic-gate * 2157c478bd9Sstevel@tonic-gate * this routine does all the hard work in this module. 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate struct fn_list * 2187c478bd9Sstevel@tonic-gate glob_reglob(struct fn *fnp) 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate struct fn_list *ret = fn_list_new(NULL); 2217c478bd9Sstevel@tonic-gate struct fn_list *newret; 2227c478bd9Sstevel@tonic-gate struct fn *nextfnp; 2237c478bd9Sstevel@tonic-gate char *mys = STRDUP(fn_s(fnp)); 2247c478bd9Sstevel@tonic-gate char *sp = mys; 2257c478bd9Sstevel@tonic-gate char *slash; 2267c478bd9Sstevel@tonic-gate int skipdotfiles; 2277c478bd9Sstevel@tonic-gate char *re; 2287c478bd9Sstevel@tonic-gate char ret0[MAXPATHLEN]; 2297c478bd9Sstevel@tonic-gate 230*b493790cSbasabi 2317c478bd9Sstevel@tonic-gate /* start with the initial directory in the list */ 2327c478bd9Sstevel@tonic-gate if (*sp == '/') { 2337c478bd9Sstevel@tonic-gate fn_list_adds(ret, "/"); 2347c478bd9Sstevel@tonic-gate while (*sp == '/') 2357c478bd9Sstevel@tonic-gate sp++; 2367c478bd9Sstevel@tonic-gate } else 2377c478bd9Sstevel@tonic-gate fn_list_adds(ret, "./"); 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate /* while components remain... */ 2407c478bd9Sstevel@tonic-gate do { 2417c478bd9Sstevel@tonic-gate if ((slash = strchr(sp, '/')) != NULL) { 2427c478bd9Sstevel@tonic-gate *slash++ = '\0'; 2437c478bd9Sstevel@tonic-gate /* skip superfluous slashes */ 2447c478bd9Sstevel@tonic-gate while (*slash == '/') 2457c478bd9Sstevel@tonic-gate slash++; 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* dot files are skipped unless a dot was specifically given */ 2497c478bd9Sstevel@tonic-gate if (sp[0] == '\\' && sp[1] == '.') 2507c478bd9Sstevel@tonic-gate skipdotfiles = 0; 2517c478bd9Sstevel@tonic-gate else 2527c478bd9Sstevel@tonic-gate skipdotfiles = 1; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate /* compile the regex */ 2557c478bd9Sstevel@tonic-gate if ((re = regcmp("^", sp, "$", (char *)0)) == NULL) 2567c478bd9Sstevel@tonic-gate err(EF_FILE|EF_JMP, "regcmp failed on <%s>", sp); 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate /* apply regex to every filename we've matched so far */ 2597c478bd9Sstevel@tonic-gate newret = fn_list_new(NULL); 2607c478bd9Sstevel@tonic-gate fn_list_rewind(ret); 2617c478bd9Sstevel@tonic-gate while ((nextfnp = fn_list_next(ret)) != NULL) { 2627c478bd9Sstevel@tonic-gate DIR *dirp; 2637c478bd9Sstevel@tonic-gate struct dirent *dp; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate /* go through directory looking for matches */ 2667c478bd9Sstevel@tonic-gate if ((dirp = opendir(fn_s(nextfnp))) == NULL) 2677c478bd9Sstevel@tonic-gate continue; 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate while ((dp = readdir(dirp)) != NULL) { 2707c478bd9Sstevel@tonic-gate if (skipdotfiles && dp->d_name[0] == '.') 2717c478bd9Sstevel@tonic-gate continue; 2727c478bd9Sstevel@tonic-gate *ret0 = '\0'; 2737c478bd9Sstevel@tonic-gate if (regex(re, dp->d_name, ret0)) { 2747c478bd9Sstevel@tonic-gate struct fn *matchfnp = fn_dup(nextfnp); 2757c478bd9Sstevel@tonic-gate struct stat stbuf; 2767c478bd9Sstevel@tonic-gate int n; 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate fn_puts(matchfnp, dp->d_name); 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate if (stat(fn_s(matchfnp), &stbuf) < 0) { 2817c478bd9Sstevel@tonic-gate fn_free(matchfnp); 2827c478bd9Sstevel@tonic-gate continue; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate /* skip non-dirs if more components */ 2867c478bd9Sstevel@tonic-gate if (slash && 2877c478bd9Sstevel@tonic-gate (stbuf.st_mode & S_IFMT) != 2887c478bd9Sstevel@tonic-gate S_IFDIR) { 2897c478bd9Sstevel@tonic-gate fn_free(matchfnp); 2907c478bd9Sstevel@tonic-gate continue; 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate /* 2947c478bd9Sstevel@tonic-gate * component matched, fill in "n" 2957c478bd9Sstevel@tonic-gate * value, stat information, and 2967c478bd9Sstevel@tonic-gate * append component to directory 2977c478bd9Sstevel@tonic-gate * name just searched. 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if (*ret0) 3017c478bd9Sstevel@tonic-gate n = atoi(ret0); 3027c478bd9Sstevel@tonic-gate else 3037c478bd9Sstevel@tonic-gate n = -1; 3047c478bd9Sstevel@tonic-gate fn_setn(matchfnp, n); 3057c478bd9Sstevel@tonic-gate fn_setstat(matchfnp, &stbuf); 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if (slash) 3087c478bd9Sstevel@tonic-gate fn_putc(matchfnp, '/'); 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate fn_list_addfn(newret, matchfnp); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate (void) closedir(dirp); 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate fn_list_free(ret); 3167c478bd9Sstevel@tonic-gate ret = newret; 3177c478bd9Sstevel@tonic-gate sp = slash; 3187c478bd9Sstevel@tonic-gate } while (slash); 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate FREE(mys); 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate return (ret); 3237c478bd9Sstevel@tonic-gate } 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* reglob a list of filenames */ 3267c478bd9Sstevel@tonic-gate static struct fn_list * 3277c478bd9Sstevel@tonic-gate glob_reglob_list(struct fn_list *fnlp) 3287c478bd9Sstevel@tonic-gate { 3297c478bd9Sstevel@tonic-gate struct fn_list *ret = fn_list_new(NULL); 3307c478bd9Sstevel@tonic-gate struct fn *fnp; 3317c478bd9Sstevel@tonic-gate 3327c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 3337c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 3347c478bd9Sstevel@tonic-gate fn_list_addfn_list(ret, glob_reglob(fnp)); 3357c478bd9Sstevel@tonic-gate return (ret); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate 3387c478bd9Sstevel@tonic-gate /* 3397c478bd9Sstevel@tonic-gate * glob_to_reglob -- convert a glob (*, ?, etc) to a reglob (.*, ., etc.) 3407c478bd9Sstevel@tonic-gate */ 3417c478bd9Sstevel@tonic-gate struct fn * 3427c478bd9Sstevel@tonic-gate glob_to_reglob(struct fn *fnp) 3437c478bd9Sstevel@tonic-gate { 3447c478bd9Sstevel@tonic-gate int c; 3457c478bd9Sstevel@tonic-gate struct fn *ret = fn_new(NULL); 3467c478bd9Sstevel@tonic-gate 3477c478bd9Sstevel@tonic-gate fn_rewind(fnp); 3487c478bd9Sstevel@tonic-gate while ((c = fn_getc(fnp)) != '\0') 3497c478bd9Sstevel@tonic-gate switch (c) { 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 case '}': 3577c478bd9Sstevel@tonic-gate case '$': 3587c478bd9Sstevel@tonic-gate /* magic characters need backslash */ 3597c478bd9Sstevel@tonic-gate fn_putc(ret, '\\'); 3607c478bd9Sstevel@tonic-gate fn_putc(ret, c); 3617c478bd9Sstevel@tonic-gate break; 3627c478bd9Sstevel@tonic-gate case '?': 3637c478bd9Sstevel@tonic-gate /* change '?' to a single dot */ 3647c478bd9Sstevel@tonic-gate fn_putc(ret, '.'); 3657c478bd9Sstevel@tonic-gate break; 3667c478bd9Sstevel@tonic-gate case '*': 3677c478bd9Sstevel@tonic-gate /* change '*' to ".*" */ 3687c478bd9Sstevel@tonic-gate fn_putc(ret, '.'); 3697c478bd9Sstevel@tonic-gate fn_putc(ret, '*'); 3707c478bd9Sstevel@tonic-gate break; 3717c478bd9Sstevel@tonic-gate default: 3727c478bd9Sstevel@tonic-gate fn_putc(ret, c); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate return (ret); 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate 3787c478bd9Sstevel@tonic-gate #ifdef TESTMODULE 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate /* 3817c478bd9Sstevel@tonic-gate * test main for glob module, usage: a.out [-r] [pattern...] 3827c478bd9Sstevel@tonic-gate * -r means the patterns are reglobs instead of globs 3837c478bd9Sstevel@tonic-gate */ 384*b493790cSbasabi int 3857c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 3867c478bd9Sstevel@tonic-gate { 3877c478bd9Sstevel@tonic-gate int i; 3887c478bd9Sstevel@tonic-gate int reglobs = 0; 3897c478bd9Sstevel@tonic-gate struct fn *argfnp = fn_new(NULL); 3907c478bd9Sstevel@tonic-gate struct fn *fnp; 3917c478bd9Sstevel@tonic-gate struct fn_list *fnlp; 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate err_init(argv[0]); 3947c478bd9Sstevel@tonic-gate setbuf(stdout, NULL); 3957c478bd9Sstevel@tonic-gate 3967c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 3977c478bd9Sstevel@tonic-gate if (strcmp(argv[i], "-r") == 0) { 3987c478bd9Sstevel@tonic-gate reglobs = 1; 3997c478bd9Sstevel@tonic-gate continue; 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate if (SETJMP) { 4037c478bd9Sstevel@tonic-gate printf(" skipped due to errors\n"); 4047c478bd9Sstevel@tonic-gate continue; 4057c478bd9Sstevel@tonic-gate } else { 4067c478bd9Sstevel@tonic-gate printf("<%s>:\n", argv[i]); 4077c478bd9Sstevel@tonic-gate fn_renew(argfnp, argv[i]); 4087c478bd9Sstevel@tonic-gate if (reglobs) 4097c478bd9Sstevel@tonic-gate fnlp = glob_reglob(argfnp); 4107c478bd9Sstevel@tonic-gate else 4117c478bd9Sstevel@tonic-gate fnlp = glob_glob(argfnp); 4127c478bd9Sstevel@tonic-gate } 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate fn_list_rewind(fnlp); 4157c478bd9Sstevel@tonic-gate while ((fnp = fn_list_next(fnlp)) != NULL) 4167c478bd9Sstevel@tonic-gate printf(" <%s>\n", fn_s(fnp)); 4177c478bd9Sstevel@tonic-gate 418*b493790cSbasabi printf("total size: %lld\n", fn_list_totalsize(fnlp)); 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate while ((fnp = fn_list_popoldest(fnlp)) != NULL) { 4217c478bd9Sstevel@tonic-gate printf(" oldest <%s>\n", fn_s(fnp)); 4227c478bd9Sstevel@tonic-gate fn_free(fnp); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate fn_list_free(fnlp); 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate fn_free(argfnp); 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate err_done(0); 430*b493790cSbasabi /* NOTREACHED */ 431*b493790cSbasabi return (0); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate #endif /* TESTMODULE */ 435