1 /* $OpenBSD: test.c,v 1.11 2009/10/27 23:59:22 deraadt 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>
24 /* test(1) accepts the following grammar:
25 oexpr ::= aexpr | aexpr "-o" oexpr ;
26 aexpr ::= nexpr | nexpr "-a" aexpr ;
27 nexpr ::= primary | "!" primary
28 primary ::= unary-operator operand
29 | operand binary-operator operand
33 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
34 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
36 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
38 operand ::= <any legal UNIX file name>
94 short op_num
, op_type
;
99 {"-e", FILEXIST
,UNOP
},
100 {"-f", FILREG
, UNOP
},
101 {"-d", FILDIR
, UNOP
},
102 {"-c", FILCDEV
,UNOP
},
103 {"-b", FILBDEV
,UNOP
},
104 {"-p", FILFIFO
,UNOP
},
105 {"-u", FILSUID
,UNOP
},
106 {"-g", FILSGID
,UNOP
},
107 {"-k", FILSTCK
,UNOP
},
112 {"-h", FILSYM
, UNOP
}, /* for backwards compat */
113 {"-O", FILUID
, UNOP
},
114 {"-G", FILGID
, UNOP
},
115 {"-L", FILSYM
, UNOP
},
116 {"-S", FILSOCK
,UNOP
},
118 {"!=", STRNE
, BINOP
},
121 {"-eq", INTEQ
, BINOP
},
122 {"-ne", INTNE
, BINOP
},
123 {"-ge", INTGE
, BINOP
},
124 {"-gt", INTGT
, BINOP
},
125 {"-le", INTLE
, BINOP
},
126 {"-lt", INTLT
, BINOP
},
127 {"-nt", FILNT
, BINOP
},
128 {"-ot", FILOT
, BINOP
},
129 {"-ef", FILEQ
, BINOP
},
131 {"-a", BAND
, BBINOP
},
133 {"(", LPAREN
, PAREN
},
134 {")", RPAREN
, PAREN
},
139 struct t_op
const *t_wp_op
;
141 static enum token
t_lex(char *);
142 static enum token
t_lex_type(char *);
143 static int oexpr(enum token n
);
144 static int aexpr(enum token n
);
145 static int nexpr(enum token n
);
146 static int binop(void);
147 static int primary(enum token n
);
148 static int filstat(char *nm
, enum token mode
);
149 static int getn(const char *s
);
150 static int newerf(const char *, const char *);
151 static int olderf(const char *, const char *);
152 static int equalf(const char *, const char *);
153 static void syntax(const char *op
, char *msg
);
156 main(int argc
, char *argv
[])
160 if (strcmp(argv
[0], "[") == 0) {
161 if (strcmp(argv
[--argc
], "]"))
162 errx(2, "missing ]");
166 /* Implement special cases from POSIX.2, section 4.62.4 */
171 return (*argv
[1] == '\0');
173 if (argv
[1][0] == '!' && argv
[1][1] == '\0') {
174 return !(*argv
[2] == '\0');
178 if (argv
[1][0] != '!' || argv
[1][1] != '\0') {
180 t_wp_op
&& t_wp_op
->op_type
== BINOP
) {
182 return (binop() == 0);
187 if (argv
[1][0] == '!' && argv
[1][1] == '\0') {
189 t_wp_op
&& t_wp_op
->op_type
== BINOP
) {
191 return !(binop() == 0);
198 res
= !oexpr(t_lex(*t_wp
));
200 if (*t_wp
!= NULL
&& *++t_wp
!= NULL
)
201 syntax(*t_wp
, "unknown operand");
207 syntax(const char *op
, char *msg
)
210 errx(2, "%s: %s", op
, msg
);
221 if (t_lex(*++t_wp
) == BOR
)
222 return oexpr(t_lex(*++t_wp
)) || res
;
233 if (t_lex(*++t_wp
) == BAND
)
234 return aexpr(t_lex(*++t_wp
)) && res
;
243 return !nexpr(t_lex(*++t_wp
));
248 primary(enum token n
)
253 syntax(NULL
, "argument expected");
255 res
= oexpr(t_lex(*++t_wp
));
256 if (t_lex(*++t_wp
) != RPAREN
)
257 syntax(NULL
, "closing paren expected");
261 * We need this, if not binary operations with more than 4
262 * arguments will always fall into unary.
264 if(t_lex_type(t_wp
[1]) == BINOP
) {
266 if (t_wp_op
&& t_wp_op
->op_type
== BINOP
)
270 if (t_wp_op
&& t_wp_op
->op_type
== UNOP
) {
271 /* unary expression */
273 syntax(t_wp_op
->op_text
, "argument expected");
276 return strlen(*t_wp
) == 0;
278 return strlen(*t_wp
) != 0;
280 return isatty(getn(*t_wp
));
282 return filstat(*t_wp
, n
);
286 return strlen(*t_wp
) > 0;
292 const char *opnd1
, *opnd2
;
293 struct t_op
const *op
;
296 (void) t_lex(*++t_wp
);
299 if ((opnd2
= *++t_wp
) == NULL
)
300 syntax(op
->op_text
, "argument expected");
302 switch (op
->op_num
) {
304 return strcmp(opnd1
, opnd2
) == 0;
306 return strcmp(opnd1
, opnd2
) != 0;
308 return strcmp(opnd1
, opnd2
) < 0;
310 return strcmp(opnd1
, opnd2
) > 0;
312 return getn(opnd1
) == getn(opnd2
);
314 return getn(opnd1
) != getn(opnd2
);
316 return getn(opnd1
) >= getn(opnd2
);
318 return getn(opnd1
) > getn(opnd2
);
320 return getn(opnd1
) <= getn(opnd2
);
322 return getn(opnd1
) < getn(opnd2
);
324 return newerf(opnd1
, opnd2
);
326 return olderf(opnd1
, opnd2
);
328 return equalf(opnd1
, opnd2
);
336 struct t_op
const *op
= ops
;
341 while (op
->op_text
) {
342 if (strcmp(s
, op
->op_text
) == 0)
350 filstat(char *nm
, enum token mode
)
355 if (mode
== FILSYM
) {
357 if (lstat(nm
, &s
) == 0) {
365 if (stat(nm
, &s
) != 0)
370 return access(nm
, R_OK
) == 0;
372 return access(nm
, W_OK
) == 0;
374 return access(nm
, X_OK
) == 0;
376 return access(nm
, F_OK
) == 0;
413 return s
.st_size
> 0L;
415 return s
.st_uid
== geteuid();
417 return s
.st_gid
== getegid();
423 return ((s
.st_mode
& S_IFMT
) == i
);
426 return ((s
.st_mode
& i
) != 0);
432 struct t_op
const *op
= ops
;
438 while (op
->op_text
) {
439 if (strcmp(s
, op
->op_text
) == 0) {
449 /* atoi with error detection */
457 r
= strtol(s
, &p
, 10);
460 errx(2, "%s: out of range", s
);
466 errx(2, "%s: bad number", s
);
472 newerf(const char *f1
, const char *f2
)
476 return (stat(f1
, &b1
) == 0 &&
477 stat(f2
, &b2
) == 0 &&
478 b1
.st_mtime
> b2
.st_mtime
);
482 olderf(const char *f1
, const char *f2
)
486 return (stat(f1
, &b1
) == 0 &&
487 stat(f2
, &b2
) == 0 &&
488 b1
.st_mtime
< b2
.st_mtime
);
492 equalf(const char *f1
, const char *f2
)
496 return (stat(f1
, &b1
) == 0 &&
497 stat(f2
, &b2
) == 0 &&
498 b1
.st_dev
== b2
.st_dev
&&
499 b1
.st_ino
== b2
.st_ino
);