]>
git.cameronkatri.com Git - apple_cmds.git/blob - shell_cmds/test/test.c
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.
13 * Important: This file is used both as a standalone program /bin/test and
14 * as a builtin for /bin/sh (#define SHELL).
17 #include <sys/cdefs.h>
18 __FBSDID("$FreeBSD$");
20 #include <sys/types.h>
28 #endif /* __APPLE__ */
38 #define eaccess(path, mode) faccessat(AT_FDCWD, path, mode, AT_EACCESS)
39 #define st_mtim st_mtimespec
40 #endif /* __APPLE__ */
44 #include "bltin/bltin.h"
48 static void error(const char *, ...) __dead2
__printf0like(1, 2);
51 error(const char *msg
, ...)
61 /* test(1) accepts the following grammar:
62 oexpr ::= aexpr | aexpr "-o" oexpr ;
63 aexpr ::= nexpr | nexpr "-a" aexpr ;
64 nexpr ::= primary | "!" primary
65 primary ::= unary-operator operand
66 | operand binary-operator operand
70 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
71 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
73 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
75 operand ::= <any legal UNIX file name>
129 #define TOKEN_TYPE(token) ((token) & 0xff00)
151 {"-h", FILSYM
}, /* for backwards compat */
180 static int parenlevel
;
182 static int aexpr(enum token
);
183 static int binop(enum token
);
184 static int equalf(const char *, const char *);
185 static int filstat(char *, enum token
);
186 static int getn(const char *);
187 static intmax_t getq(const char *);
188 static int intcmp(const char *, const char *);
189 static int isunopoperand(void);
190 static int islparenoperand(void);
191 static int isrparenoperand(void);
192 static int newerf(const char *, const char *);
193 static int nexpr(enum token
);
194 static int oexpr(enum token
);
195 static int olderf(const char *, const char *);
196 static int primary(enum token
);
197 static void syntax(const char *, const char *);
198 static enum token
t_lex(char *);
201 main(int argc
, char **argv
)
210 if ((p
= strrchr(argv
[0], '/')) == NULL
)
214 if (strcmp(p
, "[") == 0) {
215 if (strcmp(argv
[--argc
], "]") != 0)
220 /* no expression => false */
225 (void)setlocale(LC_CTYPE
, "");
230 if (nargc
== 4 && strcmp(*t_wp
, "!") == 0) {
231 /* Things like ! "" -o x do not fit in the normal grammar. */
234 res
= oexpr(t_lex(*t_wp
));
236 res
= !oexpr(t_lex(*t_wp
));
239 syntax(*t_wp
, "unexpected operator");
245 syntax(const char *op
, const char *msg
)
249 error("%s: %s", op
, msg
);
260 if (t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) == BOR
)
261 return oexpr(t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
)) ||
274 if (t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) == BAND
)
275 return aexpr(t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
)) &&
286 return !nexpr(t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
));
291 primary(enum token n
)
297 return 0; /* missing expression */
300 if ((nn
= t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
)) ==
303 return 0; /* missing expression */
306 if (t_lex(nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) != RPAREN
)
307 syntax(NULL
, "closing paren expected");
311 if (TOKEN_TYPE(n
) == UNOP
) {
312 /* unary expression */
314 syntax(NULL
, "argument expected"); /* impossible */
317 return strlen(*++t_wp
) == 0;
319 return strlen(*++t_wp
) != 0;
321 return isatty(getn(*++t_wp
));
323 return filstat(*++t_wp
, n
);
327 nn
= t_lex(nargc
> 0 ? t_wp
[1] : NULL
);
328 if (TOKEN_TYPE(nn
) == BINOP
)
331 return strlen(*t_wp
) > 0;
337 const char *opnd1
, *op
, *opnd2
;
340 op
= nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
;
342 if ((opnd2
= nargc
> 0 ? (--nargc
, *++t_wp
) : NULL
) == NULL
)
343 syntax(op
, "argument expected");
347 return strcmp(opnd1
, opnd2
) == 0;
349 return strcmp(opnd1
, opnd2
) != 0;
351 return strcmp(opnd1
, opnd2
) < 0;
353 return strcmp(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 intcmp(opnd1
, opnd2
) < 0;
367 return newerf (opnd1
, opnd2
);
369 return olderf (opnd1
, opnd2
);
371 return equalf (opnd1
, opnd2
);
379 filstat(char *nm
, enum token mode
)
383 if (mode
== FILSYM
? lstat(nm
, &s
) : stat(nm
, &s
))
388 return (eaccess(nm
, R_OK
) == 0);
390 return (eaccess(nm
, W_OK
) == 0);
392 /* XXX work around eaccess(2) false positives for superuser */
393 if (eaccess(nm
, X_OK
) != 0)
395 if (S_ISDIR(s
.st_mode
) || geteuid() != 0)
397 return (s
.st_mode
& (S_IXUSR
| S_IXGRP
| S_IXOTH
)) != 0;
399 return (eaccess(nm
, F_OK
) == 0);
401 return S_ISREG(s
.st_mode
);
403 return S_ISDIR(s
.st_mode
);
405 return S_ISCHR(s
.st_mode
);
407 return S_ISBLK(s
.st_mode
);
409 return S_ISFIFO(s
.st_mode
);
411 return S_ISSOCK(s
.st_mode
);
413 return S_ISLNK(s
.st_mode
);
415 return (s
.st_mode
& S_ISUID
) != 0;
417 return (s
.st_mode
& S_ISGID
) != 0;
419 return (s
.st_mode
& S_ISVTX
) != 0;
421 return s
.st_size
> (off_t
)0;
423 return s
.st_uid
== geteuid();
425 return s
.st_gid
== getegid();
434 struct t_op
const *op
= ops
;
439 while (*op
->op_text
) {
440 if (strcmp(s
, op
->op_text
) == 0) {
441 if (((TOKEN_TYPE(op
->op_num
) == UNOP
||
442 TOKEN_TYPE(op
->op_num
) == BUNOP
)
443 && isunopoperand()) ||
444 (op
->op_num
== LPAREN
&& islparenoperand()) ||
445 (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 TOKEN_TYPE(op
->op_num
) == 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 TOKEN_TYPE(op
->op_num
) == 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
);