VirtualBox

source: kBuild/trunk/src/ash/bltin/test.c@ 632

Last change on this file since 632 was 632, checked in by bird, 18 years ago

MSC hacking.

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1/* $NetBSD: test.c,v 1.26 2005/02/10 06:56:55 simonb Exp $ */
2
3/*
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.
9 *
10 * This program is in the Public Domain.
11 */
12
13#include <sys/cdefs.h>
14#ifndef lint
15__RCSID("$NetBSD: test.c,v 1.26 2005/02/10 06:56:55 simonb Exp $");
16#endif
17
18#include <sys/stat.h>
19#include <sys/types.h>
20
21#include <ctype.h>
22#include <err.h>
23#include <errno.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdarg.h>
29
30/* test(1) accepts the following grammar:
31 oexpr ::= aexpr | aexpr "-o" oexpr ;
32 aexpr ::= nexpr | nexpr "-a" aexpr ;
33 nexpr ::= primary | "!" primary
34 primary ::= unary-operator operand
35 | operand binary-operator operand
36 | operand
37 | "(" oexpr ")"
38 ;
39 unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"|
40 "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S";
41
42 binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"|
43 "-nt"|"-ot"|"-ef";
44 operand ::= <any legal UNIX file name>
45*/
46
47enum token {
48 EOI,
49 FILRD,
50 FILWR,
51 FILEX,
52 FILEXIST,
53 FILREG,
54 FILDIR,
55 FILCDEV,
56 FILBDEV,
57 FILFIFO,
58 FILSOCK,
59 FILSYM,
60 FILGZ,
61 FILTT,
62 FILSUID,
63 FILSGID,
64 FILSTCK,
65 FILNT,
66 FILOT,
67 FILEQ,
68 FILUID,
69 FILGID,
70 STREZ,
71 STRNZ,
72 STREQ,
73 STRNE,
74 STRLT,
75 STRGT,
76 INTEQ,
77 INTNE,
78 INTGE,
79 INTGT,
80 INTLE,
81 INTLT,
82 UNOT,
83 BAND,
84 BOR,
85 LPAREN,
86 RPAREN,
87 OPERAND
88};
89
90enum token_types {
91 UNOP,
92 BINOP,
93 BUNOP,
94 BBINOP,
95 PAREN
96};
97
98static struct t_op {
99 const char *op_text;
100 short op_num, op_type;
101} const ops [] = {
102 {"-r", FILRD, UNOP},
103 {"-w", FILWR, UNOP},
104 {"-x", FILEX, UNOP},
105 {"-e", FILEXIST,UNOP},
106 {"-f", FILREG, UNOP},
107 {"-d", FILDIR, UNOP},
108 {"-c", FILCDEV,UNOP},
109 {"-b", FILBDEV,UNOP},
110 {"-p", FILFIFO,UNOP},
111 {"-u", FILSUID,UNOP},
112 {"-g", FILSGID,UNOP},
113 {"-k", FILSTCK,UNOP},
114 {"-s", FILGZ, UNOP},
115 {"-t", FILTT, UNOP},
116 {"-z", STREZ, UNOP},
117 {"-n", STRNZ, UNOP},
118 {"-h", FILSYM, UNOP}, /* for backwards compat */
119 {"-O", FILUID, UNOP},
120 {"-G", FILGID, UNOP},
121 {"-L", FILSYM, UNOP},
122 {"-S", FILSOCK,UNOP},
123 {"=", STREQ, BINOP},
124 {"!=", STRNE, BINOP},
125 {"<", STRLT, BINOP},
126 {">", STRGT, BINOP},
127 {"-eq", INTEQ, BINOP},
128 {"-ne", INTNE, BINOP},
129 {"-ge", INTGE, BINOP},
130 {"-gt", INTGT, BINOP},
131 {"-le", INTLE, BINOP},
132 {"-lt", INTLT, BINOP},
133 {"-nt", FILNT, BINOP},
134 {"-ot", FILOT, BINOP},
135 {"-ef", FILEQ, BINOP},
136 {"!", UNOT, BUNOP},
137 {"-a", BAND, BBINOP},
138 {"-o", BOR, BBINOP},
139 {"(", LPAREN, PAREN},
140 {")", RPAREN, PAREN},
141 {0, 0, 0}
142};
143
144static char **t_wp;
145static struct t_op const *t_wp_op;
146
147static void syntax(const char *, const char *);
148static int oexpr(enum token);
149static int aexpr(enum token);
150static int nexpr(enum token);
151static int primary(enum token);
152static int binop(void);
153static int filstat(char *, enum token);
154static enum token t_lex(char *);
155static int isoperand(void);
156static int getn(const char *);
157static int newerf(const char *, const char *);
158static int olderf(const char *, const char *);
159static int equalf(const char *, const char *);
160
161#if defined(SHELL)
162extern void error(const char *, ...) __attribute__((__noreturn__));
163#else
164static void error(const char *, ...) __attribute__((__noreturn__));
165
166static void
167error(const char *msg, ...)
168{
169 va_list ap;
170
171 va_start(ap, msg);
172 verrx(2, msg, ap);
173 /*NOTREACHED*/
174 va_end(ap);
175}
176#endif
177
178#ifdef SHELL
179int testcmd(int, char **);
180
181int
182testcmd(int argc, char **argv)
183#else
184int main(int, char *[]);
185
186int
187main(int argc, char *argv[])
188#endif
189{
190 int res;
191
192#ifdef HAVE_SETPROGNAME
193 setprogname(argv[0]);
194#endif
195 if (strcmp(argv[0], "[") == 0) {
196 if (strcmp(argv[--argc], "]"))
197 error("missing ]");
198 argv[argc] = NULL;
199 }
200
201 if (argc < 2)
202 return 1;
203
204 t_wp = &argv[1];
205 res = !oexpr(t_lex(*t_wp));
206
207 if (*t_wp != NULL && *++t_wp != NULL)
208 syntax(*t_wp, "unexpected operator");
209
210 return res;
211}
212
213static void
214syntax(const char *op, const char *msg)
215{
216
217 if (op && *op)
218 error("%s: %s", op, msg);
219 else
220 error("%s", msg);
221}
222
223static int
224oexpr(enum token n)
225{
226 int res;
227
228 res = aexpr(n);
229 if (t_lex(*++t_wp) == BOR)
230 return oexpr(t_lex(*++t_wp)) || res;
231 t_wp--;
232 return res;
233}
234
235static int
236aexpr(enum token n)
237{
238 int res;
239
240 res = nexpr(n);
241 if (t_lex(*++t_wp) == BAND)
242 return aexpr(t_lex(*++t_wp)) && res;
243 t_wp--;
244 return res;
245}
246
247static int
248nexpr(enum token n)
249{
250
251 if (n == UNOT)
252 return !nexpr(t_lex(*++t_wp));
253 return primary(n);
254}
255
256static int
257primary(enum token n)
258{
259 enum token nn;
260 int res;
261
262 if (n == EOI)
263 return 0; /* missing expression */
264 if (n == LPAREN) {
265 if ((nn = t_lex(*++t_wp)) == RPAREN)
266 return 0; /* missing expression */
267 res = oexpr(nn);
268 if (t_lex(*++t_wp) != RPAREN)
269 syntax(NULL, "closing paren expected");
270 return res;
271 }
272 if (t_wp_op && t_wp_op->op_type == UNOP) {
273 /* unary expression */
274 if (*++t_wp == NULL)
275 syntax(t_wp_op->op_text, "argument expected");
276 switch (n) {
277 case STREZ:
278 return strlen(*t_wp) == 0;
279 case STRNZ:
280 return strlen(*t_wp) != 0;
281 case FILTT:
282 return isatty(getn(*t_wp));
283 default:
284 return filstat(*t_wp, n);
285 }
286 }
287
288 if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) {
289 return binop();
290 }
291
292 return strlen(*t_wp) > 0;
293}
294
295static int
296binop(void)
297{
298 const char *opnd1, *opnd2;
299 struct t_op const *op;
300
301 opnd1 = *t_wp;
302 (void) t_lex(*++t_wp);
303 op = t_wp_op;
304
305 if ((opnd2 = *++t_wp) == NULL)
306 syntax(op->op_text, "argument expected");
307
308 switch (op->op_num) {
309 case STREQ:
310 return strcmp(opnd1, opnd2) == 0;
311 case STRNE:
312 return strcmp(opnd1, opnd2) != 0;
313 case STRLT:
314 return strcmp(opnd1, opnd2) < 0;
315 case STRGT:
316 return strcmp(opnd1, opnd2) > 0;
317 case INTEQ:
318 return getn(opnd1) == getn(opnd2);
319 case INTNE:
320 return getn(opnd1) != getn(opnd2);
321 case INTGE:
322 return getn(opnd1) >= getn(opnd2);
323 case INTGT:
324 return getn(opnd1) > getn(opnd2);
325 case INTLE:
326 return getn(opnd1) <= getn(opnd2);
327 case INTLT:
328 return getn(opnd1) < getn(opnd2);
329 case FILNT:
330 return newerf(opnd1, opnd2);
331 case FILOT:
332 return olderf(opnd1, opnd2);
333 case FILEQ:
334 return equalf(opnd1, opnd2);
335 default:
336 abort();
337 /* NOTREACHED */
338 }
339}
340
341static int
342filstat(char *nm, enum token mode)
343{
344 struct stat s;
345
346 if (mode == FILSYM ? lstat(nm, &s) : stat(nm, &s))
347 return 0;
348
349 switch (mode) {
350 case FILRD:
351 return access(nm, R_OK) == 0;
352 case FILWR:
353 return access(nm, W_OK) == 0;
354 case FILEX:
355 return access(nm, X_OK) == 0;
356 case FILEXIST:
357 return access(nm, F_OK) == 0;
358 case FILREG:
359 return S_ISREG(s.st_mode);
360 case FILDIR:
361 return S_ISDIR(s.st_mode);
362 case FILCDEV:
363#ifdef S_ISCHR
364 return S_ISCHR(s.st_mode);
365#else
366 return 0;
367#endif
368 case FILBDEV:
369#ifdef S_ISBLK
370 return S_ISBLK(s.st_mode);
371#else
372 return 0;
373#endif
374 case FILFIFO:
375#ifdef S_ISFIFO
376 return S_ISFIFO(s.st_mode);
377#else
378 return 0;
379#endif
380 case FILSOCK:
381#ifdef S_ISSOCK
382 return S_ISSOCK(s.st_mode);
383#else
384 return 0;
385#endif
386 case FILSYM:
387 return S_ISLNK(s.st_mode);
388 case FILSUID:
389 return (s.st_mode & S_ISUID) != 0;
390 case FILSGID:
391 return (s.st_mode & S_ISGID) != 0;
392 case FILSTCK:
393#ifdef S_ISVTX
394 return (s.st_mode & S_ISVTX) != 0;
395#else
396 return 0;
397#endif
398 case FILGZ:
399 return s.st_size > (off_t)0;
400 case FILUID:
401 return s.st_uid == geteuid();
402 case FILGID:
403 return s.st_gid == getegid();
404 default:
405 return 1;
406 }
407}
408
409static enum token
410t_lex(char *s)
411{
412 struct t_op const *op;
413
414 op = ops;
415
416 if (s == 0) {
417 t_wp_op = NULL;
418 return EOI;
419 }
420 while (op->op_text) {
421 if (strcmp(s, op->op_text) == 0) {
422 if ((op->op_type == UNOP && isoperand()) ||
423 (op->op_num == LPAREN && *(t_wp+1) == 0))
424 break;
425 t_wp_op = op;
426 return op->op_num;
427 }
428 op++;
429 }
430 t_wp_op = NULL;
431 return OPERAND;
432}
433
434static int
435isoperand(void)
436{
437 struct t_op const *op;
438 char *s, *t;
439
440 op = ops;
441 if ((s = *(t_wp+1)) == 0)
442 return 1;
443 if ((t = *(t_wp+2)) == 0)
444 return 0;
445 while (op->op_text) {
446 if (strcmp(s, op->op_text) == 0)
447 return op->op_type == BINOP &&
448 (t[0] != ')' || t[1] != '\0');
449 op++;
450 }
451 return 0;
452}
453
454/* atoi with error detection */
455static int
456getn(const char *s)
457{
458 char *p;
459 long r;
460
461 errno = 0;
462 r = strtol(s, &p, 10);
463
464 if (errno != 0)
465 error("%s: out of range", s);
466
467 while (isspace((unsigned char)*p))
468 p++;
469
470 if (*p)
471 error("%s: bad number", s);
472
473 return (int) r;
474}
475
476static int
477newerf(const char *f1, const char *f2)
478{
479 struct stat b1, b2;
480
481 return (stat(f1, &b1) == 0 &&
482 stat(f2, &b2) == 0 &&
483 b1.st_mtime > b2.st_mtime);
484}
485
486static int
487olderf(const char *f1, const char *f2)
488{
489 struct stat b1, b2;
490
491 return (stat(f1, &b1) == 0 &&
492 stat(f2, &b2) == 0 &&
493 b1.st_mtime < b2.st_mtime);
494}
495
496static int
497equalf(const char *f1, const char *f2)
498{
499 struct stat b1, b2;
500
501 return (stat(f1, &b1) == 0 &&
502 stat(f2, &b2) == 0 &&
503 b1.st_dev == b2.st_dev &&
504 b1.st_ino == b2.st_ino);
505}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette