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 int newerf __P((const char *, const char *)); 157 static int olderf __P((const char *, const char *)); 158 static int equalf __P((const char *, const char *)); 159 160 int 161 main(argc, argv) 162 int argc; 163 char **argv; 164 { 165 int res; 166 167 if (strcmp(argv[0], "[") == 0) { 168 if (strcmp(argv[--argc], "]")) 169 errx(2, "missing ]"); 170 argv[argc] = NULL; 171 } 172 173 /* XXX work around the absence of an eaccess(2) syscall */ 174 (void)setgid(getegid()); 175 (void)setuid(geteuid()); 176 177 t_wp = &argv[1]; 178 res = !oexpr(t_lex(*t_wp)); 179 180 if (*t_wp != NULL && *++t_wp != NULL) 181 syntax(*t_wp, "unexpected operator"); 182 183 return res; 184 } 185 186 static void 187 syntax(op, msg) 188 const char *op; 189 const char *msg; 190 { 191 192 if (op && *op) 193 errx(2, "%s: %s", op, msg); 194 else 195 errx(2, "%s", msg); 196 } 197 198 static int 199 oexpr(n) 200 enum token n; 201 { 202 int res; 203 204 res = aexpr(n); 205 if (t_lex(*++t_wp) == BOR) 206 return oexpr(t_lex(*++t_wp)) || res; 207 t_wp--; 208 return res; 209 } 210 211 static int 212 aexpr(n) 213 enum token n; 214 { 215 int res; 216 217 res = nexpr(n); 218 if (t_lex(*++t_wp) == BAND) 219 return aexpr(t_lex(*++t_wp)) && res; 220 t_wp--; 221 return res; 222 } 223 224 static int 225 nexpr(n) 226 enum token n; /* token */ 227 { 228 if (n == UNOT) 229 return !nexpr(t_lex(*++t_wp)); 230 return primary(n); 231 } 232 233 static int 234 primary(n) 235 enum token n; 236 { 237 enum token nn; 238 int res; 239 240 if (n == EOI) 241 return 0; /* missing expression */ 242 if (n == LPAREN) { 243 if ((nn = t_lex(*++t_wp)) == RPAREN) 244 return 0; /* missing expression */ 245 res = oexpr(nn); 246 if (t_lex(*++t_wp) != RPAREN) 247 syntax(NULL, "closing paren expected"); 248 return res; 249 } 250 if (t_wp_op && t_wp_op->op_type == UNOP) { 251 /* unary expression */ 252 if (*++t_wp == NULL) 253 syntax(t_wp_op->op_text, "argument expected"); 254 switch (n) { 255 case STREZ: 256 return strlen(*t_wp) == 0; 257 case STRNZ: 258 return strlen(*t_wp) != 0; 259 case FILTT: 260 return isatty(getn(*t_wp)); 261 default: 262 return filstat(*t_wp, n); 263 } 264 } 265 266 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { 267 return binop(); 268 } 269 270 return strlen(*t_wp) > 0; 271 } 272 273 static int 274 binop() 275 { 276 const char *opnd1, *opnd2; 277 struct t_op const *op; 278 279 opnd1 = *t_wp; 280 (void) t_lex(*++t_wp); 281 op = t_wp_op; 282 283 if ((opnd2 = *++t_wp) == NULL) 284 syntax(op->op_text, "argument expected"); 285 286 switch (op->op_num) { 287 case STREQ: 288 return strcmp(opnd1, opnd2) == 0; 289 case STRNE: 290 return strcmp(opnd1, opnd2) != 0; 291 case STRLT: 292 return strcmp(opnd1, opnd2) < 0; 293 case STRGT: 294 return strcmp(opnd1, opnd2) > 0; 295 case INTEQ: 296 return getn(opnd1) == getn(opnd2); 297 case INTNE: 298 return getn(opnd1) != getn(opnd2); 299 case INTGE: 300 return getn(opnd1) >= getn(opnd2); 301 case INTGT: 302 return getn(opnd1) > getn(opnd2); 303 case INTLE: 304 return getn(opnd1) <= getn(opnd2); 305 case INTLT: 306 return getn(opnd1) < getn(opnd2); 307 case FILNT: 308 return newerf (opnd1, opnd2); 309 case FILOT: 310 return olderf (opnd1, opnd2); 311 case FILEQ: 312 return equalf (opnd1, opnd2); 313 default: 314 abort(); 315 /* NOTREACHED */ 316 } 317 } 318 319 static int 320 filstat(nm, mode) 321 char *nm; 322 enum token mode; 323 { 324 struct stat s; 325 326 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s)) 327 return 0; 328 329 switch (mode) { 330 case FILRD: 331 return access(nm, R_OK) == 0; 332 case FILWR: 333 return access(nm, W_OK) == 0; 334 case FILEX: 335 /* XXX work around access(2) false positives for superuser */ 336 if (access(nm, X_OK) != 0) 337 return 0; 338 if (S_ISDIR(s.st_mode) || getuid() != 0) 339 return 1; 340 return (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0; 341 case FILEXIST: 342 return access(nm, F_OK) == 0; 343 case FILREG: 344 return S_ISREG(s.st_mode); 345 case FILDIR: 346 return S_ISDIR(s.st_mode); 347 case FILCDEV: 348 return S_ISCHR(s.st_mode); 349 case FILBDEV: 350 return S_ISBLK(s.st_mode); 351 case FILFIFO: 352 return S_ISFIFO(s.st_mode); 353 case FILSOCK: 354 return S_ISSOCK(s.st_mode); 355 case FILSYM: 356 return S_ISLNK(s.st_mode); 357 case FILSUID: 358 return (s.st_mode & S_ISUID) != 0; 359 case FILSGID: 360 return (s.st_mode & S_ISGID) != 0; 361 case FILSTCK: 362 return (s.st_mode & S_ISVTX) != 0; 363 case FILGZ: 364 return s.st_size > (off_t)0; 365 case FILUID: 366 return s.st_uid == geteuid(); 367 case FILGID: 368 return s.st_gid == getegid(); 369 default: 370 return 1; 371 } 372 } 373 374 static enum token 375 t_lex(s) 376 char *s; 377 { 378 struct t_op const *op = ops; 379 380 if (s == 0) { 381 t_wp_op = NULL; 382 return EOI; 383 } 384 while (op->op_text) { 385 if (strcmp(s, op->op_text) == 0) { 386 if ((op->op_type == UNOP && isoperand()) || 387 (op->op_num == LPAREN && *(t_wp+1) == 0)) 388 break; 389 t_wp_op = op; 390 return op->op_num; 391 } 392 op++; 393 } 394 t_wp_op = NULL; 395 return OPERAND; 396 } 397 398 static int 399 isoperand() 400 { 401 struct t_op const *op = ops; 402 char *s; 403 char *t; 404 405 if ((s = *(t_wp+1)) == 0) 406 return 1; 407 if ((t = *(t_wp+2)) == 0) 408 return 0; 409 while (op->op_text) { 410 if (strcmp(s, op->op_text) == 0) 411 return op->op_type == BINOP && 412 (t[0] != ')' || t[1] != '\0'); 413 op++; 414 } 415 return 0; 416 } 417 418 /* atoi with error detection */ 419 static int 420 getn(s) 421 const char *s; 422 { 423 char *p; 424 long r; 425 426 errno = 0; 427 r = strtol(s, &p, 10); 428 429 if (errno != 0) 430 errx(2, "%s: out of range", s); 431 432 while (isspace((unsigned char)*p)) 433 p++; 434 435 if (*p) 436 errx(2, "%s: bad number", s); 437 438 return (int) r; 439 } 440 441 static int 442 newerf (f1, f2) 443 const char *f1, *f2; 444 { 445 struct stat b1, b2; 446 447 return (stat (f1, &b1) == 0 && 448 stat (f2, &b2) == 0 && 449 b1.st_mtime > b2.st_mtime); 450 } 451 452 static int 453 olderf (f1, f2) 454 const char *f1, *f2; 455 { 456 struct stat b1, b2; 457 458 return (stat (f1, &b1) == 0 && 459 stat (f2, &b2) == 0 && 460 b1.st_mtime < b2.st_mtime); 461 } 462 463 static int 464 equalf (f1, f2) 465 const char *f1, *f2; 466 { 467 struct stat b1, b2; 468 469 return (stat (f1, &b1) == 0 && 470 stat (f2, &b2) == 0 && 471 b1.st_dev == b2.st_dev && 472 b1.st_ino == b2.st_ino); 473 } 474