test.c (247274) | test.c (251208) |
---|---|
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 * Important: This file is used both as a standalone program /bin/test and 14 * as a builtin for /bin/sh (#define SHELL). 15 */ 16 17#include <sys/cdefs.h> | 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 * Important: This file is used both as a standalone program /bin/test and 14 * as a builtin for /bin/sh (#define SHELL). 15 */ 16 17#include <sys/cdefs.h> |
18__FBSDID("$FreeBSD: head/bin/test/test.c 247274 2013-02-25 19:05:40Z peterj $"); | 18__FBSDID("$FreeBSD: head/bin/test/test.c 251208 2013-05-31 22:54:20Z jilles $"); |
19 20#include <sys/types.h> 21#include <sys/stat.h> 22 23#include <ctype.h> 24#include <err.h> 25#include <errno.h> 26#include <inttypes.h> --- 31 unchanged lines hidden (view full) --- 58 | operand binary-operator operand 59 | operand 60 | "(" oexpr ")" 61 ; 62 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| 63 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"; 64 65 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| | 19 20#include <sys/types.h> 21#include <sys/stat.h> 22 23#include <ctype.h> 24#include <err.h> 25#include <errno.h> 26#include <inttypes.h> --- 31 unchanged lines hidden (view full) --- 58 | operand binary-operator operand 59 | operand 60 | "(" oexpr ")" 61 ; 62 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| 63 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"; 64 65 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| |
66 "-nt"|"-nt[abcm][abcm]"|"-ot"|"-ot[abcm][abcm])"|"-ef"; | 66 "-nt"|"-ot"|"-ef"; |
67 operand ::= <any legal UNIX file name> 68*/ 69 70enum token { 71 EOI, 72 FILRD, 73 FILWR, 74 FILEX, --- 5 unchanged lines hidden (view full) --- 80 FILFIFO, 81 FILSOCK, 82 FILSYM, 83 FILGZ, 84 FILTT, 85 FILSUID, 86 FILSGID, 87 FILSTCK, | 67 operand ::= <any legal UNIX file name> 68*/ 69 70enum token { 71 EOI, 72 FILRD, 73 FILWR, 74 FILEX, --- 5 unchanged lines hidden (view full) --- 80 FILFIFO, 81 FILSOCK, 82 FILSYM, 83 FILGZ, 84 FILTT, 85 FILSUID, 86 FILSGID, 87 FILSTCK, |
88 FILNTAA, 89 FILNTAB, 90 FILNTAC, 91 FILNTAM, 92 FILNTBA, 93 FILNTBB, 94 FILNTBC, 95 FILNTBM, 96 FILNTCA, 97 FILNTCB, 98 FILNTCC, 99 FILNTCM, 100 FILNTMA, 101 FILNTMB, 102 FILNTMC, 103 FILNTMM, 104 FILOTAA, 105 FILOTAB, 106 FILOTAC, 107 FILOTAM, 108 FILOTBA, 109 FILOTBB, 110 FILOTBC, 111 FILOTBM, 112 FILOTCA, 113 FILOTCB, 114 FILOTCC, 115 FILOTCM, 116 FILOTMA, 117 FILOTMB, 118 FILOTMC, 119 FILOTMM, | 88 FILNT, 89 FILOT, |
120 FILEQ, 121 FILUID, 122 FILGID, 123 STREZ, 124 STRNZ, 125 STREQ, 126 STRNE, 127 STRLT, --- 15 unchanged lines hidden (view full) --- 143enum token_types { 144 UNOP, 145 BINOP, 146 BUNOP, 147 BBINOP, 148 PAREN 149}; 150 | 90 FILEQ, 91 FILUID, 92 FILGID, 93 STREZ, 94 STRNZ, 95 STREQ, 96 STRNE, 97 STRLT, --- 15 unchanged lines hidden (view full) --- 113enum token_types { 114 UNOP, 115 BINOP, 116 BUNOP, 117 BBINOP, 118 PAREN 119}; 120 |
151enum time_types { 152 ATIME, 153 BTIME, 154 CTIME, 155 MTIME 156}; 157 | |
158static struct t_op { | 121static struct t_op { |
159 char op_text[6]; 160 char op_num, op_type; | 122 char op_text[4]; 123 short op_num, op_type; |
161} const ops [] = { 162 {"-r", FILRD, UNOP}, 163 {"-w", FILWR, UNOP}, 164 {"-x", FILEX, UNOP}, 165 {"-e", FILEXIST,UNOP}, 166 {"-f", FILREG, UNOP}, 167 {"-d", FILDIR, UNOP}, 168 {"-c", FILCDEV,UNOP}, --- 17 unchanged lines hidden (view full) --- 186 {"<", STRLT, BINOP}, 187 {">", STRGT, BINOP}, 188 {"-eq", INTEQ, BINOP}, 189 {"-ne", INTNE, BINOP}, 190 {"-ge", INTGE, BINOP}, 191 {"-gt", INTGT, BINOP}, 192 {"-le", INTLE, BINOP}, 193 {"-lt", INTLT, BINOP}, | 124} const ops [] = { 125 {"-r", FILRD, UNOP}, 126 {"-w", FILWR, UNOP}, 127 {"-x", FILEX, UNOP}, 128 {"-e", FILEXIST,UNOP}, 129 {"-f", FILREG, UNOP}, 130 {"-d", FILDIR, UNOP}, 131 {"-c", FILCDEV,UNOP}, --- 17 unchanged lines hidden (view full) --- 149 {"<", STRLT, BINOP}, 150 {">", STRGT, BINOP}, 151 {"-eq", INTEQ, BINOP}, 152 {"-ne", INTNE, BINOP}, 153 {"-ge", INTGE, BINOP}, 154 {"-gt", INTGT, BINOP}, 155 {"-le", INTLE, BINOP}, 156 {"-lt", INTLT, BINOP}, |
194 {"-nt", FILNTMM, BINOP}, 195 {"-ntaa", FILNTAA, BINOP}, 196 {"-ntab", FILNTAB, BINOP}, 197 {"-ntac", FILNTAC, BINOP}, 198 {"-ntam", FILNTAM, BINOP}, 199 {"-ntba", FILNTBA, BINOP}, 200 {"-ntbb", FILNTBB, BINOP}, 201 {"-ntbc", FILNTBC, BINOP}, 202 {"-ntbm", FILNTBM, BINOP}, 203 {"-ntca", FILNTCA, BINOP}, 204 {"-ntcb", FILNTCB, BINOP}, 205 {"-ntcc", FILNTCC, BINOP}, 206 {"-ntcm", FILNTCM, BINOP}, 207 {"-ntma", FILNTMA, BINOP}, 208 {"-ntmb", FILNTMB, BINOP}, 209 {"-ntmc", FILNTMC, BINOP}, 210 {"-ntmm", FILNTMM, BINOP}, 211 {"-ot", FILOTMM, BINOP}, 212 {"-otaa", FILOTAA, BINOP}, 213 {"-otab", FILOTBB, BINOP}, 214 {"-otac", FILOTAC, BINOP}, 215 {"-otam", FILOTAM, BINOP}, 216 {"-otba", FILOTBA, BINOP}, 217 {"-otbb", FILOTBB, BINOP}, 218 {"-otbc", FILOTBC, BINOP}, 219 {"-otbm", FILOTBM, BINOP}, 220 {"-otca", FILOTCA, BINOP}, 221 {"-otcb", FILOTCB, BINOP}, 222 {"-otcc", FILOTCC, BINOP}, 223 {"-otcm", FILOTCM, BINOP}, 224 {"-otma", FILOTMA, BINOP}, 225 {"-otmb", FILOTMB, BINOP}, 226 {"-otmc", FILOTMC, BINOP}, 227 {"-otmm", FILOTMM, BINOP}, | 157 {"-nt", FILNT, BINOP}, 158 {"-ot", FILOT, BINOP}, |
228 {"-ef", FILEQ, BINOP}, 229 {"!", UNOT, BUNOP}, 230 {"-a", BAND, BBINOP}, 231 {"-o", BOR, BBINOP}, 232 {"(", LPAREN, PAREN}, 233 {")", RPAREN, PAREN}, 234 {"", 0, 0} 235}; --- 8 unchanged lines hidden (view full) --- 244static int equalf(const char *, const char *); 245static int filstat(char *, enum token); 246static int getn(const char *); 247static intmax_t getq(const char *); 248static int intcmp(const char *, const char *); 249static int isunopoperand(void); 250static int islparenoperand(void); 251static int isrparenoperand(void); | 159 {"-ef", FILEQ, BINOP}, 160 {"!", UNOT, BUNOP}, 161 {"-a", BAND, BBINOP}, 162 {"-o", BOR, BBINOP}, 163 {"(", LPAREN, PAREN}, 164 {")", RPAREN, PAREN}, 165 {"", 0, 0} 166}; --- 8 unchanged lines hidden (view full) --- 175static int equalf(const char *, const char *); 176static int filstat(char *, enum token); 177static int getn(const char *); 178static intmax_t getq(const char *); 179static int intcmp(const char *, const char *); 180static int isunopoperand(void); 181static int islparenoperand(void); 182static int isrparenoperand(void); |
252static int newerf(const char *, const char *, enum time_types, 253 enum time_types); | 183static int newerf(const char *, const char *); |
254static int nexpr(enum token); 255static int oexpr(enum token); | 184static int nexpr(enum token); 185static int oexpr(enum token); |
186static int olderf(const char *, const char *); |
|
256static int primary(enum token); 257static void syntax(const char *, const char *); 258static enum token t_lex(char *); 259 260int 261main(int argc, char **argv) 262{ 263 int res; --- 153 unchanged lines hidden (view full) --- 417 case INTGE: 418 return intcmp(opnd1, opnd2) >= 0; 419 case INTGT: 420 return intcmp(opnd1, opnd2) > 0; 421 case INTLE: 422 return intcmp(opnd1, opnd2) <= 0; 423 case INTLT: 424 return intcmp(opnd1, opnd2) < 0; | 187static int primary(enum token); 188static void syntax(const char *, const char *); 189static enum token t_lex(char *); 190 191int 192main(int argc, char **argv) 193{ 194 int res; --- 153 unchanged lines hidden (view full) --- 348 case INTGE: 349 return intcmp(opnd1, opnd2) >= 0; 350 case INTGT: 351 return intcmp(opnd1, opnd2) > 0; 352 case INTLE: 353 return intcmp(opnd1, opnd2) <= 0; 354 case INTLT: 355 return intcmp(opnd1, opnd2) < 0; |
425 case FILNTAA: 426 return newerf(opnd1, opnd2, ATIME, ATIME); 427 case FILNTAB: 428 return newerf(opnd1, opnd2, ATIME, BTIME); 429 case FILNTAC: 430 return newerf(opnd1, opnd2, ATIME, CTIME); 431 case FILNTAM: 432 return newerf(opnd1, opnd2, ATIME, MTIME); 433 case FILNTBA: 434 return newerf(opnd1, opnd2, BTIME, ATIME); 435 case FILNTBB: 436 return newerf(opnd1, opnd2, BTIME, BTIME); 437 case FILNTBC: 438 return newerf(opnd1, opnd2, BTIME, CTIME); 439 case FILNTBM: 440 return newerf(opnd1, opnd2, BTIME, MTIME); 441 case FILNTCA: 442 return newerf(opnd1, opnd2, CTIME, ATIME); 443 case FILNTCB: 444 return newerf(opnd1, opnd2, CTIME, BTIME); 445 case FILNTCC: 446 return newerf(opnd1, opnd2, CTIME, CTIME); 447 case FILNTCM: 448 return newerf(opnd1, opnd2, CTIME, MTIME); 449 case FILNTMA: 450 return newerf(opnd1, opnd2, MTIME, ATIME); 451 case FILNTMB: 452 return newerf(opnd1, opnd2, MTIME, BTIME); 453 case FILNTMC: 454 return newerf(opnd1, opnd2, MTIME, CTIME); 455 case FILNTMM: 456 return newerf(opnd1, opnd2, MTIME, MTIME); 457 case FILOTAA: 458 return newerf(opnd2, opnd1, ATIME, ATIME); 459 case FILOTAB: 460 return newerf(opnd2, opnd1, BTIME, ATIME); 461 case FILOTAC: 462 return newerf(opnd2, opnd1, CTIME, ATIME); 463 case FILOTAM: 464 return newerf(opnd2, opnd1, MTIME, ATIME); 465 case FILOTBA: 466 return newerf(opnd2, opnd1, ATIME, BTIME); 467 case FILOTBB: 468 return newerf(opnd2, opnd1, BTIME, BTIME); 469 case FILOTBC: 470 return newerf(opnd2, opnd1, CTIME, BTIME); 471 case FILOTBM: 472 return newerf(opnd2, opnd1, MTIME, BTIME); 473 case FILOTCA: 474 return newerf(opnd2, opnd1, ATIME, CTIME); 475 case FILOTCB: 476 return newerf(opnd2, opnd1, BTIME, CTIME); 477 case FILOTCC: 478 return newerf(opnd2, opnd1, CTIME, CTIME); 479 case FILOTCM: 480 return newerf(opnd2, opnd1, MTIME, CTIME); 481 case FILOTMA: 482 return newerf(opnd2, opnd1, ATIME, MTIME); 483 case FILOTMB: 484 return newerf(opnd2, opnd1, BTIME, MTIME); 485 case FILOTMC: 486 return newerf(opnd2, opnd1, CTIME, MTIME); 487 case FILOTMM: 488 return newerf(opnd2, opnd1, MTIME, MTIME); | 356 case FILNT: 357 return newerf (opnd1, opnd2); 358 case FILOT: 359 return olderf (opnd1, opnd2); |
489 case FILEQ: 490 return equalf (opnd1, opnd2); 491 default: 492 abort(); 493 /* NOTREACHED */ 494 } 495} 496 --- 197 unchanged lines hidden (view full) --- 694 695 if (q1 < q2) 696 return -1; 697 698 return 0; 699} 700 701static int | 360 case FILEQ: 361 return equalf (opnd1, opnd2); 362 default: 363 abort(); 364 /* NOTREACHED */ 365 } 366} 367 --- 197 unchanged lines hidden (view full) --- 565 566 if (q1 < q2) 567 return -1; 568 569 return 0; 570} 571 572static int |
702newerf (const char *f1, const char *f2, enum time_types t1, enum time_types t2) | 573newerf (const char *f1, const char *f2) |
703{ 704 struct stat b1, b2; | 574{ 575 struct stat b1, b2; |
705 struct timespec *ts1, *ts2; | |
706 707 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0) 708 return 0; 709 | 576 577 if (stat(f1, &b1) != 0 || stat(f2, &b2) != 0) 578 return 0; 579 |
710 switch (t1) { 711 case ATIME: ts1 = &b1.st_atim; break; 712 case BTIME: ts1 = &b1.st_birthtim; break; 713 case CTIME: ts1 = &b1.st_ctim; break; 714 default: ts1 = &b1.st_mtim; break; 715 } 716 717 switch (t2) { 718 case ATIME: ts2 = &b2.st_atim; break; 719 case BTIME: ts2 = &b2.st_birthtim; break; 720 case CTIME: ts2 = &b2.st_ctim; break; 721 default: ts2 = &b2.st_mtim; break; 722 } 723 724 if (ts1->tv_sec > ts2->tv_sec) | 580 if (b1.st_mtim.tv_sec > b2.st_mtim.tv_sec) |
725 return 1; | 581 return 1; |
726 if (ts1->tv_sec < ts2->tv_sec) | 582 if (b1.st_mtim.tv_sec < b2.st_mtim.tv_sec) |
727 return 0; 728 | 583 return 0; 584 |
729 return (ts1->tv_nsec > ts2->tv_nsec); | 585 return (b1.st_mtim.tv_nsec > b2.st_mtim.tv_nsec); |
730} 731 732static int | 586} 587 588static int |
589olderf (const char *f1, const char *f2) 590{ 591 return (newerf(f2, f1)); 592} 593 594static int |
|
733equalf (const char *f1, const char *f2) 734{ 735 struct stat b1, b2; 736 737 return (stat (f1, &b1) == 0 && 738 stat (f2, &b2) == 0 && 739 b1.st_dev == b2.st_dev && 740 b1.st_ino == b2.st_ino); 741} | 595equalf (const char *f1, const char *f2) 596{ 597 struct stat b1, b2; 598 599 return (stat (f1, &b1) == 0 && 600 stat (f2, &b2) == 0 && 601 b1.st_dev == b2.st_dev && 602 b1.st_ino == b2.st_ino); 603} |