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