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 /*
317c478bd9Sstevel@tonic-gate * UNIX shell
327c478bd9Sstevel@tonic-gate *
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #include "defs.h"
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <dirent.h>
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * globals (file name generation)
447c478bd9Sstevel@tonic-gate *
457c478bd9Sstevel@tonic-gate * "*" in params matches r.e ".*"
467c478bd9Sstevel@tonic-gate * "?" in params matches r.e. "."
477c478bd9Sstevel@tonic-gate * "[...]" in params matches character class
487c478bd9Sstevel@tonic-gate * "[...a-z...]" in params matches a through z.
497c478bd9Sstevel@tonic-gate *
507c478bd9Sstevel@tonic-gate */
51965005c8Schin static void addg(unsigned char *, unsigned char *, unsigned char *,
52965005c8Schin unsigned char *);
53965005c8Schin void makearg(struct argnod *);
547c478bd9Sstevel@tonic-gate
55965005c8Schin int
expand(unsigned char * as,int rcnt)56965005c8Schin expand(unsigned char *as, int rcnt)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate int count;
597c478bd9Sstevel@tonic-gate DIR *dirf;
607c478bd9Sstevel@tonic-gate BOOL dir = 0;
617c478bd9Sstevel@tonic-gate unsigned char *rescan = 0;
627c478bd9Sstevel@tonic-gate unsigned char *slashsav = 0;
63965005c8Schin unsigned char *s, *cs;
647c478bd9Sstevel@tonic-gate unsigned char *s2 = 0;
657c478bd9Sstevel@tonic-gate struct argnod *schain = gchain;
667c478bd9Sstevel@tonic-gate BOOL slash;
677c478bd9Sstevel@tonic-gate int len;
687c478bd9Sstevel@tonic-gate wchar_t wc;
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate if (trapnote & SIGSET)
717c478bd9Sstevel@tonic-gate return (0);
727c478bd9Sstevel@tonic-gate s = cs = as;
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * check for meta chars
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate {
77965005c8Schin BOOL open;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate slash = 0;
807c478bd9Sstevel@tonic-gate open = 0;
817c478bd9Sstevel@tonic-gate do
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)cs, MB_LEN_MAX)) <= 0) {
847c478bd9Sstevel@tonic-gate len = 1;
857c478bd9Sstevel@tonic-gate wc = (unsigned char)*cs;
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate cs += len;
897c478bd9Sstevel@tonic-gate switch (wc) {
907c478bd9Sstevel@tonic-gate case 0:
917c478bd9Sstevel@tonic-gate if (rcnt && slash)
927c478bd9Sstevel@tonic-gate break;
937c478bd9Sstevel@tonic-gate else
947c478bd9Sstevel@tonic-gate return (0);
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate case '/':
977c478bd9Sstevel@tonic-gate slash++;
987c478bd9Sstevel@tonic-gate open = 0;
997c478bd9Sstevel@tonic-gate continue;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate case '[':
1027c478bd9Sstevel@tonic-gate open++;
1037c478bd9Sstevel@tonic-gate continue;
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate case ']':
1067c478bd9Sstevel@tonic-gate if (open == 0)
1077c478bd9Sstevel@tonic-gate continue;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate case '?':
1107c478bd9Sstevel@tonic-gate case '*':
1117c478bd9Sstevel@tonic-gate if (rcnt > slash)
1127c478bd9Sstevel@tonic-gate continue;
1137c478bd9Sstevel@tonic-gate else
1147c478bd9Sstevel@tonic-gate cs--;
1157c478bd9Sstevel@tonic-gate break;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate case '\\':
1187c478bd9Sstevel@tonic-gate cs++;
1197c478bd9Sstevel@tonic-gate default:
1207c478bd9Sstevel@tonic-gate continue;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate } while (TRUE);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate
126*db397771Schin for (;;) {
127*db397771Schin if (cs == s) {
1287c478bd9Sstevel@tonic-gate s = (unsigned char *)nullstr;
1297c478bd9Sstevel@tonic-gate break;
130*db397771Schin } else if (*--cs == '/') {
1317c478bd9Sstevel@tonic-gate *cs = 0;
1327c478bd9Sstevel@tonic-gate if (s == cs)
1337c478bd9Sstevel@tonic-gate s = (unsigned char *)"/";
1347c478bd9Sstevel@tonic-gate else {
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate * push trimmed copy of directory prefix
1377c478bd9Sstevel@tonic-gate * onto stack
1387c478bd9Sstevel@tonic-gate */
1397c478bd9Sstevel@tonic-gate s2 = cpystak(s);
1407c478bd9Sstevel@tonic-gate trim(s2);
1417c478bd9Sstevel@tonic-gate s = s2;
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate break;
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate if ((dirf = opendir(*s ? (char *)s : (char *)".")) != 0)
1487c478bd9Sstevel@tonic-gate dir++;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate /* Let s point to original string because it will be trimmed later */
1517c478bd9Sstevel@tonic-gate if (s2)
1527c478bd9Sstevel@tonic-gate s = as;
1537c478bd9Sstevel@tonic-gate count = 0;
1547c478bd9Sstevel@tonic-gate if (*cs == 0)
1557c478bd9Sstevel@tonic-gate slashsav = cs++; /* remember where first slash in as is */
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate /* check for rescan */
158*db397771Schin if (dir) {
159965005c8Schin unsigned char *rs;
1607c478bd9Sstevel@tonic-gate struct dirent *e;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate rs = cs;
1637c478bd9Sstevel@tonic-gate do /* find next / in as */
1647c478bd9Sstevel@tonic-gate {
165*db397771Schin if (*rs == '/') {
1667c478bd9Sstevel@tonic-gate rescan = rs;
1677c478bd9Sstevel@tonic-gate *rs = 0;
1687c478bd9Sstevel@tonic-gate gchain = 0;
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate } while (*rs++);
1717c478bd9Sstevel@tonic-gate
172*db397771Schin while ((e = readdir(dirf)) && (trapnote & SIGSET) == 0) {
1737c478bd9Sstevel@tonic-gate if (e->d_name[0] == '.' && *cs != '.')
1747c478bd9Sstevel@tonic-gate continue;
1757c478bd9Sstevel@tonic-gate
176*db397771Schin if (gmatch(e->d_name, cs)) {
177965005c8Schin addg(s, (unsigned char *)e->d_name, rescan,
178965005c8Schin slashsav);
1797c478bd9Sstevel@tonic-gate count++;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate (void) closedir(dirf);
1837c478bd9Sstevel@tonic-gate
184*db397771Schin if (rescan) {
185965005c8Schin struct argnod *rchain;
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate rchain = gchain;
1887c478bd9Sstevel@tonic-gate gchain = schain;
189*db397771Schin if (count) {
1907c478bd9Sstevel@tonic-gate count = 0;
191*db397771Schin while (rchain) {
1927c478bd9Sstevel@tonic-gate count += expand(rchain->argval,
1937c478bd9Sstevel@tonic-gate slash + 1);
1947c478bd9Sstevel@tonic-gate rchain = rchain->argnxt;
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate *rescan = '/';
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if (slashsav)
2027c478bd9Sstevel@tonic-gate *slashsav = '/';
2037c478bd9Sstevel@tonic-gate return (count);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
206965005c8Schin static void
addg(unsigned char * as1,unsigned char * as2,unsigned char * as3,unsigned char * as4)207965005c8Schin addg(unsigned char *as1, unsigned char *as2, unsigned char *as3,
208965005c8Schin unsigned char *as4)
2097c478bd9Sstevel@tonic-gate {
210965005c8Schin unsigned char *s1, *s2;
211965005c8Schin int c;
2127c478bd9Sstevel@tonic-gate int len;
2137c478bd9Sstevel@tonic-gate wchar_t wc;
2147c478bd9Sstevel@tonic-gate
2157c478bd9Sstevel@tonic-gate s2 = locstak() + BYTESPERWORD;
2167c478bd9Sstevel@tonic-gate s1 = as1;
2177c478bd9Sstevel@tonic-gate if (as4) {
218*db397771Schin while (c = *s1++) {
2197c478bd9Sstevel@tonic-gate if (s2 >= brkend)
2207c478bd9Sstevel@tonic-gate growstak(s2);
2217c478bd9Sstevel@tonic-gate *s2++ = c;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate * Restore first slash before the first metacharacter
2257c478bd9Sstevel@tonic-gate * if as1 is not "/"
2267c478bd9Sstevel@tonic-gate */
2277c478bd9Sstevel@tonic-gate if (as4 + 1 == s1) {
2287c478bd9Sstevel@tonic-gate if (s2 >= brkend)
2297c478bd9Sstevel@tonic-gate growstak(s2);
2307c478bd9Sstevel@tonic-gate *s2++ = '/';
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate /* add matched entries, plus extra \\ to escape \\'s */
2347c478bd9Sstevel@tonic-gate s1 = as2;
235*db397771Schin for (;;) {
2367c478bd9Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) {
2377c478bd9Sstevel@tonic-gate len = 1;
2387c478bd9Sstevel@tonic-gate wc = (unsigned char)*s1;
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate if (s2 >= brkend)
2417c478bd9Sstevel@tonic-gate growstak(s2);
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate if (wc == 0) {
2447c478bd9Sstevel@tonic-gate *s2 = *s1++;
2457c478bd9Sstevel@tonic-gate break;
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate if (wc == '\\') {
2497c478bd9Sstevel@tonic-gate *s2++ = '\\';
2507c478bd9Sstevel@tonic-gate if (s2 >= brkend)
2517c478bd9Sstevel@tonic-gate growstak(s2);
2527c478bd9Sstevel@tonic-gate *s2++ = '\\';
2537c478bd9Sstevel@tonic-gate s1++;
2547c478bd9Sstevel@tonic-gate continue;
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate if ((s2 + len) >= brkend)
2577c478bd9Sstevel@tonic-gate growstak(s2 + len);
2587c478bd9Sstevel@tonic-gate memcpy(s2, s1, len);
2597c478bd9Sstevel@tonic-gate s2 += len;
2607c478bd9Sstevel@tonic-gate s1 += len;
2617c478bd9Sstevel@tonic-gate }
262*db397771Schin if (s1 = as3) {
2637c478bd9Sstevel@tonic-gate if (s2 >= brkend)
2647c478bd9Sstevel@tonic-gate growstak(s2);
2657c478bd9Sstevel@tonic-gate *s2++ = '/';
2667c478bd9Sstevel@tonic-gate do
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate if (s2 >= brkend)
2697c478bd9Sstevel@tonic-gate growstak(s2);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate while (*s2++ = *++s1);
2727c478bd9Sstevel@tonic-gate }
273965005c8Schin makearg((struct argnod *)endstak(s2));
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
276965005c8Schin void
makearg(struct argnod * args)277965005c8Schin makearg(struct argnod *args)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate args->argnxt = gchain;
2807c478bd9Sstevel@tonic-gate gchain = args;
2817c478bd9Sstevel@tonic-gate }
282