| 1 | #include <errno.h> | 
|---|
| 2 | #include <fcntl.h> | 
|---|
| 3 | #include <regex.h> | 
|---|
| 4 | #include <stdio.h> | 
|---|
| 5 | #include <stdlib.h> | 
|---|
| 6 | #include <string.h> | 
|---|
| 7 | #include <sys/stat.h> | 
|---|
| 8 | #include <unistd.h> | 
|---|
| 9 |  | 
|---|
| 10 | int | 
|---|
| 11 | main (int argc, char *argv[]) | 
|---|
| 12 | { | 
|---|
| 13 | struct stat st; | 
|---|
| 14 | static const char *pat[] = { | 
|---|
| 15 | ".?.?.?.?.?.?.?argc", | 
|---|
| 16 | "(.?)(.?)(.?)(.?)(.?)(.?)(.?)argc", | 
|---|
| 17 | "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))" | 
|---|
| 18 | "((((((((((.?))))))))))((((((((((.?))))))))))((((((((((.?))))))))))" | 
|---|
| 19 | "((((((((((.?))))))))))argc" }; | 
|---|
| 20 |  | 
|---|
| 21 | size_t len; | 
|---|
| 22 | int fd; | 
|---|
| 23 | int testno, i, j, k, l; | 
|---|
| 24 | char *string; | 
|---|
| 25 | char *buf; | 
|---|
| 26 |  | 
|---|
| 27 | if (argc < 2) | 
|---|
| 28 | abort (); | 
|---|
| 29 |  | 
|---|
| 30 | fd = open (argv[1], O_RDONLY); | 
|---|
| 31 | if (fd < 0) | 
|---|
| 32 | { | 
|---|
| 33 | printf ("Couldn't open %s: %s\n", argv[1], strerror (errno)); | 
|---|
| 34 | abort (); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 | if (fstat (fd, &st) < 0) | 
|---|
| 38 | { | 
|---|
| 39 | printf ("Couldn't fstat %s: %s\n", argv[1], strerror (errno)); | 
|---|
| 40 | abort (); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | buf = malloc (st.st_size + 1); | 
|---|
| 44 | if (buf == NULL) | 
|---|
| 45 | { | 
|---|
| 46 | printf ("Couldn't allocate buffer: %s\n", strerror (errno)); | 
|---|
| 47 | abort (); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | if (read (fd, buf, st.st_size) != (ssize_t) st.st_size) | 
|---|
| 51 | { | 
|---|
| 52 | printf ("Couldn't read %s", argv[1]); | 
|---|
| 53 | abort (); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | close (fd); | 
|---|
| 57 | buf[st.st_size] = '\0'; | 
|---|
| 58 |  | 
|---|
| 59 | string = buf; | 
|---|
| 60 | len = st.st_size; | 
|---|
| 61 |  | 
|---|
| 62 | for (testno = 0; testno < 4; ++testno) | 
|---|
| 63 | for (i = 0; i < sizeof (pat) / sizeof (pat[0]); ++i) | 
|---|
| 64 | { | 
|---|
| 65 | regex_t rbuf; | 
|---|
| 66 | struct re_pattern_buffer rpbuf; | 
|---|
| 67 | int err; | 
|---|
| 68 |  | 
|---|
| 69 | printf ("test %d pattern %d", testno, i); | 
|---|
| 70 | if (testno < 2) | 
|---|
| 71 | { | 
|---|
| 72 | err = regcomp (&rbuf, pat[i], | 
|---|
| 73 | REG_EXTENDED | (testno ? REG_NOSUB : 0)); | 
|---|
| 74 | if (err != 0) | 
|---|
| 75 | { | 
|---|
| 76 | char errstr[300]; | 
|---|
| 77 | putchar ('\n'); | 
|---|
| 78 | regerror (err, &rbuf, errstr, sizeof (errstr)); | 
|---|
| 79 | puts (errstr); | 
|---|
| 80 | return err; | 
|---|
| 81 | } | 
|---|
| 82 | } | 
|---|
| 83 | else | 
|---|
| 84 | { | 
|---|
| 85 | const char *s; | 
|---|
| 86 | re_set_syntax (RE_SYNTAX_POSIX_EGREP | 
|---|
| 87 | | (testno == 3 ? RE_NO_SUB : 0)); | 
|---|
| 88 |  | 
|---|
| 89 | memset (&rpbuf, 0, sizeof (rpbuf)); | 
|---|
| 90 | s = re_compile_pattern (pat[i], strlen (pat[i]), &rpbuf); | 
|---|
| 91 | if (s != NULL) | 
|---|
| 92 | { | 
|---|
| 93 | printf ("\n%s\n", s); | 
|---|
| 94 | abort (); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | /* Just so that this can be tested with earlier glibc as well.  */ | 
|---|
| 98 | if (testno == 3) | 
|---|
| 99 | rpbuf.no_sub = 1; | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | if (testno < 2) | 
|---|
| 103 | { | 
|---|
| 104 | regmatch_t pmatch[71]; | 
|---|
| 105 | err = regexec (&rbuf, string, 71, pmatch, 0); | 
|---|
| 106 | if (err == REG_NOMATCH) | 
|---|
| 107 | { | 
|---|
| 108 | puts ("\nregexec failed"); | 
|---|
| 109 | abort (); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | if (testno == 0) | 
|---|
| 113 | { | 
|---|
| 114 | if (pmatch[0].rm_eo != pmatch[0].rm_so + 11 | 
|---|
| 115 | || pmatch[0].rm_eo > len | 
|---|
| 116 | || string + pmatch[0].rm_so >= strchr (string, 'R') | 
|---|
| 117 | || strncmp (string + pmatch[0].rm_so, | 
|---|
| 118 | "n (int argc", | 
|---|
| 119 | sizeof "n (int argc" - 1) | 
|---|
| 120 | != 0) | 
|---|
| 121 | { | 
|---|
| 122 | puts ("\nregexec without REG_NOSUB did not find the correct match"); | 
|---|
| 123 | abort (); | 
|---|
| 124 | } | 
|---|
| 125 |  | 
|---|
| 126 | if (i > 0) | 
|---|
| 127 | for (j = 0, l = 1; j < 7; ++j) | 
|---|
| 128 | for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l) | 
|---|
| 129 | if (pmatch[l].rm_so != pmatch[0].rm_so + j | 
|---|
| 130 | || pmatch[l].rm_eo != pmatch[l].rm_so + 1) | 
|---|
| 131 | { | 
|---|
| 132 | printf ("\npmatch[%d] incorrect\n", l); | 
|---|
| 133 | abort (); | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 | else | 
|---|
| 138 | { | 
|---|
| 139 | struct re_registers regs; | 
|---|
| 140 | int match; | 
|---|
| 141 |  | 
|---|
| 142 | memset (®s, 0, sizeof (regs)); | 
|---|
| 143 | match = re_search (&rpbuf, string, len, 0, len, | 
|---|
| 144 | ®s); | 
|---|
| 145 | if (match < 0) | 
|---|
| 146 | { | 
|---|
| 147 | puts ("\nre_search failed"); | 
|---|
| 148 | abort (); | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | if (match + 11 > len | 
|---|
| 152 | || string + match >= strchr (string, 'R') | 
|---|
| 153 | || strncmp (string + match, | 
|---|
| 154 | "n (int argc", | 
|---|
| 155 | sizeof "n (int argc" - 1) | 
|---|
| 156 | != 0) | 
|---|
| 157 | { | 
|---|
| 158 | puts ("\nre_search did not find the correct match"); | 
|---|
| 159 | abort (); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | if (testno == 2) | 
|---|
| 163 | { | 
|---|
| 164 | if (regs.num_regs != 2 + (i == 0 ? 0 : i == 1 ? 7 : 70)) | 
|---|
| 165 | { | 
|---|
| 166 | printf ("\nincorrect num_regs %d\n", regs.num_regs); | 
|---|
| 167 | abort (); | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | if (regs.start[0] != match || regs.end[0] != match + 11) | 
|---|
| 171 | { | 
|---|
| 172 | printf ("\nincorrect regs.{start,end}[0] = { %d, %d}\n", | 
|---|
| 173 | regs.start[0], regs.end[0]); | 
|---|
| 174 | abort (); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | if (regs.start[regs.num_regs - 1] != -1 | 
|---|
| 178 | || regs.end[regs.num_regs - 1] != -1) | 
|---|
| 179 | { | 
|---|
| 180 | puts ("\nincorrect regs.{start,end}[num_regs - 1]"); | 
|---|
| 181 | abort (); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | if (i > 0) | 
|---|
| 185 | for (j = 0, l = 1; j < 7; ++j) | 
|---|
| 186 | for (k = 0; k < (i == 1 ? 1 : 10); ++k, ++l) | 
|---|
| 187 | if (regs.start[l] != match + j | 
|---|
| 188 | || regs.end[l] != regs.start[l] + 1) | 
|---|
| 189 | { | 
|---|
| 190 | printf ("\nregs.{start,end}[%d] incorrect\n", l); | 
|---|
| 191 | abort (); | 
|---|
| 192 | } | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | putchar ('\n'); | 
|---|
| 197 |  | 
|---|
| 198 | if (testno < 2) | 
|---|
| 199 | regfree (&rbuf); | 
|---|
| 200 | else | 
|---|
| 201 | regfree (&rpbuf); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | exit (0); | 
|---|
| 205 | } | 
|---|