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 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 1995 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * UNIX shell 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include "defs.h" 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <sys/stat.h> 397c478bd9Sstevel@tonic-gate #include <dirent.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate /* 447c478bd9Sstevel@tonic-gate * globals (file name generation) 457c478bd9Sstevel@tonic-gate * 467c478bd9Sstevel@tonic-gate * "*" in params matches r.e ".*" 477c478bd9Sstevel@tonic-gate * "?" in params matches r.e. "." 487c478bd9Sstevel@tonic-gate * "[...]" in params matches character class 497c478bd9Sstevel@tonic-gate * "[...a-z...]" in params matches a through z. 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate */ 52965005c8Schin static void addg(unsigned char *, unsigned char *, unsigned char *, 53965005c8Schin unsigned char *); 54965005c8Schin void makearg(struct argnod *); 557c478bd9Sstevel@tonic-gate 56965005c8Schin int 57965005c8Schin expand(unsigned char *as, int rcnt) 587c478bd9Sstevel@tonic-gate { 597c478bd9Sstevel@tonic-gate int count; 607c478bd9Sstevel@tonic-gate DIR *dirf; 617c478bd9Sstevel@tonic-gate BOOL dir = 0; 627c478bd9Sstevel@tonic-gate unsigned char *rescan = 0; 637c478bd9Sstevel@tonic-gate unsigned char *slashsav = 0; 64965005c8Schin unsigned char *s, *cs; 657c478bd9Sstevel@tonic-gate unsigned char *s2 = 0; 667c478bd9Sstevel@tonic-gate struct argnod *schain = gchain; 677c478bd9Sstevel@tonic-gate BOOL slash; 687c478bd9Sstevel@tonic-gate int len; 697c478bd9Sstevel@tonic-gate wchar_t wc; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate if (trapnote & SIGSET) 727c478bd9Sstevel@tonic-gate return (0); 737c478bd9Sstevel@tonic-gate s = cs = as; 747c478bd9Sstevel@tonic-gate /* 757c478bd9Sstevel@tonic-gate * check for meta chars 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate { 78965005c8Schin BOOL open; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate slash = 0; 817c478bd9Sstevel@tonic-gate open = 0; 827c478bd9Sstevel@tonic-gate do 837c478bd9Sstevel@tonic-gate { 847c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)cs, MB_LEN_MAX)) <= 0) { 857c478bd9Sstevel@tonic-gate len = 1; 867c478bd9Sstevel@tonic-gate wc = (unsigned char)*cs; 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate cs += len; 907c478bd9Sstevel@tonic-gate switch (wc) { 917c478bd9Sstevel@tonic-gate case 0: 927c478bd9Sstevel@tonic-gate if (rcnt && slash) 937c478bd9Sstevel@tonic-gate break; 947c478bd9Sstevel@tonic-gate else 957c478bd9Sstevel@tonic-gate return (0); 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate case '/': 987c478bd9Sstevel@tonic-gate slash++; 997c478bd9Sstevel@tonic-gate open = 0; 1007c478bd9Sstevel@tonic-gate continue; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate case '[': 1037c478bd9Sstevel@tonic-gate open++; 1047c478bd9Sstevel@tonic-gate continue; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate case ']': 1077c478bd9Sstevel@tonic-gate if (open == 0) 1087c478bd9Sstevel@tonic-gate continue; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate case '?': 1117c478bd9Sstevel@tonic-gate case '*': 1127c478bd9Sstevel@tonic-gate if (rcnt > slash) 1137c478bd9Sstevel@tonic-gate continue; 1147c478bd9Sstevel@tonic-gate else 1157c478bd9Sstevel@tonic-gate cs--; 1167c478bd9Sstevel@tonic-gate break; 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate case '\\': 1197c478bd9Sstevel@tonic-gate cs++; 1207c478bd9Sstevel@tonic-gate default: 1217c478bd9Sstevel@tonic-gate continue; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate break; 1247c478bd9Sstevel@tonic-gate } while (TRUE); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate 127*db397771Schin for (;;) { 128*db397771Schin if (cs == s) { 1297c478bd9Sstevel@tonic-gate s = (unsigned char *)nullstr; 1307c478bd9Sstevel@tonic-gate break; 131*db397771Schin } else if (*--cs == '/') { 1327c478bd9Sstevel@tonic-gate *cs = 0; 1337c478bd9Sstevel@tonic-gate if (s == cs) 1347c478bd9Sstevel@tonic-gate s = (unsigned char *)"/"; 1357c478bd9Sstevel@tonic-gate else { 1367c478bd9Sstevel@tonic-gate /* 1377c478bd9Sstevel@tonic-gate * push trimmed copy of directory prefix 1387c478bd9Sstevel@tonic-gate * onto stack 1397c478bd9Sstevel@tonic-gate */ 1407c478bd9Sstevel@tonic-gate s2 = cpystak(s); 1417c478bd9Sstevel@tonic-gate trim(s2); 1427c478bd9Sstevel@tonic-gate s = s2; 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate break; 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate if ((dirf = opendir(*s ? (char *)s : (char *)".")) != 0) 1497c478bd9Sstevel@tonic-gate dir++; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* Let s point to original string because it will be trimmed later */ 1527c478bd9Sstevel@tonic-gate if (s2) 1537c478bd9Sstevel@tonic-gate s = as; 1547c478bd9Sstevel@tonic-gate count = 0; 1557c478bd9Sstevel@tonic-gate if (*cs == 0) 1567c478bd9Sstevel@tonic-gate slashsav = cs++; /* remember where first slash in as is */ 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* check for rescan */ 159*db397771Schin if (dir) { 160965005c8Schin unsigned char *rs; 1617c478bd9Sstevel@tonic-gate struct dirent *e; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate rs = cs; 1647c478bd9Sstevel@tonic-gate do /* find next / in as */ 1657c478bd9Sstevel@tonic-gate { 166*db397771Schin if (*rs == '/') { 1677c478bd9Sstevel@tonic-gate rescan = rs; 1687c478bd9Sstevel@tonic-gate *rs = 0; 1697c478bd9Sstevel@tonic-gate gchain = 0; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate } while (*rs++); 1727c478bd9Sstevel@tonic-gate 173*db397771Schin while ((e = readdir(dirf)) && (trapnote & SIGSET) == 0) { 1747c478bd9Sstevel@tonic-gate if (e->d_name[0] == '.' && *cs != '.') 1757c478bd9Sstevel@tonic-gate continue; 1767c478bd9Sstevel@tonic-gate 177*db397771Schin if (gmatch(e->d_name, cs)) { 178965005c8Schin addg(s, (unsigned char *)e->d_name, rescan, 179965005c8Schin slashsav); 1807c478bd9Sstevel@tonic-gate count++; 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate } 1837c478bd9Sstevel@tonic-gate (void) closedir(dirf); 1847c478bd9Sstevel@tonic-gate 185*db397771Schin if (rescan) { 186965005c8Schin struct argnod *rchain; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate rchain = gchain; 1897c478bd9Sstevel@tonic-gate gchain = schain; 190*db397771Schin if (count) { 1917c478bd9Sstevel@tonic-gate count = 0; 192*db397771Schin while (rchain) { 1937c478bd9Sstevel@tonic-gate count += expand(rchain->argval, 1947c478bd9Sstevel@tonic-gate slash + 1); 1957c478bd9Sstevel@tonic-gate rchain = rchain->argnxt; 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate *rescan = '/'; 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate if (slashsav) 2037c478bd9Sstevel@tonic-gate *slashsav = '/'; 2047c478bd9Sstevel@tonic-gate return (count); 2057c478bd9Sstevel@tonic-gate } 2067c478bd9Sstevel@tonic-gate 207965005c8Schin static void 208965005c8Schin addg(unsigned char *as1, unsigned char *as2, unsigned char *as3, 209965005c8Schin unsigned char *as4) 2107c478bd9Sstevel@tonic-gate { 211965005c8Schin unsigned char *s1, *s2; 212965005c8Schin int c; 2137c478bd9Sstevel@tonic-gate int len; 2147c478bd9Sstevel@tonic-gate wchar_t wc; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate s2 = locstak() + BYTESPERWORD; 2177c478bd9Sstevel@tonic-gate s1 = as1; 2187c478bd9Sstevel@tonic-gate if (as4) { 219*db397771Schin while (c = *s1++) { 2207c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2217c478bd9Sstevel@tonic-gate growstak(s2); 2227c478bd9Sstevel@tonic-gate *s2++ = c; 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * Restore first slash before the first metacharacter 2267c478bd9Sstevel@tonic-gate * if as1 is not "/" 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate if (as4 + 1 == s1) { 2297c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2307c478bd9Sstevel@tonic-gate growstak(s2); 2317c478bd9Sstevel@tonic-gate *s2++ = '/'; 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate /* add matched entries, plus extra \\ to escape \\'s */ 2357c478bd9Sstevel@tonic-gate s1 = as2; 236*db397771Schin for (;;) { 2377c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) { 2387c478bd9Sstevel@tonic-gate len = 1; 2397c478bd9Sstevel@tonic-gate wc = (unsigned char)*s1; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2427c478bd9Sstevel@tonic-gate growstak(s2); 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (wc == 0) { 2457c478bd9Sstevel@tonic-gate *s2 = *s1++; 2467c478bd9Sstevel@tonic-gate break; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate if (wc == '\\') { 2507c478bd9Sstevel@tonic-gate *s2++ = '\\'; 2517c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2527c478bd9Sstevel@tonic-gate growstak(s2); 2537c478bd9Sstevel@tonic-gate *s2++ = '\\'; 2547c478bd9Sstevel@tonic-gate s1++; 2557c478bd9Sstevel@tonic-gate continue; 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate if ((s2 + len) >= brkend) 2587c478bd9Sstevel@tonic-gate growstak(s2 + len); 2597c478bd9Sstevel@tonic-gate memcpy(s2, s1, len); 2607c478bd9Sstevel@tonic-gate s2 += len; 2617c478bd9Sstevel@tonic-gate s1 += len; 2627c478bd9Sstevel@tonic-gate } 263*db397771Schin if (s1 = as3) { 2647c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2657c478bd9Sstevel@tonic-gate growstak(s2); 2667c478bd9Sstevel@tonic-gate *s2++ = '/'; 2677c478bd9Sstevel@tonic-gate do 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2707c478bd9Sstevel@tonic-gate growstak(s2); 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate while (*s2++ = *++s1); 2737c478bd9Sstevel@tonic-gate } 274965005c8Schin makearg((struct argnod *)endstak(s2)); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 277965005c8Schin void 278965005c8Schin makearg(struct argnod *args) 2797c478bd9Sstevel@tonic-gate { 2807c478bd9Sstevel@tonic-gate args->argnxt = gchain; 2817c478bd9Sstevel@tonic-gate gchain = args; 2827c478bd9Sstevel@tonic-gate } 283