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

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

Skip blank program argument (kmk).

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