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