--- glob.c-ill Wed Oct 31 09:06:10 2012 +++ glob.c-sty Wed Oct 31 10:34:02 2012 @@ -122,7 +122,7 @@ #define M_MASK 0xffff #define M_ASCII 0x00ff -typedef u_short Char; +typedef ushort_t Char; #else @@ -152,7 +152,7 @@ #define GLOB_LIMIT_READDIR 16384 /* Limit of recursion during matching attempts. */ -#define GLOB_LIMIT_RECUR 64 +#define GLOB_LIMIT_RECUR 64 struct glob_lim { size_t glim_malloc; @@ -167,7 +167,7 @@ static int compare(const void *, const void *); static int compare_gps(const void *, const void *); -static int g_Ctoc(const Char *, char *, u_int); +static int g_Ctoc(const Char *, char *, uint_t); static int g_lstat(Char *, struct stat *, glob_t *); static DIR *g_opendir(Char *, glob_t *); static Char *g_strchr(const Char *, int); @@ -174,23 +174,23 @@ static int g_strncmp(const Char *, const char *, size_t); static int g_stat(Char *, struct stat *, glob_t *); static int glob0(const Char *, glob_t *, struct glob_lim *, - int (*)(const char *, int)); + int (*)(const char *, int)); static int glob1(Char *, Char *, glob_t *, struct glob_lim *, - int (*)(const char *, int)); + int (*)(const char *, int)); static int glob2(Char *, Char *, Char *, Char *, Char *, Char *, - glob_t *, struct glob_lim *, - int (*)(const char *, int)); + glob_t *, struct glob_lim *, + int (*)(const char *, int)); static int glob3(Char *, Char *, Char *, Char *, Char *, - Char *, Char *, glob_t *, struct glob_lim *, - int (*)(const char *, int)); + Char *, Char *, glob_t *, struct glob_lim *, + int (*)(const char *, int)); static int globextend(const Char *, glob_t *, struct glob_lim *, struct stat *); -static const Char * - globtilde(const Char *, Char *, size_t, glob_t *); +static +const Char *globtilde(const Char *, Char *, size_t, glob_t *); static int globexp1(const Char *, glob_t *, struct glob_lim *, - int (*)(const char *, int)); + int (*)(const char *, int)); static int globexp2(const Char *, const Char *, glob_t *, - struct glob_lim *, int (*)(const char *, int)); + struct glob_lim *, int (*)(const char *, int)); static int match(Char *, Char *, Char *, int); #ifdef DEBUG static void qprintf(const char *, Char *); @@ -200,20 +200,20 @@ glob(const char *pattern, int flags, int (*errfunc)(const char *, int), glob_t *pglob) { - const u_char *patnext; + const uchar_t *patnext; int c; Char *bufnext, *bufend, patbuf[MAXPATHLEN]; struct glob_lim limit = { 0, 0, 0 }; if (strnlen(pattern, PATH_MAX) == PATH_MAX) - return(GLOB_NOMATCH); + return (GLOB_NOMATCH); - patnext = (u_char *) pattern; + patnext = (uchar_t *)pattern; if (!(flags & GLOB_APPEND)) { pglob->gl_pathc = 0; pglob->gl_pathv = NULL; if ((flags & GLOB_KEEPSTAT) != 0) - pglob->gl_statv = NULL; + pglob->gl_statv = NULL; if (!(flags & GLOB_DOOFFS)) pglob->gl_offs = 0; } @@ -223,7 +223,7 @@ if (pglob->gl_offs < 0 || pglob->gl_pathc < 0 || pglob->gl_offs >= INT_MAX || pglob->gl_pathc >= INT_MAX || pglob->gl_pathc >= INT_MAX - pglob->gl_offs - 1) - return GLOB_NOSPACE; + return (GLOB_NOSPACE); bufnext = patbuf; bufend = bufnext + MAXPATHLEN - 1; @@ -245,9 +245,9 @@ *bufnext = EOS; if (flags & GLOB_BRACE) - return globexp1(patbuf, pglob, &limit, errfunc); + return (globexp1(patbuf, pglob, &limit, errfunc)); else - return glob0(patbuf, pglob, &limit, errfunc); + return (glob0(patbuf, pglob, &limit, errfunc)); } /* @@ -257,18 +257,18 @@ */ static int globexp1(const Char *pattern, glob_t *pglob, struct glob_lim *limitp, - int (*errfunc)(const char *, int)) + int (*errfunc)(const char *, int)) { const Char* ptr = pattern; /* Protect a single {}, for find(1), like csh */ if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) - return glob0(pattern, pglob, limitp, errfunc); + return (glob0(pattern, pglob, limitp, errfunc)); if ((ptr = (const Char *) g_strchr(ptr, LBRACE)) != NULL) - return globexp2(ptr, pattern, pglob, limitp, errfunc); + return (globexp2(ptr, pattern, pglob, limitp, errfunc)); - return glob0(pattern, pglob, limitp, errfunc); + return (glob0(pattern, pglob, limitp, errfunc)); } @@ -279,9 +279,9 @@ */ static int globexp2(const Char *ptr, const Char *pattern, glob_t *pglob, - struct glob_lim *limitp, int (*errfunc)(const char *, int)) + struct glob_lim *limitp, int (*errfunc)(const char *, int)) { - int i, rv; + int i, rv; Char *lm, *ls; const Char *pe, *pm, *pl; Char patbuf[MAXPATHLEN]; @@ -315,7 +315,7 @@ /* Non matching braces; just glob the pattern */ if (i != 0 || *pe == EOS) - return glob0(patbuf, pglob, limitp, errfunc); + return (glob0(patbuf, pglob, limitp, errfunc)); for (i = 0, pl = pm = ptr; pm <= pe; pm++) { switch (*pm) { @@ -363,7 +363,7 @@ #endif rv = globexp1(patbuf, pglob, limitp, errfunc); if (rv && rv != GLOB_NOMATCH) - return rv; + return (rv); /* move after the comma, to the next string */ pl = pm + 1; @@ -374,7 +374,7 @@ break; } } - return 0; + return (0); } @@ -391,11 +391,11 @@ Char *b, *eb; if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) - return pattern; + return (pattern); /* Copy up to the end of the string or / */ eb = &patbuf[patbuf_len - 1]; - for (p = pattern + 1, h = (char *) patbuf; + for (p = pattern + 1, h = (char *)patbuf; h < (char *)eb && *p && *p != SLASH; *h++ = *p++) ; @@ -403,10 +403,10 @@ #if 0 if (h == (char *)eb) - return what; + return (what); #endif - if (((char *) patbuf)[0] == EOS) { + if (((char *)patbuf)[0] == EOS) { /* * handle a plain ~ or ~/ by expanding $HOME * first and then trying the password file @@ -413,7 +413,7 @@ */ if (issetugid() != 0 || (h = getenv("HOME")) == NULL) { if ((pwd = getpwuid(getuid())) == NULL) - return pattern; + return (pattern); else h = pwd->pw_dir; } @@ -421,8 +421,8 @@ /* * Expand a ~user */ - if ((pwd = getpwnam((char*) patbuf)) == NULL) - return pattern; + if ((pwd = getpwnam((char *)patbuf)) == NULL) + return (pattern); else h = pwd->pw_dir; } @@ -436,7 +436,7 @@ ; *b = EOS; - return patbuf; + return (patbuf); } static int @@ -451,7 +451,7 @@ if (*s1++ == '\0') break; } - return rv; + return (rv); } static int @@ -464,7 +464,7 @@ size_t len; if ((colon = g_strchr(pattern, ':')) == NULL || colon[1] != ']') - return 1; /* not a character class */ + return (1); /* not a character class */ len = (size_t)(colon - pattern); for (cc = cclasses; cc->name != NULL; cc++) { @@ -472,13 +472,13 @@ break; } if (cc->name == NULL) - return -1; /* invalid character class */ + return (-1); /* invalid character class */ *bufnext++ = M_CLASS; *bufnext++ = (Char)(cc - &cclasses[0]); *bufnextp = bufnext; *patternp += len + 3; - return 0; + return (0); } /* @@ -490,7 +490,7 @@ */ static int glob0(const Char *pattern, glob_t *pglob, struct glob_lim *limitp, - int (*errfunc)(const char *, int)) + int (*errfunc)(const char *, int)) { const Char *qpatnext; int c, err, oldpathc; @@ -526,10 +526,11 @@ if (err) break; c = *qpatnext++; - } while (c == LBRACKET && *qpatnext == ':'); + } while (c == LBRACKET && + *qpatnext == ':'); if (err == -1 && !(pglob->gl_flags & GLOB_NOCHECK)) - return GLOB_NOMATCH; + return (GLOB_NOMATCH); if (c == RBRACKET) break; } @@ -550,7 +551,8 @@ break; case STAR: pglob->gl_flags |= GLOB_MAGCHAR; - /* collapse adjacent stars to one, + /* + * collapse adjacent stars to one, * to avoid exponential behavior */ if (bufnext == patbuf || bufnext[-1] != M_ALL) @@ -568,7 +570,7 @@ if ((err = glob1(patbuf, patbuf+MAXPATHLEN-1, pglob, limitp, errfunc)) != 0) - return(err); + return (err); /* * If there was no match we are going to append the pattern @@ -580,9 +582,9 @@ if ((pglob->gl_flags & GLOB_NOCHECK) || ((pglob->gl_flags & GLOB_NOMAGIC) && !(pglob->gl_flags & GLOB_MAGCHAR))) - return(globextend(pattern, pglob, limitp, NULL)); + return (globextend(pattern, pglob, limitp, NULL)); else - return(GLOB_NOMATCH); + return (GLOB_NOMATCH); } if (!(pglob->gl_flags & GLOB_NOSORT)) { if ((pglob->gl_flags & GLOB_KEEPSTAT)) { @@ -592,13 +594,14 @@ int n = pglob->gl_pathc - oldpathc; int o = pglob->gl_offs + oldpathc; - if ((path_stat = calloc(n, sizeof(*path_stat))) == NULL) - return GLOB_NOSPACE; + if ((path_stat = calloc(n, sizeof (*path_stat))) == + NULL) + return (GLOB_NOSPACE); for (i = 0; i < n; i++) { path_stat[i].gps_path = pglob->gl_pathv[o + i]; path_stat[i].gps_stat = pglob->gl_statv[o + i]; } - qsort(path_stat, n, sizeof(*path_stat), compare_gps); + qsort(path_stat, n, sizeof (*path_stat), compare_gps); for (i = 0; i < n; i++) { pglob->gl_pathv[o + i] = path_stat[i].gps_path; pglob->gl_statv[o + i] = path_stat[i].gps_stat; @@ -606,17 +609,17 @@ free(path_stat); } else { qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, - pglob->gl_pathc - oldpathc, sizeof(char *), + pglob->gl_pathc - oldpathc, sizeof (char *), compare); } } - return(0); + return (0); } static int compare(const void *p, const void *q) { - return(strcmp(*(char **)p, *(char **)q)); + return (strcmp(*(char **)p, *(char **)q)); } static int @@ -625,21 +628,21 @@ const struct glob_path_stat *p = (const struct glob_path_stat *)_p; const struct glob_path_stat *q = (const struct glob_path_stat *)_q; - return(strcmp(p->gps_path, q->gps_path)); + return (strcmp(p->gps_path, q->gps_path)); } static int glob1(Char *pattern, Char *pattern_last, glob_t *pglob, - struct glob_lim *limitp, int (*errfunc)(const char *, int)) + struct glob_lim *limitp, int (*errfunc)(const char *, int)) { Char pathbuf[MAXPATHLEN]; /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ if (*pattern == EOS) - return(0); - return(glob2(pathbuf, pathbuf+MAXPATHLEN-1, + return (0); + return (glob2(pathbuf, pathbuf+MAXPATHLEN-1, pathbuf, pathbuf+MAXPATHLEN-1, - pattern, pattern_last, pglob, limitp, errfunc)); + pattern, pattern_last, pglob, limitp, errfunc)); } /* @@ -650,7 +653,7 @@ static int glob2(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last, Char *pattern, Char *pattern_last, glob_t *pglob, - struct glob_lim *limitp, int (*errfunc)(const char *, int)) + struct glob_lim *limitp, int (*errfunc)(const char *, int)) { struct stat sb; Char *p, *q; @@ -660,7 +663,7 @@ * Loop over pattern segments until end of pattern or until * segment with meta character found. */ - for (anymeta = 0;;) { + for (anymeta = 0; ; ) { if (*pattern == EOS) { /* End of pattern? */ *pathend = EOS; @@ -669,10 +672,10 @@ errno = 0; *pathend++ = SEP; *pathend = EOS; - return(GLOB_NOSPACE); + return (GLOB_NOSPACE); } if (g_lstat(pathbuf, &sb, pglob)) - return(0); + return (0); if (((pglob->gl_flags & GLOB_MARK) && pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) || @@ -685,7 +688,7 @@ *pathend = EOS; } ++pglob->gl_matchc; - return(globextend(pathbuf, pglob, limitp, &sb)); + return (globextend(pathbuf, pglob, limitp, &sb)); } /* Find end of next segment, copy tentatively to pathend. */ @@ -709,9 +712,9 @@ } } else /* Need expansion, recurse. */ - return(glob3(pathbuf, pathbuf_last, pathend, + return (glob3(pathbuf, pathbuf_last, pathend, pathend_last, pattern, p, pattern_last, - pglob, limitp, errfunc)); + pglob, limitp, errfunc)); } /* NOTREACHED */ } @@ -719,7 +722,7 @@ static int glob3(Char *pathbuf, Char *pathbuf_last, Char *pathend, Char *pathend_last, Char *pattern, Char *restpattern, Char *restpattern_last, glob_t *pglob, - struct glob_lim *limitp, int (*errfunc)(const char *, int)) + struct glob_lim *limitp, int (*errfunc)(const char *, int)) { struct dirent *dp; DIR *dirp; @@ -735,13 +738,13 @@ if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { /* TODO: don't call for ENOENT or ENOTDIR? */ if (errfunc) { - if (g_Ctoc(pathbuf, buf, sizeof(buf))) - return(GLOB_ABORTED); + if (g_Ctoc(pathbuf, buf, sizeof (buf))) + return (GLOB_ABORTED); if (errfunc(buf, errno) || pglob->gl_flags & GLOB_ERR) - return(GLOB_ABORTED); + return (GLOB_ABORTED); } - return(0); + return (0); } err = 0; @@ -752,7 +755,7 @@ else readdirfunc = readdir; while ((dp = (*readdirfunc)(dirp))) { - u_char *sc; + uchar_t *sc; Char *dc; if ((pglob->gl_flags & GLOB_LIMIT) && @@ -768,7 +771,7 @@ if (dp->d_name[0] == DOT && *pattern != DOT) continue; dc = pathend; - sc = (u_char *) dp->d_name; + sc = (uchar_t *)dp->d_name; while (dc < pathend_last && (*dc++ = *sc++) != EOS) ; if (dc >= pathend_last) { @@ -782,8 +785,8 @@ continue; } err = glob2(pathbuf, pathbuf_last, --dc, pathend_last, - restpattern, restpattern_last, pglob, limitp, - errfunc); + restpattern, restpattern_last, pglob, limitp, + errfunc); if (err) break; } @@ -792,7 +795,7 @@ (*pglob->gl_closedir)(dirp); else closedir(dirp); - return(err); + return (err); } @@ -825,9 +828,9 @@ if (pglob->gl_offs >= INT_MAX || pglob->gl_pathc >= INT_MAX || newn >= INT_MAX || - SIZE_MAX / sizeof(*pathv) <= newn || - SIZE_MAX / sizeof(*statv) <= newn) { - nospace: + SIZE_MAX / sizeof (*pathv) <= newn || + SIZE_MAX / sizeof (*statv) <= newn) { + nospace: for (i = pglob->gl_offs; i < (ssize_t)(newn - 2); i++) { if (pglob->gl_pathv && pglob->gl_pathv[i]) free(pglob->gl_pathv[i]); @@ -844,10 +847,10 @@ free(pglob->gl_statv); pglob->gl_statv = NULL; } - return(GLOB_NOSPACE); + return (GLOB_NOSPACE); } - pathv = realloc(pglob->gl_pathv, newn * sizeof(*pathv)); + pathv = realloc(pglob->gl_pathv, newn * sizeof (*pathv)); if (pathv == NULL) goto nospace; if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { @@ -859,7 +862,7 @@ pglob->gl_pathv = pathv; if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0) { - statv = realloc(pglob->gl_statv, newn * sizeof(*statv)); + statv = realloc(pglob->gl_statv, newn * sizeof (*statv)); if (statv == NULL) goto nospace; if (pglob->gl_statv == NULL && pglob->gl_offs > 0) { @@ -872,22 +875,22 @@ if (sb == NULL) statv[pglob->gl_offs + pglob->gl_pathc] = NULL; else { - limitp->glim_malloc += sizeof(**statv); + limitp->glim_malloc += sizeof (**statv); if ((pglob->gl_flags & GLOB_LIMIT) && limitp->glim_malloc >= GLOB_LIMIT_MALLOC) { errno = 0; - return(GLOB_NOSPACE); + return (GLOB_NOSPACE); } if ((statv[pglob->gl_offs + pglob->gl_pathc] = - malloc(sizeof(**statv))) == NULL) + malloc(sizeof (**statv))) == NULL) goto copy_error; memcpy(statv[pglob->gl_offs + pglob->gl_pathc], sb, - sizeof(*sb)); + sizeof (*sb)); } statv[pglob->gl_offs + pglob->gl_pathc + 1] = NULL; } - for (p = path; *p++;) + for (p = path; *p++; ) ; len = (size_t)(p - path); limitp->glim_malloc += len; @@ -894,7 +897,7 @@ if ((copy = malloc(len)) != NULL) { if (g_Ctoc(path, copy, len)) { free(copy); - return(GLOB_NOSPACE); + return (GLOB_NOSPACE); } pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; } @@ -901,13 +904,13 @@ pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; if ((pglob->gl_flags & GLOB_LIMIT) && - (newn * sizeof(*pathv)) + limitp->glim_malloc > + (newn * sizeof (*pathv)) + limitp->glim_malloc > GLOB_LIMIT_MALLOC) { errno = 0; - return(GLOB_NOSPACE); + return (GLOB_NOSPACE); } - copy_error: - return(copy == NULL ? GLOB_NOSPACE : 0); + copy_error: + return (copy == NULL ? GLOB_NOSPACE : 0); } @@ -922,7 +925,7 @@ Char c, k; if (recur-- == 0) - return(GLOB_NOSPACE); + return (GLOB_NOSPACE); while (pat < patend) { c = *pat++; @@ -931,20 +934,20 @@ while (pat < patend && (*pat & M_MASK) == M_ALL) pat++; /* eat consecutive '*' */ if (pat == patend) - return(1); + return (1); do { - if (match(name, pat, patend, recur)) - return(1); + if (match(name, pat, patend, recur)) + return (1); } while (*name++ != EOS); - return(0); + return (0); case M_ONE: if (*name++ == EOS) - return(0); + return (0); break; case M_SET: ok = 0; if ((k = *name++) == EOS) - return(0); + return (0); if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) ++pat; while (((c = *pat++) & M_MASK) != M_END) { @@ -963,15 +966,15 @@ ok = 1; } if (ok == negate_range) - return(0); + return (0); break; default: if (*name++ != c) - return(0); + return (0); break; } } - return(*name == EOS); + return (*name == EOS); } /* Free allocated data belonging to a glob_t structure. */ @@ -990,7 +993,7 @@ pglob->gl_pathv = NULL; } if ((pglob->gl_flags & GLOB_KEEPSTAT) != 0 && - pglob->gl_statv != NULL) { + pglob->gl_statv != NULL) { for (i = 0; i < pglob->gl_pathc; i++) { if (pglob->gl_statv[i] != NULL) free(pglob->gl_statv[i]); @@ -1006,16 +1009,16 @@ char buf[MAXPATHLEN]; if (!*str) - strlcpy(buf, ".", sizeof buf); + strlcpy(buf, ".", sizeof (buf)); else { - if (g_Ctoc(str, buf, sizeof(buf))) - return(NULL); + if (g_Ctoc(str, buf, sizeof (buf))) + return (NULL); } if (pglob->gl_flags & GLOB_ALTDIRFUNC) - return((*pglob->gl_opendir)(buf)); + return ((*pglob->gl_opendir)(buf)); - return(opendir(buf)); + return (opendir(buf)); } static int @@ -1023,11 +1026,11 @@ { char buf[MAXPATHLEN]; - if (g_Ctoc(fn, buf, sizeof(buf))) - return(-1); + if (g_Ctoc(fn, buf, sizeof (buf))) + return (-1); if (pglob->gl_flags & GLOB_ALTDIRFUNC) - return((*pglob->gl_lstat)(buf, sb)); - return(lstat(buf, sb)); + return ((*pglob->gl_lstat)(buf, sb)); + return (lstat(buf, sb)); } static int @@ -1035,11 +1038,11 @@ { char buf[MAXPATHLEN]; - if (g_Ctoc(fn, buf, sizeof(buf))) - return(-1); + if (g_Ctoc(fn, buf, sizeof (buf))) + return (-1); if (pglob->gl_flags & GLOB_ALTDIRFUNC) - return((*pglob->gl_stat)(buf, sb)); - return(stat(buf, sb)); + return ((*pglob->gl_stat)(buf, sb)); + return (stat(buf, sb)); } static Char * @@ -1053,7 +1056,7 @@ } static int -g_Ctoc(const Char *str, char *buf, u_int len) +g_Ctoc(const Char *str, char *buf, uint_t len) { while (len--) { @@ -1069,15 +1072,15 @@ { Char *p; - (void)printf("%s:\n", str); + (void) printf("%s:\n", str); for (p = s; *p; p++) - (void)printf("%c", CHAR(*p)); - (void)printf("\n"); + (void) printf("%c", CHAR(*p)); + (void) printf("\n"); for (p = s; *p; p++) - (void)printf("%c", *p & M_PROTECT ? '"' : ' '); - (void)printf("\n"); + (void) printf("%c", *p & M_PROTECT ? '"' : ' '); + (void) printf("\n"); for (p = s; *p; p++) - (void)printf("%c", ismeta(*p) ? '_' : ' '); - (void)printf("\n"); + (void) printf("%c", ismeta(*p) ? '_' : ' '); + (void) printf("\n"); } #endif