Annotation of embedaddon/sudo/compat/regress/glob/globtest.c, revision 1.1.1.3

1.1       misho       1: /*     $OpenBSD: globtest.c,v 1.1 2008/10/01 23:04:36 millert Exp $    */
                      2: 
                      3: /*
                      4:  * Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
                      5:  */
                      6: 
                      7: #include <config.h>
                      8: 
                      9: #include <stdio.h>
                     10: #include <stdlib.h>
                     11: #ifdef HAVE_STRING_H
                     12: # include <string.h>
                     13: #endif /* HAVE_STRING_H */
                     14: #ifdef HAVE_STRINGS_H
                     15: # include <strings.h>
                     16: #endif /* HAVE_STRINGS_H */
1.1.1.2   misho      17: #ifdef HAVE_GLOB
1.1       misho      18: # include <glob.h>
                     19: #else
                     20: # include "compat/glob.h"
                     21: #endif
                     22: #include <errno.h>
                     23: 
                     24: #include "missing.h"
                     25: 
                     26: #define MAX_RESULTS    256
                     27: 
                     28: struct gl_entry {
                     29:        int flags;
                     30:        int nresults;
                     31:        char pattern[1024];
                     32:        char *results[MAX_RESULTS];
                     33: };
                     34: 
                     35: int test_glob(struct gl_entry *);
1.1.1.3 ! misho      36: __dso_public int main(int argc, char *argv[]);
1.1       misho      37: 
                     38: int
                     39: main(int argc, char **argv)
                     40: {
                     41:        FILE *fp = stdin;
                     42:        char buf[2048], *cp, *ep;
                     43:        int errors = 0, tests = 0, lineno;
                     44:        struct gl_entry entry;
                     45:        size_t len;
                     46: 
                     47:        if (argc > 1) {
                     48:                if ((fp = fopen(argv[1], "r")) == NULL) {
                     49:                        perror(argv[1]);
                     50:                        exit(1);
                     51:                }
                     52:        }
                     53: 
                     54:        /*
                     55:         * Read in test file, which is formatted thusly:
                     56:         *
                     57:         * [pattern] <flags>
                     58:         * result1
                     59:         * result2
                     60:         * result3
                     61:         * ...
                     62:         *
                     63:         */
                     64:        lineno = 0;
                     65:        memset(&entry, 0, sizeof(entry));
                     66:        while (fgets(buf, sizeof(buf), fp) != NULL) {
                     67:                lineno++;
                     68:                len = strlen(buf);
                     69:                if (len > 0) {
                     70:                        if (buf[len - 1] != '\n') {
                     71:                                fprintf(stderr,
                     72:                                    "globtest: missing newline at EOF\n");
                     73:                                exit(1);
                     74:                        }
                     75:                        buf[--len] = '\0';
                     76:                }
                     77:                if (len == 0)
                     78:                        continue; /* blank line */
                     79: 
                     80:                if (buf[0] == '[') {
                     81:                        /* check previous pattern */
                     82:                        if (entry.pattern[0]) {
                     83:                                errors += test_glob(&entry);
                     84:                                tests++;
                     85:                        }
                     86: 
                     87:                        /* start new entry */
                     88:                        if ((cp = strrchr(buf + 1, ']')) == NULL) {
                     89:                                fprintf(stderr,
                     90:                                    "globtest: invalid entry on line %d\n",
                     91:                                    lineno);
                     92:                                exit(1);
                     93:                        }
                     94:                        len = cp - buf - 1;
                     95:                        if (len >= sizeof(entry.pattern)) {
                     96:                                fprintf(stderr,
                     97:                                    "globtest: pattern too big on line %d\n",
                     98:                                    lineno);
                     99:                                exit(1);
                    100:                        }
                    101:                        memcpy(entry.pattern, buf + 1, len);
                    102:                        entry.pattern[len] = '\0';
                    103: 
                    104:                        cp += 2;
                    105:                        if (*cp++ != '<') {
                    106:                                fprintf(stderr,
                    107:                                    "globtest: invalid entry on line %d\n",
                    108:                                    lineno);
                    109:                                exit(1);
                    110:                        }
                    111:                        ep = strchr(cp, '>');
                    112:                        if (ep == NULL) {
                    113:                                fprintf(stderr,
                    114:                                    "globtest: invalid entry on line %d\n",
                    115:                                    lineno);
                    116:                                exit(1);
                    117:                        }
                    118:                        *ep = '\0';
                    119:                        entry.flags = 0;
                    120:                        for ((cp = strtok(cp, "|")); cp != NULL; (cp = strtok(NULL, "|"))) {
                    121:                                if (strcmp(cp, "GLOB_APPEND") == 0)
                    122:                                        entry.flags |= GLOB_APPEND;
                    123:                                else if (strcmp(cp, "GLOB_DOOFFS") == 0)
                    124:                                        entry.flags |= GLOB_DOOFFS;
                    125:                                else if (strcmp(cp, "GLOB_ERR") == 0)
                    126:                                        entry.flags |= GLOB_ERR;
                    127:                                else if (strcmp(cp, "GLOB_MARK") == 0)
                    128:                                        entry.flags |= GLOB_MARK;
                    129:                                else if (strcmp(cp, "GLOB_NOCHECK") == 0)
                    130:                                        entry.flags |= GLOB_NOCHECK;
                    131:                                else if (strcmp(cp, "GLOB_NOSORT") == 0)
                    132:                                        entry.flags |= GLOB_NOSORT;
                    133:                                else if (strcmp(cp, "GLOB_NOESCAPE") == 0)
                    134:                                        entry.flags |= GLOB_NOESCAPE;
                    135:                                else if (strcmp(cp, "GLOB_BRACE") == 0)
                    136:                                        entry.flags |= GLOB_BRACE;
                    137:                                else if (strcmp(cp, "GLOB_TILDE") == 0)
                    138:                                        entry.flags |= GLOB_TILDE;
                    139:                                else if (strcmp(cp, "NONE") != 0) {
                    140:                                        fprintf(stderr,
                    141:                                            "globtest: invalid flags on line %d\n",
                    142:                                            lineno);
                    143:                                        exit(1);
                    144:                                }
                    145:                        }
                    146:                        entry.nresults = 0;
                    147:                        continue;
                    148:                }
                    149:                if (!entry.pattern[0]) {
                    150:                        fprintf(stderr, "globtest: missing entry on line %d\n",
                    151:                            lineno);
                    152:                        exit(1);
                    153:                }
                    154: 
                    155:                if (entry.nresults + 1 > MAX_RESULTS) {
                    156:                        fprintf(stderr,
                    157:                            "globtest: too many results for %s, max %d\n",
                    158:                            entry.pattern, MAX_RESULTS);
                    159:                        exit(1);
                    160:                }
                    161:                entry.results[entry.nresults++] = strdup(buf);
                    162:        }
                    163:        if (entry.pattern[0]) {
                    164:                errors += test_glob(&entry); /* test last pattern */
                    165:                tests++;
                    166:        }
                    167:         if (tests != 0) {
                    168:                printf("glob: %d test%s run, %d errors, %d%% success rate\n",
                    169:                    tests, tests == 1 ? "" : "s", errors,
                    170:                    (tests - errors) * 100 / tests);
                    171:         }
                    172:        exit(errors);
                    173: }
                    174: 
                    175: int test_glob(struct gl_entry *entry)
                    176: {
                    177:        glob_t gl;
                    178:        char **ap;
                    179:        int nmatches = 0, i = 0;
                    180: 
                    181:        if (glob(entry->pattern, entry->flags, NULL, &gl) != 0) {
                    182:                fprintf(stderr, "glob failed: %s: %s\n", entry->pattern,
                    183:                    strerror(errno));
                    184:                exit(1);
                    185:        }
                    186: 
                    187:        for (ap = gl.gl_pathv; *ap != NULL; ap++)
                    188:                nmatches++;
                    189: 
                    190:        if (nmatches != entry->nresults)
                    191:                goto mismatch;
                    192: 
                    193:        for (i = 0; i < entry->nresults; i++) {
                    194:                if (strcmp(gl.gl_pathv[i], entry->results[i]) != 0)
                    195:                        goto mismatch;
                    196:                free(entry->results[i]);
                    197:        }
                    198:        return 0;
                    199:  mismatch:
                    200:        if (nmatches != entry->nresults) {
                    201:                fprintf(stderr,
                    202:                    "globtest: mismatch in number of results (found %d, expected %d) for pattern %s\n",
                    203:                    nmatches, entry->nresults, entry->pattern);
                    204:        } else {
                    205:                fprintf(stderr, "globtest: mismatch for pattern %s, flags 0x%x "
                    206:                    "(found \"%s\", expected \"%s\")\n", entry->pattern, entry->flags,
                    207:                    gl.gl_pathv[i], entry->results[i]);
                    208:                while (i < entry->nresults)
                    209:                        free(entry->results[i++]);
                    210:        }
                    211:        return 1;
                    212: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>