6 * delete all the GLOB marks from s, in place
23 globcmp(void *s, void *t)
25 return strcmp(*(char**)s, *(char**)t);
29 globsort(word *left, word *right)
34 for(a = left;a!=right;a = a->next) n++;
35 list = (char **)emalloc(n*sizeof(char *));
36 for(a = left,n = 0;a!=right;a = a->next,n++) list[n] = a->word;
37 qsort((void *)list, n, sizeof(void *), globcmp);
38 for(a = left,n = 0;a!=right;a = a->next,n++) a->word = list[n];
43 * Does the string s match the pattern p
44 * . and .. are only matched by patterns starting with .
45 * * matches any sequence of characters
46 * ? matches any single character
47 * [...] matches the enclosed list of characters
51 matchfn(char *s, char *p)
53 if(s[0]=='.' && (s[1]=='\0' || s[1]=='.' && s[2]=='\0') && p[0]!='.')
55 return match(s, p, '/');
59 globdir(word *list, char *p, char *name, char *namep)
63 /* scan the pattern looking for a component with a metacharacter in it */
65 return newword(name, list);
77 /* If we ran out of pattern, append the name if accessible */
80 if(access(name, 0)==0)
81 list = newword(name, list);
84 /* read the directory and recur for any entry that matches */
86 if((f = Opendir(name[0]?name:".")) >= 0){
87 while(*newp!='/' && *newp!='\0') newp++;
88 while(Readdir(f, namep, *newp=='/')){
89 if(matchfn(namep, p)){
90 for(t = namep;*t;t++);
91 list = globdir(list, newp, name, t);
100 * Subsitute a word with its glob in place.
111 name = emalloc(w->glob);
112 memset(name, 0, w->glob);
114 left = globdir(right, w->word, name, name);
121 globsort(left, right);
140 * Return a pointer to the next utf code in the string,
141 * not jumping past nuls in broken utf codes!
149 return p + chartorune(&dummy, p);
153 * Convert the utf code at *p to a unicode value
166 * Do p and q point at equal utf codes
170 equtf(char *p, char *q)
174 return unicode(p) == unicode(q);
178 match(char *s, char *p, int stop)
180 int compl, hit, lo, hi, t, c;
182 for(; *p!=stop && *p!='\0'; s = nextutf(s), p = nextutf(p)){
184 if(!equtf(p, s)) return 0;
193 if(match(s, nextutf(p), stop)) return 1;
214 return 0; /* syntax error */
222 return 0; /* syntax error */
225 if(hi<lo){ t = lo; lo = hi; hi = t; }