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