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 */ 52*965005c8Schin static void addg(unsigned char *, unsigned char *, unsigned char *, 53*965005c8Schin unsigned char *); 54*965005c8Schin void makearg(struct argnod *); 557c478bd9Sstevel@tonic-gate 56*965005c8Schin int 57*965005c8Schin 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; 64*965005c8Schin 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 { 78*965005c8Schin 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 1277c478bd9Sstevel@tonic-gate for (;;) 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate if (cs == s) 1307c478bd9Sstevel@tonic-gate { 1317c478bd9Sstevel@tonic-gate s = (unsigned char *)nullstr; 1327c478bd9Sstevel@tonic-gate break; 1337c478bd9Sstevel@tonic-gate } else if (*--cs == '/') 1347c478bd9Sstevel@tonic-gate { 1357c478bd9Sstevel@tonic-gate *cs = 0; 1367c478bd9Sstevel@tonic-gate if (s == cs) 1377c478bd9Sstevel@tonic-gate s = (unsigned char *)"/"; 1387c478bd9Sstevel@tonic-gate else { 1397c478bd9Sstevel@tonic-gate /* 1407c478bd9Sstevel@tonic-gate * push trimmed copy of directory prefix 1417c478bd9Sstevel@tonic-gate * onto stack 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate s2 = cpystak(s); 1447c478bd9Sstevel@tonic-gate trim(s2); 1457c478bd9Sstevel@tonic-gate s = s2; 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate break; 1487c478bd9Sstevel@tonic-gate } 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate if ((dirf = opendir(*s ? (char *)s : (char *)".")) != 0) 1527c478bd9Sstevel@tonic-gate dir++; 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* Let s point to original string because it will be trimmed later */ 1557c478bd9Sstevel@tonic-gate if (s2) 1567c478bd9Sstevel@tonic-gate s = as; 1577c478bd9Sstevel@tonic-gate count = 0; 1587c478bd9Sstevel@tonic-gate if (*cs == 0) 1597c478bd9Sstevel@tonic-gate slashsav = cs++; /* remember where first slash in as is */ 1607c478bd9Sstevel@tonic-gate 1617c478bd9Sstevel@tonic-gate /* check for rescan */ 1627c478bd9Sstevel@tonic-gate if (dir) 1637c478bd9Sstevel@tonic-gate { 164*965005c8Schin unsigned char *rs; 1657c478bd9Sstevel@tonic-gate struct dirent *e; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate rs = cs; 1687c478bd9Sstevel@tonic-gate do /* find next / in as */ 1697c478bd9Sstevel@tonic-gate { 1707c478bd9Sstevel@tonic-gate if (*rs == '/') 1717c478bd9Sstevel@tonic-gate { 1727c478bd9Sstevel@tonic-gate rescan = rs; 1737c478bd9Sstevel@tonic-gate *rs = 0; 1747c478bd9Sstevel@tonic-gate gchain = 0; 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate } while (*rs++); 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate while ((e = readdir(dirf)) && (trapnote & SIGSET) == 0) 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate if (e->d_name[0] == '.' && *cs != '.') 1817c478bd9Sstevel@tonic-gate continue; 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate if (gmatch(e->d_name, cs)) 1847c478bd9Sstevel@tonic-gate { 185*965005c8Schin addg(s, (unsigned char *)e->d_name, rescan, 186*965005c8Schin slashsav); 1877c478bd9Sstevel@tonic-gate count++; 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate (void) closedir(dirf); 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate if (rescan) 1937c478bd9Sstevel@tonic-gate { 194*965005c8Schin struct argnod *rchain; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate rchain = gchain; 1977c478bd9Sstevel@tonic-gate gchain = schain; 1987c478bd9Sstevel@tonic-gate if (count) 1997c478bd9Sstevel@tonic-gate { 2007c478bd9Sstevel@tonic-gate count = 0; 2017c478bd9Sstevel@tonic-gate while (rchain) 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate count += expand(rchain->argval, 2047c478bd9Sstevel@tonic-gate slash + 1); 2057c478bd9Sstevel@tonic-gate rchain = rchain->argnxt; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate *rescan = '/'; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate if (slashsav) 2137c478bd9Sstevel@tonic-gate *slashsav = '/'; 2147c478bd9Sstevel@tonic-gate return (count); 2157c478bd9Sstevel@tonic-gate } 2167c478bd9Sstevel@tonic-gate 217*965005c8Schin static void 218*965005c8Schin addg(unsigned char *as1, unsigned char *as2, unsigned char *as3, 219*965005c8Schin unsigned char *as4) 2207c478bd9Sstevel@tonic-gate { 221*965005c8Schin unsigned char *s1, *s2; 222*965005c8Schin int c; 2237c478bd9Sstevel@tonic-gate int len; 2247c478bd9Sstevel@tonic-gate wchar_t wc; 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate s2 = locstak() + BYTESPERWORD; 2277c478bd9Sstevel@tonic-gate s1 = as1; 2287c478bd9Sstevel@tonic-gate if (as4) { 2297c478bd9Sstevel@tonic-gate while (c = *s1++) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2327c478bd9Sstevel@tonic-gate growstak(s2); 2337c478bd9Sstevel@tonic-gate *s2++ = c; 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate /* 2367c478bd9Sstevel@tonic-gate * Restore first slash before the first metacharacter 2377c478bd9Sstevel@tonic-gate * if as1 is not "/" 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate if (as4 + 1 == s1) { 2407c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2417c478bd9Sstevel@tonic-gate growstak(s2); 2427c478bd9Sstevel@tonic-gate *s2++ = '/'; 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate } 2457c478bd9Sstevel@tonic-gate /* add matched entries, plus extra \\ to escape \\'s */ 2467c478bd9Sstevel@tonic-gate s1 = as2; 2477c478bd9Sstevel@tonic-gate for (;;) 2487c478bd9Sstevel@tonic-gate { 2497c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) { 2507c478bd9Sstevel@tonic-gate len = 1; 2517c478bd9Sstevel@tonic-gate wc = (unsigned char)*s1; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2547c478bd9Sstevel@tonic-gate growstak(s2); 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (wc == 0) { 2577c478bd9Sstevel@tonic-gate *s2 = *s1++; 2587c478bd9Sstevel@tonic-gate break; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate if (wc == '\\') { 2627c478bd9Sstevel@tonic-gate *s2++ = '\\'; 2637c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2647c478bd9Sstevel@tonic-gate growstak(s2); 2657c478bd9Sstevel@tonic-gate *s2++ = '\\'; 2667c478bd9Sstevel@tonic-gate s1++; 2677c478bd9Sstevel@tonic-gate continue; 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate if ((s2 + len) >= brkend) 2707c478bd9Sstevel@tonic-gate growstak(s2 + len); 2717c478bd9Sstevel@tonic-gate memcpy(s2, s1, len); 2727c478bd9Sstevel@tonic-gate s2 += len; 2737c478bd9Sstevel@tonic-gate s1 += len; 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate if (s1 = as3) 2767c478bd9Sstevel@tonic-gate { 2777c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2787c478bd9Sstevel@tonic-gate growstak(s2); 2797c478bd9Sstevel@tonic-gate *s2++ = '/'; 2807c478bd9Sstevel@tonic-gate do 2817c478bd9Sstevel@tonic-gate { 2827c478bd9Sstevel@tonic-gate if (s2 >= brkend) 2837c478bd9Sstevel@tonic-gate growstak(s2); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate while (*s2++ = *++s1); 2867c478bd9Sstevel@tonic-gate } 287*965005c8Schin makearg((struct argnod *)endstak(s2)); 2887c478bd9Sstevel@tonic-gate } 2897c478bd9Sstevel@tonic-gate 290*965005c8Schin void 291*965005c8Schin makearg(struct argnod *args) 2927c478bd9Sstevel@tonic-gate { 2937c478bd9Sstevel@tonic-gate args->argnxt = gchain; 2947c478bd9Sstevel@tonic-gate gchain = args; 2957c478bd9Sstevel@tonic-gate } 296