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 #ifndef lint 14 static const char rcsid[] = 15 "$FreeBSD$"; 16 #endif /* not lint */ 17 18 #include <sys/types.h> 19 #include <sys/stat.h> 20 21 #include <ctype.h> 22 #include <err.h> 23 #include <errno.h> 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 #include <unistd.h> 28 29 /* test(1) accepts the following grammar: 30 oexpr ::= aexpr | aexpr "-o" oexpr ; 31 aexpr ::= nexpr | nexpr "-a" aexpr ; 32 nexpr ::= primary | "!" primary 33 primary ::= unary-operator operand 34 | operand binary-operator operand 35 | operand 36 | "(" oexpr ")" 37 ; 38 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| 39 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"; 40 41 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| 42 "-nt"|"-ot"|"-ef"; 43 operand ::= <any legal UNIX file name> 44 */ 45 46 enum token { 47 EOI, 48 FILRD, 49 FILWR, 50 FILEX, 51 FILEXIST, 52 FILREG, 53 FILDIR, 54 FILCDEV, 55 FILBDEV, 56 FILFIFO, 57 FILSOCK, 58 FILSYM, 59 FILGZ, 60 FILTT, 61 FILSUID, 62 FILSGID, 63 FILSTCK, 64 FILNT, 65 FILOT, 66 FILEQ, 67 FILUID, 68 FILGID, 69 STREZ, 70 STRNZ, 71 STREQ, 72 STRNE, 73 STRLT, 74 STRGT, 75 INTEQ, 76 INTNE, 77 INTGE, 78 INTGT, 79 INTLE, 80 INTLT, 81 UNOT, 82 BAND, 83 BOR, 84 LPAREN, 85 RPAREN, 86 OPERAND 87 }; 88 89 enum token_types { 90 UNOP, 91 BINOP, 92 BUNOP, 93 BBINOP, 94 PAREN 95 }; 96 97 struct t_op { 98 const char *op_text; 99 short op_num, op_type; 100 } const ops [] = { 101 {"-r", FILRD, UNOP}, 102 {"-w", FILWR, UNOP}, 103 {"-x", FILEX, UNOP}, 104 {"-e", FILEXIST,UNOP}, 105 {"-f", FILREG, UNOP}, 106 {"-d", FILDIR, UNOP}, 107 {"-c", FILCDEV,UNOP}, 108 {"-b", FILBDEV,UNOP}, 109 {"-p", FILFIFO,UNOP}, 110 {"-u", FILSUID,UNOP}, 111 {"-g", FILSGID,UNOP}, 112 {"-k", FILSTCK,UNOP}, 113 {"-s", FILGZ, UNOP}, 114 {"-t", FILTT, UNOP}, 115 {"-z", STREZ, UNOP}, 116 {"-n", STRNZ, UNOP}, 117 {"-h", FILSYM, UNOP}, /* for backwards compat */ 118 {"-O", FILUID, UNOP}, 119 {"-G", FILGID, UNOP}, 120 {"-L", FILSYM, UNOP}, 121 {"-S", FILSOCK,UNOP}, 122 {"=", STREQ, BINOP}, 123 {"!=", STRNE, BINOP}, 124 {"<", STRLT, BINOP}, 125 {">", STRGT, BINOP}, 126 {"-eq", INTEQ, BINOP}, 127 {"-ne", INTNE, BINOP}, 128 {"-ge", INTGE, BINOP}, 129 {"-gt", INTGT, BINOP}, 130 {"-le", INTLE, BINOP}, 131 {"-lt", INTLT, BINOP}, 132 {"-nt", FILNT, BINOP}, 133 {"-ot", FILOT, BINOP}, 134 {"-ef", FILEQ, BINOP}, 135 {"!", UNOT, BUNOP}, 136 {"-a", BAND, BBINOP}, 137 {"-o", BOR, BBINOP}, 138 {"(", LPAREN, PAREN}, 139 {")", RPAREN, PAREN}, 140 {0, 0, 0} 141 }; 142 143 struct t_op const *t_wp_op; 144 char **t_wp; 145 146 static void syntax __P((const char *, const char *)); 147 static enum token t_lex __P((char *)); 148 static int oexpr __P((enum token)); 149 static int aexpr __P((enum token)); 150 static int nexpr __P((enum token)); 151 static int primary __P((enum token)); 152 static int binop __P((void)); 153 static int filstat __P((char *, enum token)); 154 static int isoperand __P((void)); 155 static int getn __P((const char *)); 156 static quad_t getq __P((const char *)); 157 static int intcmp __P((const char *, const char *)); 158 static int newerf __P((const char *, const char *)); 159 static int olderf __P((const char *, const char *)); 160 static int equalf __P((const char *, const char *)); 161 162 int 163 main(argc, argv) 164 int argc; 165 char **argv; 166 { 167 int res; 168 char *p; 169 170 if ((p = rindex(argv[0], '/')) == NULL) 171 p = argv[0]; 172 else 173 p++; 174 if (strcmp(p, "[") == 0) { 175 if (strcmp(argv[--argc], "]")) 176 errx(2, "missing ]"); 177 argv[argc] = NULL; 178 } 179 180 /* XXX work around the absence of an eaccess(2) syscall */ 181 (void)setgid(getegid()); 182 (void)setuid(geteuid()); 183 184 t_wp = &argv[1]; 185 res = !oexpr(t_lex(*t_wp)); 186 187 if (*t_wp != NULL && *++t_wp != NULL) 188 syntax(*t_wp, "unexpected operator"); 189 190 return res; 191 } 192 193 static void 194 syntax(op, msg) 195 const char *op; 196 const char *msg; 197 { 198 199 if (op && *op) 200 errx(2, "%s: %s", op, msg); 201 else 202 errx(2, "%s", msg); 203 } 204 205 static int 206 oexpr(n) 207 enum token n; 208 { 209 int res; 210 211 res = aexpr(n); 212 if (t_lex(*++t_wp) == BOR) 213 return oexpr(t_lex(*++t_wp)) || res; 214 t_wp--; 215 return res; 216 } 217 218 static int 219 aexpr(n) 220 enum token n; 221 { 222 int res; 223 224 res = nexpr(n); 225 if (t_lex(*++t_wp) == BAND) 226 return aexpr(t_lex(*++t_wp)) && res; 227 t_wp--; 228 return res; 229 } 230 231 static int 232 nexpr(n) 233 enum token n; /* token */ 234 { 235 if (n == UNOT) 236 return !nexpr(t_lex(*++t_wp)); 237 return primary(n); 238 } 239 240 static int 241 primary(n) 242 enum token n; 243 { 244 enum token nn; 245 int res; 246 247 if (n == EOI) 248 return 0; /* missing expression */ 249 if (n == LPAREN) { 250 if ((nn = t_lex(*++t_wp)) == RPAREN) 251 return 0; /* missing expression */ 252 res = oexpr(nn); 253 if (t_lex(*++t_wp) != RPAREN) 254 syntax(NULL, "closing paren expected"); 255 return res; 256 } 257 if (t_wp_op && t_wp_op->op_type == UNOP) { 258 /* unary expression */ 259 if (*++t_wp == NULL) 260 syntax(t_wp_op->op_text, "argument expected"); 261 switch (n) { 262 case STREZ: 263 return strlen(*t_wp) == 0; 264 case STRNZ: 265 return strlen(*t_wp) != 0; 266 case FILTT: 267 return isatty(getn(*t_wp)); 268 default: 269 return filstat(*t_wp, n); 270 } 271 } 272 273 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { 274 return binop(); 275 } 276 277 return strlen(*t_wp) > 0; 278 } 279 280 static int 281 binop() 282 { 283 const char *opnd1, *opnd2; 284 struct t_op const *op; 285 286 opnd1 = *t_wp; 287 (void) t_lex(*++t_wp); 288 op = t_wp_op; 289 290 if ((opnd2 = *++t_wp) == NULL) 291 syntax(op->op_text, "argument expected"); 292 293 switch (op->op_num) { 294 case STREQ: 295 return strcmp(opnd1, opnd2) == 0; 296 case STRNE: 297 return strcmp(opnd1, opnd2) != 0; 298 case STRLT: 299 return strcmp(opnd1, opnd2) < 0; 300 case STRGT: 301 return strcmp(opnd1, opnd2) > 0; 302 case INTEQ: 303 return intcmp(opnd1, opnd2) == 0; 304 case INTNE: 305 return intcmp(opnd1, opnd2) != 0; 306 case INTGE: 307 return intcmp(opnd1, opnd2) >= 0; 308 case INTGT: 309 return intcmp(opnd1, opnd2) > 0; 310 case INTLE: 311 return intcmp(opnd1, opnd2) <= 0; 312 case INTLT: 313 return intcmp(opnd1, opnd2) < 0; 314 case FILNT: 315 return newerf (opnd1, opnd2); 316 case FILOT: 317 return olderf (opnd1, opnd2); 318 case FILEQ: 319 return equalf (opnd1, opnd2); 320 default: 321 abort(); 322 /* NOTREACHED */ 323 } 324 } 325 326 static int 327 filstat(nm, mode) 328 char *nm; 329 enum token mode; 330 { 331 struct stat s; 332 333 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s)) 334 return 0; 335 336 switch (mode) { 337 case FILRD: 338 return access(nm, R_OK) == 0; 339 case FILWR: 340 return access(nm, W_OK) == 0; 341 case FILEX: 342 /* XXX work around access(2) false positives for superuser */ 343 if (access(nm, X_OK) != 0) 344 return 0; 345 if (S_ISDIR(s.st_mode) || getuid() != 0) 346 return 1; 347 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0; 348 case FILEXIST: 349 return access(nm, F_OK) == 0; 350 case FILREG: 351 return S_ISREG(s.st_mode); 352 case FILDIR: 353 return S_ISDIR(s.st_mode); 354 case FILCDEV: 355 return S_ISCHR(s.st_mode); 356 case FILBDEV: 357 return S_ISBLK(s.st_mode); 358 case FILFIFO: 359 return S_ISFIFO(s.st_mode); 360 case FILSOCK: 361 return S_ISSOCK(s.st_mode); 362 case FILSYM: 363 return S_ISLNK(s.st_mode); 364 case FILSUID: 365 return (s.st_mode & S_ISUID) != 0; 366 case FILSGID: 367 return (s.st_mode & S_ISGID) != 0; 368 case FILSTCK: 369 return (s.st_mode & S_ISVTX) != 0; 370 case FILGZ: 371 return s.st_size > (off_t)0; 372 case FILUID: 373 return s.st_uid == geteuid(); 374 case FILGID: 375 return s.st_gid == getegid(); 376 default: 377 return 1; 378 } 379 } 380 381 static enum token 382 t_lex(s) 383 char *s; 384 { 385 struct t_op const *op = ops; 386 387 if (s == 0) { 388 t_wp_op = NULL; 389 return EOI; 390 } 391 while (op->op_text) { 392 if (strcmp(s, op->op_text) == 0) { 393 if ((op->op_type == UNOP && isoperand()) || 394 (op->op_num == LPAREN && *(t_wp+1) == 0)) 395 break; 396 t_wp_op = op; 397 return op->op_num; 398 } 399 op++; 400 } 401 t_wp_op = NULL; 402 return OPERAND; 403 } 404 405 static int 406 isoperand() 407 { 408 struct t_op const *op = ops; 409 char *s; 410 char *t; 411 412 if ((s = *(t_wp+1)) == 0) 413 return 1; 414 if ((t = *(t_wp+2)) == 0) 415 return 0; 416 while (op->op_text) { 417 if (strcmp(s, op->op_text) == 0) 418 return op->op_type == BINOP && 419 (t[0] != ')' || t[1] != '\0'); 420 op++; 421 } 422 return 0; 423 } 424 425 /* atoi with error detection */ 426 static int 427 getn(s) 428 const char *s; 429 { 430 char *p; 431 long r; 432 433 errno = 0; 434 r = strtol(s, &p, 10); 435 436 if (errno != 0) 437 errx(2, "%s: out of range", s); 438 439 while (isspace((unsigned char)*p)) 440 p++; 441 442 if (*p) 443 errx(2, "%s: bad number", s); 444 445 return (int) r; 446 } 447 448 /* atoi with error detection and 64 bit range */ 449 static quad_t 450 getq(s) 451 const char *s; 452 { 453 char *p; 454 quad_t r; 455 456 errno = 0; 457 r = strtoq(s, &p, 10); 458 459 if (errno != 0) 460 errx(2, "%s: out of range", s); 461 462 while (isspace((unsigned char)*p)) 463 p++; 464 465 if (*p) 466 errx(2, "%s: bad number", s); 467 468 return r; 469 } 470 471 static int 472 intcmp (s1, s2) 473 const char *s1, *s2; 474 { 475 quad_t q1, q2; 476 477 478 q1 = getq(s1); 479 q2 = getq(s2); 480 481 if (q1 > q2) 482 return 1; 483 484 if (q1 < q2) 485 return -1; 486 487 return 0; 488 } 489 490 static int 491 newerf (f1, f2) 492 const char *f1, *f2; 493 { 494 struct stat b1, b2; 495 496 return (stat (f1, &b1) == 0 && 497 stat (f2, &b2) == 0 && 498 b1.st_mtime > b2.st_mtime); 499 } 500 501 static int 502 olderf (f1, f2) 503 const char *f1, *f2; 504 { 505 struct stat b1, b2; 506 507 return (stat (f1, &b1) == 0 && 508 stat (f2, &b2) == 0 && 509 b1.st_mtime < b2.st_mtime); 510 } 511 512 static int 513 equalf (f1, f2) 514 const char *f1, *f2; 515 { 516 struct stat b1, b2; 517 518 return (stat (f1, &b1) == 0 && 519 stat (f2, &b2) == 0 && 520 b1.st_dev == b2.st_dev && 521 b1.st_ino == b2.st_ino); 522 } 523