]> git.ipfire.org Git - thirdparty/bash.git/blame - test.c~
commit bash-20121005 snapshot
[thirdparty/bash.git] / test.c~
CommitLineData
15623760
CR
1/* test.c - GNU test program (ksb and mjb) */
2
3/* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
4
5/* Copyright (C) 1987-2010 Free Software Foundation, Inc.
6
7 This file is part of GNU Bash, the Bourne Again SHell.
8
9 Bash is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 Bash is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23/* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
24 binary operators. */
25/* #define PATTERN_MATCHING */
26
27#if defined (HAVE_CONFIG_H)
28# include <config.h>
29#endif
30
31#include <stdio.h>
32
33#include "bashtypes.h"
34
35#if !defined (HAVE_LIMITS_H) && defined (HAVE_SYS_PARAM_H)
36# include <sys/param.h>
37#endif
38
39#if defined (HAVE_UNISTD_H)
40# include <unistd.h>
41#endif
42
43#include <errno.h>
44#if !defined (errno)
45extern int errno;
46#endif /* !errno */
47
48#if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H)
49# include <sys/file.h>
50#endif /* !_POSIX_VERSION */
51#include "posixstat.h"
52#include "filecntl.h"
53#include "stat-time.h"
54
55#include "bashintl.h"
56
57#include "shell.h"
58#include "pathexp.h"
59#include "test.h"
60#include "builtins/common.h"
61
62#include <glob/strmatch.h>
63
64#if !defined (STRLEN)
65# define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
66#endif
67
68#if !defined (STREQ)
69# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0)
70#endif /* !STREQ */
71#define STRCOLLEQ(a, b) ((a)[0] == (b)[0] && strcoll ((a), (b)) == 0)
72
73#if !defined (R_OK)
74#define R_OK 4
75#define W_OK 2
76#define X_OK 1
77#define F_OK 0
78#endif /* R_OK */
79
80#define EQ 0
81#define NE 1
82#define LT 2
83#define GT 3
84#define LE 4
85#define GE 5
86
87#define NT 0
88#define OT 1
89#define EF 2
90
91/* The following few defines control the truth and false output of each stage.
92 TRUE and FALSE are what we use to compute the final output value.
93 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
94 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
95#define TRUE 1
96#define FALSE 0
97#define SHELL_BOOLEAN(value) (!(value))
98
99#define TEST_ERREXIT_STATUS 2
100
101static procenv_t test_exit_buf;
102static int test_error_return;
103#define test_exit(val) \
104 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
105
106extern int sh_stat __P((const char *, struct stat *));
107
108static int pos; /* The offset of the current argument in ARGV. */
109static int argc; /* The number of arguments present in ARGV. */
110static char **argv; /* The argument list. */
111static int noeval;
112
113static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__));
114static void beyond __P((void)) __attribute__((__noreturn__));
115static void integer_expected_error __P((char *)) __attribute__((__noreturn__));
116
117static int unary_operator __P((void));
118static int binary_operator __P((void));
119static int two_arguments __P((void));
120static int three_arguments __P((void));
121static int posixtest __P((void));
122
123static int expr __P((void));
124static int term __P((void));
125static int and __P((void));
126static int or __P((void));
127
128static int filecomp __P((char *, char *, int));
129static int arithcomp __P((char *, char *, int, int));
130static int patcomp __P((char *, char *, int));
131
132static void
133test_syntax_error (format, arg)
134 char *format, *arg;
135{
136 builtin_error (format, arg);
137 test_exit (TEST_ERREXIT_STATUS);
138}
139
140/*
141 * beyond - call when we're beyond the end of the argument list (an
142 * error condition)
143 */
144static void
145beyond ()
146{
147 test_syntax_error (_("argument expected"), (char *)NULL);
148}
149
150/* Syntax error for when an integer argument was expected, but
151 something else was found. */
152static void
153integer_expected_error (pch)
154 char *pch;
155{
156 test_syntax_error (_("%s: integer expression expected"), pch);
157}
158
159/* Increment our position in the argument list. Check that we're not
160 past the end of the argument list. This check is supressed if the
161 argument is FALSE. Made a macro for efficiency. */
162#define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
163#define unary_advance() do { advance (1); ++pos; } while (0)
164
165/*
166 * expr:
167 * or
168 */
169static int
170expr ()
171{
172 if (pos >= argc)
173 beyond ();
174
175 return (FALSE ^ or ()); /* Same with this. */
176}
177
178/*
179 * or:
180 * and
181 * and '-o' or
182 */
183static int
184or ()
185{
186 int value, v2;
187
188 value = and ();
189 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
190 {
191 advance (0);
192 v2 = or ();
193 return (value || v2);
194 }
195
196 return (value);
197}
198
199/*
200 * and:
201 * term
202 * term '-a' and
203 */
204static int
205and ()
206{
207 int value, v2;
208
209 value = term ();
210 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
211 {
212 advance (0);
213 v2 = and ();
214 return (value && v2);
215 }
216 return (value);
217}
218
219/*
220 * term - parse a term and return 1 or 0 depending on whether the term
221 * evaluates to true or false, respectively.
222 *
223 * term ::=
224 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
225 * '-'('G'|'L'|'O'|'S'|'N') filename
226 * '-t' [int]
227 * '-'('z'|'n') string
228 * '-o' option
229 * string
230 * string ('!='|'='|'==') string
231 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
232 * file '-'(nt|ot|ef) file
233 * '(' <expr> ')'
234 * int ::=
235 * positive and negative integers
236 */
237static int
238term ()
239{
240 int value;
241
242 if (pos >= argc)
243 beyond ();
244
245 /* Deal with leading `not's. */
246 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
247 {
248 value = 0;
249 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
250 {
251 advance (1);
252 value = 1 - value;
253 }
254
255 return (value ? !term() : term());
256 }
257
258 /* A paren-bracketed argument. */
259 if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */
260 {
261 advance (1);
262 value = expr ();
263 if (argv[pos] == 0) /* ( */
264 test_syntax_error (_("`)' expected"), (char *)NULL);
265 else if (argv[pos][0] != ')' || argv[pos][1]) /* ( */
266 test_syntax_error (_("`)' expected, found %s"), argv[pos]);
267 advance (0);
268 return (value);
269 }
270
271 /* are there enough arguments left that this could be dyadic? */
272 if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
273 value = binary_operator ();
274
275 /* Might be a switch type argument */
276 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
277 {
278 if (test_unop (argv[pos]))
279 value = unary_operator ();
280 else
281 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
282 }
283 else
284 {
285 value = argv[pos][0] != '\0';
286 advance (0);
287 }
288
289 return (value);
290}
291
292static int
293stat_mtime (fn, st, ts)
294 char *fn;
295 struct stat *st;
296 struct timespec *ts;
297{
298 int r;
299
300 r = sh_stat (fn, st);
301 if (r < 0)
302 return r;
303 *ts = get_stat_mtime (st);
304 return 0;
305}
306
307static int
308filecomp (s, t, op)
309 char *s, *t;
310 int op;
311{
312 struct stat st1, st2;
313 struct timespec ts1, ts2;
314 int r1, r2;
315
316 if ((r1 = stat_mtime (s, &st1, &ts1)) < 0)
317 {
318 if (op == EF)
319 return (FALSE);
320 }
321 if ((r2 = stat_mtime (t, &st2, &ts2)) < 0)
322 {
323 if (op == EF)
324 return (FALSE);
325 }
326
327 switch (op)
328 {
329 case OT: return (r1 < r2 || (r2 == 0 && timespec_cmp (ts1, ts2) < 0));
330 case NT: return (r1 > r2 || (r1 == 0 && timespec_cmp (ts1, ts2) > 0));
331 case EF: return (same_file (s, t, &st1, &st2));
332 }
333 return (FALSE);
334}
335
336static int
337arithcomp (s, t, op, flags)
338 char *s, *t;
339 int op, flags;
340{
341 intmax_t l, r;
342 int expok;
343
344 if (flags & TEST_ARITHEXP)
345 {
346 l = evalexp (s, &expok);
347 if (expok == 0)
348 return (FALSE); /* should probably longjmp here */
349 r = evalexp (t, &expok);
350 if (expok == 0)
351 return (FALSE); /* ditto */
352 }
353 else
354 {
355 if (legal_number (s, &l) == 0)
356 integer_expected_error (s);
357 if (legal_number (t, &r) == 0)
358 integer_expected_error (t);
359 }
360
361 switch (op)
362 {
363 case EQ: return (l == r);
364 case NE: return (l != r);
365 case LT: return (l < r);
366 case GT: return (l > r);
367 case LE: return (l <= r);
368 case GE: return (l >= r);
369 }
370
371 return (FALSE);
372}
373
374static int
375patcomp (string, pat, op)
376 char *string, *pat;
377 int op;
378{
379 int m;
380
381 m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE);
382 return ((op == EQ) ? (m == 0) : (m != 0));
383}
384
385int
386binary_test (op, arg1, arg2, flags)
387 char *op, *arg1, *arg2;
388 int flags;
389{
390 int patmatch;
391
392 patmatch = (flags & TEST_PATMATCH);
393
394 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
395 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
396 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
397 {
398 if (shell_compatibility_level > 40 && flags & TEST_LOCALE)
399 return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0));
400 else
401 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
402 }
403 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
404 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
405
406
407 else if (op[2] == 't')
408 {
409 switch (op[1])
410 {
411 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
412 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
413 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
414 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
415 }
416 }
417 else if (op[1] == 'e')
418 {
419 switch (op[2])
420 {
421 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
422 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
423 }
424 }
425 else if (op[2] == 'e')
426 {
427 switch (op[1])
428 {
429 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
430 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
431 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
432 }
433 }
434
435 return (FALSE); /* should never get here */
436}
437
438
439static int
440binary_operator ()
441{
442 int value;
443 char *w;
444
445 w = argv[pos + 1];
446 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
447 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
448 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
449 {
450 value = binary_test (w, argv[pos], argv[pos + 2], 0);
451 pos += 3;
452 return (value);
453 }
454
455#if defined (PATTERN_MATCHING)
456 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
457 {
458 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
459 pos += 3;
460 return (value);
461 }
462#endif
463
464 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
465 {
466 test_syntax_error (_("%s: binary operator expected"), w);
467 /* NOTREACHED */
468 return (FALSE);
469 }
470
471 value = binary_test (w, argv[pos], argv[pos + 2], 0);
472 pos += 3;
473 return value;
474}
475
476static int
477unary_operator ()
478{
479 char *op;
480 intmax_t r;
481
482 op = argv[pos];
483 if (test_unop (op) == 0)
484 return (FALSE);
485
486 /* the only tricky case is `-t', which may or may not take an argument. */
487 if (op[1] == 't')
488 {
489 advance (0);
490 if (pos < argc)
491 {
492 if (legal_number (argv[pos], &r))
493 {
494 advance (0);
495 return (unary_test (op, argv[pos - 1]));
496 }
497 else
498 return (FALSE);
499 }
500 else
501 return (unary_test (op, "1"));
502 }
503
504 /* All of the unary operators take an argument, so we first call
505 unary_advance (), which checks to make sure that there is an
506 argument, and then advances pos right past it. This means that
507 pos - 1 is the location of the argument. */
508 unary_advance ();
509 return (unary_test (op, argv[pos - 1]));
510}
511
512int
513unary_test (op, arg)
514 char *op, *arg;
515{
516 intmax_t r;
517 struct stat stat_buf;
518 SHELL_VAR *v;
519
520 switch (op[1])
521 {
522 case 'a': /* file exists in the file system? */
523 case 'e':
524 return (sh_stat (arg, &stat_buf) == 0);
525
526 case 'r': /* file is readable? */
527 return (sh_eaccess (arg, R_OK) == 0);
528
529 case 'w': /* File is writeable? */
530 return (sh_eaccess (arg, W_OK) == 0);
531
532 case 'x': /* File is executable? */
533 return (sh_eaccess (arg, X_OK) == 0);
534
535 case 'O': /* File is owned by you? */
536 return (sh_stat (arg, &stat_buf) == 0 &&
537 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
538
539 case 'G': /* File is owned by your group? */
540 return (sh_stat (arg, &stat_buf) == 0 &&
541 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
542
543 case 'N':
544 return (sh_stat (arg, &stat_buf) == 0 &&
545 stat_buf.st_atime <= stat_buf.st_mtime);
546
547 case 'f': /* File is a file? */
548 if (sh_stat (arg, &stat_buf) < 0)
549 return (FALSE);
550
551 /* -f is true if the given file exists and is a regular file. */
552#if defined (S_IFMT)
553 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
554#else
555 return (S_ISREG (stat_buf.st_mode));
556#endif /* !S_IFMT */
557
558 case 'd': /* File is a directory? */
559 return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
560
561 case 's': /* File has something in it? */
562 return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
563
564 case 'S': /* File is a socket? */
565#if !defined (S_ISSOCK)
566 return (FALSE);
567#else
568 return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
569#endif /* S_ISSOCK */
570
571 case 'c': /* File is character special? */
572 return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
573
574 case 'b': /* File is block special? */
575 return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
576
577 case 'p': /* File is a named pipe? */
578#ifndef S_ISFIFO
579 return (FALSE);
580#else
581 return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
582#endif /* S_ISFIFO */
583
584 case 'L': /* Same as -h */
585 case 'h': /* File is a symbolic link? */
586#if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
587 return (FALSE);
588#else
589 return ((arg[0] != '\0') &&
590 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
591#endif /* S_IFLNK && HAVE_LSTAT */
592
593 case 'u': /* File is setuid? */
594 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
595
596 case 'g': /* File is setgid? */
597 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
598
599 case 'k': /* File has sticky bit set? */
600#if !defined (S_ISVTX)
601 /* This is not Posix, and is not defined on some Posix systems. */
602 return (FALSE);
603#else
604 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
605#endif
606
607 case 't': /* File fd is a terminal? */
608 if (legal_number (arg, &r) == 0)
609 return (FALSE);
610 return ((r == (int)r) && isatty ((int)r));
611
612 case 'n': /* True if arg has some length. */
613 return (arg[0] != '\0');
614
615 case 'z': /* True if arg has no length. */
616 return (arg[0] == '\0');
617
618 case 'o': /* True if option `arg' is set. */
619 return (minus_o_option_value (arg) == 1);
620
621 case 'v':
622 v = find_variable (arg);
623 return (v && var_isset (v) ? TRUE : FALSE);
624 }
625
626 /* We can't actually get here, but this shuts up gcc. */
627 return (FALSE);
628}
629
630/* Return TRUE if OP is one of the test command's binary operators. */
631int
632test_binop (op)
633 char *op;
634{
635 if (op[0] == '=' && op[1] == '\0')
636 return (1); /* '=' */
637 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
638 return (1);
639 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
640 return (1); /* `==' and `!=' */
641#if defined (PATTERN_MATCHING)
642 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
643 return (1);
644#endif
645 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
646 return (0);
647 else
648 {
649 if (op[2] == 't')
650 switch (op[1])
651 {
652 case 'n': /* -nt */
653 case 'o': /* -ot */
654 case 'l': /* -lt */
655 case 'g': /* -gt */
656 return (1);
657 default:
658 return (0);
659 }
660 else if (op[1] == 'e')
661 switch (op[2])
662 {
663 case 'q': /* -eq */
664 case 'f': /* -ef */
665 return (1);
666 default:
667 return (0);
668 }
669 else if (op[2] == 'e')
670 switch (op[1])
671 {
672 case 'n': /* -ne */
673 case 'g': /* -ge */
674 case 'l': /* -le */
675 return (1);
676 default:
677 return (0);
678 }
679 else
680 return (0);
681 }
682}
683
684/* Return non-zero if OP is one of the test command's unary operators. */
685int
686test_unop (op)
687 char *op;
688{
689 if (op[0] != '-' || op[2] != 0)
690 return (0);
691
692 switch (op[1])
693 {
694 case 'a': case 'b': case 'c': case 'd': case 'e':
695 case 'f': case 'g': case 'h': case 'k': case 'n':
696 case 'o': case 'p': case 'r': case 's': case 't':
697 case 'u': case 'v': case 'w': case 'x': case 'z':
698 case 'G': case 'L': case 'O': case 'S': case 'N':
699 return (1);
700 }
701
702 return (0);
703}
704
705static int
706two_arguments ()
707{
708 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
709 return (argv[pos + 1][0] == '\0');
710 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
711 {
712 if (test_unop (argv[pos]))
713 return (unary_operator ());
714 else
715 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
716 }
717 else
718 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
719
720 return (0);
721}
722
723#define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
724
725/* This could be augmented to handle `-t' as equivalent to `-t 1', but
726 POSIX requires that `-t' be given an argument. */
727#define ONE_ARG_TEST(s) ((s)[0] != '\0')
728
729static int
730three_arguments ()
731{
732 int value;
733
734 if (test_binop (argv[pos+1]))
735 {
736 value = binary_operator ();
737 pos = argc;
738 }
739 else if (ANDOR (argv[pos+1]))
740 {
741 if (argv[pos+1][1] == 'a')
742 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
743 else
744 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
745 pos = argc;
746 }
747 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
748 {
749 advance (1);
750 value = !two_arguments ();
751 }
752 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
753 {
754 value = ONE_ARG_TEST(argv[pos+1]);
755 pos = argc;
756 }
757 else
758 test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
759
760 return (value);
761}
762
763/* This is an implementation of a Posix.2 proposal by David Korn. */
764static int
765posixtest ()
766{
767 int value;
768
769 switch (argc - 1) /* one extra passed in */
770 {
771 case 0:
772 value = FALSE;
773 pos = argc;
774 break;
775
776 case 1:
777 value = ONE_ARG_TEST(argv[1]);
778 pos = argc;
779 break;
780
781 case 2:
782 value = two_arguments ();
783 pos = argc;
784 break;
785
786 case 3:
787 value = three_arguments ();
788 break;
789
790 case 4:
791 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
792 {
793 advance (1);
794 value = !three_arguments ();
795 break;
796 }
797 /* FALLTHROUGH */
798 default:
799 value = expr ();
800 }
801
802 return (value);
803}
804
805/*
806 * [:
807 * '[' expr ']'
808 * test:
809 * test expr
810 */
811int
812test_command (margc, margv)
813 int margc;
814 char **margv;
815{
816 int value;
817 int code;
818
819 USE_VAR(margc);
820
821 code = setjmp (test_exit_buf);
822
823 if (code)
824 return (test_error_return);
825
826 argv = margv;
827
828 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
829 {
830 --margc;
831
832 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
833 test_syntax_error (_("missing `]'"), (char *)NULL);
834
835 if (margc < 2)
836 test_exit (SHELL_BOOLEAN (FALSE));
837 }
838
839 argc = margc;
840 pos = 1;
841
842 if (pos >= argc)
843 test_exit (SHELL_BOOLEAN (FALSE));
844
845 noeval = 0;
846 value = posixtest ();
847
848 if (pos != argc)
849 test_syntax_error (_("too many arguments"), (char *)NULL);
850
851 test_exit (SHELL_BOOLEAN (value));
852}