test.c (13024B)
1 /* $netbsd: test.c,v 1.21 1999/04/05 09:48:38 kleink exp $ */ 2 3 /* - 4 * test(1); version 7-like, author erik baalbergen 5 * modified by eric gisin to be used as built-in 6 * modified by arnold robbins to add SVR3 compatibility 7 * (-x -c -b -p -u -g -k) plus korn's -L -nt -ot -ef and new -S (socket) 8 * modified by J.T. conklin for netbsd 9 * 10 * this program is in the public domain 11 */ 12 /* 13 * important: this file is used both as a standalone program /bin/test and 14 * as a builtin for /bin/sh (#define SHELL) 15 */ 16 17 #ifndef SHELL 18 #ifndef __dead2 19 #define __dead2 __attribute__((__noreturn__)) 20 #endif 21 #ifndef __printf0like 22 #define __printf0like(n, m) __attribute__((__format__(__printf__, n, m))) 23 #endif 24 #ifndef __nonstring 25 #if defined(__has_attribute) 26 #if __has_attribute(__nonstring__) 27 #define __nonstring __attribute__((__nonstring__)) 28 #elif __has_attribute(nonstring) 29 #define __nonstring __attribute__((nonstring)) 30 #endif 31 #endif 32 #endif 33 #ifndef __nonstring 34 #define __nonstring 35 #endif 36 #ifndef eaccess 37 #ifdef AT_EACCESS 38 #define eaccess(path, mode) faccessat(AT_FDCWD, (path), (mode), AT_EACCESS) 39 #else 40 #define eaccess(path, mode) access((path), (mode)) 41 #endif 42 #endif 43 #endif 44 45 #if FEATURE_SH_EXT 46 #ifndef _DEFAULT_SOURCE 47 #define _DEFAULT_SOURCE 48 #endif 49 #ifndef _GNU_SOURCE 50 #define _GNU_SOURCE 51 #endif 52 #endif 53 #include <sys/stat.h> 54 #include <sys/types.h> 55 56 #include <ctype.h> 57 #include <err.h> 58 #include <errno.h> 59 #include <inttypes.h> 60 #include <stdarg.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <unistd.h> 64 65 #ifdef SHELL 66 #define main testcmd 67 #include "bltin.h" 68 #else 69 #include <locale.h> 70 71 static void error(const char *, ...) __dead2 __printf0like(1, 2); 72 73 static void 74 error(const char *msg, ...) 75 { 76 va_list ap; 77 va_start(ap, msg); 78 verrx(2, msg, ap); 79 /* NOTREACHED */ 80 va_end(ap); 81 } 82 #endif 83 84 /* test(1) accepts the following grammar: 85 * oexpr ::= aexpr | aexpr "-o" oexpr ; 86 * aexpr ::= nexpr | nexpr "-a" aexpr ; 87 * nexpr ::= primary | "!" primary 88 * primary ::= unary-operator operand 89 * | operand binary-operator operand 90 * | operand 91 * | "(" oexpr ")" 92 * ; 93 * unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| 94 * "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-o"|"-g"|"-l"|"-s"; 95 * 96 * binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| 97 * "-nt"|"-ot"|"-ef"; 98 * operand ::= <any legal UNIX file name> 99 */ 100 101 enum token_types { UNOP = 0x100, BINOP = 0x200, BUNOP = 0x300, BBINOP = 0x400, PAREN = 0x500 }; 102 103 enum token { 104 EOI, 105 OPERAND, 106 FILRD = UNOP + 1, 107 FILWR, 108 FILEX, 109 FILEXIST, 110 FILREG, 111 FILDIR, 112 FILCDEV, 113 FILBDEV, 114 FILFIFO, 115 FILSOCK, 116 FILSYM, 117 FILGZ, 118 FILTT, 119 FILSUID, 120 FILSGID, 121 FILSTCK, 122 STREZ, 123 STRNZ, 124 FILUID, 125 FILGID, 126 FILNT = BINOP + 1, 127 FILOT, 128 FILEQ, 129 STREQ, 130 STRNE, 131 STRLT, 132 STRGT, 133 INTEQ, 134 INTNE, 135 INTGE, 136 INTGT, 137 INTLE, 138 INTLT, 139 UNOT = BUNOP + 1, 140 BAND = BBINOP + 1, 141 BOR, 142 LPAREN = PAREN + 1, 143 RPAREN 144 }; 145 146 #define TOKEN_TYPE(token) ((token) & 0xff00) 147 148 static const struct t_op { 149 char op_text[2] __nonstring; 150 short op_num; 151 } ops1[] = 152 { 153 {"=", STREQ}, 154 {"<", STRLT}, 155 {">", STRGT}, 156 {"!", UNOT}, 157 {"(", LPAREN}, 158 {")", RPAREN}, 159 }, 160 opsm1[] = 161 { 162 {"r", FILRD}, {"w", FILWR}, {"x", FILEX}, {"e", FILEXIST}, {"f", FILREG}, 163 {"d", FILDIR}, {"c", FILCDEV}, {"b", FILBDEV}, {"p", FILFIFO}, {"u", FILSUID}, 164 {"g", FILSGID}, {"k", FILSTCK}, {"s", FILGZ}, {"t", FILTT}, {"z", STREZ}, 165 {"n", STRNZ}, {"h", FILSYM}, /* for backwards compat */ 166 {"O", FILUID}, {"G", FILGID}, {"L", FILSYM}, {"S", FILSOCK}, {"a", BAND}, 167 {"o", BOR}, 168 }, 169 ops2[] = 170 { 171 {"==", STREQ}, 172 {"!=", STRNE}, 173 }, 174 opsm2[] = { 175 {"eq", INTEQ}, 176 {"ne", INTNE}, 177 {"ge", INTGE}, 178 {"gt", INTGT}, 179 {"le", INTLE}, 180 {"lt", INTLT}, 181 {"nt", FILNT}, 182 {"ot", FILOT}, 183 {"ef", FILEQ}, 184 }; 185 186 static int nargc; 187 static char **t_wp; 188 static int parenlevel; 189 190 static int aexpr(enum token); 191 static int binop(enum token); 192 static int equalf(const char *, const char *); 193 static int filstat(char *, enum token); 194 static int getn(const char *); 195 static intmax_t getq(const char *); 196 static int intcmp(const char *, const char *); 197 static int isunopoperand(void); 198 static int islparenoperand(void); 199 static int isrparenoperand(void); 200 static int newerf(const char *, const char *); 201 static int nexpr(enum token); 202 static int oexpr(enum token); 203 static int olderf(const char *, const char *); 204 static int primary(enum token); 205 static void syntax(const char *, const char *); 206 static enum token t_lex(char *); 207 208 int 209 main(int argc, char **argv) 210 { 211 int res; 212 char *p; 213 214 if ((p = strrchr(argv[0], '/')) == NULL) 215 p = argv[0]; 216 else 217 p++; 218 if (strcmp(p, "[") == 0) { 219 if (strcmp(argv[--argc], "]") != 0) 220 error("missing ']'"); 221 argv[argc] = NULL; 222 } 223 224 /* no expression => false */ 225 if (--argc <= 0) 226 return 1; 227 228 #ifndef SHELL 229 (void)setlocale(LC_CTYPE, ""); 230 #endif 231 nargc = argc; 232 t_wp = &argv[1]; 233 parenlevel = 0; 234 if (nargc == 4 && strcmp(*t_wp, "!") == 0) { 235 /* things like ! "" -o x do not fit in the normal grammar */ 236 --nargc; 237 ++t_wp; 238 res = oexpr(t_lex(*t_wp)); 239 } else 240 res = !oexpr(t_lex(*t_wp)); 241 242 if (--nargc > 0) 243 syntax(*t_wp, "unexpected operator"); 244 245 return res; 246 } 247 248 static void 249 syntax(const char *op, const char *msg) 250 { 251 if (op && *op) 252 error("%s: %s", op, msg); 253 else 254 error("%s", msg); 255 } 256 257 static int 258 oexpr(enum token n) 259 { 260 int res; 261 262 res = aexpr(n); 263 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BOR) 264 return oexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) || res; 265 t_wp--; 266 nargc++; 267 return res; 268 } 269 270 static int 271 aexpr(enum token n) 272 { 273 int res; 274 275 res = nexpr(n); 276 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) == BAND) 277 return aexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) && res; 278 t_wp--; 279 nargc++; 280 return res; 281 } 282 283 static int 284 nexpr(enum token n) 285 { 286 if (n == UNOT) 287 return !nexpr(t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)); 288 return primary(n); 289 } 290 291 static int 292 primary(enum token n) 293 { 294 enum token nn; 295 int res; 296 297 if (n == EOI) 298 return 0; /* missing expression */ 299 if (n == LPAREN) { 300 parenlevel++; 301 if ((nn = t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL)) == RPAREN) { 302 parenlevel--; 303 return 0; /* missing expression */ 304 } 305 res = oexpr(nn); 306 if (t_lex(nargc > 0 ? (--nargc, *++t_wp) : NULL) != RPAREN) 307 syntax(NULL, "closing paren expected"); 308 parenlevel--; 309 return res; 310 } 311 if (TOKEN_TYPE(n) == UNOP) { 312 /* unary expression */ 313 if (--nargc == 0) 314 syntax(NULL, "argument expected"); /* impossible */ 315 switch (n) { 316 case STREZ: 317 return strlen(*++t_wp) == 0; 318 case STRNZ: 319 return strlen(*++t_wp) != 0; 320 case FILTT: 321 return isatty(getn(*++t_wp)); 322 default: 323 return filstat(*++t_wp, n); 324 } 325 } 326 327 nn = t_lex(nargc > 0 ? t_wp[1] : NULL); 328 if (TOKEN_TYPE(nn) == BINOP) 329 return binop(nn); 330 331 return strlen(*t_wp) > 0; 332 } 333 334 static int 335 binop(enum token n) 336 { 337 const char *opnd1, *op, *opnd2; 338 339 opnd1 = *t_wp; 340 op = nargc > 0 ? (--nargc, *++t_wp) : NULL; 341 342 if ((opnd2 = nargc > 0 ? (--nargc, *++t_wp) : NULL) == NULL) 343 syntax(op, "argument expected"); 344 345 switch (n) { 346 case STREQ: 347 return strcmp(opnd1, opnd2) == 0; 348 case STRNE: 349 return strcmp(opnd1, opnd2) != 0; 350 case STRLT: 351 return strcmp(opnd1, opnd2) < 0; 352 case STRGT: 353 return strcmp(opnd1, opnd2) > 0; 354 case INTEQ: 355 return intcmp(opnd1, opnd2) == 0; 356 case INTNE: 357 return intcmp(opnd1, opnd2) != 0; 358 case INTGE: 359 return intcmp(opnd1, opnd2) >= 0; 360 case INTGT: 361 return intcmp(opnd1, opnd2) > 0; 362 case INTLE: 363 return intcmp(opnd1, opnd2) <= 0; 364 case INTLT: 365 return intcmp(opnd1, opnd2) < 0; 366 case FILNT: 367 return newerf(opnd1, opnd2); 368 case FILOT: 369 return olderf(opnd1, opnd2); 370 case FILEQ: 371 return equalf(opnd1, opnd2); 372 default: 373 abort(); 374 /* NOTREACHED */ 375 } 376 } 377 378 static int 379 filstat(char *nm, enum token mode) 380 { 381 struct stat s; 382 383 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s)) 384 return 0; 385 386 switch (mode) { 387 case FILRD: 388 return (eaccess(nm, R_OK) == 0); 389 case FILWR: 390 return (eaccess(nm, W_OK) == 0); 391 case FILEX: 392 /* XXX work around eaccess(2) false positives for superuser */ 393 if (eaccess(nm, X_OK) != 0) 394 return 0; 395 if (S_ISDIR(s.st_mode) || geteuid() != 0) 396 return 1; 397 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0; 398 case FILEXIST: 399 return (eaccess(nm, F_OK) == 0); 400 case FILREG: 401 return S_ISREG(s.st_mode); 402 case FILDIR: 403 return S_ISDIR(s.st_mode); 404 case FILCDEV: 405 return S_ISCHR(s.st_mode); 406 case FILBDEV: 407 return S_ISBLK(s.st_mode); 408 case FILFIFO: 409 return S_ISFIFO(s.st_mode); 410 case FILSOCK: 411 return S_ISSOCK(s.st_mode); 412 case FILSYM: 413 return S_ISLNK(s.st_mode); 414 case FILSUID: 415 return (s.st_mode & S_ISUID) != 0; 416 case FILSGID: 417 return (s.st_mode & S_ISGID) != 0; 418 case FILSTCK: 419 return (s.st_mode & S_ISVTX) != 0; 420 case FILGZ: 421 return s.st_size > (off_t)0; 422 case FILUID: 423 return s.st_uid == geteuid(); 424 case FILGID: 425 return s.st_gid == getegid(); 426 default: 427 return 1; 428 } 429 } 430 431 static int 432 find_op_1char(const struct t_op *op, const struct t_op *end, const char *s) 433 { 434 char c; 435 436 c = s[0]; 437 while (op != end) { 438 if (c == *op->op_text) 439 return op->op_num; 440 op++; 441 } 442 return OPERAND; 443 } 444 445 static int 446 find_op_2char(const struct t_op *op, const struct t_op *end, const char *s) 447 { 448 while (op != end) { 449 if (s[0] == op->op_text[0] && s[1] == op->op_text[1]) 450 return op->op_num; 451 op++; 452 } 453 return OPERAND; 454 } 455 456 static int 457 find_op(const char *s) 458 { 459 if (s[0] == '\0') 460 return OPERAND; 461 else if (s[1] == '\0') 462 return find_op_1char(ops1, (&ops1)[1], s); 463 else if (s[2] == '\0') 464 return s[0] == '-' ? find_op_1char(opsm1, (&opsm1)[1], s + 1) 465 : find_op_2char(ops2, (&ops2)[1], s); 466 else if (s[3] == '\0') 467 return s[0] == '-' ? find_op_2char(opsm2, (&opsm2)[1], s + 1) : OPERAND; 468 else 469 return OPERAND; 470 } 471 472 static enum token 473 t_lex(char *s) 474 { 475 int num; 476 477 if (s == NULL) { 478 return EOI; 479 } 480 num = find_op(s); 481 if (((TOKEN_TYPE(num) == UNOP || TOKEN_TYPE(num) == BUNOP) && isunopoperand()) 482 || (num == LPAREN && islparenoperand()) || (num == RPAREN && isrparenoperand())) 483 return OPERAND; 484 return num; 485 } 486 487 static int 488 isunopoperand(void) 489 { 490 char *s; 491 char *t; 492 int num; 493 494 if (nargc == 1) 495 return 1; 496 s = *(t_wp + 1); 497 if (nargc == 2) 498 return parenlevel == 1 && strcmp(s, ")") == 0; 499 t = *(t_wp + 2); 500 num = find_op(s); 501 return TOKEN_TYPE(num) == BINOP && (parenlevel == 0 || t[0] != ')' || t[1] != '\0'); 502 } 503 504 static int 505 islparenoperand(void) 506 { 507 char *s; 508 int num; 509 510 if (nargc == 1) 511 return 1; 512 s = *(t_wp + 1); 513 if (nargc == 2) 514 return parenlevel == 1 && strcmp(s, ")") == 0; 515 if (nargc != 3) 516 return 0; 517 num = find_op(s); 518 return TOKEN_TYPE(num) == BINOP; 519 } 520 521 static int 522 isrparenoperand(void) 523 { 524 char *s; 525 526 if (nargc == 1) 527 return 0; 528 s = *(t_wp + 1); 529 if (nargc == 2) 530 return parenlevel == 1 && strcmp(s, ")") == 0; 531 return 0; 532 } 533 534 /* atoi with error detection */ 535 static int 536 getn(const char *s) 537 { 538 char *p; 539 long r; 540 541 errno = 0; 542 r = strtol(s, &p, 10); 543 544 if (s == p) 545 error("%s: bad number", s); 546 547 if (errno != 0) 548 error((errno == EINVAL) ? "%s: bad number" : "%s: out of range", s); 549 550 while (isspace((unsigned char)*p)) 551 p++; 552 553 if (*p) 554 error("%s: bad number", s); 555 556 return (int)r; 557 } 558 559 /* atoi with error detection and 64 bit range */ 560 static intmax_t 561 getq(const char *s) 562 { 563 char *p; 564 intmax_t r; 565 566 errno = 0; 567 r = strtoimax(s, &p, 10); 568 569 if (s == p) 570 error("%s: bad number", s); 571 572 if (errno != 0) 573 error((errno == EINVAL) ? "%s: bad number" : "%s: out of range", s); 574 575 while (isspace((unsigned char)*p)) 576 p++; 577 578 if (*p) 579 error("%s: bad number", s); 580 581 return r; 582 } 583 584 static int 585 intcmp(const char *s1, const char *s2) 586 { 587 intmax_t q1, q2; 588 589 q1 = getq(s1); 590 q2 = getq(s2); 591 592 if (q1 > q2) 593 return 1; 594 595 if (q1 < q2) 596 return -1; 597 598 return 0; 599 } 600 601 static int 602 newerf(const char *f1, const char *f2) 603 { 604 struct stat b1, b2; 605 606 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0) 607 return 0; 608 609 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec) 610 return 1; 611 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec) 612 return 0; 613 614 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec); 615 } 616 617 static int 618 olderf(const char *f1, const char *f2) 619 { 620 return (newerf(f2, f1)); 621 } 622 623 static int 624 equalf(const char *f1, const char *f2) 625 { 626 struct stat b1, b2; 627 628 return ( 629 stat(f1, &b1) == 0 && stat(f2, &b2) == 0 && b1.st_dev == b2.st_dev && b1.st_ino == b2.st_ino 630 ); 631 }