|
|
1.1 root 1: #include <sys/types.h>
2: #include <sys/stat.h>
3: #ifndef SEQUENT
4: #include <ndir.h>
5: #else
6: #include <dir.h>
7: #endif
8: #include <string.h>
9: #include "mk.h"
10:
11: #define MAXPATH BIGBLOCK
12:
13: static char result[MAXPATH];
14: static doit();
15:
16: /*
17: * globals (file name generation)
18: *
19: * "*" in params matches r.e ".*"
20: * "?" in params matches r.e. "."
21: * "[...]" in params matches character class
22: * "[...a-z...]" in params matches a through z.
23: *
24: */
25:
26: static
27: gglob(arg, dest, fn)
28: char *arg, *dest;
29: void (*fn)();
30: {
31: int meta;
32: char *slash, *s;
33: DIR *dirf;
34:
35: /*
36: check for meta chars
37: */
38: for(s = arg, slash = 0, meta = 0; *s && !meta; s++)
39: switch(*s)
40: {
41: case '/':
42: slash = s+1;
43: break;
44: case '\\':
45: if(s[1])
46: s++;
47: break;
48: case '[':
49: case '*':
50: case '?':
51: meta = 1;
52: break;
53: }
54: if(meta == 0){
55: strcpy(dest, arg);
56: (*fn)(result);
57: return;
58: }
59: if(slash){
60: memcpy(dest, arg, slash-arg);
61: dest += slash-arg;
62: *dest = 0;
63: } else
64: slash = arg;
65: s = strchr(slash, '/');
66: if(dirf = opendir(result[0] ? result : ".")){
67: struct direct *e;
68: int found;
69:
70: found = 0;
71: if(s) *s = 0;
72: /* slash is null terminated (s) pattern */
73: while(e = readdir(dirf)){
74: if(e->d_name[0] == '.' && *slash != '.'){
75: if (e->d_name[1] == 0)
76: continue;
77: if (e->d_name[1] == '.' && e->d_name[2] == 0)
78: continue;
79: }
80: if(gmatch(e->d_name, slash)){
81: found = 1;
82: strcpy(dest, e->d_name);
83: doit(s, dest, fn);
84: }
85: }
86: if(!found){
87: strcpy(dest, slash);
88: doit(s, dest, fn);
89: }
90: closedir(dirf);
91: } else {
92: if(s)
93: *s = 0;
94: strcpy(dest, slash);
95: dest = strchr(dest, 0);
96: if(s){
97: *dest++ = '/';
98: *dest = 0;
99: gglob(s+1, dest, fn);
100: } else
101: (*fn)(result);
102: }
103: if(s) *s = '/';
104: }
105:
106: static
107: doit(s, dest, fn)
108: char *s, *dest;
109: void (*fn)();
110: {
111: if(s){
112: register char *ss;
113:
114: ss = strchr(dest, 0);
115: *ss++ = '/';
116: *ss = 0;
117: gglob(s+1, ss, fn);
118: } else
119: (*fn)(result);
120: }
121:
122: gmatch(s, p)
123: register char *s, *p;
124: {
125: register int scc;
126: unsigned char c;
127:
128: scc = *s++&(0xFF&~EBIT);
129: switch (c = *p++)
130: {
131: case '[':
132: {
133: int ok;
134: int lc;
135: int notflag = 0;
136:
137: ok = 0;
138: lc = 077777;
139: if (*p == '^'){
140: notflag = 1;
141: p++;
142: }
143: while (c = *p++){
144: if (c == ']')
145: return(ok ? gmatch(s, p) : 0);
146: else if (c == '-'){
147: if (notflag){
148: if (scc < lc || scc > *(p++))
149: ok++;
150: else
151: return(0);
152: } else {
153: if (lc <= scc && scc <= (*p++))
154: ok++;
155: }
156: } else {
157: lc = c&~EBIT;
158: if (notflag){
159: if (scc && scc != lc)
160: ok++;
161: else
162: return(0);
163: } else {
164: if (scc == lc)
165: ok++;
166: }
167: }
168: }
169: return(0);
170: }
171:
172: default:
173: if ((c&~EBIT) != scc)
174: return(0);
175:
176: case '?':
177: return(scc ? gmatch(s, p) : 0);
178:
179: case '*':
180: while (*p == '*')
181: p++;
182:
183: if (*p == 0)
184: return(1);
185: --s;
186: while (*s){
187: if (gmatch(s++, p))
188: return(1);
189: }
190: return(0);
191:
192: case 0:
193: return(scc == 0);
194: }
195: }
196:
197: glob(s, fn)
198: char *s;
199: void (*fn)();
200: {
201: result[0] = 0;
202: if(strlen(s) >= MAXPATH){
203: SYNERR(inline-1);
204: Fprint(2, "too much input for glob expansion; max=%d, given %d\n", MAXPATH, strlen(s));
205: Exit();
206: }
207: gglob(s, result, fn);
208: }
209:
210: #ifdef MAIN
211:
212: void
213: pr(s)
214: char *s;
215: {
216: Fwrite(1, s, strlen(s));
217: Fputc(1, '\n');
218: }
219:
220: main(argc, argv)
221: char **argv;
222: {
223: for(argv++; *argv; argv++){
224: Fprint(1, "<%s>:\n", *argv);
225: glob(*argv, pr);
226: Fprint(1, "*******\n");
227: }
228: exit(0);
229: }
230: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.