1 /* $NetBSD: test.c,v 1.21 1999/04/05 09:48:38 kleink Exp $ */
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.
10 * This program is in the Public Domain.
12 * $FreeBSD: src/bin/test/test.c,v 1.55 2010/03/28 13:16:08 ed Exp $
15 #include <sys/types.h>
31 #include "bltin/bltin.h"
36 static void error(const char *, ...) __dead2
__printf0like(1, 2);
39 error(const char *msg
, ...)
50 /* test(1) accepts the following grammar:
51 oexpr ::= aexpr | aexpr "-o" oexpr ;
52 aexpr ::= nexpr | nexpr "-a" aexpr ;
53 nexpr ::= primary | "!" primary
54 primary ::= unary-operator operand
55 | operand binary-operator operand
59 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
60 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
62 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
64 operand ::= <any legal UNIX file name>
120 short op_num
, op_type
;
125 {"-e", FILEXIST
,UNOP
},
126 {"-f", FILREG
, UNOP
},
127 {"-d", FILDIR
, UNOP
},
128 {"-c", FILCDEV
,UNOP
},
129 {"-b", FILBDEV
,UNOP
},
130 {"-p", FILFIFO
,UNOP
},
131 {"-u", FILSUID
,UNOP
},
132 {"-g", FILSGID
,UNOP
},
133 {"-k", FILSTCK
,UNOP
},
138 {"-h", FILSYM
, UNOP
}, /* for backwards compat */
139 {"-O", FILUID
, UNOP
},
140 {"-G", FILGID
, UNOP
},
141 {"-L", FILSYM
, UNOP
},
142 {"-S", FILSOCK
,UNOP
},
144 {"!=", STRNE
, BINOP
},
147 {"-eq", INTEQ
, BINOP
},
148 {"-ne", INTNE
, BINOP
},
149 {"-ge", INTGE
, BINOP
},
150 {"-gt", INTGT
, BINOP
},
151 {"-le", INTLE
, BINOP
},
152 {"-lt", INTLT
, BINOP
},
153 {"-nt", FILNT
, BINOP
},
154 {"-ot", FILOT
, BINOP
},
155 {"-ef", FILEQ
, BINOP
},
157 {"-a", BAND
, BBINOP
},
159 {"(", LPAREN
, PAREN
},
160 {")", RPAREN
, PAREN
},
164 struct t_op
const *t_wp_op
;
169 static int aexpr(enum token
);
170 static int binop(void);
171 static int equalf(const char *, const char *);
172 static int filstat(char *, enum token
);
173 static int getn(const char *);
174 static intmax_t getq(const char *);
175 static int intcmp(const char *, const char *);
176 static int isunopoperand(void);
177 static int islparenoperand(void);
178 static int isrparenoperand(void);
179 static int newerf(const char *, const char *);
180 static int nexpr(enum token
);
181 static int oexpr(enum token
);
182 static int olderf(const char *, const char *);
183 static int primary(enum token
);
184 static void syntax(const char *, const char *);
185 static enum token
t_lex(char *);
188 main(int argc
, char **argv
)
195 if ((p
= strrchr(argv
[0], '/')) == NULL
)
199 if (strcmp(p
, "[") == 0) {
200 if (strcmp(argv
[--argc
], "]") != 0)
205 /* no expression => false */
210 setlocale(LC_CTYPE
, "");
212 /* XXX work around the absence of an eaccess(2) syscall */
223 if (nargc
== 4 && strcmp(*t_wp
, "!") == 0) {
224 /* Things like ! "" -o x do not fit in the normal grammar. */
227 res
= oexpr(t_lex(*t_wp
));
229 res
= !oexpr(t_lex(*t_wp
));
232 syntax(*t_wp
, "unexpected operator");
240 syntax(const char *op
, const char *msg
)
244 error("%s: %s", op
, msg
);
255 if (t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) == BOR
)
256 return oexpr(t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
)) ||
269 if (t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) == BAND
)
270 return aexpr(t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
)) &&
281 return !nexpr(t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
));
286 primary(enum token n
)
292 return 0; /* missing expression */
295 if ((nn
= t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
)) ==
298 return 0; /* missing expression */
301 if (t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) != RPAREN
)
302 syntax(NULL
, "closing paren expected");
306 if (t_wp_op
&& t_wp_op
->op_type
== UNOP
) {
307 /* unary expression */
309 syntax(t_wp_op
->op_text
, "argument expected");
312 return strlen(*++t_wp
) == 0;
314 return strlen(*++t_wp
) != 0;
316 return isatty(getn(*++t_wp
));
318 return filstat(*++t_wp
, n
);
322 if (t_lex(nargc
> 0 ? t_wp
[1] : NULL
), t_wp_op
&& t_wp_op
->op_type
==
327 return strlen(*t_wp
) > 0;
333 const char *opnd1
, *opnd2
;
334 struct t_op
const *op
;
337 t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
);
340 if ((opnd2
= nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) == NULL
)
341 syntax(op
->op_text
, "argument expected");
343 switch (op
->op_num
) {
345 return strcmp(opnd1
, opnd2
) == 0;
347 return strcmp(opnd1
, opnd2
) != 0;
349 return strcmp(opnd1
, opnd2
) < 0;
351 return strcmp(opnd1
, opnd2
) > 0;
353 return intcmp(opnd1
, opnd2
) == 0;
355 return intcmp(opnd1
, opnd2
) != 0;
357 return intcmp(opnd1
, opnd2
) >= 0;
359 return intcmp(opnd1
, opnd2
) > 0;
361 return intcmp(opnd1
, opnd2
) <= 0;
363 return intcmp(opnd1
, opnd2
) < 0;
365 return newerf (opnd1
, opnd2
);
367 return olderf (opnd1
, opnd2
);
369 return equalf (opnd1
, opnd2
);
377 filstat(char *nm
, enum token mode
)
381 if (mode
== FILSYM
? lstat(nm
, &s
) : stat(nm
, &s
))
386 return access(nm
, R_OK
) == 0;
388 return access(nm
, W_OK
) == 0;
390 /* XXX work around access(2) false positives for superuser */
391 if (access(nm
, X_OK
) != 0)
393 if (S_ISDIR(s
.st_mode
) || getuid() != 0)
395 return (s
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) != 0;
397 return access(nm
, F_OK
) == 0;
399 return S_ISREG(s
.st_mode
);
401 return S_ISDIR(s
.st_mode
);
403 return S_ISCHR(s
.st_mode
);
405 return S_ISBLK(s
.st_mode
);
407 return S_ISFIFO(s
.st_mode
);
409 return S_ISSOCK(s
.st_mode
);
411 return S_ISLNK(s
.st_mode
);
413 return (s
.st_mode
& S_ISUID
) != 0;
415 return (s
.st_mode
& S_ISGID
) != 0;
417 return (s
.st_mode
& S_ISVTX
) != 0;
419 return s
.st_size
> (off_t
)0;
421 return s
.st_uid
== geteuid();
423 return s
.st_gid
== getegid();
432 struct t_op
const *op
= ops
;
438 while (op
->op_text
) {
439 if (strcmp(s
, op
->op_text
) == 0) {
440 if (((op
->op_type
== UNOP
|| op
->op_type
== BUNOP
)
441 && isunopoperand()) ||
442 (op
->op_num
== LPAREN
&& islparenoperand()) ||
443 (op
->op_num
== RPAREN
&& isrparenoperand()))
457 struct t_op
const *op
= ops
;
465 return parenlevel
== 1 && strcmp(s
, ")") == 0;
467 while (op
->op_text
) {
468 if (strcmp(s
, op
->op_text
) == 0)
469 return op
->op_type
== BINOP
&&
470 (parenlevel
== 0 || t
[0] != ')' || t
[1] != '\0');
477 islparenoperand(void)
479 struct t_op
const *op
= ops
;
486 return parenlevel
== 1 && strcmp(s
, ")") == 0;
489 while (op
->op_text
) {
490 if (strcmp(s
, op
->op_text
) == 0)
491 return op
->op_type
== BINOP
;
498 isrparenoperand(void)
506 return parenlevel
== 1 && strcmp(s
, ")") == 0;
510 /* atoi with error detection */
518 r
= strtol(s
, &p
, 10);
521 error("%s: bad number", s
);
524 error((errno
== EINVAL
) ? "%s: bad number" :
525 "%s: out of range", s
);
527 while (isspace((unsigned char)*p
))
531 error("%s: bad number", s
);
536 /* atoi with error detection and 64 bit range */
544 r
= strtoimax(s
, &p
, 10);
547 error("%s: bad number", s
);
550 error((errno
== EINVAL
) ? "%s: bad number" :
551 "%s: out of range", s
);
553 while (isspace((unsigned char)*p
))
557 error("%s: bad number", s
);
563 intcmp (const char *s1
, const char *s2
)
581 newerf (const char *f1
, const char *f2
)
585 if (stat(f1
, &b1
) != 0 || stat(f2
, &b2
) != 0)
588 if (b1
.st_mtim
.tv_sec
> b2
.st_mtim
.tv_sec
)
590 if (b1
.st_mtim
.tv_sec
< b2
.st_mtim
.tv_sec
)
593 return (b1
.st_mtim
.tv_nsec
> b2
.st_mtim
.tv_nsec
);
597 olderf (const char *f1
, const char *f2
)
599 return (newerf(f2
, f1
));
603 equalf (const char *f1
, const char *f2
)
607 return (stat (f1
, &b1
) == 0 &&
608 stat (f2
, &b2
) == 0 &&
609 b1
.st_dev
== b2
.st_dev
&&
610 b1
.st_ino
== b2
.st_ino
);