1 /* $NetBSD: c_test.c,v 1.9 2017/06/30 04:41:19 kamil 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 Michael Rendell to add Korn's [[ .. ]] expressions. 9 * modified by J.T. Conklin to add POSIX compatibility. 10 */ 11 #include <sys/cdefs.h> 12 13 #ifndef lint 14 __RCSID("$NetBSD: c_test.c,v 1.9 2017/06/30 04:41:19 kamil Exp $"); 15 #endif 16 17 #include <sys/stat.h> 18 19 #include "sh.h" 20 #include "c_test.h" 21 22 /* test(1) accepts the following grammar: 23 oexpr ::= aexpr | aexpr "-o" oexpr ; 24 aexpr ::= nexpr | nexpr "-a" aexpr ; 25 nexpr ::= primary | "!" nexpr ; 26 primary ::= unary-operator operand 27 | operand binary-operator operand 28 | operand 29 | "(" oexpr ")" 30 ; 31 32 unary-operator ::= "-a"|"-r"|"-w"|"-x"|"-e"|"-f"|"-d"|"-c"|"-b"|"-p"| 33 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"| 34 "-L"|"-h"|"-S"|"-H"; 35 36 binary-operator ::= "="|"=="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| 37 "-nt"|"-ot"|"-ef"| 38 "<"|">" # rules used for [[ .. ]] expressions 39 ; 40 operand ::= <any thing> 41 */ 42 43 #define T_ERR_EXIT 2 /* POSIX says > 1 for errors */ 44 45 struct t_op { 46 char op_text[4]; 47 Test_op op_num; 48 }; 49 static const struct t_op u_ops [] = { 50 {"-a", TO_FILAXST }, 51 {"-b", TO_FILBDEV }, 52 {"-c", TO_FILCDEV }, 53 {"-d", TO_FILID }, 54 {"-e", TO_FILEXST }, 55 {"-f", TO_FILREG }, 56 {"-G", TO_FILGID }, 57 {"-g", TO_FILSETG }, 58 {"-h", TO_FILSYM }, 59 {"-H", TO_FILCDF }, 60 {"-k", TO_FILSTCK }, 61 {"-L", TO_FILSYM }, 62 {"-n", TO_STNZE }, 63 {"-O", TO_FILUID }, 64 {"-o", TO_OPTION }, 65 {"-p", TO_FILFIFO }, 66 {"-r", TO_FILRD }, 67 {"-s", TO_FILGZ }, 68 {"-S", TO_FILSOCK }, 69 {"-t", TO_FILTT }, 70 {"-u", TO_FILSETU }, 71 {"-w", TO_FILWR }, 72 {"-x", TO_FILEX }, 73 {"-z", TO_STZER }, 74 {"", TO_NONOP } 75 }; 76 static const struct t_op b_ops [] = { 77 {"=", TO_STEQL }, 78 #ifdef KSH 79 {"==", TO_STEQL }, 80 #endif /* KSH */ 81 {"!=", TO_STNEQ }, 82 {"<", TO_STLT }, 83 {">", TO_STGT }, 84 {"-eq", TO_INTEQ }, 85 {"-ne", TO_INTNE }, 86 {"-gt", TO_INTGT }, 87 {"-ge", TO_INTGE }, 88 {"-lt", TO_INTLT }, 89 {"-le", TO_INTLE }, 90 {"-ef", TO_FILEQ }, 91 {"-nt", TO_FILNT }, 92 {"-ot", TO_FILOT }, 93 {"", TO_NONOP } 94 }; 95 96 static int test_stat ARGS((const char *, struct stat *)); 97 static int test_eaccess ARGS((const char *, int)); 98 static int test_oexpr ARGS((Test_env *, int)); 99 static int test_aexpr ARGS((Test_env *, int)); 100 static int test_nexpr ARGS((Test_env *, int)); 101 static int test_primary ARGS((Test_env *, int)); 102 static int ptest_isa ARGS((Test_env *, Test_meta)); 103 static const char *ptest_getopnd ARGS((Test_env *, Test_op, int)); 104 static int ptest_eval ARGS((Test_env *, Test_op, const char *, 105 const char *, int)); 106 static void ptest_error ARGS((Test_env *, int, const char *)); 107 108 int 109 c_test(wp) 110 char **wp; 111 { 112 int argc; 113 int res; 114 Test_env te; 115 116 te.flags = 0; 117 te.isa = ptest_isa; 118 te.getopnd = ptest_getopnd; 119 te.eval = ptest_eval; 120 te.error = ptest_error; 121 122 for (argc = 0; wp[argc]; argc++) 123 ; 124 125 if (strcmp(wp[0], "[") == 0) { 126 if (strcmp(wp[--argc], "]") != 0) { 127 bi_errorf("missing ]"); 128 return T_ERR_EXIT; 129 } 130 } 131 132 te.pos.wp = wp + 1; 133 te.wp_end = wp + argc; 134 135 /* 136 * Handle the special cases from POSIX.2, section 4.62.4. 137 * Implementation of all the rules isn't necessary since 138 * our parser does the right thing for the omitted steps. 139 */ 140 if (argc <= 5) { 141 char **owp = wp; 142 int invert = 0; 143 Test_op op; 144 const char *opnd1, *opnd2; 145 146 while (--argc >= 0) { 147 if ((*te.isa)(&te, TM_END)) 148 return !0; 149 if (argc == 3) { 150 opnd1 = (*te.getopnd)(&te, TO_NONOP, 1); 151 if ((op = (Test_op) (*te.isa)(&te, TM_BINOP))) { 152 opnd2 = (*te.getopnd)(&te, op, 1); 153 res = (*te.eval)(&te, op, opnd1, opnd2, 154 1); 155 if (te.flags & TEF_ERROR) 156 return T_ERR_EXIT; 157 if (invert & 1) 158 res = !res; 159 return !res; 160 } 161 /* back up to opnd1 */ 162 te.pos.wp--; 163 } 164 if (argc == 1) { 165 opnd1 = (*te.getopnd)(&te, TO_NONOP, 1); 166 /* Historically, -t by itself test if fd 1 167 * is a file descriptor, but POSIX says its 168 * a string test... 169 */ 170 if (!Flag(FPOSIX) && strcmp(opnd1, "-t") == 0) 171 break; 172 res = (*te.eval)(&te, TO_STNZE, opnd1, 173 (char *) 0, 1); 174 if (invert & 1) 175 res = !res; 176 return !res; 177 } 178 if ((*te.isa)(&te, TM_NOT)) { 179 invert++; 180 } else 181 break; 182 } 183 te.pos.wp = owp + 1; 184 } 185 186 return test_parse(&te); 187 } 188 189 /* 190 * Generic test routines. 191 */ 192 193 Test_op 194 test_isop(te, meta, s) 195 Test_env *te; 196 Test_meta meta; 197 const char *s; 198 { 199 char sc1; 200 const struct t_op *otab; 201 202 otab = meta == TM_UNOP ? u_ops : b_ops; 203 if (*s) { 204 sc1 = s[1]; 205 for (; otab->op_text[0]; otab++) 206 if (sc1 == otab->op_text[1] 207 && strcmp(s, otab->op_text) == 0 208 && ((te->flags & TEF_DBRACKET) 209 || (otab->op_num != TO_STLT 210 && otab->op_num != TO_STGT))) 211 return otab->op_num; 212 } 213 return TO_NONOP; 214 } 215 216 int 217 test_eval(te, op, opnd1, opnd2, do_eval) 218 Test_env *te; 219 Test_op op; 220 const char *opnd1; 221 const char *opnd2; 222 int do_eval; 223 { 224 int res; 225 int not; 226 struct stat b1, b2; 227 228 if (!do_eval) 229 return 0; 230 231 switch ((int) op) { 232 /* 233 * Unary Operators 234 */ 235 case TO_STNZE: /* -n */ 236 return *opnd1 != '\0'; 237 case TO_STZER: /* -z */ 238 return *opnd1 == '\0'; 239 case TO_OPTION: /* -o */ 240 if ((not = *opnd1 == '!')) 241 opnd1++; 242 if ((res = option(opnd1)) < 0) 243 res = 0; 244 else { 245 res = Flag(res); 246 if (not) 247 res = !res; 248 } 249 return res; 250 case TO_FILRD: /* -r */ 251 return test_eaccess(opnd1, R_OK) == 0; 252 case TO_FILWR: /* -w */ 253 return test_eaccess(opnd1, W_OK) == 0; 254 case TO_FILEX: /* -x */ 255 return test_eaccess(opnd1, X_OK) == 0; 256 case TO_FILAXST: /* -a */ 257 return test_stat(opnd1, &b1) == 0; 258 case TO_FILEXST: /* -e */ 259 /* at&t ksh does not appear to do the /dev/fd/ thing for 260 * this (unless the os itself handles it) 261 */ 262 return stat(opnd1, &b1) == 0; 263 case TO_FILREG: /* -r */ 264 return test_stat(opnd1, &b1) == 0 && S_ISREG(b1.st_mode); 265 case TO_FILID: /* -d */ 266 return test_stat(opnd1, &b1) == 0 && S_ISDIR(b1.st_mode); 267 case TO_FILCDEV: /* -c */ 268 #ifdef S_ISCHR 269 return test_stat(opnd1, &b1) == 0 && S_ISCHR(b1.st_mode); 270 #else 271 return 0; 272 #endif 273 case TO_FILBDEV: /* -b */ 274 #ifdef S_ISBLK 275 return test_stat(opnd1, &b1) == 0 && S_ISBLK(b1.st_mode); 276 #else 277 return 0; 278 #endif 279 case TO_FILFIFO: /* -p */ 280 #ifdef S_ISFIFO 281 return test_stat(opnd1, &b1) == 0 && S_ISFIFO(b1.st_mode); 282 #else 283 return 0; 284 #endif 285 case TO_FILSYM: /* -h -L */ 286 #ifdef S_ISLNK 287 return lstat(opnd1, &b1) == 0 && S_ISLNK(b1.st_mode); 288 #else 289 return 0; 290 #endif 291 case TO_FILSOCK: /* -S */ 292 #ifdef S_ISSOCK 293 return test_stat(opnd1, &b1) == 0 && S_ISSOCK(b1.st_mode); 294 #else 295 return 0; 296 #endif 297 case TO_FILCDF:/* -H HP context dependent files (directories) */ 298 #ifdef S_ISCDF 299 { 300 /* Append a + to filename and check to see if result is a 301 * setuid directory. CDF stuff in general is hookey, since 302 * it breaks for the following sequence: echo hi > foo+; 303 * mkdir foo; echo bye > foo/default; chmod u+s foo 304 * (foo+ refers to the file with hi in it, there is no way 305 * to get at the file with bye in it - please correct me if 306 * I'm wrong about this). 307 */ 308 int len = strlen(opnd1); 309 char *p = str_nsave(opnd1, len + 1, ATEMP); 310 311 p[len++] = '+'; 312 p[len] = '\0'; 313 return stat(p, &b1) == 0 && S_ISCDF(b1.st_mode); 314 } 315 #else 316 return 0; 317 #endif 318 case TO_FILSETU: /* -u */ 319 #ifdef S_ISUID 320 return test_stat(opnd1, &b1) == 0 321 && (b1.st_mode & S_ISUID) == S_ISUID; 322 #else 323 return 0; 324 #endif 325 case TO_FILSETG: /* -g */ 326 #ifdef S_ISGID 327 return test_stat(opnd1, &b1) == 0 328 && (b1.st_mode & S_ISGID) == S_ISGID; 329 #else 330 return 0; 331 #endif 332 case TO_FILSTCK: /* -k */ 333 return test_stat(opnd1, &b1) == 0 334 && (b1.st_mode & S_ISVTX) == S_ISVTX; 335 case TO_FILGZ: /* -s */ 336 return test_stat(opnd1, &b1) == 0 && b1.st_size > 0L; 337 case TO_FILTT: /* -t */ 338 if (opnd1 && !bi_getn(opnd1, &res)) { 339 te->flags |= TEF_ERROR; 340 res = 0; 341 } else { 342 /* generate error if in FPOSIX mode? */ 343 res = isatty(opnd1 ? res : 0); 344 } 345 return res; 346 case TO_FILUID: /* -O */ 347 return test_stat(opnd1, &b1) == 0 && b1.st_uid == ksheuid; 348 case TO_FILGID: /* -G */ 349 return test_stat(opnd1, &b1) == 0 && b1.st_gid == getegid(); 350 /* 351 * Binary Operators 352 */ 353 case TO_STEQL: /* = */ 354 if (te->flags & TEF_DBRACKET) 355 return gmatch(opnd1, opnd2, false); 356 return strcmp(opnd1, opnd2) == 0; 357 case TO_STNEQ: /* != */ 358 if (te->flags & TEF_DBRACKET) 359 return !gmatch(opnd1, opnd2, false); 360 return strcmp(opnd1, opnd2) != 0; 361 case TO_STLT: /* < */ 362 return strcmp(opnd1, opnd2) < 0; 363 case TO_STGT: /* > */ 364 return strcmp(opnd1, opnd2) > 0; 365 case TO_INTEQ: /* -eq */ 366 case TO_INTNE: /* -ne */ 367 case TO_INTGE: /* -ge */ 368 case TO_INTGT: /* -gt */ 369 case TO_INTLE: /* -le */ 370 case TO_INTLT: /* -lt */ 371 { 372 long v1, v2; 373 374 if (!evaluate(opnd1, &v1, KSH_RETURN_ERROR) 375 || !evaluate(opnd2, &v2, KSH_RETURN_ERROR)) 376 { 377 /* error already printed.. */ 378 te->flags |= TEF_ERROR; 379 return 1; 380 } 381 switch ((int) op) { 382 case TO_INTEQ: 383 return v1 == v2; 384 case TO_INTNE: 385 return v1 != v2; 386 case TO_INTGE: 387 return v1 >= v2; 388 case TO_INTGT: 389 return v1 > v2; 390 case TO_INTLE: 391 return v1 <= v2; 392 case TO_INTLT: 393 return v1 < v2; 394 } 395 } 396 case TO_FILNT: /* -nt */ 397 { 398 int s2; 399 /* ksh88/ksh93 succeed if file2 can't be stated 400 * (subtly different from `does not exist'). 401 */ 402 return stat(opnd1, &b1) == 0 403 && (((s2 = stat(opnd2, &b2)) == 0 404 && b1.st_mtime > b2.st_mtime) || s2 < 0); 405 } 406 case TO_FILOT: /* -ot */ 407 { 408 int s1; 409 /* ksh88/ksh93 succeed if file1 can't be stated 410 * (subtly different from `does not exist'). 411 */ 412 return stat(opnd2, &b2) == 0 413 && (((s1 = stat(opnd1, &b1)) == 0 414 && b1.st_mtime < b2.st_mtime) || s1 < 0); 415 } 416 case TO_FILEQ: /* -ef */ 417 return stat (opnd1, &b1) == 0 && stat (opnd2, &b2) == 0 418 && b1.st_dev == b2.st_dev 419 && b1.st_ino == b2.st_ino; 420 } 421 (*te->error)(te, 0, "internal error: unknown op"); 422 return 1; 423 } 424 425 static int 426 test_stat(pathx, statb) 427 const char *pathx; 428 struct stat *statb; 429 { 430 return stat(pathx, statb); 431 } 432 433 /* Routine to handle Korn's /dev/fd hack, and to deal with X_OK on 434 * non-directories when running as root. 435 */ 436 static int 437 test_eaccess(pathx, mode) 438 const char *pathx; 439 int mode; 440 { 441 int res; 442 443 res = eaccess(pathx, mode); 444 /* 445 * On most (all?) unixes, access() says everything is executable for 446 * root - avoid this on files by using stat(). 447 */ 448 if (res == 0 && ksheuid == 0 && (mode & X_OK)) { 449 struct stat statb; 450 451 if (stat(pathx, &statb) < 0) 452 res = -1; 453 else if (S_ISDIR(statb.st_mode)) 454 res = 0; 455 else 456 res = (statb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) 457 ? 0 : -1; 458 } 459 460 return res; 461 } 462 463 int 464 test_parse(te) 465 Test_env *te; 466 { 467 int res; 468 469 res = test_oexpr(te, 1); 470 471 if (!(te->flags & TEF_ERROR) && !(*te->isa)(te, TM_END)) 472 (*te->error)(te, 0, "unexpected operator/operand"); 473 474 return (te->flags & TEF_ERROR) ? T_ERR_EXIT : !res; 475 } 476 477 static int 478 test_oexpr(te, do_eval) 479 Test_env *te; 480 int do_eval; 481 { 482 int res; 483 484 res = test_aexpr(te, do_eval); 485 if (res) 486 do_eval = 0; 487 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_OR)) 488 return test_oexpr(te, do_eval) || res; 489 return res; 490 } 491 492 static int 493 test_aexpr(te, do_eval) 494 Test_env *te; 495 int do_eval; 496 { 497 int res; 498 499 res = test_nexpr(te, do_eval); 500 if (!res) 501 do_eval = 0; 502 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_AND)) 503 return test_aexpr(te, do_eval) && res; 504 return res; 505 } 506 507 static int 508 test_nexpr(te, do_eval) 509 Test_env *te; 510 int do_eval; 511 { 512 if (!(te->flags & TEF_ERROR) && (*te->isa)(te, TM_NOT)) 513 return !test_nexpr(te, do_eval); 514 return test_primary(te, do_eval); 515 } 516 517 static int 518 test_primary(te, do_eval) 519 Test_env *te; 520 int do_eval; 521 { 522 const char *opnd1, *opnd2; 523 int res; 524 Test_op op; 525 526 if (te->flags & TEF_ERROR) 527 return 0; 528 if ((*te->isa)(te, TM_OPAREN)) { 529 res = test_oexpr(te, do_eval); 530 if (te->flags & TEF_ERROR) 531 return 0; 532 if (!(*te->isa)(te, TM_CPAREN)) { 533 (*te->error)(te, 0, "missing closing paren"); 534 return 0; 535 } 536 return res; 537 } 538 if ((op = (Test_op) (*te->isa)(te, TM_UNOP))) { 539 /* unary expression */ 540 opnd1 = (*te->getopnd)(te, op, do_eval); 541 if (!opnd1) { 542 (*te->error)(te, -1, "missing argument"); 543 return 0; 544 } 545 546 return (*te->eval)(te, op, opnd1, (const char *) 0, do_eval); 547 } 548 opnd1 = (*te->getopnd)(te, TO_NONOP, do_eval); 549 if (!opnd1) { 550 (*te->error)(te, 0, "expression expected"); 551 return 0; 552 } 553 if ((op = (Test_op) (*te->isa)(te, TM_BINOP))) { 554 /* binary expression */ 555 opnd2 = (*te->getopnd)(te, op, do_eval); 556 if (!opnd2) { 557 (*te->error)(te, -1, "missing second argument"); 558 return 0; 559 } 560 561 return (*te->eval)(te, op, opnd1, opnd2, do_eval); 562 } 563 if (te->flags & TEF_DBRACKET) { 564 (*te->error)(te, -1, "missing expression operator"); 565 return 0; 566 } 567 return (*te->eval)(te, TO_STNZE, opnd1, (const char *) 0, do_eval); 568 } 569 570 /* 571 * Plain test (test and [ .. ]) specific routines. 572 */ 573 574 /* Test if the current token is a whatever. Accepts the current token if 575 * it is. Returns 0 if it is not, non-zero if it is (in the case of 576 * TM_UNOP and TM_BINOP, the returned value is a Test_op). 577 */ 578 static int 579 ptest_isa(te, meta) 580 Test_env *te; 581 Test_meta meta; 582 { 583 /* Order important - indexed by Test_meta values */ 584 static const char *const tokens[] = { 585 "-o", "-a", "!", "(", ")" 586 }; 587 int ret; 588 589 if (te->pos.wp >= te->wp_end) 590 return meta == TM_END; 591 592 if (meta == TM_UNOP || meta == TM_BINOP) 593 ret = (int) test_isop(te, meta, *te->pos.wp); 594 else if (meta == TM_END) 595 ret = 0; 596 else 597 ret = strcmp(*te->pos.wp, tokens[(int) meta]) == 0; 598 599 /* Accept the token? */ 600 if (ret) 601 te->pos.wp++; 602 603 return ret; 604 } 605 606 static const char * 607 ptest_getopnd(te, op, do_eval) 608 Test_env *te; 609 Test_op op; 610 int do_eval; 611 { 612 if (te->pos.wp >= te->wp_end) 613 return op == TO_FILTT ? "1" : (const char *) 0; 614 return *te->pos.wp++; 615 } 616 617 static int 618 ptest_eval(te, op, opnd1, opnd2, do_eval) 619 Test_env *te; 620 Test_op op; 621 const char *opnd1; 622 const char *opnd2; 623 int do_eval; 624 { 625 return test_eval(te, op, opnd1, opnd2, do_eval); 626 } 627 628 static void 629 ptest_error(te, offset, msg) 630 Test_env *te; 631 int offset; 632 const char *msg; 633 { 634 const char *op = te->pos.wp + offset >= te->wp_end ? 635 (const char *) 0 : te->pos.wp[offset]; 636 637 te->flags |= TEF_ERROR; 638 if (op) 639 bi_errorf("%s: %s", op, msg); 640 else 641 bi_errorf("%s", msg); 642 } 643