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 1995 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*0Sstevel@tonic-gate /* 32*0Sstevel@tonic-gate * UNIX shell 33*0Sstevel@tonic-gate * 34*0Sstevel@tonic-gate */ 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #include "defs.h" 37*0Sstevel@tonic-gate #include <sys/types.h> 38*0Sstevel@tonic-gate #include <sys/stat.h> 39*0Sstevel@tonic-gate #include <dirent.h> 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate /* 44*0Sstevel@tonic-gate * globals (file name generation) 45*0Sstevel@tonic-gate * 46*0Sstevel@tonic-gate * "*" in params matches r.e ".*" 47*0Sstevel@tonic-gate * "?" in params matches r.e. "." 48*0Sstevel@tonic-gate * "[...]" in params matches character class 49*0Sstevel@tonic-gate * "[...a-z...]" in params matches a through z. 50*0Sstevel@tonic-gate * 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate static int addg(); 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate expand(as, rcnt) 55*0Sstevel@tonic-gate unsigned char *as; 56*0Sstevel@tonic-gate { 57*0Sstevel@tonic-gate int count; 58*0Sstevel@tonic-gate DIR *dirf; 59*0Sstevel@tonic-gate BOOL dir = 0; 60*0Sstevel@tonic-gate unsigned char *rescan = 0; 61*0Sstevel@tonic-gate unsigned char *slashsav = 0; 62*0Sstevel@tonic-gate register unsigned char *s, *cs; 63*0Sstevel@tonic-gate unsigned char *s2 = 0; 64*0Sstevel@tonic-gate struct argnod *schain = gchain; 65*0Sstevel@tonic-gate BOOL slash; 66*0Sstevel@tonic-gate int len; 67*0Sstevel@tonic-gate wchar_t wc; 68*0Sstevel@tonic-gate 69*0Sstevel@tonic-gate if (trapnote & SIGSET) 70*0Sstevel@tonic-gate return (0); 71*0Sstevel@tonic-gate s = cs = as; 72*0Sstevel@tonic-gate /* 73*0Sstevel@tonic-gate * check for meta chars 74*0Sstevel@tonic-gate */ 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate register BOOL open; 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gate slash = 0; 79*0Sstevel@tonic-gate open = 0; 80*0Sstevel@tonic-gate do 81*0Sstevel@tonic-gate { 82*0Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)cs, MB_LEN_MAX)) <= 0) { 83*0Sstevel@tonic-gate len = 1; 84*0Sstevel@tonic-gate wc = (unsigned char)*cs; 85*0Sstevel@tonic-gate } 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate cs += len; 88*0Sstevel@tonic-gate switch (wc) { 89*0Sstevel@tonic-gate case 0: 90*0Sstevel@tonic-gate if (rcnt && slash) 91*0Sstevel@tonic-gate break; 92*0Sstevel@tonic-gate else 93*0Sstevel@tonic-gate return (0); 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate case '/': 96*0Sstevel@tonic-gate slash++; 97*0Sstevel@tonic-gate open = 0; 98*0Sstevel@tonic-gate continue; 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate case '[': 101*0Sstevel@tonic-gate open++; 102*0Sstevel@tonic-gate continue; 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate case ']': 105*0Sstevel@tonic-gate if (open == 0) 106*0Sstevel@tonic-gate continue; 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate case '?': 109*0Sstevel@tonic-gate case '*': 110*0Sstevel@tonic-gate if (rcnt > slash) 111*0Sstevel@tonic-gate continue; 112*0Sstevel@tonic-gate else 113*0Sstevel@tonic-gate cs--; 114*0Sstevel@tonic-gate break; 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate case '\\': 117*0Sstevel@tonic-gate cs++; 118*0Sstevel@tonic-gate default: 119*0Sstevel@tonic-gate continue; 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate break; 122*0Sstevel@tonic-gate } while (TRUE); 123*0Sstevel@tonic-gate } 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate for (;;) 126*0Sstevel@tonic-gate { 127*0Sstevel@tonic-gate if (cs == s) 128*0Sstevel@tonic-gate { 129*0Sstevel@tonic-gate s = (unsigned char *)nullstr; 130*0Sstevel@tonic-gate break; 131*0Sstevel@tonic-gate } else if (*--cs == '/') 132*0Sstevel@tonic-gate { 133*0Sstevel@tonic-gate *cs = 0; 134*0Sstevel@tonic-gate if (s == cs) 135*0Sstevel@tonic-gate s = (unsigned char *)"/"; 136*0Sstevel@tonic-gate else { 137*0Sstevel@tonic-gate /* 138*0Sstevel@tonic-gate * push trimmed copy of directory prefix 139*0Sstevel@tonic-gate * onto stack 140*0Sstevel@tonic-gate */ 141*0Sstevel@tonic-gate s2 = cpystak(s); 142*0Sstevel@tonic-gate trim(s2); 143*0Sstevel@tonic-gate s = s2; 144*0Sstevel@tonic-gate } 145*0Sstevel@tonic-gate break; 146*0Sstevel@tonic-gate } 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate if ((dirf = opendir(*s ? (char *)s : (char *)".")) != 0) 150*0Sstevel@tonic-gate dir++; 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate /* Let s point to original string because it will be trimmed later */ 153*0Sstevel@tonic-gate if (s2) 154*0Sstevel@tonic-gate s = as; 155*0Sstevel@tonic-gate count = 0; 156*0Sstevel@tonic-gate if (*cs == 0) 157*0Sstevel@tonic-gate slashsav = cs++; /* remember where first slash in as is */ 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate /* check for rescan */ 160*0Sstevel@tonic-gate if (dir) 161*0Sstevel@tonic-gate { 162*0Sstevel@tonic-gate register unsigned char *rs; 163*0Sstevel@tonic-gate struct dirent *e; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate rs = cs; 166*0Sstevel@tonic-gate do /* find next / in as */ 167*0Sstevel@tonic-gate { 168*0Sstevel@tonic-gate if (*rs == '/') 169*0Sstevel@tonic-gate { 170*0Sstevel@tonic-gate rescan = rs; 171*0Sstevel@tonic-gate *rs = 0; 172*0Sstevel@tonic-gate gchain = 0; 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate } while (*rs++); 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate while ((e = readdir(dirf)) && (trapnote & SIGSET) == 0) 177*0Sstevel@tonic-gate { 178*0Sstevel@tonic-gate if (e->d_name[0] == '.' && *cs != '.') 179*0Sstevel@tonic-gate continue; 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate if (gmatch(e->d_name, cs)) 182*0Sstevel@tonic-gate { 183*0Sstevel@tonic-gate addg(s, e->d_name, rescan, slashsav); 184*0Sstevel@tonic-gate count++; 185*0Sstevel@tonic-gate } 186*0Sstevel@tonic-gate } 187*0Sstevel@tonic-gate (void) closedir(dirf); 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate if (rescan) 190*0Sstevel@tonic-gate { 191*0Sstevel@tonic-gate register struct argnod *rchain; 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate rchain = gchain; 194*0Sstevel@tonic-gate gchain = schain; 195*0Sstevel@tonic-gate if (count) 196*0Sstevel@tonic-gate { 197*0Sstevel@tonic-gate count = 0; 198*0Sstevel@tonic-gate while (rchain) 199*0Sstevel@tonic-gate { 200*0Sstevel@tonic-gate count += expand(rchain->argval, 201*0Sstevel@tonic-gate slash + 1); 202*0Sstevel@tonic-gate rchain = rchain->argnxt; 203*0Sstevel@tonic-gate } 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate *rescan = '/'; 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate if (slashsav) 210*0Sstevel@tonic-gate *slashsav = '/'; 211*0Sstevel@tonic-gate return (count); 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate static int 215*0Sstevel@tonic-gate addg(as1, as2, as3, as4) 216*0Sstevel@tonic-gate unsigned char *as1, *as2, *as3, *as4; 217*0Sstevel@tonic-gate { 218*0Sstevel@tonic-gate register unsigned char *s1, *s2; 219*0Sstevel@tonic-gate register int c; 220*0Sstevel@tonic-gate int len; 221*0Sstevel@tonic-gate wchar_t wc; 222*0Sstevel@tonic-gate 223*0Sstevel@tonic-gate s2 = locstak() + BYTESPERWORD; 224*0Sstevel@tonic-gate s1 = as1; 225*0Sstevel@tonic-gate if (as4) { 226*0Sstevel@tonic-gate while (c = *s1++) 227*0Sstevel@tonic-gate { 228*0Sstevel@tonic-gate if (s2 >= brkend) 229*0Sstevel@tonic-gate growstak(s2); 230*0Sstevel@tonic-gate *s2++ = c; 231*0Sstevel@tonic-gate } 232*0Sstevel@tonic-gate /* 233*0Sstevel@tonic-gate * Restore first slash before the first metacharacter 234*0Sstevel@tonic-gate * if as1 is not "/" 235*0Sstevel@tonic-gate */ 236*0Sstevel@tonic-gate if (as4 + 1 == s1) { 237*0Sstevel@tonic-gate if (s2 >= brkend) 238*0Sstevel@tonic-gate growstak(s2); 239*0Sstevel@tonic-gate *s2++ = '/'; 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate } 242*0Sstevel@tonic-gate /* add matched entries, plus extra \\ to escape \\'s */ 243*0Sstevel@tonic-gate s1 = as2; 244*0Sstevel@tonic-gate for (;;) 245*0Sstevel@tonic-gate { 246*0Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) { 247*0Sstevel@tonic-gate len = 1; 248*0Sstevel@tonic-gate wc = (unsigned char)*s1; 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate if (s2 >= brkend) 251*0Sstevel@tonic-gate growstak(s2); 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate if (wc == 0) { 254*0Sstevel@tonic-gate *s2 = *s1++; 255*0Sstevel@tonic-gate break; 256*0Sstevel@tonic-gate } 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate if (wc == '\\') { 259*0Sstevel@tonic-gate *s2++ = '\\'; 260*0Sstevel@tonic-gate if (s2 >= brkend) 261*0Sstevel@tonic-gate growstak(s2); 262*0Sstevel@tonic-gate *s2++ = '\\'; 263*0Sstevel@tonic-gate s1++; 264*0Sstevel@tonic-gate continue; 265*0Sstevel@tonic-gate } 266*0Sstevel@tonic-gate if ((s2 + len) >= brkend) 267*0Sstevel@tonic-gate growstak(s2 + len); 268*0Sstevel@tonic-gate memcpy(s2, s1, len); 269*0Sstevel@tonic-gate s2 += len; 270*0Sstevel@tonic-gate s1 += len; 271*0Sstevel@tonic-gate } 272*0Sstevel@tonic-gate if (s1 = as3) 273*0Sstevel@tonic-gate { 274*0Sstevel@tonic-gate if (s2 >= brkend) 275*0Sstevel@tonic-gate growstak(s2); 276*0Sstevel@tonic-gate *s2++ = '/'; 277*0Sstevel@tonic-gate do 278*0Sstevel@tonic-gate { 279*0Sstevel@tonic-gate if (s2 >= brkend) 280*0Sstevel@tonic-gate growstak(s2); 281*0Sstevel@tonic-gate } 282*0Sstevel@tonic-gate while (*s2++ = *++s1); 283*0Sstevel@tonic-gate } 284*0Sstevel@tonic-gate makearg(endstak(s2)); 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate makearg(args) 288*0Sstevel@tonic-gate register struct argnod *args; 289*0Sstevel@tonic-gate { 290*0Sstevel@tonic-gate args->argnxt = gchain; 291*0Sstevel@tonic-gate gchain = args; 292*0Sstevel@tonic-gate } 293