source: trunk/src/kmk/kmkbuiltin/test.c@ 1294

Last change on this file since 1294 was 1294, checked in by bird, 18 years ago

copy & past bug.

File size: 19.1 KB
Line 
1/* $NetBSD: test.c,v 1.33 2007/06/24 18:54:58 christos 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/*#include <sys/cdefs.h>
14#ifndef lint
15__RCSID("$NetBSD: test.c,v 1.33 2007/06/24 18:54:58 christos Exp $");
16#endif*/
17
18#include <sys/stat.h>
19#include <sys/types.h>
20
21#include <ctype.h>
22#include "err.h"
23#include <errno.h>
24#include <limits.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#ifdef _MSC_VER
29# include <direct.h>
30# include <io.h>
31# include <process.h>
32# include "mscfakes.h"
33#else
34# include <unistd.h>
35#endif
36#include <stdarg.h>
37#include <sys/stat.h>
38
39#include "kmkbuiltin.h"
40
41#ifndef __arraycount
42# define __arraycount(a) ( sizeof(a) / sizeof(a[0]) )
43#endif
44
45
46/* test(1) accepts the following grammar:
47 oexpr ::= aexpr | aexpr "-o" oexpr ;
48 aexpr ::= nexpr | nexpr "-a" aexpr ;
49 nexpr ::= primary | "!" primary
50 primary ::= unary-operator operand
51 | operand binary-operator operand
52 | operand
53 | "(" oexpr ")"
54 ;
55 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
56 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
57
58 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
59 "-nt"|"-ot"|"-ef";
60 operand ::= <any legal UNIX file name>
61*/
62
63enum token {
64 EOI,
65 FILRD,
66 FILWR,
67 FILEX,
68 FILEXIST,
69 FILREG,
70 FILDIR,
71 FILCDEV,
72 FILBDEV,
73 FILFIFO,
74 FILSOCK,
75 FILSYM,
76 FILGZ,
77 FILTT,
78 FILSUID,
79 FILSGID,
80 FILSTCK,
81 FILNT,
82 FILOT,
83 FILEQ,
84 FILUID,
85 FILGID,
86 STREZ,
87 STRNZ,
88 STREQ,
89 STRNE,
90 STRLT,
91 STRGT,
92 INTEQ,
93 INTNE,
94 INTGE,
95 INTGT,
96 INTLE,
97 INTLT,
98 UNOT,
99 BAND,
100 BOR,
101 LPAREN,
102 RPAREN,
103 OPERAND
104};
105
106enum token_types {
107 UNOP,
108 BINOP,
109 BUNOP,
110 BBINOP,
111 PAREN
112};
113
114struct t_op {
115 const char *op_text;
116 short op_num, op_type;
117};
118
119static const struct t_op cop[] = {
120 {"!", UNOT, BUNOP},
121 {"(", LPAREN, PAREN},
122 {")", RPAREN, PAREN},
123 {"<", STRLT, BINOP},
124 {"=", STREQ, BINOP},
125 {">", STRGT, BINOP},
126};
127
128static const struct t_op cop2[] = {
129 {"!=", STRNE, BINOP},
130};
131
132static const struct t_op mop3[] = {
133 {"ef", FILEQ, BINOP},
134 {"eq", INTEQ, BINOP},
135 {"ge", INTGE, BINOP},
136 {"gt", INTGT, BINOP},
137 {"le", INTLE, BINOP},
138 {"lt", INTLT, BINOP},
139 {"ne", INTNE, BINOP},
140 {"nt", FILNT, BINOP},
141 {"ot", FILOT, BINOP},
142};
143
144static const struct t_op mop2[] = {
145 {"G", FILGID, UNOP},
146 {"L", FILSYM, UNOP},
147 {"O", FILUID, UNOP},
148 {"S", FILSOCK,UNOP},
149 {"a", BAND, BBINOP},
150 {"b", FILBDEV,UNOP},
151 {"c", FILCDEV,UNOP},
152 {"d", FILDIR, UNOP},
153 {"e", FILEXIST,UNOP},
154 {"f", FILREG, UNOP},
155 {"g", FILSGID,UNOP},
156 {"h", FILSYM, UNOP}, /* for backwards compat */
157 {"k", FILSTCK,UNOP},
158 {"n", STRNZ, UNOP},
159 {"o", BOR, BBINOP},
160 {"p", FILFIFO,UNOP},
161 {"r", FILRD, UNOP},
162 {"s", FILGZ, UNOP},
163 {"t", FILTT, UNOP},
164 {"u", FILSUID,UNOP},
165 {"w", FILWR, UNOP},
166 {"x", FILEX, UNOP},
167 {"z", STREZ, UNOP},
168};
169
170static char **t_wp;
171static struct t_op const *t_wp_op;
172
173static int syntax(const char *, const char *);
174static int oexpr(enum token);
175static int aexpr(enum token);
176static int nexpr(enum token);
177static int primary(enum token);
178static int binop(void);
179static int test_access(struct stat *, mode_t);
180static int filstat(char *, enum token);
181static enum token t_lex(char *);
182static int isoperand(void);
183static int getn(const char *);
184static int newerf(const char *, const char *);
185static int olderf(const char *, const char *);
186static int equalf(const char *, const char *);
187static int usage(const char *);
188
189#ifdef kmk_builtin_test
190extern void *xmalloc(unsigned int);
191#else
192static void *xmalloc(unsigned int sz)
193{
194 void *p = malloc(sz);
195 if (!p) {
196 fprintf(stderr, "%s: malloc(%u) failed\n", g_progname, sz);
197 exit(1);
198 }
199 return p;
200}
201#endif
202
203#ifdef kmk_builtin_test
204int kmk_builtin_test(int argc, char **argv)
205#else
206int kmk_builtin_test(int argc, char **argv, char **envp, char ***ppapszArgvSpawn)
207#endif
208{
209 int res;
210 char **argv_spawn;
211 int i;
212
213 g_progname = argv[0];
214
215 /* look for the '--', '--help' and '--version'. */
216 argv_spawn = NULL;
217 for (i = 1; i < argc; i++) {
218 if ( argv[i][0] == '-'
219 && argv[i][1] == '-') {
220 if (argv[i][2] == '\0') {
221 argc = i;
222 argv[i] = NULL;
223 argv_spawn = &argv[i + 1];
224 break;
225 }
226 if (!strcmp(argv[i], "--help"))
227 return usage(argv[0]);
228 if (!strcmp(argv[i], "--version"))
229 return kbuild_version(argv[0]);
230 }
231 }
232
233 /* are we '['? then check for ']'. */
234 if (strcmp(g_progname, "[") == 0) { /** @todo should skip the path in g_progname */
235 if (strcmp(argv[--argc], "]"))
236 return errx(1, "missing ]");
237 argv[argc] = NULL;
238 }
239
240 /* evaluate the expression */
241 if (argc < 2)
242 res = 1;
243 else {
244 t_wp = &argv[1];
245 res = oexpr(t_lex(*t_wp));
246 if (res != -42 && *t_wp != NULL && *++t_wp != NULL)
247 res = syntax(*t_wp, "unexpected operator");
248 if (res == -42)
249 return 1; /* don't mix syntax errors with the argv_spawn ignore */
250 res = !res;
251 }
252
253 /* anything to execute on success? */
254 if (argv_spawn) {
255 if (res != 0 || !argv_spawn[0])
256 res = 0; /* ignored */
257 else {
258#ifdef kmk_builtin_test
259 /* try exec the specified process */
260# if defined(_MSC_VER)
261 res = _spawnvp(_P_WAIT, argv_spawn[0], argv_spawn);
262 if (res == -1)
263 res = err(1, "_spawnvp(_P_WAIT,%s,..)", argv_spawn[i]);
264# else
265 execvp(argv_spawn[i], &argv_spawn[i]);
266 res = err(1, "execvp(%s,..)", argv_spawn[i]);
267# endif
268#else /* in kmk */
269 /* let job.c spawn the process, make a job.c style argv_spawn copy. */
270 char *buf, *cur, **argv_new;
271 size_t sz = 0;
272 int argc_new = 0;
273 while (argv_spawn[argc_new]) {
274 size_t len = strlen(argv_spawn[argc_new]) + 1;
275 sz += (len + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
276 argc_new++;
277 }
278
279 argv_new = xmalloc((argc_new + 1) * sizeof(char *));
280 buf = cur = xmalloc(sz);
281 for (i = 0; i < argc_new; i++) {
282 size_t len = strlen(argv_spawn[i]) + 1;
283 argv_new[i] = memcpy(cur, argv_spawn[i], len);
284 cur += (len + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
285 }
286 argv_new[i] = NULL;
287
288 *ppapszArgvSpawn = argv_new;
289 res = 0;
290#endif /* in kmk */
291 }
292 }
293
294 return res;
295}
296
297static int
298syntax(const char *op, const char *msg)
299{
300
301 if (op && *op)
302 errx(1, "%s: %s", op, msg);
303 else
304 errx(1, "%s", msg);
305 return -42;
306}
307
308static int
309oexpr(enum token n)
310{
311 int res;
312
313 res = aexpr(n);
314 if (res == -42 || *t_wp == NULL)
315 return res;
316 if (t_lex(*++t_wp) == BOR) {
317 int res2 = oexpr(t_lex(*++t_wp));
318 return res2 != -42 ? res2 || res : res2;
319 }
320 t_wp--;
321 return res;
322}
323
324static int
325aexpr(enum token n)
326{
327 int res;
328
329 res = nexpr(n);
330 if (res == -42 || *t_wp == NULL)
331 return res;
332 if (t_lex(*++t_wp) == BAND) {
333 int res2 = aexpr(t_lex(*++t_wp));
334 return res2 != -42 ? res2 && res : res2;
335 }
336 t_wp--;
337 return res;
338}
339
340static int
341nexpr(enum token n)
342{
343 if (n == UNOT) {
344 int res = nexpr(t_lex(*++t_wp));
345 return res != -42 ? !res : res;
346 }
347 return primary(n);
348}
349
350static int
351primary(enum token n)
352{
353 enum token nn;
354 int res;
355
356 if (n == EOI)
357 return 0; /* missing expression */
358 if (n == LPAREN) {
359 if ((nn = t_lex(*++t_wp)) == RPAREN)
360 return 0; /* missing expression */
361 res = oexpr(nn);
362 if (res != -42 && t_lex(*++t_wp) != RPAREN)
363 return syntax(NULL, "closing paren expected");
364 return res;
365 }
366 if (t_wp_op && t_wp_op->op_type == UNOP) {
367 /* unary expression */
368 if (*++t_wp == NULL)
369 return syntax(t_wp_op->op_text, "argument expected");
370 switch (n) {
371 case STREZ:
372 return strlen(*t_wp) == 0;
373 case STRNZ:
374 return strlen(*t_wp) != 0;
375 case FILTT:
376 return isatty(getn(*t_wp));
377 default:
378 return filstat(*t_wp, n);
379 }
380 }
381
382 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
383 return binop();
384 }
385
386 return strlen(*t_wp) > 0;
387}
388
389static int
390binop(void)
391{
392 const char *opnd1, *opnd2;
393 struct t_op const *op;
394
395 opnd1 = *t_wp;
396 (void) t_lex(*++t_wp);
397 op = t_wp_op;
398
399 if ((opnd2 = *++t_wp) == NULL)
400 return syntax(op->op_text, "argument expected");
401
402 switch (op->op_num) {
403 case STREQ:
404 return strcmp(opnd1, opnd2) == 0;
405 case STRNE:
406 return strcmp(opnd1, opnd2) != 0;
407 case STRLT:
408 return strcmp(opnd1, opnd2) < 0;
409 case STRGT:
410 return strcmp(opnd1, opnd2) > 0;
411 case INTEQ:
412 return getn(opnd1) == getn(opnd2);
413 case INTNE:
414 return getn(opnd1) != getn(opnd2);
415 case INTGE:
416 return getn(opnd1) >= getn(opnd2);
417 case INTGT:
418 return getn(opnd1) > getn(opnd2);
419 case INTLE:
420 return getn(opnd1) <= getn(opnd2);
421 case INTLT:
422 return getn(opnd1) < getn(opnd2);
423 case FILNT:
424 return newerf(opnd1, opnd2);
425 case FILOT:
426 return olderf(opnd1, opnd2);
427 case FILEQ:
428 return equalf(opnd1, opnd2);
429 default:
430 abort();
431 /* NOTREACHED */
432#ifdef _MSC_VER
433 return -42;
434#endif
435 }
436}
437
438/*
439 * The manual, and IEEE POSIX 1003.2, suggests this should check the mode bits,
440 * not use access():
441 *
442 * True shall indicate only that the write flag is on. The file is not
443 * writable on a read-only file system even if this test indicates true.
444 *
445 * Unfortunately IEEE POSIX 1003.1-2001, as quoted in SuSv3, says only:
446 *
447 * True shall indicate that permission to read from file will be granted,
448 * as defined in "File Read, Write, and Creation".
449 *
450 * and that section says:
451 *
452 * When a file is to be read or written, the file shall be opened with an
453 * access mode corresponding to the operation to be performed. If file
454 * access permissions deny access, the requested operation shall fail.
455 *
456 * and of course access permissions are described as one might expect:
457 *
458 * * If a process has the appropriate privilege:
459 *
460 * * If read, write, or directory search permission is requested,
461 * access shall be granted.
462 *
463 * * If execute permission is requested, access shall be granted if
464 * execute permission is granted to at least one user by the file
465 * permission bits or by an alternate access control mechanism;
466 * otherwise, access shall be denied.
467 *
468 * * Otherwise:
469 *
470 * * The file permission bits of a file contain read, write, and
471 * execute/search permissions for the file owner class, file group
472 * class, and file other class.
473 *
474 * * Access shall be granted if an alternate access control mechanism
475 * is not enabled and the requested access permission bit is set for
476 * the class (file owner class, file group class, or file other class)
477 * to which the process belongs, or if an alternate access control
478 * mechanism is enabled and it allows the requested access; otherwise,
479 * access shall be denied.
480 *
481 * and when I first read this I thought: surely we can't go about using
482 * open(O_WRONLY) to try this test! However the POSIX 1003.1-2001 Rationale
483 * section for test does in fact say:
484 *
485 * On historical BSD systems, test -w directory always returned false
486 * because test tried to open the directory for writing, which always
487 * fails.
488 *
489 * and indeed this is in fact true for Seventh Edition UNIX, UNIX 32V, and UNIX
490 * System III, and thus presumably also for BSD up to and including 4.3.
491 *
492 * Secondly I remembered why using open() and/or access() are bogus. They
493 * don't work right for detecting read and write permissions bits when called
494 * by root.
495 *
496 * Interestingly the 'test' in 4.4BSD was closer to correct (as per
497 * 1003.2-1992) and it was implemented efficiently with stat() instead of
498 * open().
499 *
500 * This was apparently broken in NetBSD around about 1994/06/30 when the old
501 * 4.4BSD implementation was replaced with a (arguably much better coded)
502 * implementation derived from pdksh.
503 *
504 * Note that modern pdksh is yet different again, but still not correct, at
505 * least not w.r.t. 1003.2-1992.
506 *
507 * As I think more about it and read more of the related IEEE docs I don't like
508 * that wording about 'test -r' and 'test -w' in 1003.1-2001 at all. I very
509 * much prefer the original wording in 1003.2-1992. It is much more useful,
510 * and so that's what I've implemented.
511 *
512 * (Note that a strictly conforming implementation of 1003.1-2001 is in fact
513 * totally useless for the case in question since its 'test -w' and 'test -r'
514 * can never fail for root for any existing files, i.e. files for which 'test
515 * -e' succeeds.)
516 *
517 * The rationale for 1003.1-2001 suggests that the wording was "clarified" in
518 * 1003.1-2001 to align with the 1003.2b draft. 1003.2b Draft 12 (July 1999),
519 * which is the latest copy I have, does carry the same suggested wording as is
520 * in 1003.1-2001, with its rationale saying:
521 *
522 * This change is a clarification and is the result of interpretation
523 * request PASC 1003.2-92 #23 submitted for IEEE Std 1003.2-1992.
524 *
525 * That interpretation can be found here:
526 *
527 * http://www.pasc.org/interps/unofficial/db/p1003.2/pasc-1003.2-23.html
528 *
529 * Not terribly helpful, unfortunately. I wonder who that fence sitter was.
530 *
531 * Worse, IMVNSHO, I think the authors of 1003.2b-D12 have mis-interpreted the
532 * PASC interpretation and appear to be gone against at least one widely used
533 * implementation (namely 4.4BSD). The problem is that for file access by root
534 * this means that if test '-r' and '-w' are to behave as if open() were called
535 * then there's no way for a shell script running as root to check if a file
536 * has certain access bits set other than by the grotty means of interpreting
537 * the output of 'ls -l'. This was widely considered to be a bug in V7's
538 * "test" and is, I believe, one of the reasons why direct use of access() was
539 * avoided in some more recent implementations!
540 *
541 * I have always interpreted '-r' to match '-w' and '-x' as per the original
542 * wording in 1003.2-1992, not the other way around. I think 1003.2b goes much
543 * too far the wrong way without any valid rationale and that it's best if we
544 * stick with 1003.2-1992 and test the flags, and not mimic the behaviour of
545 * open() since we already know very well how it will work -- existance of the
546 * file is all that matters to open() for root.
547 *
548 * Unfortunately the SVID is no help at all (which is, I guess, partly why
549 * we're in this mess in the first place :-).
550 *
551 * The SysV implementation (at least in the 'test' builtin in /bin/sh) does use
552 * access(name, 2) even though it also goes to much greater lengths for '-x'
553 * matching the 1003.2-1992 definition (which is no doubt where that definition
554 * came from).
555 *
556 * The ksh93 implementation uses access() for '-r' and '-w' if
557 * (euid==uid&&egid==gid), but uses st_mode for '-x' iff running as root.
558 * i.e. it does strictly conform to 1003.1-2001 (and presumably 1003.2b).
559 */
560static int
561test_access(struct stat *sp, mode_t stmode)
562{
563#ifdef _MSC_VER
564 /* just pretend to be root for now. */
565 stmode = (stmode << 6) | (stmode << 3) | stmode;
566 return !!(sp->st_mode & stmode);
567#else
568 gid_t *groups;
569 register int n;
570 uid_t euid;
571 int maxgroups;
572
573 /*
574 * I suppose we could use access() if not running as root and if we are
575 * running with ((euid == uid) && (egid == gid)), but we've already
576 * done the stat() so we might as well just test the permissions
577 * directly instead of asking the kernel to do it....
578 */
579 euid = geteuid();
580 if (euid == 0) /* any bit is good enough */
581 stmode = (stmode << 6) | (stmode << 3) | stmode;
582 else if (sp->st_uid == euid)
583 stmode <<= 6;
584 else if (sp->st_gid == getegid())
585 stmode <<= 3;
586 else {
587 /* XXX stolen almost verbatim from ksh93.... */
588 /* on some systems you can be in several groups */
589 if ((maxgroups = getgroups(0, NULL)) <= 0)
590 maxgroups = NGROUPS_MAX; /* pre-POSIX system? */
591 groups = xmalloc((maxgroups + 1) * sizeof(gid_t));
592 n = getgroups(maxgroups, groups);
593 while (--n >= 0) {
594 if (groups[n] == sp->st_gid) {
595 stmode <<= 3;
596 break;
597 }
598 }
599 free(groups);
600 }
601
602 return !!(sp->st_mode & stmode);
603#endif
604}
605
606static int
607filstat(char *nm, enum token mode)
608{
609 struct stat s;
610
611 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
612 return 0;
613
614 switch (mode) {
615 case FILRD:
616 return test_access(&s, S_IROTH);
617 case FILWR:
618 return test_access(&s, S_IWOTH);
619 case FILEX:
620 return test_access(&s, S_IXOTH);
621 case FILEXIST:
622 return 1; /* the successful lstat()/stat() is good enough */
623 case FILREG:
624 return S_ISREG(s.st_mode);
625 case FILDIR:
626 return S_ISDIR(s.st_mode);
627 case FILCDEV:
628#ifdef S_ISCHR
629 return S_ISCHR(s.st_mode);
630#else
631 return 0;
632#endif
633 case FILBDEV:
634#ifdef S_ISBLK
635 return S_ISBLK(s.st_mode);
636#else
637 return 0;
638#endif
639 case FILFIFO:
640#ifdef S_ISFIFO
641 return S_ISFIFO(s.st_mode);
642#else
643 return 0;
644#endif
645 case FILSOCK:
646#ifdef S_ISSOCK
647 return S_ISSOCK(s.st_mode);
648#else
649 return 0;
650#endif
651 case FILSYM:
652#ifdef S_ISLNK
653 return S_ISLNK(s.st_mode);
654#else
655 return 0;
656#endif
657 case FILSUID:
658 return (s.st_mode & S_ISUID) != 0;
659 case FILSGID:
660 return (s.st_mode & S_ISGID) != 0;
661 case FILSTCK:
662#ifdef S_ISVTX
663 return (s.st_mode & S_ISVTX) != 0;
664#else
665 return 0;
666#endif
667 case FILGZ:
668 return s.st_size > (off_t)0;
669 case FILUID:
670 return s.st_uid == geteuid();
671 case FILGID:
672 return s.st_gid == getegid();
673 default:
674 return 1;
675 }
676}
677
678#define VTOC(x) (const unsigned char *)((const struct t_op *)x)->op_text
679
680static int
681compare1(const void *va, const void *vb)
682{
683 const unsigned char *a = va;
684 const unsigned char *b = VTOC(vb);
685
686 return a[0] - b[0];
687}
688
689static int
690compare2(const void *va, const void *vb)
691{
692 const unsigned char *a = va;
693 const unsigned char *b = VTOC(vb);
694 int z = a[0] - b[0];
695
696 return z ? z : (a[1] - b[1]);
697}
698
699static struct t_op const *
700findop(const char *s)
701{
702 if (s[0] == '-') {
703 if (s[1] == '\0')
704 return NULL;
705 if (s[2] == '\0')
706 return bsearch(s + 1, mop2, __arraycount(mop2),
707 sizeof(*mop2), compare1);
708 else if (s[3] != '\0')
709 return NULL;
710 else
711 return bsearch(s + 1, mop3, __arraycount(mop3),
712 sizeof(*mop3), compare2);
713 } else {
714 if (s[1] == '\0')
715 return bsearch(s, cop, __arraycount(cop), sizeof(*cop),
716 compare1);
717 else if (strcmp(s, cop2[0].op_text) == 0)
718 return cop2;
719 else
720 return NULL;
721 }
722}
723
724static enum token
725t_lex(char *s)
726{
727 struct t_op const *op;
728
729 if (s == NULL) {
730 t_wp_op = NULL;
731 return EOI;
732 }
733
734 if ((op = findop(s)) != NULL) {
735 if (!((op->op_type == UNOP && isoperand()) ||
736 (op->op_num == LPAREN && *(t_wp+1) == 0))) {
737 t_wp_op = op;
738 return op->op_num;
739 }
740 }
741 t_wp_op = NULL;
742 return OPERAND;
743}
744
745static int
746isoperand(void)
747{
748 struct t_op const *op;
749 char *s, *t;
750
751 if ((s = *(t_wp+1)) == 0)
752 return 1;
753 if ((t = *(t_wp+2)) == 0)
754 return 0;
755 if ((op = findop(s)) != NULL)
756 return op->op_type == BINOP && (t[0] != ')' || t[1] != '\0');
757 return 0;
758}
759
760/* atoi with error detection */
761static int
762getn(const char *s)
763{
764 char *p;
765 long r;
766
767 errno = 0;
768 r = strtol(s, &p, 10);
769
770 if (errno != 0)
771 return errx(-42, "%s: out of range", s);
772
773 while (isspace((unsigned char)*p))
774 p++;
775
776 if (*p)
777 return errx(-42, "%s: bad number", s);
778
779 return (int) r;
780}
781
782static int
783newerf(const char *f1, const char *f2)
784{
785 struct stat b1, b2;
786
787 return (stat(f1, &b1) == 0 &&
788 stat(f2, &b2) == 0 &&
789 b1.st_mtime > b2.st_mtime);
790}
791
792static int
793olderf(const char *f1, const char *f2)
794{
795 struct stat b1, b2;
796
797 return (stat(f1, &b1) == 0 &&
798 stat(f2, &b2) == 0 &&
799 b1.st_mtime < b2.st_mtime);
800}
801
802static int
803equalf(const char *f1, const char *f2)
804{
805 struct stat b1, b2;
806
807 return (stat(f1, &b1) == 0 &&
808 stat(f2, &b2) == 0 &&
809 b1.st_dev == b2.st_dev &&
810 b1.st_ino == b2.st_ino);
811}
812
813static int
814usage(const char *argv0)
815{
816 fprintf(stdout,
817 "usage: %s expression [-- <prog> [args]]\n", argv0);
818 return 0; /* only used in --help. */
819}
Note: See TracBrowser for help on using the repository browser.