test.c (6896B)
1 /* see LICENSE file for copyright and license details */ 2 3 #if FEATURE_FILE_MODE_EXT 4 #ifndef _DEFAULT_SOURCE 5 #define _DEFAULT_SOURCE 6 #endif 7 #endif 8 #include <sys/stat.h> 9 10 #include <ctype.h> 11 #include <fcntl.h> 12 #include <string.h> 13 #include <unistd.h> 14 15 #include "util.h" 16 17 static int 18 intcmp(char *a, char *b) 19 { 20 char *s; 21 int asign = *a == '-' ? -1 : 1; 22 int bsign = *b == '-' ? -1 : 1; 23 24 if (*a == '-' || *a == '+') 25 a += 1; 26 if (*b == '-' || *b == '+') 27 b += 1; 28 29 if (!*a || !*b) 30 goto noint; 31 for (s = a; *s; s++) 32 if (!isdigit(*s)) 33 goto noint; 34 for (s = b; *s; s++) 35 if (!isdigit(*s)) 36 goto noint; 37 38 while (*a == '0') 39 a++; 40 while (*b == '0') 41 b++; 42 asign *= !!*a; 43 bsign *= !!*b; 44 45 if (asign != bsign) 46 return asign < bsign ? -1 : 1; 47 else if (strlen(a) != strlen(b)) 48 return asign * (strlen(a) < strlen(b) ? -1 : 1); 49 else 50 return asign * strcmp(a, b); 51 52 noint: 53 enprintf(2, "expected integer operands\n"); 54 55 return 0; /* not reached */ 56 } 57 58 static int 59 mtimecmp(struct stat *buf1, struct stat *buf2) 60 { 61 if (buf1->st_mtime < buf2->st_mtime) 62 return -1; 63 if (buf1->st_mtime > buf2->st_mtime) 64 return +1; 65 #ifdef st_mtime 66 if (buf1->st_mtim.tv_nsec < buf2->st_mtim.tv_nsec) 67 return -1; 68 if (buf1->st_mtim.tv_nsec > buf2->st_mtim.tv_nsec) 69 return +1; 70 #endif 71 return 0; 72 } 73 74 static int 75 unary_b(char *s) 76 { 77 struct stat buf; 78 if (stat(s, &buf)) 79 return 0; 80 return S_ISBLK(buf.st_mode); 81 } 82 static int 83 unary_c(char *s) 84 { 85 struct stat buf; 86 if (stat(s, &buf)) 87 return 0; 88 return S_ISCHR(buf.st_mode); 89 } 90 static int 91 unary_d(char *s) 92 { 93 struct stat buf; 94 if (stat(s, &buf)) 95 return 0; 96 return S_ISDIR(buf.st_mode); 97 } 98 static int 99 unary_f(char *s) 100 { 101 struct stat buf; 102 if (stat(s, &buf)) 103 return 0; 104 return S_ISREG(buf.st_mode); 105 } 106 static int 107 unary_g(char *s) 108 { 109 struct stat buf; 110 if (stat(s, &buf)) 111 return 0; 112 return S_ISGID & buf.st_mode; 113 } 114 static int 115 unary_h(char *s) 116 { 117 struct stat buf; 118 if (lstat(s, &buf)) 119 return 0; 120 return S_ISLNK(buf.st_mode); 121 } 122 static int 123 unary_k(char *s) 124 { 125 struct stat buf; 126 if (stat(s, &buf)) 127 return 0; 128 return S_ISVTX & buf.st_mode; 129 } 130 static int 131 unary_p(char *s) 132 { 133 struct stat buf; 134 if (stat(s, &buf)) 135 return 0; 136 return S_ISFIFO(buf.st_mode); 137 } 138 static int 139 unary_S(char *s) 140 { 141 struct stat buf; 142 if (stat(s, &buf)) 143 return 0; 144 return S_ISSOCK(buf.st_mode); 145 } 146 static int 147 unary_s(char *s) 148 { 149 struct stat buf; 150 if (stat(s, &buf)) 151 return 0; 152 return buf.st_size; 153 } 154 static int 155 unary_u(char *s) 156 { 157 struct stat buf; 158 if (stat(s, &buf)) 159 return 0; 160 return S_ISUID & buf.st_mode; 161 } 162 163 static int 164 unary_n(char *s) 165 { 166 return *s; 167 } 168 static int 169 unary_z(char *s) 170 { 171 return !*s; 172 } 173 174 static int 175 unary_e(char *s) 176 { 177 return !faccessat(AT_FDCWD, s, F_OK, AT_EACCESS); 178 } 179 static int 180 unary_r(char *s) 181 { 182 return !faccessat(AT_FDCWD, s, R_OK, AT_EACCESS); 183 } 184 static int 185 unary_w(char *s) 186 { 187 return !faccessat(AT_FDCWD, s, W_OK, AT_EACCESS); 188 } 189 static int 190 unary_x(char *s) 191 { 192 return !faccessat(AT_FDCWD, s, X_OK, AT_EACCESS); 193 } 194 195 static int 196 unary_t(char *s) 197 { 198 int fd = enstrtonum(2, s, 0, INT_MAX); 199 return isatty(fd); 200 } 201 202 static int 203 binary_se(char *s1, char *s2) 204 { 205 return !strcmp(s1, s2); 206 } 207 static int 208 binary_sn(char *s1, char *s2) 209 { 210 return strcmp(s1, s2); 211 } 212 213 static int 214 binary_eq(char *s1, char *s2) 215 { 216 return intcmp(s1, s2) == 0; 217 } 218 static int 219 binary_ne(char *s1, char *s2) 220 { 221 return intcmp(s1, s2) != 0; 222 } 223 static int 224 binary_gt(char *s1, char *s2) 225 { 226 return intcmp(s1, s2) > 0; 227 } 228 static int 229 binary_ge(char *s1, char *s2) 230 { 231 return intcmp(s1, s2) >= 0; 232 } 233 static int 234 binary_lt(char *s1, char *s2) 235 { 236 return intcmp(s1, s2) < 0; 237 } 238 static int 239 binary_le(char *s1, char *s2) 240 { 241 return intcmp(s1, s2) <= 0; 242 } 243 244 static int 245 binary_ef(char *s1, char *s2) 246 { 247 struct stat buf1, buf2; 248 if (stat(s1, &buf1) || stat(s2, &buf2)) 249 return 0; 250 return buf1.st_dev == buf2.st_dev && buf1.st_ino == buf2.st_ino; 251 } 252 253 static int 254 binary_ot(char *s1, char *s2) 255 { 256 struct stat buf1, buf2; 257 if (stat(s1, &buf1) || stat(s2, &buf2)) 258 return 0; 259 return mtimecmp(&buf1, &buf2) < 0; 260 } 261 262 static int 263 binary_nt(char *s1, char *s2) 264 { 265 struct stat buf1, buf2; 266 if (stat(s1, &buf1) || stat(s2, &buf2)) 267 return 0; 268 return mtimecmp(&buf1, &buf2) > 0; 269 } 270 271 struct test { 272 char *name; 273 union { 274 int (*u)(char *); 275 int (*b)(char *, char *); 276 } func; 277 }; 278 279 static struct test unary[] = { 280 {"-b", {.u = unary_b}}, 281 {"-c", {.u = unary_c}}, 282 {"-d", {.u = unary_d}}, 283 {"-e", {.u = unary_e}}, 284 {"-f", {.u = unary_f}}, 285 {"-g", {.u = unary_g}}, 286 {"-h", {.u = unary_h}}, 287 {"-k", {.u = unary_k}}, 288 {"-L", {.u = unary_h}}, 289 {"-n", {.u = unary_n}}, 290 {"-p", {.u = unary_p}}, 291 {"-r", {.u = unary_r}}, 292 {"-S", {.u = unary_S}}, 293 {"-s", {.u = unary_s}}, 294 {"-t", {.u = unary_t}}, 295 {"-u", {.u = unary_u}}, 296 {"-w", {.u = unary_w}}, 297 {"-x", {.u = unary_x}}, 298 {"-z", {.u = unary_z}}, 299 300 {NULL, {0}}, 301 }; 302 303 static struct test binary[] = { 304 {"=", {.b = binary_se}}, 305 {"!=", {.b = binary_sn}}, 306 {"-eq", {.b = binary_eq}}, 307 {"-ne", {.b = binary_ne}}, 308 {"-gt", {.b = binary_gt}}, 309 {"-ge", {.b = binary_ge}}, 310 {"-lt", {.b = binary_lt}}, 311 {"-le", {.b = binary_le}}, 312 {"-ef", {.b = binary_ef}}, 313 {"-ot", {.b = binary_ot}}, 314 {"-nt", {.b = binary_nt}}, 315 316 {NULL, {0}}, 317 }; 318 319 static struct test * 320 find_test(struct test *tests, char *name) 321 { 322 struct test *t; 323 324 for (t = tests; t->name; t++) 325 if (!strcmp(t->name, name)) 326 return t; 327 328 return NULL; 329 } 330 331 static int 332 noarg(char *argv[]) 333 { 334 (void)argv; 335 return 0; 336 } 337 338 static int 339 onearg(char *argv[]) 340 { 341 return unary_n(argv[0]); 342 } 343 344 static int 345 twoarg(char *argv[]) 346 { 347 struct test *t; 348 349 if (!strcmp(argv[0], "!")) 350 return !onearg(argv + 1); 351 352 if ((t = find_test(unary, *argv))) 353 return t->func.u(argv[1]); 354 355 enprintf(2, "bad unary test %s\n", argv[0]); 356 357 return 0; /* not reached */ 358 } 359 360 static int 361 threearg(char *argv[]) 362 { 363 struct test *t = find_test(binary, argv[1]); 364 365 if (t) 366 return t->func.b(argv[0], argv[2]); 367 368 if (!strcmp(argv[0], "!")) 369 return !twoarg(argv + 1); 370 371 enprintf(2, "bad binary test %s\n", argv[1]); 372 373 return 0; /* not reached */ 374 } 375 376 static int 377 fourarg(char *argv[]) 378 { 379 if (!strcmp(argv[0], "!")) 380 return !threearg(argv + 1); 381 382 enprintf(2, "too many arguments\n"); 383 384 return 0; /* not reached */ 385 } 386 387 // ?man test: evaluate condition 388 // ?man check file types and compare values, returning 0 or 1 389 int 390 main(int argc, char *argv[]) 391 { 392 int (*narg[])(char *[]) = {noarg, onearg, twoarg, threearg, fourarg}; 393 size_t len; 394 395 argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0; 396 397 len = argv0 ? strlen(argv0) : 0; 398 if (len && argv0[--len] == '[' && (!len || argv0[--len] == '/') && strcmp(argv[--argc], "]")) 399 enprintf(2, "no matching ]\n"); 400 401 if (argc > 4) 402 enprintf(2, "too many arguments\n"); 403 404 return !narg[argc](argv); 405 }