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