test.c (88471) | test.c (90111) |
---|---|
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 14static const char rcsid[] = | 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 14static const char rcsid[] = |
15 "$FreeBSD: head/bin/test/test.c 88471 2001-12-25 08:10:34Z ache $"; | 15 "$FreeBSD: head/bin/test/test.c 90111 2002-02-02 06:50:57Z imp $"; |
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> --- 8 unchanged lines hidden (view full) --- 32#define main testcmd 33#include "bltin/bltin.h" 34#else 35#include <locale.h> 36 37static void error(const char *, ...) __attribute__((__noreturn__)); 38 39static void | 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> --- 8 unchanged lines hidden (view full) --- 32#define main testcmd 33#include "bltin/bltin.h" 34#else 35#include <locale.h> 36 37static void error(const char *, ...) __attribute__((__noreturn__)); 38 39static void |
40#ifdef __STDC__ | |
41error(const char *msg, ...) | 40error(const char *msg, ...) |
42#else 43error(va_alist) 44 va_dcl 45#endif | |
46{ 47 va_list ap; | 41{ 42 va_list ap; |
48#ifndef __STDC__ 49 const char *msg; 50 51 va_start(ap); 52 msg = va_arg(ap, const char *); 53#else | |
54 va_start(ap, msg); | 43 va_start(ap, msg); |
55#endif | |
56 verrx(2, msg, ap); 57 /*NOTREACHED*/ 58 va_end(ap); 59} 60#endif 61 62/* test(1) accepts the following grammar: 63 oexpr ::= aexpr | aexpr "-o" oexpr ; --- 107 unchanged lines hidden (view full) --- 171 {"(", LPAREN, PAREN}, 172 {")", RPAREN, PAREN}, 173 {0, 0, 0} 174}; 175 176struct t_op const *t_wp_op; 177char **t_wp; 178 | 44 verrx(2, msg, ap); 45 /*NOTREACHED*/ 46 va_end(ap); 47} 48#endif 49 50/* test(1) accepts the following grammar: 51 oexpr ::= aexpr | aexpr "-o" oexpr ; --- 107 unchanged lines hidden (view full) --- 159 {"(", LPAREN, PAREN}, 160 {")", RPAREN, PAREN}, 161 {0, 0, 0} 162}; 163 164struct t_op const *t_wp_op; 165char **t_wp; 166 |
179static int aexpr __P((enum token)); 180static int binop __P((void)); 181static int equalf __P((const char *, const char *)); 182static int filstat __P((char *, enum token)); 183static int getn __P((const char *)); 184static long long getq __P((const char *)); 185static int intcmp __P((const char *, const char *)); 186static int isoperand __P((void)); 187int main __P((int, char **)); 188static int newerf __P((const char *, const char *)); 189static int nexpr __P((enum token)); 190static int oexpr __P((enum token)); 191static int olderf __P((const char *, const char *)); 192static int primary __P((enum token)); 193static void syntax __P((const char *, const char *)); 194static enum token t_lex __P((char *)); | 167static int aexpr(enum token); 168static int binop(void); 169static int equalf(const char *, const char *); 170static int filstat(char *, enum token); 171static int getn(const char *); 172static long long getq(const char *); 173static int intcmp(const char *, const char *); 174static int isoperand(void); 175int main(int, char **); 176static int newerf(const char *, const char *); 177static int nexpr(enum token); 178static int oexpr(enum token); 179static int olderf(const char *, const char *); 180static int primary(enum token); 181static void syntax(const char *, const char *); 182static enum token t_lex(char *); |
195 196int | 183 184int |
197main(argc, argv) 198 int argc; 199 char **argv; | 185main(int argc, char **argv) |
200{ 201 int res; 202 char *p; 203 204 if ((p = rindex(argv[0], '/')) == NULL) 205 p = argv[0]; 206 else 207 p++; --- 19 unchanged lines hidden (view full) --- 227 228 if (*t_wp != NULL && *++t_wp != NULL) 229 syntax(*t_wp, "unexpected operator"); 230 231 return res; 232} 233 234static void | 186{ 187 int res; 188 char *p; 189 190 if ((p = rindex(argv[0], '/')) == NULL) 191 p = argv[0]; 192 else 193 p++; --- 19 unchanged lines hidden (view full) --- 213 214 if (*t_wp != NULL && *++t_wp != NULL) 215 syntax(*t_wp, "unexpected operator"); 216 217 return res; 218} 219 220static void |
235syntax(op, msg) 236 const char *op; 237 const char *msg; | 221syntax(const char *op, const char *msg) |
238{ 239 240 if (op && *op) 241 error("%s: %s", op, msg); 242 else 243 error("%s", msg); 244} 245 246static int | 222{ 223 224 if (op && *op) 225 error("%s: %s", op, msg); 226 else 227 error("%s", msg); 228} 229 230static int |
247oexpr(n) 248 enum token n; | 231oexpr(enum token n) |
249{ 250 int res; 251 252 res = aexpr(n); 253 if (t_lex(*++t_wp) == BOR) 254 return oexpr(t_lex(*++t_wp)) || res; 255 t_wp--; 256 return res; 257} 258 259static int | 232{ 233 int res; 234 235 res = aexpr(n); 236 if (t_lex(*++t_wp) == BOR) 237 return oexpr(t_lex(*++t_wp)) || res; 238 t_wp--; 239 return res; 240} 241 242static int |
260aexpr(n) 261 enum token n; | 243aexpr(enum token n) |
262{ 263 int res; 264 265 res = nexpr(n); 266 if (t_lex(*++t_wp) == BAND) 267 return aexpr(t_lex(*++t_wp)) && res; 268 t_wp--; 269 return res; 270} 271 272static int | 244{ 245 int res; 246 247 res = nexpr(n); 248 if (t_lex(*++t_wp) == BAND) 249 return aexpr(t_lex(*++t_wp)) && res; 250 t_wp--; 251 return res; 252} 253 254static int |
273nexpr(n) 274 enum token n; /* token */ | 255nexpr(enum token n) |
275{ 276 if (n == UNOT) 277 return !nexpr(t_lex(*++t_wp)); 278 return primary(n); 279} 280 281static int | 256{ 257 if (n == UNOT) 258 return !nexpr(t_lex(*++t_wp)); 259 return primary(n); 260} 261 262static int |
282primary(n) 283 enum token n; | 263primary(enum token n) |
284{ 285 enum token nn; 286 int res; 287 288 if (n == EOI) 289 return 0; /* missing expression */ 290 if (n == LPAREN) { 291 if ((nn = t_lex(*++t_wp)) == RPAREN) --- 22 unchanged lines hidden (view full) --- 314 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { 315 return binop(); 316 } 317 318 return strlen(*t_wp) > 0; 319} 320 321static int | 264{ 265 enum token nn; 266 int res; 267 268 if (n == EOI) 269 return 0; /* missing expression */ 270 if (n == LPAREN) { 271 if ((nn = t_lex(*++t_wp)) == RPAREN) --- 22 unchanged lines hidden (view full) --- 294 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { 295 return binop(); 296 } 297 298 return strlen(*t_wp) > 0; 299} 300 301static int |
322binop() | 302binop(void) |
323{ 324 const char *opnd1, *opnd2; 325 struct t_op const *op; 326 327 opnd1 = *t_wp; 328 (void) t_lex(*++t_wp); 329 op = t_wp_op; 330 --- 29 unchanged lines hidden (view full) --- 360 return equalf (opnd1, opnd2); 361 default: 362 abort(); 363 /* NOTREACHED */ 364 } 365} 366 367static int | 303{ 304 const char *opnd1, *opnd2; 305 struct t_op const *op; 306 307 opnd1 = *t_wp; 308 (void) t_lex(*++t_wp); 309 op = t_wp_op; 310 --- 29 unchanged lines hidden (view full) --- 340 return equalf (opnd1, opnd2); 341 default: 342 abort(); 343 /* NOTREACHED */ 344 } 345} 346 347static int |
368filstat(nm, mode) 369 char *nm; 370 enum token mode; | 348filstat(char *nm, enum token mode) |
371{ 372 struct stat s; 373 374 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s)) 375 return 0; 376 377 switch (mode) { 378 case FILRD: --- 36 unchanged lines hidden (view full) --- 415 case FILGID: 416 return s.st_gid == getegid(); 417 default: 418 return 1; 419 } 420} 421 422static enum token | 349{ 350 struct stat s; 351 352 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s)) 353 return 0; 354 355 switch (mode) { 356 case FILRD: --- 36 unchanged lines hidden (view full) --- 393 case FILGID: 394 return s.st_gid == getegid(); 395 default: 396 return 1; 397 } 398} 399 400static enum token |
423t_lex(s) 424 char *s; | 401t_lex(char *s) |
425{ 426 struct t_op const *op = ops; 427 428 if (s == 0) { 429 t_wp_op = NULL; 430 return EOI; 431 } 432 while (op->op_text) { --- 6 unchanged lines hidden (view full) --- 439 } 440 op++; 441 } 442 t_wp_op = NULL; 443 return OPERAND; 444} 445 446static int | 402{ 403 struct t_op const *op = ops; 404 405 if (s == 0) { 406 t_wp_op = NULL; 407 return EOI; 408 } 409 while (op->op_text) { --- 6 unchanged lines hidden (view full) --- 416 } 417 op++; 418 } 419 t_wp_op = NULL; 420 return OPERAND; 421} 422 423static int |
447isoperand() | 424isoperand(void) |
448{ 449 struct t_op const *op = ops; 450 char *s; 451 char *t; 452 453 if ((s = *(t_wp+1)) == 0) 454 return 1; 455 if ((t = *(t_wp+2)) == 0) --- 4 unchanged lines hidden (view full) --- 460 (t[0] != ')' || t[1] != '\0'); 461 op++; 462 } 463 return 0; 464} 465 466/* atoi with error detection */ 467static int | 425{ 426 struct t_op const *op = ops; 427 char *s; 428 char *t; 429 430 if ((s = *(t_wp+1)) == 0) 431 return 1; 432 if ((t = *(t_wp+2)) == 0) --- 4 unchanged lines hidden (view full) --- 437 (t[0] != ')' || t[1] != '\0'); 438 op++; 439 } 440 return 0; 441} 442 443/* atoi with error detection */ 444static int |
468getn(s) 469 const char *s; | 445getn(const char *s) |
470{ 471 char *p; 472 long r; 473 474 errno = 0; 475 r = strtol(s, &p, 10); 476 477 if (s == p) --- 9 unchanged lines hidden (view full) --- 487 if (*p) 488 error("%s: bad number", s); 489 490 return (int) r; 491} 492 493/* atoi with error detection and 64 bit range */ 494static long long | 446{ 447 char *p; 448 long r; 449 450 errno = 0; 451 r = strtol(s, &p, 10); 452 453 if (s == p) --- 9 unchanged lines hidden (view full) --- 463 if (*p) 464 error("%s: bad number", s); 465 466 return (int) r; 467} 468 469/* atoi with error detection and 64 bit range */ 470static long long |
495getq(s) 496 const char *s; | 471getq(const char *s) |
497{ 498 char *p; 499 long long r; 500 501 errno = 0; 502 r = strtoll(s, &p, 10); 503 504 if (s == p) --- 8 unchanged lines hidden (view full) --- 513 514 if (*p) 515 error("%s: bad number", s); 516 517 return r; 518} 519 520static int | 472{ 473 char *p; 474 long long r; 475 476 errno = 0; 477 r = strtoll(s, &p, 10); 478 479 if (s == p) --- 8 unchanged lines hidden (view full) --- 488 489 if (*p) 490 error("%s: bad number", s); 491 492 return r; 493} 494 495static int |
521intcmp (s1, s2) 522 const char *s1, *s2; | 496intcmp (const char *s1, const char *s2) |
523{ 524 long long q1, q2; 525 526 527 q1 = getq(s1); 528 q2 = getq(s2); 529 530 if (q1 > q2) 531 return 1; 532 533 if (q1 < q2) 534 return -1; 535 536 return 0; 537} 538 539static int | 497{ 498 long long q1, q2; 499 500 501 q1 = getq(s1); 502 q2 = getq(s2); 503 504 if (q1 > q2) 505 return 1; 506 507 if (q1 < q2) 508 return -1; 509 510 return 0; 511} 512 513static int |
540newerf (f1, f2) 541 const char *f1, *f2; | 514newerf (const char *f1, const char *f2) |
542{ 543 struct stat b1, b2; 544 545 return (stat (f1, &b1) == 0 && 546 stat (f2, &b2) == 0 && 547 b1.st_mtime > b2.st_mtime); 548} 549 550static int | 515{ 516 struct stat b1, b2; 517 518 return (stat (f1, &b1) == 0 && 519 stat (f2, &b2) == 0 && 520 b1.st_mtime > b2.st_mtime); 521} 522 523static int |
551olderf (f1, f2) 552 const char *f1, *f2; | 524olderf (const char *f1, const char *f2) |
553{ 554 struct stat b1, b2; 555 556 return (stat (f1, &b1) == 0 && 557 stat (f2, &b2) == 0 && 558 b1.st_mtime < b2.st_mtime); 559} 560 561static int | 525{ 526 struct stat b1, b2; 527 528 return (stat (f1, &b1) == 0 && 529 stat (f2, &b2) == 0 && 530 b1.st_mtime < b2.st_mtime); 531} 532 533static int |
562equalf (f1, f2) 563 const char *f1, *f2; | 534equalf (const char *f1, const char *f2) |
564{ 565 struct stat b1, b2; 566 567 return (stat (f1, &b1) == 0 && 568 stat (f2, &b2) == 0 && 569 b1.st_dev == b2.st_dev && 570 b1.st_ino == b2.st_ino); 571} | 535{ 536 struct stat b1, b2; 537 538 return (stat (f1, &b1) == 0 && 539 stat (f2, &b2) == 0 && 540 b1.st_dev == b2.st_dev && 541 b1.st_ino == b2.st_ino); 542} |