Xinqi Bao's Git
a5596eabbd9bd0376ecc94e1b6b4223e33ce732e
1 /* See LICENSE file for copyright and license details. */
9 #define OPER(x) (oper[(x)-'a'])
11 static bool test(const char *);
13 static bool quiet
= false;
15 static struct stat old
, new;
18 main(int argc
, char *argv
[]) {
23 while((opt
= getopt(argc
, argv
, "C:bcdefghn:o:pqrsuwx")) != -1)
25 case 'C': /* tests relative to directory */
26 if(chdir(optarg
) == -1) {
31 case 'n': /* newer than file */
32 case 'o': /* older than file */
33 if(!(OPER(opt
) = stat(optarg
, (opt
== 'n' ? &new : &old
)) == 0))
36 case 'q': /* quiet (no output, just status) */
39 default: /* miscellaneous operators */
42 case '?': /* error: unknown flag */
43 fprintf(stderr
, "usage: %s [-bcdefghpqrsuwx] [-C dir] [-n file] [-o file] [file...]\n", argv
[0]);
47 while(fgets(buf
, sizeof buf
, stdin
)) {
48 if(*(p
= &buf
[strlen(buf
)-1]) == '\n')
54 match
|= test(argv
[optind
++]);
60 test(const char *path
) {
63 if((!OPER('b') || (stat(path
, &st
) == 0 && S_ISBLK(st
.st_mode
))) /* block special */
64 && (!OPER('c') || (stat(path
, &st
) == 0 && S_ISCHR(st
.st_mode
))) /* character special */
65 && (!OPER('d') || (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))) /* directory */
66 && (!OPER('e') || (access(path
, F_OK
) == 0)) /* exists */
67 && (!OPER('f') || (stat(path
, &st
) == 0 && S_ISREG(st
.st_mode
))) /* regular file */
68 && (!OPER('g') || (stat(path
, &st
) == 0 && (st
.st_mode
& S_ISGID
))) /* set-group-id flag */
69 && (!OPER('h') || (lstat(path
, &st
) == 0 && S_ISLNK(st
.st_mode
))) /* symbolic link */
70 && (!OPER('n') || (stat(path
, &st
) == 0 && st
.st_mtime
> new.st_mtime
)) /* newer than file */
71 && (!OPER('o') || (stat(path
, &st
) == 0 && st
.st_mtime
< old
.st_mtime
)) /* older than file */
72 && (!OPER('p') || (stat(path
, &st
) == 0 && S_ISFIFO(st
.st_mode
))) /* named pipe */
73 && (!OPER('r') || (access(path
, R_OK
) == 0)) /* readable */
74 && (!OPER('s') || (stat(path
, &st
) == 0 && st
.st_size
> 0)) /* not empty */
75 && (!OPER('u') || (stat(path
, &st
) == 0 && (st
.st_mode
& S_ISUID
))) /* set-user-id flag */
76 && (!OPER('w') || (access(path
, W_OK
) == 0)) /* writable */
77 && (!OPER('x') || (access(path
, X_OK
) == 0))) { /* executable */