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