diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-03-16 08:09:57 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-03-16 08:09:57 +0000 |
commit | 13456d1fcd0122d8464c3c3e1c356d86a56e6c08 (patch) | |
tree | 0d037e1e4bdccc54b86db8f4b03e096804124904 /coreutils/test.c | |
parent | d75af99529879e6cd38164fd110732052a9cdda4 (diff) | |
download | busybox-w32-13456d1fcd0122d8464c3c3e1c356d86a56e6c08.tar.gz busybox-w32-13456d1fcd0122d8464c3c3e1c356d86a56e6c08.tar.bz2 busybox-w32-13456d1fcd0122d8464c3c3e1c356d86a56e6c08.zip |
Forgot these files...
-Erik
Diffstat (limited to 'coreutils/test.c')
-rw-r--r-- | coreutils/test.c | 583 |
1 files changed, 583 insertions, 0 deletions
diff --git a/coreutils/test.c b/coreutils/test.c new file mode 100644 index 000000000..85d06a83a --- /dev/null +++ b/coreutils/test.c | |||
@@ -0,0 +1,583 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * echo implementation for busybox | ||
4 | * | ||
5 | * Copyright (c) by a whole pile of folks: | ||
6 | * | ||
7 | * test(1); version 7-like -- author Erik Baalbergen | ||
8 | * modified by Eric Gisin to be used as built-in. | ||
9 | * modified by Arnold Robbins to add SVR3 compatibility | ||
10 | * (-x -c -b -p -u -g -k) plus Korn's -L -nt -ot -ef and new -S (socket). | ||
11 | * modified by J.T. Conklin for NetBSD. | ||
12 | * modified by Herbert Xu to be used as built-in in ash. | ||
13 | * modified by Erik Andersen <andersee@debian.org> to be used | ||
14 | * in busybox. | ||
15 | * | ||
16 | * This program is free software; you can redistribute it and/or modify | ||
17 | * it under the terms of the GNU General Public License as published by | ||
18 | * the Free Software Foundation; either version 2 of the License, or | ||
19 | * (at your option) any later version. | ||
20 | * | ||
21 | * This program is distributed in the hope that it will be useful, | ||
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
24 | * General Public License for more details. | ||
25 | * | ||
26 | * You should have received a copy of the GNU General Public License | ||
27 | * along with this program; if not, write to the Free Software | ||
28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
29 | * | ||
30 | * Original copyright notice states: | ||
31 | * "This program is in the Public Domain." | ||
32 | */ | ||
33 | |||
34 | #include "internal.h" | ||
35 | #include <sys/types.h> | ||
36 | #include <sys/stat.h> | ||
37 | #include <unistd.h> | ||
38 | #include <ctype.h> | ||
39 | #include <errno.h> | ||
40 | #include <stdlib.h> | ||
41 | #include <string.h> | ||
42 | |||
43 | /* test(1) accepts the following grammar: | ||
44 | oexpr ::= aexpr | aexpr "-o" oexpr ; | ||
45 | aexpr ::= nexpr | nexpr "-a" aexpr ; | ||
46 | nexpr ::= primary | "!" primary | ||
47 | primary ::= unary-operator operand | ||
48 | | operand binary-operator operand | ||
49 | | operand | ||
50 | | "(" oexpr ")" | ||
51 | ; | ||
52 | unary-operator ::= "-r"|"-w"|"-x"|"-f"|"-d"|"-c"|"-b"|"-p"| | ||
53 | "-u"|"-g"|"-k"|"-s"|"-t"|"-z"|"-n"|"-o"|"-O"|"-G"|"-L"|"-S"; | ||
54 | |||
55 | binary-operator ::= "="|"!="|"-eq"|"-ne"|"-ge"|"-gt"|"-le"|"-lt"| | ||
56 | "-nt"|"-ot"|"-ef"; | ||
57 | operand ::= <any legal UNIX file name> | ||
58 | */ | ||
59 | |||
60 | enum token { | ||
61 | EOI, | ||
62 | FILRD, | ||
63 | FILWR, | ||
64 | FILEX, | ||
65 | FILEXIST, | ||
66 | FILREG, | ||
67 | FILDIR, | ||
68 | FILCDEV, | ||
69 | FILBDEV, | ||
70 | FILFIFO, | ||
71 | FILSOCK, | ||
72 | FILSYM, | ||
73 | FILGZ, | ||
74 | FILTT, | ||
75 | FILSUID, | ||
76 | FILSGID, | ||
77 | FILSTCK, | ||
78 | FILNT, | ||
79 | FILOT, | ||
80 | FILEQ, | ||
81 | FILUID, | ||
82 | FILGID, | ||
83 | STREZ, | ||
84 | STRNZ, | ||
85 | STREQ, | ||
86 | STRNE, | ||
87 | STRLT, | ||
88 | STRGT, | ||
89 | INTEQ, | ||
90 | INTNE, | ||
91 | INTGE, | ||
92 | INTGT, | ||
93 | INTLE, | ||
94 | INTLT, | ||
95 | UNOT, | ||
96 | BAND, | ||
97 | BOR, | ||
98 | LPAREN, | ||
99 | RPAREN, | ||
100 | OPERAND | ||
101 | }; | ||
102 | |||
103 | enum token_types { | ||
104 | UNOP, | ||
105 | BINOP, | ||
106 | BUNOP, | ||
107 | BBINOP, | ||
108 | PAREN | ||
109 | }; | ||
110 | |||
111 | struct t_op { | ||
112 | const char *op_text; | ||
113 | short op_num, op_type; | ||
114 | } const ops [] = { | ||
115 | {"-r", FILRD, UNOP}, | ||
116 | {"-w", FILWR, UNOP}, | ||
117 | {"-x", FILEX, UNOP}, | ||
118 | {"-e", FILEXIST,UNOP}, | ||
119 | {"-f", FILREG, UNOP}, | ||
120 | {"-d", FILDIR, UNOP}, | ||
121 | {"-c", FILCDEV,UNOP}, | ||
122 | {"-b", FILBDEV,UNOP}, | ||
123 | {"-p", FILFIFO,UNOP}, | ||
124 | {"-u", FILSUID,UNOP}, | ||
125 | {"-g", FILSGID,UNOP}, | ||
126 | {"-k", FILSTCK,UNOP}, | ||
127 | {"-s", FILGZ, UNOP}, | ||
128 | {"-t", FILTT, UNOP}, | ||
129 | {"-z", STREZ, UNOP}, | ||
130 | {"-n", STRNZ, UNOP}, | ||
131 | {"-h", FILSYM, UNOP}, /* for backwards compat */ | ||
132 | {"-O", FILUID, UNOP}, | ||
133 | {"-G", FILGID, UNOP}, | ||
134 | {"-L", FILSYM, UNOP}, | ||
135 | {"-S", FILSOCK,UNOP}, | ||
136 | {"=", STREQ, BINOP}, | ||
137 | {"!=", STRNE, BINOP}, | ||
138 | {"<", STRLT, BINOP}, | ||
139 | {">", STRGT, BINOP}, | ||
140 | {"-eq", INTEQ, BINOP}, | ||
141 | {"-ne", INTNE, BINOP}, | ||
142 | {"-ge", INTGE, BINOP}, | ||
143 | {"-gt", INTGT, BINOP}, | ||
144 | {"-le", INTLE, BINOP}, | ||
145 | {"-lt", INTLT, BINOP}, | ||
146 | {"-nt", FILNT, BINOP}, | ||
147 | {"-ot", FILOT, BINOP}, | ||
148 | {"-ef", FILEQ, BINOP}, | ||
149 | {"!", UNOT, BUNOP}, | ||
150 | {"-a", BAND, BBINOP}, | ||
151 | {"-o", BOR, BBINOP}, | ||
152 | {"(", LPAREN, PAREN}, | ||
153 | {")", RPAREN, PAREN}, | ||
154 | {0, 0, 0} | ||
155 | }; | ||
156 | |||
157 | char **t_wp; | ||
158 | struct t_op const *t_wp_op; | ||
159 | static gid_t *group_array = NULL; | ||
160 | static int ngroups; | ||
161 | |||
162 | static enum token t_lex(); | ||
163 | static int oexpr(); | ||
164 | static int aexpr(); | ||
165 | static int nexpr(); | ||
166 | static int binop(); | ||
167 | static int primary(); | ||
168 | static int filstat(); | ||
169 | static int getn(); | ||
170 | static int newerf(); | ||
171 | static int olderf(); | ||
172 | static int equalf(); | ||
173 | static void syntax(); | ||
174 | static int test_eaccess(); | ||
175 | static int is_a_group_member(); | ||
176 | static void initialize_group_array(); | ||
177 | |||
178 | extern int | ||
179 | test_main(int argc, char** argv) | ||
180 | { | ||
181 | int res; | ||
182 | |||
183 | if (strcmp(argv[0], "[") == 0) { | ||
184 | if (strcmp(argv[--argc], "]")) | ||
185 | fatalError("missing ]"); | ||
186 | argv[argc] = NULL; | ||
187 | } | ||
188 | |||
189 | /* Implement special cases from POSIX.2, section 4.62.4 */ | ||
190 | switch (argc) { | ||
191 | case 1: | ||
192 | exit( 1); | ||
193 | case 2: | ||
194 | exit (*argv[1] == '\0'); | ||
195 | case 3: | ||
196 | if (argv[1][0] == '!' && argv[1][1] == '\0') { | ||
197 | exit (!(*argv[2] == '\0')); | ||
198 | } | ||
199 | break; | ||
200 | case 4: | ||
201 | if (argv[1][0] != '!' || argv[1][1] != '\0') { | ||
202 | if (t_lex(argv[2]), | ||
203 | t_wp_op && t_wp_op->op_type == BINOP) { | ||
204 | t_wp = &argv[1]; | ||
205 | exit (binop() == 0); | ||
206 | } | ||
207 | } | ||
208 | break; | ||
209 | case 5: | ||
210 | if (argv[1][0] == '!' && argv[1][1] == '\0') { | ||
211 | if (t_lex(argv[3]), | ||
212 | t_wp_op && t_wp_op->op_type == BINOP) { | ||
213 | t_wp = &argv[2]; | ||
214 | exit (!(binop() == 0)); | ||
215 | } | ||
216 | } | ||
217 | break; | ||
218 | } | ||
219 | |||
220 | t_wp = &argv[1]; | ||
221 | res = !oexpr(t_lex(*t_wp)); | ||
222 | |||
223 | if (*t_wp != NULL && *++t_wp != NULL) | ||
224 | syntax(*t_wp, "unknown operand"); | ||
225 | |||
226 | exit( res); | ||
227 | } | ||
228 | |||
229 | static void | ||
230 | syntax(op, msg) | ||
231 | char *op; | ||
232 | char *msg; | ||
233 | { | ||
234 | if (op && *op) | ||
235 | fatalError("%s: %s", op, msg); | ||
236 | else | ||
237 | fatalError("%s", msg); | ||
238 | } | ||
239 | |||
240 | static int | ||
241 | oexpr(n) | ||
242 | enum token n; | ||
243 | { | ||
244 | int res; | ||
245 | |||
246 | res = aexpr(n); | ||
247 | if (t_lex(*++t_wp) == BOR) | ||
248 | return oexpr(t_lex(*++t_wp)) || res; | ||
249 | t_wp--; | ||
250 | return res; | ||
251 | } | ||
252 | |||
253 | static int | ||
254 | aexpr(n) | ||
255 | enum token n; | ||
256 | { | ||
257 | int res; | ||
258 | |||
259 | res = nexpr(n); | ||
260 | if (t_lex(*++t_wp) == BAND) | ||
261 | return aexpr(t_lex(*++t_wp)) && res; | ||
262 | t_wp--; | ||
263 | return res; | ||
264 | } | ||
265 | |||
266 | static int | ||
267 | nexpr(n) | ||
268 | enum token n; /* token */ | ||
269 | { | ||
270 | if (n == UNOT) | ||
271 | return !nexpr(t_lex(*++t_wp)); | ||
272 | return primary(n); | ||
273 | } | ||
274 | |||
275 | static int | ||
276 | primary(n) | ||
277 | enum token n; | ||
278 | { | ||
279 | int res; | ||
280 | |||
281 | if (n == EOI) | ||
282 | syntax(NULL, "argument expected"); | ||
283 | if (n == LPAREN) { | ||
284 | res = oexpr(t_lex(*++t_wp)); | ||
285 | if (t_lex(*++t_wp) != RPAREN) | ||
286 | syntax(NULL, "closing paren expected"); | ||
287 | return res; | ||
288 | } | ||
289 | if (t_wp_op && t_wp_op->op_type == UNOP) { | ||
290 | /* unary expression */ | ||
291 | if (*++t_wp == NULL) | ||
292 | syntax(t_wp_op->op_text, "argument expected"); | ||
293 | switch (n) { | ||
294 | case STREZ: | ||
295 | return strlen(*t_wp) == 0; | ||
296 | case STRNZ: | ||
297 | return strlen(*t_wp) != 0; | ||
298 | case FILTT: | ||
299 | return isatty(getn(*t_wp)); | ||
300 | default: | ||
301 | return filstat(*t_wp, n); | ||
302 | } | ||
303 | } | ||
304 | |||
305 | if (t_lex(t_wp[1]), t_wp_op && t_wp_op->op_type == BINOP) { | ||
306 | return binop(); | ||
307 | } | ||
308 | |||
309 | return strlen(*t_wp) > 0; | ||
310 | } | ||
311 | |||
312 | static int | ||
313 | binop() | ||
314 | { | ||
315 | const char *opnd1, *opnd2; | ||
316 | struct t_op const *op; | ||
317 | |||
318 | opnd1 = *t_wp; | ||
319 | (void) t_lex(*++t_wp); | ||
320 | op = t_wp_op; | ||
321 | |||
322 | if ((opnd2 = *++t_wp) == (char *)0) | ||
323 | syntax(op->op_text, "argument expected"); | ||
324 | |||
325 | switch (op->op_num) { | ||
326 | case STREQ: | ||
327 | return strcmp(opnd1, opnd2) == 0; | ||
328 | case STRNE: | ||
329 | return strcmp(opnd1, opnd2) != 0; | ||
330 | case STRLT: | ||
331 | return strcmp(opnd1, opnd2) < 0; | ||
332 | case STRGT: | ||
333 | return strcmp(opnd1, opnd2) > 0; | ||
334 | case INTEQ: | ||
335 | return getn(opnd1) == getn(opnd2); | ||
336 | case INTNE: | ||
337 | return getn(opnd1) != getn(opnd2); | ||
338 | case INTGE: | ||
339 | return getn(opnd1) >= getn(opnd2); | ||
340 | case INTGT: | ||
341 | return getn(opnd1) > getn(opnd2); | ||
342 | case INTLE: | ||
343 | return getn(opnd1) <= getn(opnd2); | ||
344 | case INTLT: | ||
345 | return getn(opnd1) < getn(opnd2); | ||
346 | case FILNT: | ||
347 | return newerf (opnd1, opnd2); | ||
348 | case FILOT: | ||
349 | return olderf (opnd1, opnd2); | ||
350 | case FILEQ: | ||
351 | return equalf (opnd1, opnd2); | ||
352 | } | ||
353 | /* NOTREACHED */ | ||
354 | return 1; | ||
355 | } | ||
356 | |||
357 | static int | ||
358 | filstat(nm, mode) | ||
359 | char *nm; | ||
360 | enum token mode; | ||
361 | { | ||
362 | struct stat s; | ||
363 | int i; | ||
364 | |||
365 | if (mode == FILSYM) { | ||
366 | #ifdef S_IFLNK | ||
367 | if (lstat(nm, &s) == 0) { | ||
368 | i = S_IFLNK; | ||
369 | goto filetype; | ||
370 | } | ||
371 | #endif | ||
372 | return 0; | ||
373 | } | ||
374 | |||
375 | if (stat(nm, &s) != 0) | ||
376 | return 0; | ||
377 | |||
378 | switch (mode) { | ||
379 | case FILRD: | ||
380 | return test_eaccess(nm, R_OK) == 0; | ||
381 | case FILWR: | ||
382 | return test_eaccess(nm, W_OK) == 0; | ||
383 | case FILEX: | ||
384 | return test_eaccess(nm, X_OK) == 0; | ||
385 | case FILEXIST: | ||
386 | return 1; | ||
387 | case FILREG: | ||
388 | i = S_IFREG; | ||
389 | goto filetype; | ||
390 | case FILDIR: | ||
391 | i = S_IFDIR; | ||
392 | goto filetype; | ||
393 | case FILCDEV: | ||
394 | i = S_IFCHR; | ||
395 | goto filetype; | ||
396 | case FILBDEV: | ||
397 | i = S_IFBLK; | ||
398 | goto filetype; | ||
399 | case FILFIFO: | ||
400 | #ifdef S_IFIFO | ||
401 | i = S_IFIFO; | ||
402 | goto filetype; | ||
403 | #else | ||
404 | return 0; | ||
405 | #endif | ||
406 | case FILSOCK: | ||
407 | #ifdef S_IFSOCK | ||
408 | i = S_IFSOCK; | ||
409 | goto filetype; | ||
410 | #else | ||
411 | return 0; | ||
412 | #endif | ||
413 | case FILSUID: | ||
414 | i = S_ISUID; | ||
415 | goto filebit; | ||
416 | case FILSGID: | ||
417 | i = S_ISGID; | ||
418 | goto filebit; | ||
419 | case FILSTCK: | ||
420 | i = S_ISVTX; | ||
421 | goto filebit; | ||
422 | case FILGZ: | ||
423 | return s.st_size > 0L; | ||
424 | case FILUID: | ||
425 | return s.st_uid == geteuid(); | ||
426 | case FILGID: | ||
427 | return s.st_gid == getegid(); | ||
428 | default: | ||
429 | return 1; | ||
430 | } | ||
431 | |||
432 | filetype: | ||
433 | return ((s.st_mode & S_IFMT) == i); | ||
434 | |||
435 | filebit: | ||
436 | return ((s.st_mode & i) != 0); | ||
437 | } | ||
438 | |||
439 | static enum token | ||
440 | t_lex(s) | ||
441 | char *s; | ||
442 | { | ||
443 | struct t_op const *op = ops; | ||
444 | |||
445 | if (s == 0) { | ||
446 | t_wp_op = (struct t_op *)0; | ||
447 | return EOI; | ||
448 | } | ||
449 | while (op->op_text) { | ||
450 | if (strcmp(s, op->op_text) == 0) { | ||
451 | t_wp_op = op; | ||
452 | return op->op_num; | ||
453 | } | ||
454 | op++; | ||
455 | } | ||
456 | t_wp_op = (struct t_op *)0; | ||
457 | return OPERAND; | ||
458 | } | ||
459 | |||
460 | /* atoi with error detection */ | ||
461 | static int | ||
462 | getn(s) | ||
463 | char *s; | ||
464 | { | ||
465 | char *p; | ||
466 | long r; | ||
467 | |||
468 | errno = 0; | ||
469 | r = strtol(s, &p, 10); | ||
470 | |||
471 | if (errno != 0) | ||
472 | fatalError("%s: out of range", s); | ||
473 | |||
474 | while (isspace(*p)) | ||
475 | p++; | ||
476 | |||
477 | if (*p) | ||
478 | fatalError("%s: bad number", s); | ||
479 | |||
480 | return (int) r; | ||
481 | } | ||
482 | |||
483 | static int | ||
484 | newerf (f1, f2) | ||
485 | char *f1, *f2; | ||
486 | { | ||
487 | struct stat b1, b2; | ||
488 | |||
489 | return (stat (f1, &b1) == 0 && | ||
490 | stat (f2, &b2) == 0 && | ||
491 | b1.st_mtime > b2.st_mtime); | ||
492 | } | ||
493 | |||
494 | static int | ||
495 | olderf (f1, f2) | ||
496 | char *f1, *f2; | ||
497 | { | ||
498 | struct stat b1, b2; | ||
499 | |||
500 | return (stat (f1, &b1) == 0 && | ||
501 | stat (f2, &b2) == 0 && | ||
502 | b1.st_mtime < b2.st_mtime); | ||
503 | } | ||
504 | |||
505 | static int | ||
506 | equalf (f1, f2) | ||
507 | char *f1, *f2; | ||
508 | { | ||
509 | struct stat b1, b2; | ||
510 | |||
511 | return (stat (f1, &b1) == 0 && | ||
512 | stat (f2, &b2) == 0 && | ||
513 | b1.st_dev == b2.st_dev && | ||
514 | b1.st_ino == b2.st_ino); | ||
515 | } | ||
516 | |||
517 | /* Do the same thing access(2) does, but use the effective uid and gid, | ||
518 | and don't make the mistake of telling root that any file is | ||
519 | executable. */ | ||
520 | static int | ||
521 | test_eaccess (path, mode) | ||
522 | char *path; | ||
523 | int mode; | ||
524 | { | ||
525 | struct stat st; | ||
526 | int euid = geteuid(); | ||
527 | |||
528 | if (stat (path, &st) < 0) | ||
529 | return (-1); | ||
530 | |||
531 | if (euid == 0) { | ||
532 | /* Root can read or write any file. */ | ||
533 | if (mode != X_OK) | ||
534 | return (0); | ||
535 | |||
536 | /* Root can execute any file that has any one of the execute | ||
537 | bits set. */ | ||
538 | if (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) | ||
539 | return (0); | ||
540 | } | ||
541 | |||
542 | if (st.st_uid == euid) /* owner */ | ||
543 | mode <<= 6; | ||
544 | else if (is_a_group_member (st.st_gid)) | ||
545 | mode <<= 3; | ||
546 | |||
547 | if (st.st_mode & mode) | ||
548 | return (0); | ||
549 | |||
550 | return (-1); | ||
551 | } | ||
552 | |||
553 | static void | ||
554 | initialize_group_array () | ||
555 | { | ||
556 | ngroups = getgroups(0, NULL); | ||
557 | if ((group_array = realloc(group_array, ngroups * sizeof(gid_t))) == NULL) | ||
558 | fatalError("Out of space"); | ||
559 | |||
560 | getgroups(ngroups, group_array); | ||
561 | } | ||
562 | |||
563 | /* Return non-zero if GID is one that we have in our groups list. */ | ||
564 | static int | ||
565 | is_a_group_member (gid) | ||
566 | gid_t gid; | ||
567 | { | ||
568 | register int i; | ||
569 | |||
570 | /* Short-circuit if possible, maybe saving a call to getgroups(). */ | ||
571 | if (gid == getgid() || gid == getegid()) | ||
572 | return (1); | ||
573 | |||
574 | if (ngroups == 0) | ||
575 | initialize_group_array (); | ||
576 | |||
577 | /* Search through the list looking for GID. */ | ||
578 | for (i = 0; i < ngroups; i++) | ||
579 | if (gid == group_array[i]) | ||
580 | return (1); | ||
581 | |||
582 | return (0); | ||
583 | } | ||