VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/test.c@ 2134

Last change on this file since 2134 was 2113, checked in by bird, 16 years ago

kmkbuiltin: include config.h

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette