static void matchdir (char *pattern) { static struct stat stb; struct dirent *dp; DIR *dirp; dirp = opendir(gpath); if (dirp == NULL) { if (globbed) return; goto patherr2; } if (fstat(dirp->dd_fd, &stb) < 0) goto patherr1; if (!isdir(stb)) { errno = ENOTDIR; goto patherr1; } while ((dp = readdir(dirp)) != NULL) { if (dp->d_ino == 0) continue; if (match(dp->d_name, pattern)) { Gcat(gpath, dp->d_name); globcnt++; } } closedir(dirp); return; patherr1: closedir(dirp); patherr2: globerr = "Bad directory components"; }
static void collect (char *as) { if (eq(as, "{") || eq(as, "{}")) { Gcat(as, ""); sort(); } else acollect(as); }
/* * Pack up more characters in this word */ static Char * Dpack(Char *wbuf, Char *wp) { int c; int i = MAXWLEN - (wp - wbuf); for (;;) { c = DgetC(DODOL); if (c == '\\') { c = DgetC(0); if (c == DEOF) { unDredc(c); *wp = 0; Gcat(STRNULL, wbuf); return (NULL); } if (c == '\n') c = ' '; else c |= QUOTE; } if (c == DEOF) { unDredc(c); *wp = 0; Gcat(STRNULL, wbuf); return (NULL); } if (cmap(c, _SP | _NL | _QF | _QB)) { /* sp \t\n'"` */ unDgetC(c); if (cmap(c, QUOTES)) return (wp); *wp++ = 0; Gcat(STRNULL, wbuf); return (NULL); } if (--i <= 0) stderror(ERR_WTOOLONG); *wp++ = c; } }
static void matchdir(char *pattern) { struct stat stb; #ifdef HAVE_DIRENT_H register struct dirent *dp; #else register struct direct *dp; #endif DIR *dirp; dirp = opendir(*gpath == '\0' ? "." : gpath); if (dirp == NULL) { if (globbed) return; goto patherr2; } #ifdef HAVE_DIRFD if (fstat(dirfd(dirp), &stb) < 0) #else /* HAVE_DIRFD */ if (fstat(dirp->dd_fd, &stb) < 0) #endif /* HAVE_DIRFD */ goto patherr1; if (!isdir(stb)) { errno = ENOTDIR; goto patherr1; } while (!globerr && ((dp = readdir(dirp)) != NULL)) { if (dp->d_ino == 0) continue; if (match(dp->d_name, pattern)) { Gcat(gpath, dp->d_name); globcnt++; } } closedir(dirp); return; patherr1: closedir(dirp); patherr2: globerr = "Bad directory components"; }
static void matchdir(const char *pattern) { struct stat stb; register struct dirent *dp; DIR *dirp; #if 0 #ifdef __linux__ if (gpath == NULL || *gpath == '\0') gpath = "./"; #endif #endif dirp = opendir((!gpath || !*gpath) ? "./" : gpath); if (dirp == NULL) { if (globbed) return; goto patherr2; } if (fstat(dirfd(dirp), &stb) < 0) goto patherr1; if (!isdir(stb)) { errno = ENOTDIR; goto patherr1; } while ((dp = readdir(dirp)) != NULL) { if (dp->d_ino == 0) continue; if (match(dp->d_name, pattern)) { Gcat(gpath, dp->d_name); globcnt++; } } closedir(dirp); return; patherr1: closedir(dirp); patherr2: globerr = "Bad directory components"; }
static int amatch(char *s, char *p) { register int scc; int ok, lc; char *sgpathp; struct stat stb; int c, cc; globbed = 1; for (;;) { scc = *s++ & TRIM; switch (c = *p++) { case '{': return (execbrc(p - 1, s - 1)); case '[': ok = 0; lc = 077777; while ((cc = *p++)) { if (cc == ']') { if (ok) break; return (0); } if (cc == '-') { if (lc <= scc && scc <= *p++) ok++; } else if (scc == (lc = cc)) ok++; } if (cc == 0) { globerr = "Missing ]"; return (0); } continue; case '*': if (!*p) return (1); if (*p == '/') { p++; goto slash; } s--; do { if (amatch(s, p)) return (1); } while (*s++); return (0); case 0: return (scc == 0); default: if (c != scc) return (0); continue; case '?': if (scc == 0) return (0); continue; case '/': if (scc) return (0); slash: s = entp; sgpathp = gpathp; while (*s) addpath(*s++); addpath('/'); if (stat(gpath, &stb) == 0 && isdir(stb)) if (*p == 0) { Gcat(gpath, ""); globcnt++; } else expand(p); gpathp = sgpathp; *gpathp = 0; return (0); } } }
/* * Get a word. This routine is analogous to the routine * word() in sh.lex.c for the main lexical input. One difference * here is that we don't get a newline to terminate our expansion. * Rather, DgetC will return a DEOF when we hit the end-of-input. */ static int Dword(void) { int c, c1; Char wbuf[BUFSIZ]; Char *wp = wbuf; int i = MAXWLEN; bool dolflg; bool sofar = 0, done = 0; while (!done) { done = 1; c = DgetC(DODOL); switch (c) { case DEOF: if (sofar == 0) return (0); /* finish this word and catch the code above the next time */ unDredc(c); /* fall into ... */ case '\n': *wp = 0; Gcat(STRNULL, wbuf); return (1); case ' ': case '\t': done = 0; break; case '`': /* We preserve ` quotations which are done yet later */ *wp++ = c, --i; case '\'': case '"': /* * Note that DgetC never returns a QUOTES character from an * expansion, so only true input quotes will get us here or out. */ c1 = c; dolflg = c1 == '"' ? DODOL : 0; for (;;) { c = DgetC(dolflg); if (c == c1) break; if (c == '\n' || c == DEOF) stderror(ERR_UNMATCHED, c1); if ((c & (QUOTE | TRIM)) == ('\n' | QUOTE)) --wp, ++i; if (--i <= 0) stderror(ERR_WTOOLONG); switch (c1) { case '"': /* * Leave any `s alone for later. Other chars are all * quoted, thus `...` can tell it was within "...". */ *wp++ = c == '`' ? '`' : c | QUOTE; break; case '\'': /* Prevent all further interpretation */ *wp++ = c | QUOTE; break; case '`': /* Leave all text alone for later */ *wp++ = c; break; default: break; } } if (c1 == '`') *wp++ = '`' /* i--; eliminated */; sofar = 1; if ((wp = Dpack(wbuf, wp)) == NULL) return (1); else { i = MAXWLEN - (wp - wbuf); done = 0; } break; case '\\': c = DgetC(0); /* No $ subst! */ if (c == '\n' || c == DEOF) { done = 0; break; } c |= QUOTE; break; default: break; } if (done) { unDgetC(c); sofar = 1; if ((wp = Dpack(wbuf, wp)) == NULL) return (1); else { i = MAXWLEN - (wp - wbuf); done = 0; } } } /* Really NOTREACHED */ return (0); }
/* * Get a word. This routine is analogous to the routine * word() in sh.lex.c for the main lexical input. One difference * here is that we don't get a newline to terminate our expansion. * Rather, DgetC will return a DEOF when we hit the end-of-input. */ int Dword(void) { int c, c1; static tchar *wbuf = NULL; static int wbufsiz = BUFSIZ; int wp = 0; bool dolflg; bool sofar = 0; #define DYNAMICBUFFER() \ do { \ if (wp >= wbufsiz) { \ wbufsiz += BUFSIZ; \ wbuf = xrealloc(wbuf, (wbufsiz+1) * sizeof (tchar)); \ } \ } while (0) #ifdef TRACE tprintf("TRACE- Dword()\n"); #endif if (wbuf == NULL) wbuf = xalloc((wbufsiz+1) * sizeof (tchar)); loop: c = DgetC(DODOL); switch (c) { case DEOF: deof: if (sofar == 0) return (0); /* finish this word and catch the code above the next time */ unDredc(c); /* fall into ... */ case '\n': wbuf[wp] = 0; goto ret; case ' ': case '\t': goto loop; case '`': /* We preserve ` quotations which are done yet later */ wbuf[wp++] = c; case '\'': case '"': /* * Note that DgetC never returns a QUOTES character * from an expansion, so only true input quotes will * get us here or out. */ c1 = c; dolflg = c1 == '"' ? DODOL : 0; for (;;) { c = DgetC(dolflg); if (c == c1) break; if (c == '\n' || c == DEOF) error("Unmatched %c", (tchar) c1); if ((c & (QUOTE|TRIM)) == ('\n' | QUOTE)) --wp; DYNAMICBUFFER(); switch (c1) { case '"': /* * Leave any `s alone for later. * Other chars are all quoted, thus `...` * can tell it was within "...". */ wbuf[wp++] = c == '`' ? '`' : c | QUOTE; break; case '\'': /* Prevent all further interpretation */ wbuf[wp++] = c | QUOTE; break; case '`': /* Leave all text alone for later */ wbuf[wp++] = c; break; } } if (c1 == '`') { DYNAMICBUFFER(); wbuf[wp++] = '`'; } goto pack; /* continue the word */ case '\\': c = DgetC(0); /* No $ subst! */ if (c == '\n' || c == DEOF) goto loop; c |= QUOTE; break; #ifdef MBCHAR /* Could be a space char from aux. codeset. */ default: if (isauxsp(c)) goto loop; #endif /* MBCHAR */ } unDgetC(c); pack: sofar = 1; /* pack up more characters in this word */ for (;;) { c = DgetC(DODOL); if (c == '\\') { c = DgetC(0); if (c == DEOF) goto deof; if (c == '\n') c = ' '; else c |= QUOTE; } if (c == DEOF) goto deof; if (cmap(c, _SP|_NL|_Q|_Q1) || isauxsp(c)) { /* sp \t\n'"` or aux. sp */ unDgetC(c); if (cmap(c, QUOTES)) goto loop; DYNAMICBUFFER(); wbuf[wp++] = 0; goto ret; } DYNAMICBUFFER(); wbuf[wp++] = c; } ret: Gcat(S_ /* "" */, wbuf); return (1); }