1 /* $OpenBSD: test.c,v 1.19 2018/04/02 06:47:43 tobias Exp $ */
2 /* $NetBSD: test.c,v 1.15 1995/03/21 07:04:06 cgd Exp $ */
5 * test(1); version 7-like -- author Erik Baalbergen
6 * modified by Eric Gisin to be used as built-in.
7 * modified by Arnold Robbins to add SVR3 compatibility
8 * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket).
9 * modified by J.T. Conklin for NetBSD.
11 * This program is in the Public Domain.
14 #include <sys/types.h>
25 /* test(1) accepts the following grammar:
26 oexpr ::= aexpr | aexpr "-o" oexpr ;
27 aexpr ::= nexpr | nexpr "-a" aexpr ;
28 nexpr ::= primary | "!" primary
29 primary ::= unary-operator operand
30 | operand binary-operator operand
34 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
35 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
37 binary-operator ::= "="|"!="|"<"|">"|"-eq"|"-ne"|"-ge"|"-gt"|
38 "-le"|"-lt"|"-nt"|"-ot"|"-ef";
39 operand ::= <any legal UNIX file name>
95 short op_num
, op_type
;
100 {"-e", FILEXIST
,UNOP
},
101 {"-f", FILREG
, UNOP
},
102 {"-d", FILDIR
, UNOP
},
103 {"-c", FILCDEV
,UNOP
},
104 {"-b", FILBDEV
,UNOP
},
105 {"-p", FILFIFO
,UNOP
},
106 {"-u", FILSUID
,UNOP
},
107 {"-g", FILSGID
,UNOP
},
108 {"-k", FILSTCK
,UNOP
},
113 {"-h", FILSYM
, UNOP
}, /* for backwards compat */
114 {"-O", FILUID
, UNOP
},
115 {"-G", FILGID
, UNOP
},
116 {"-L", FILSYM
, UNOP
},
117 {"-S", FILSOCK
,UNOP
},
119 {"!=", STRNE
, BINOP
},
122 {"-eq", INTEQ
, BINOP
},
123 {"-ne", INTNE
, BINOP
},
124 {"-ge", INTGE
, BINOP
},
125 {"-gt", INTGT
, BINOP
},
126 {"-le", INTLE
, BINOP
},
127 {"-lt", INTLT
, BINOP
},
128 {"-nt", FILNT
, BINOP
},
129 {"-ot", FILOT
, BINOP
},
130 {"-ef", FILEQ
, BINOP
},
132 {"-a", BAND
, BBINOP
},
134 {"(", LPAREN
, PAREN
},
135 {")", RPAREN
, PAREN
},
140 struct t_op
const *t_wp_op
;
142 static enum token
t_lex(char *);
143 static enum token
t_lex_type(char *);
144 static int oexpr(enum token n
);
145 static int aexpr(enum token n
);
146 static int nexpr(enum token n
);
147 static int binop(void);
148 static int primary(enum token n
);
149 static const char *getnstr(const char *, int *, size_t *);
150 static int intcmp(const char *, const char *);
151 static int filstat(char *nm
, enum token mode
);
152 static int getn(const char *s
);
153 static int newerf(const char *, const char *);
154 static int olderf(const char *, const char *);
155 static int equalf(const char *, const char *);
156 static __dead
void syntax(const char *op
, char *msg
);
159 main(int argc
, char *argv
[])
161 extern char *__progname
;
164 if (pledge("stdio rpath", NULL
) == -1)
167 if (strcmp(__progname
, "[") == 0) {
168 if (strcmp(argv
[--argc
], "]"))
169 errx(2, "missing ]");
173 /* Implement special cases from POSIX.2, section 4.62.4 */
178 return (*argv
[1] == '\0');
180 if (argv
[1][0] == '!' && argv
[1][1] == '\0') {
181 return !(*argv
[2] == '\0');
185 if (argv
[1][0] != '!' || argv
[1][1] != '\0') {
187 t_wp_op
&& t_wp_op
->op_type
== BINOP
) {
189 return (binop() == 0);
194 if (argv
[1][0] == '!' && argv
[1][1] == '\0') {
196 t_wp_op
&& t_wp_op
->op_type
== BINOP
) {
198 return !(binop() == 0);
205 res
= !oexpr(t_lex(*t_wp
));
207 if (*t_wp
!= NULL
&& *++t_wp
!= NULL
)
208 syntax(*t_wp
, "unknown operand");
214 syntax(const char *op
, char *msg
)
217 errx(2, "%s: %s", op
, msg
);
228 if (t_lex(*++t_wp
) == BOR
)
229 return oexpr(t_lex(*++t_wp
)) || res
;
240 if (t_lex(*++t_wp
) == BAND
)
241 return aexpr(t_lex(*++t_wp
)) && res
;
250 return !nexpr(t_lex(*++t_wp
));
255 primary(enum token n
)
260 syntax(NULL
, "argument expected");
262 res
= oexpr(t_lex(*++t_wp
));
263 if (t_lex(*++t_wp
) != RPAREN
)
264 syntax(NULL
, "closing paren expected");
268 * We need this, if not binary operations with more than 4
269 * arguments will always fall into unary.
271 if(t_lex_type(t_wp
[1]) == BINOP
) {
273 if (t_wp_op
&& t_wp_op
->op_type
== BINOP
)
277 if (t_wp_op
&& t_wp_op
->op_type
== UNOP
) {
278 /* unary expression */
280 syntax(t_wp_op
->op_text
, "argument expected");
283 return strlen(*t_wp
) == 0;
285 return strlen(*t_wp
) != 0;
287 return isatty(getn(*t_wp
));
289 return filstat(*t_wp
, n
);
293 return strlen(*t_wp
) > 0;
297 getnstr(const char *s
, int *signum
, size_t *len
)
299 const char *p
, *start
;
301 /* skip leading whitespaces */
303 while (isspace((unsigned char)*p
))
306 /* accept optional sign */
316 /* skip leading zeros */
317 while (*p
== '0' && isdigit((unsigned char)p
[1]))
320 /* turn 0 always positive */
325 while (isdigit((unsigned char)*p
))
329 /* allow trailing whitespaces */
330 while (isspace((unsigned char)*p
))
333 /* validate number */
334 if (*p
!= '\0' || *start
== '\0')
335 errx(2, "%s: invalid", s
);
341 intcmp(const char *opnd1
, const char *opnd2
)
347 p1
= getnstr(opnd1
, &sig1
, &len1
);
348 p2
= getnstr(opnd2
, &sig2
, &len2
);
352 else if (len1
!= len2
)
353 c
= (len1
< len2
) ? -sig1
: sig1
;
355 c
= strncmp(p1
, p2
, len1
) * sig1
;
363 const char *opnd1
, *opnd2
;
364 struct t_op
const *op
;
367 (void) t_lex(*++t_wp
);
370 if ((opnd2
= *++t_wp
) == NULL
)
371 syntax(op
->op_text
, "argument expected");
373 switch (op
->op_num
) {
375 return strcmp(opnd1
, opnd2
) == 0;
377 return strcmp(opnd1
, opnd2
) != 0;
379 return strcmp(opnd1
, opnd2
) < 0;
381 return strcmp(opnd1
, opnd2
) > 0;
383 return intcmp(opnd1
, opnd2
) == 0;
385 return intcmp(opnd1
, opnd2
) != 0;
387 return intcmp(opnd1
, opnd2
) >= 0;
389 return intcmp(opnd1
, opnd2
) > 0;
391 return intcmp(opnd1
, opnd2
) <= 0;
393 return intcmp(opnd1
, opnd2
) < 0;
395 return newerf(opnd1
, opnd2
);
397 return olderf(opnd1
, opnd2
);
399 return equalf(opnd1
, opnd2
);
402 syntax(op
->op_text
, "not a binary operator");
408 struct t_op
const *op
= ops
;
413 while (op
->op_text
) {
414 if (strcmp(s
, op
->op_text
) == 0)
422 filstat(char *nm
, enum token mode
)
427 if (mode
== FILSYM
) {
429 if (lstat(nm
, &s
) == 0) {
437 if (stat(nm
, &s
) != 0)
442 return access(nm
, R_OK
) == 0;
444 return access(nm
, W_OK
) == 0;
446 return access(nm
, X_OK
) == 0;
448 return access(nm
, F_OK
) == 0;
485 return s
.st_size
> 0L;
487 return s
.st_uid
== geteuid();
489 return s
.st_gid
== getegid();
495 return ((s
.st_mode
& S_IFMT
) == i
);
498 return ((s
.st_mode
& i
) != 0);
504 struct t_op
const *op
= ops
;
510 while (op
->op_text
) {
511 if (strcmp(s
, op
->op_text
) == 0) {
521 /* atoi with error detection */
526 const char *errstr
, *p
;
530 p
= getnstr(s
, &sig
, &len
);
532 errstr
= "too small";
533 else if (len
>= sizeof(buf
))
534 errstr
= "too large";
536 strlcpy(buf
, p
, sizeof(buf
));
538 r
= strtonum(buf
, 0, INT_MAX
, &errstr
);
542 errx(2, "%s: %s", s
, errstr
);
548 newerf(const char *f1
, const char *f2
)
552 return (stat(f1
, &b1
) == 0 &&
553 stat(f2
, &b2
) == 0 &&
554 b1
.st_mtime
> b2
.st_mtime
);
558 olderf(const char *f1
, const char *f2
)
562 return (stat(f1
, &b1
) == 0 &&
563 stat(f2
, &b2
) == 0 &&
564 b1
.st_mtime
< b2
.st_mtime
);
568 equalf(const char *f1
, const char *f2
)
572 return (stat(f1
, &b1
) == 0 &&
573 stat(f2
, &b2
) == 0 &&
574 b1
.st_dev
== b2
.st_dev
&&
575 b1
.st_ino
== b2
.st_ino
);