VirtualBox

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

最後變更 在這個檔案從2736是 2736,由 bird 提交於 10 年 前

llvm build fixes.

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

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