8 * I wrote this glob so that there would be no limit
9 * on element or path size. The one in rc is probably
10 * better, certainly faster. - presotto
18 g = mallocz(sizeof(*g), 1);
20 sysfatal("globnew: %r");
36 for(; g != nil; g = next){
47 gl = mallocz(sizeof *gl, 1);
49 sysfatal("globlistnew: %r");
50 gl->first = globnew();
51 gl->first->glob = s_copy(x);
52 gl->l = &gl->first->next;
57 globlistfree(Globlist *gl)
66 globadd(Globlist *gl, char *dir, char *file)
71 g->glob = s_copy(dir);
72 if(strcmp(dir, "/") != 0 && *dir != 0)
73 s_append(g->glob, "/");
74 s_append(g->glob, file);
80 globdir(Globlist *gl, char *dir, Reprog *re)
86 fd = open(".", OREAD);
88 fd = open(dir, OREAD);
91 n = dirreadall(fd, &d);
95 for(i = 0; i < n; i++)
96 if(regexec(re, d[i].name, nil, 0))
97 globadd(gl, dir, d[i].name);
102 globdot(Globlist *gl, char *dir)
107 globadd(gl, "", ".");
113 if(d->qid.type & QTDIR)
114 globadd(gl, dir, ".");
119 globnext(Globlist *gl, char *pattern)
134 /* pick off next pattern and turn into a reg exp */
137 for(; c = *pattern; pattern++){
167 if(strcmp(s_to_c(np), "^\\.$") == 0){
168 /* anything that's a directory works */
169 for(g = inlist; g != nil; g = g->next)
170 globdot(gl, s_to_c(g->glob));
172 re = regcomp(s_to_c(np));
174 /* run input list as directories */
175 for(g = inlist; g != nil; g = g->next)
176 globdir(gl, s_to_c(g->glob), re);
183 globnext(gl, pattern);
187 globiter(Globlist *gl)
198 s = strdup(s_to_c(g->glob));
200 sysfatal("globiter: %r");
210 if(pattern == nil || *pattern == 0)
214 gl = globlistnew("/");
216 gl = globlistnew("");
217 globnext(gl, pattern);