]>
git.saurik.com Git - apple/shell_cmds.git/blob - test/test.c
366ccda940ba66d55f82ef189f6179b4e4f0a02d
1 /* $NetBSD: test.c,v 1.19 1998/07/28 11:41:59 mycroft 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 #include <sys/cdefs.h>
15 __RCSID("$NetBSD: test.c,v 1.19 1998/07/28 11:41:59 mycroft Exp $");
18 #include <sys/types.h>
28 /* test(1) accepts the following grammar:
29 oexpr ::= aexpr | aexpr "-o" oexpr ;
30 aexpr ::= nexpr | nexpr "-a" aexpr ;
31 nexpr ::= primary | "!" primary
32 primary ::= unary-operator operand
33 | operand binary-operator operand
37 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
38 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
40 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
42 operand ::= <any legal UNIX file name>
98 short op_num
, op_type
;
103 {"-e", FILEXIST
,UNOP
},
104 {"-f", FILREG
, UNOP
},
105 {"-d", FILDIR
, UNOP
},
106 {"-c", FILCDEV
,UNOP
},
107 {"-b", FILBDEV
,UNOP
},
108 {"-p", FILFIFO
,UNOP
},
109 {"-u", FILSUID
,UNOP
},
110 {"-g", FILSGID
,UNOP
},
111 {"-k", FILSTCK
,UNOP
},
116 {"-h", FILSYM
, UNOP
}, /* for backwards compat */
117 {"-O", FILUID
, UNOP
},
118 {"-G", FILGID
, UNOP
},
119 {"-L", FILSYM
, UNOP
},
120 {"-S", FILSOCK
,UNOP
},
122 {"!=", STRNE
, BINOP
},
125 {"-eq", INTEQ
, BINOP
},
126 {"-ne", INTNE
, BINOP
},
127 {"-ge", INTGE
, BINOP
},
128 {"-gt", INTGT
, BINOP
},
129 {"-le", INTLE
, BINOP
},
130 {"-lt", INTLT
, BINOP
},
131 {"-nt", FILNT
, BINOP
},
132 {"-ot", FILOT
, BINOP
},
133 {"-ef", FILEQ
, BINOP
},
135 {"-a", BAND
, BBINOP
},
137 {"(", LPAREN
, PAREN
},
138 {")", RPAREN
, PAREN
},
143 struct t_op
const *t_wp_op
;
145 static void syntax
__P((const char *, const char *));
146 static int oexpr
__P((enum token
));
147 static int aexpr
__P((enum token
));
148 static int nexpr
__P((enum token
));
149 static int primary
__P((enum token
));
150 static int binop
__P((void));
151 static int filstat
__P((char *, enum token
));
152 static enum token t_lex
__P((char *));
153 static int getn
__P((const char *));
154 static int newerf
__P((const char *, const char *));
155 static int olderf
__P((const char *, const char *));
156 static int equalf
__P((const char *, const char *));
158 int main
__P((int, char **));
167 if (strcmp(argv
[0], "[") == 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");
219 errx(2, "%s: %s", op
, msg
);
231 if (t_lex(*++t_wp
) == BOR
)
232 return oexpr(t_lex(*++t_wp
)) || res
;
244 if (t_lex(*++t_wp
) == BAND
)
245 return aexpr(t_lex(*++t_wp
)) && res
;
252 enum token n
; /* token */
255 return !nexpr(t_lex(*++t_wp
));
266 syntax(NULL
, "argument expected");
268 res
= oexpr(t_lex(*++t_wp
));
269 if (t_lex(*++t_wp
) != RPAREN
)
270 syntax(NULL
, "closing paren expected");
273 if (t_wp_op
&& t_wp_op
->op_type
== UNOP
) {
274 /* unary expression */
276 syntax(t_wp_op
->op_text
, "argument expected");
279 return strlen(*t_wp
) == 0;
281 return strlen(*t_wp
) != 0;
283 return isatty(getn(*t_wp
));
285 return filstat(*t_wp
, n
);
289 if (t_lex(t_wp
[1]), t_wp_op
&& t_wp_op
->op_type
== BINOP
) {
293 return strlen(*t_wp
) > 0;
299 const char *opnd1
, *opnd2
;
300 struct t_op
const *op
;
303 (void) t_lex(*++t_wp
);
306 if ((opnd2
= *++t_wp
) == (char *)0)
307 syntax(op
->op_text
, "argument expected");
309 switch (op
->op_num
) {
311 return strcmp(opnd1
, opnd2
) == 0;
313 return strcmp(opnd1
, opnd2
) != 0;
315 return strcmp(opnd1
, opnd2
) < 0;
317 return strcmp(opnd1
, opnd2
) > 0;
319 return getn(opnd1
) == getn(opnd2
);
321 return getn(opnd1
) != getn(opnd2
);
323 return getn(opnd1
) >= getn(opnd2
);
325 return getn(opnd1
) > getn(opnd2
);
327 return getn(opnd1
) <= getn(opnd2
);
329 return getn(opnd1
) < getn(opnd2
);
331 return newerf (opnd1
, opnd2
);
333 return olderf (opnd1
, opnd2
);
335 return equalf (opnd1
, opnd2
);
349 if (mode
== FILSYM
? lstat(nm
, &s
) : stat(nm
, &s
))
354 return access(nm
, R_OK
) == 0;
356 return access(nm
, W_OK
) == 0;
358 return access(nm
, X_OK
) == 0;
360 return access(nm
, F_OK
) == 0;
362 return S_ISREG(s
.st_mode
);
364 return S_ISDIR(s
.st_mode
);
366 return S_ISCHR(s
.st_mode
);
368 return S_ISBLK(s
.st_mode
);
370 return S_ISFIFO(s
.st_mode
);
372 return S_ISSOCK(s
.st_mode
);
374 return S_ISLNK(s
.st_mode
);
376 return (s
.st_mode
& S_ISUID
) != 0;
378 return (s
.st_mode
& S_ISGID
) != 0;
380 return (s
.st_mode
& S_ISVTX
) != 0;
382 return s
.st_size
> (off_t
)0;
384 return s
.st_uid
== geteuid();
386 return s
.st_gid
== getegid();
396 struct t_op
const *op
= ops
;
399 t_wp_op
= (struct t_op
*)0;
402 while (op
->op_text
) {
403 if (strcmp(s
, op
->op_text
) == 0) {
409 t_wp_op
= (struct t_op
*)0;
413 /* atoi with error detection */
422 r
= strtol(s
, &p
, 10);
425 errx(2, "%s: out of range", s
);
431 errx(2, "%s: bad number", s
);
442 return (stat (f1
, &b1
) == 0 &&
443 stat (f2
, &b2
) == 0 &&
444 b1
.st_mtime
> b2
.st_mtime
);
453 return (stat (f1
, &b1
) == 0 &&
454 stat (f2
, &b2
) == 0 &&
455 b1
.st_mtime
< b2
.st_mtime
);
464 return (stat (f1
, &b1
) == 0 &&
465 stat (f2
, &b2
) == 0 &&
466 b1
.st_dev
== b2
.st_dev
&&
467 b1
.st_ino
== b2
.st_ino
);