blob: 4cb3343cda15171b22d145ce68e9e88f44210a7c [file] [log] [blame]
Jari Aalto31859422009-01-12 13:36:28 +00001/* test.c - GNU test program (ksb and mjb) */
Jari Aalto726f6381996-08-26 18:22:31 +00002
3/* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
4
Chet Ramey495aee42011-11-22 19:11:26 -05005/* Copyright (C) 1987-2010 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00006
7 This file is part of GNU Bash, the Bourne Again SHell.
8
Jari Aalto31859422009-01-12 13:36:28 +00009 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.
Jari Aalto726f6381996-08-26 18:22:31 +000013
Jari Aalto31859422009-01-12 13:36:28 +000014 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.
Jari Aalto726f6381996-08-26 18:22:31 +000018
Jari Aalto31859422009-01-12 13:36:28 +000019 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*/
Jari Aalto726f6381996-08-26 18:22:31 +000022
Jari Aaltoccc6cda1996-12-23 17:02:34 +000023/* 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
Jari Aalto726f6381996-08-26 18:22:31 +000031#include <stdio.h>
Jari Aaltoccc6cda1996-12-23 17:02:34 +000032
Jari Aaltod166f041997-06-05 14:59:13 +000033#include "bashtypes.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000034
Chet Rameyac50fba2014-02-26 09:36:43 -050035#if !defined (HAVE_LIMITS_H) && defined (HAVE_SYS_PARAM_H)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000036# include <sys/param.h>
37#endif
38
39#if defined (HAVE_UNISTD_H)
40# include <unistd.h>
41#endif
42
Jari Aaltoe8ce7751997-09-22 20:22:27 +000043#include <errno.h>
44#if !defined (errno)
45extern int errno;
46#endif /* !errno */
47
Jari Aaltob80f6442004-07-27 13:29:18 +000048#if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H)
Jari Aaltoccc6cda1996-12-23 17:02:34 +000049# include <sys/file.h>
50#endif /* !_POSIX_VERSION */
51#include "posixstat.h"
52#include "filecntl.h"
Chet Rameyac50fba2014-02-26 09:36:43 -050053#include "stat-time.h"
Jari Aalto726f6381996-08-26 18:22:31 +000054
Jari Aaltob80f6442004-07-27 13:29:18 +000055#include "bashintl.h"
56
Jari Aaltod166f041997-06-05 14:59:13 +000057#include "shell.h"
Jari Aaltocce855b1998-04-17 19:52:44 +000058#include "pathexp.h"
59#include "test.h"
Jari Aaltod166f041997-06-05 14:59:13 +000060#include "builtins/common.h"
Jari Aalto726f6381996-08-26 18:22:31 +000061
Jari Aaltof73dda02001-11-13 17:56:06 +000062#include <glob/strmatch.h>
Jari Aaltocce855b1998-04-17 19:52:44 +000063
Jari Aalto726f6381996-08-26 18:22:31 +000064#if !defined (STRLEN)
65# define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
66#endif
67
Jari Aalto726f6381996-08-26 18:22:31 +000068#if !defined (STREQ)
Chet Ramey00018032011-11-21 20:51:19 -050069# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +000070#endif /* !STREQ */
Chet Ramey00018032011-11-21 20:51:19 -050071#define STRCOLLEQ(a, b) ((a)[0] == (b)[0] && strcoll ((a), (b)) == 0)
Jari Aalto726f6381996-08-26 18:22:31 +000072
Jari Aalto726f6381996-08-26 18:22:31 +000073#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
Jari Aaltoccc6cda1996-12-23 17:02:34 +000080#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
Jari Aalto726f6381996-08-26 18:22:31 +000091/* 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.
Jari Aaltoccc6cda1996-12-23 17:02:34 +000094 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
Jari Aalto726f6381996-08-26 18:22:31 +000095#define TRUE 1
96#define FALSE 0
97#define SHELL_BOOLEAN(value) (!(value))
Jari Aalto726f6381996-08-26 18:22:31 +000098
Jari Aaltob72432f1999-02-19 17:11:39 +000099#define TEST_ERREXIT_STATUS 2
100
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000101static procenv_t test_exit_buf;
102static int test_error_return;
Jari Aaltod166f041997-06-05 14:59:13 +0000103#define test_exit(val) \
Chet Rameya0c0a002016-09-15 16:59:08 -0400104 do { test_error_return = val; sh_longjmp (test_exit_buf, 1); } while (0)
Jari Aalto726f6381996-08-26 18:22:31 +0000105
Jari Aalto06285672006-10-10 14:15:34 +0000106extern int sh_stat __P((const char *, struct stat *));
Jari Aalto726f6381996-08-26 18:22:31 +0000107
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
Jari Aaltof73dda02001-11-13 17:56:06 +0000113static 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__));
Jari Aalto726f6381996-08-26 18:22:31 +0000116
Jari Aaltof73dda02001-11-13 17:56:06 +0000117static 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));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000131
Jari Aalto726f6381996-08-26 18:22:31 +0000132static void
133test_syntax_error (format, arg)
134 char *format, *arg;
135{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000136 builtin_error (format, arg);
Jari Aaltob72432f1999-02-19 17:11:39 +0000137 test_exit (TEST_ERREXIT_STATUS);
Jari Aalto726f6381996-08-26 18:22:31 +0000138}
139
Jari Aaltocce855b1998-04-17 19:52:44 +0000140/*
141 * beyond - call when we're beyond the end of the argument list (an
142 * error condition)
143 */
144static void
145beyond ()
146{
Jari Aaltob80f6442004-07-27 13:29:18 +0000147 test_syntax_error (_("argument expected"), (char *)NULL);
Jari Aaltocce855b1998-04-17 19:52:44 +0000148}
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{
Jari Aaltob80f6442004-07-27 13:29:18 +0000156 test_syntax_error (_("%s: integer expression expected"), pch);
Jari Aaltocce855b1998-04-17 19:52:44 +0000157}
158
Jari Aalto726f6381996-08-26 18:22:31 +0000159/* Increment our position in the argument list. Check that we're not
Chet Rameyac50fba2014-02-26 09:36:43 -0500160 past the end of the argument list. This check is suppressed if the
Jari Aalto726f6381996-08-26 18:22:31 +0000161 argument is FALSE. Made a macro for efficiency. */
Jari Aalto726f6381996-08-26 18:22:31 +0000162#define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
Jari Aalto726f6381996-08-26 18:22:31 +0000163#define unary_advance() do { advance (1); ++pos; } while (0)
164
165/*
Jari Aaltocce855b1998-04-17 19:52:44 +0000166 * expr:
167 * or
Jari Aalto726f6381996-08-26 18:22:31 +0000168 */
Jari Aaltocce855b1998-04-17 19:52:44 +0000169static int
170expr ()
Jari Aalto726f6381996-08-26 18:22:31 +0000171{
Jari Aaltocce855b1998-04-17 19:52:44 +0000172 if (pos >= argc)
173 beyond ();
174
175 return (FALSE ^ or ()); /* Same with this. */
Jari Aalto726f6381996-08-26 18:22:31 +0000176}
177
Jari Aaltocce855b1998-04-17 19:52:44 +0000178/*
179 * or:
180 * and
181 * and '-o' or
182 */
183static int
184or ()
Jari Aalto726f6381996-08-26 18:22:31 +0000185{
Jari Aaltocce855b1998-04-17 19:52:44 +0000186 int value, v2;
187
188 value = and ();
Jari Aaltof73dda02001-11-13 17:56:06 +0000189 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
Jari Aaltocce855b1998-04-17 19:52:44 +0000190 {
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 ();
Jari Aaltof73dda02001-11-13 17:56:06 +0000210 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
Jari Aaltocce855b1998-04-17 19:52:44 +0000211 {
212 advance (0);
213 v2 = and ();
214 return (value && v2);
215 }
216 return (value);
Jari Aalto726f6381996-08-26 18:22:31 +0000217}
218
Jari Aalto726f6381996-08-26 18:22:31 +0000219/*
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 ::=
Jari Aaltocce855b1998-04-17 19:52:44 +0000224 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
225 * '-'('G'|'L'|'O'|'S'|'N') filename
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000226 * '-t' [int]
Jari Aalto726f6381996-08-26 18:22:31 +0000227 * '-'('z'|'n') string
Chet Rameya0c0a002016-09-15 16:59:08 -0400228 * '-'('v'|'R') varname
Jari Aaltocce855b1998-04-17 19:52:44 +0000229 * '-o' option
Jari Aalto726f6381996-08-26 18:22:31 +0000230 * string
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000231 * string ('!='|'='|'==') string
Jari Aalto726f6381996-08-26 18:22:31 +0000232 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
233 * file '-'(nt|ot|ef) file
234 * '(' <expr> ')'
235 * int ::=
Jari Aalto726f6381996-08-26 18:22:31 +0000236 * positive and negative integers
237 */
238static int
239term ()
240{
241 int value;
242
243 if (pos >= argc)
244 beyond ();
245
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000246 /* Deal with leading `not's. */
247 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000248 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000249 value = 0;
250 while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000251 {
252 advance (1);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000253 value = 1 - value;
Jari Aalto726f6381996-08-26 18:22:31 +0000254 }
255
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000256 return (value ? !term() : term());
Jari Aalto726f6381996-08-26 18:22:31 +0000257 }
258
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000259 /* A paren-bracketed argument. */
Jari Aaltob80f6442004-07-27 13:29:18 +0000260 if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */
Jari Aalto726f6381996-08-26 18:22:31 +0000261 {
262 advance (1);
263 value = expr ();
Jari Aaltob80f6442004-07-27 13:29:18 +0000264 if (argv[pos] == 0) /* ( */
265 test_syntax_error (_("`)' expected"), (char *)NULL);
266 else if (argv[pos][0] != ')' || argv[pos][1]) /* ( */
267 test_syntax_error (_("`)' expected, found %s"), argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000268 advance (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000269 return (value);
Jari Aalto726f6381996-08-26 18:22:31 +0000270 }
271
272 /* are there enough arguments left that this could be dyadic? */
Jari Aaltocce855b1998-04-17 19:52:44 +0000273 if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
Jari Aalto726f6381996-08-26 18:22:31 +0000274 value = binary_operator ();
275
276 /* Might be a switch type argument */
Chet Rameyd233b482019-01-07 09:27:52 -0500277 else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000278 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000279 if (test_unop (argv[pos]))
Jari Aalto726f6381996-08-26 18:22:31 +0000280 value = unary_operator ();
281 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000282 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000283 }
284 else
285 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000286 value = argv[pos][0] != '\0';
Jari Aalto726f6381996-08-26 18:22:31 +0000287 advance (0);
288 }
289
290 return (value);
291}
292
293static int
Chet Rameyac50fba2014-02-26 09:36:43 -0500294stat_mtime (fn, st, ts)
295 char *fn;
296 struct stat *st;
297 struct timespec *ts;
298{
299 int r;
300
301 r = sh_stat (fn, st);
302 if (r < 0)
303 return r;
304 *ts = get_stat_mtime (st);
305 return 0;
306}
307
308static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000309filecomp (s, t, op)
310 char *s, *t;
311 int op;
Jari Aalto726f6381996-08-26 18:22:31 +0000312{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000313 struct stat st1, st2;
Chet Rameyac50fba2014-02-26 09:36:43 -0500314 struct timespec ts1, ts2;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000315 int r1, r2;
Jari Aalto726f6381996-08-26 18:22:31 +0000316
Chet Rameyac50fba2014-02-26 09:36:43 -0500317 if ((r1 = stat_mtime (s, &st1, &ts1)) < 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000318 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000319 if (op == EF)
320 return (FALSE);
321 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500322 if ((r2 = stat_mtime (t, &st2, &ts2)) < 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000323 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000324 if (op == EF)
325 return (FALSE);
326 }
327
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000328 switch (op)
Jari Aalto726f6381996-08-26 18:22:31 +0000329 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500330 case OT: return (r1 < r2 || (r2 == 0 && timespec_cmp (ts1, ts2) < 0));
331 case NT: return (r1 > r2 || (r1 == 0 && timespec_cmp (ts1, ts2) > 0));
Jari Aalto31859422009-01-12 13:36:28 +0000332 case EF: return (same_file (s, t, &st1, &st2));
Jari Aalto726f6381996-08-26 18:22:31 +0000333 }
334 return (FALSE);
335}
336
337static int
Jari Aaltocce855b1998-04-17 19:52:44 +0000338arithcomp (s, t, op, flags)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000339 char *s, *t;
Jari Aaltocce855b1998-04-17 19:52:44 +0000340 int op, flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000341{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000342 intmax_t l, r;
Jari Aaltocce855b1998-04-17 19:52:44 +0000343 int expok;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000344
Jari Aaltocce855b1998-04-17 19:52:44 +0000345 if (flags & TEST_ARITHEXP)
346 {
Chet Rameyd233b482019-01-07 09:27:52 -0500347 l = evalexp (s, 0, &expok);
Jari Aaltocce855b1998-04-17 19:52:44 +0000348 if (expok == 0)
349 return (FALSE); /* should probably longjmp here */
Chet Rameyd233b482019-01-07 09:27:52 -0500350 r = evalexp (t, 0, &expok);
Jari Aaltocce855b1998-04-17 19:52:44 +0000351 if (expok == 0)
352 return (FALSE); /* ditto */
353 }
354 else
355 {
356 if (legal_number (s, &l) == 0)
357 integer_expected_error (s);
358 if (legal_number (t, &r) == 0)
359 integer_expected_error (t);
360 }
361
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000362 switch (op)
363 {
364 case EQ: return (l == r);
365 case NE: return (l != r);
366 case LT: return (l < r);
367 case GT: return (l > r);
368 case LE: return (l <= r);
369 case GE: return (l >= r);
370 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000371
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000372 return (FALSE);
373}
374
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000375static int
376patcomp (string, pat, op)
377 char *string, *pat;
378 int op;
379{
380 int m;
381
Jari Aalto95732b42005-12-07 14:08:12 +0000382 m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE);
Jari Aaltocce855b1998-04-17 19:52:44 +0000383 return ((op == EQ) ? (m == 0) : (m != 0));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000384}
Jari Aaltocce855b1998-04-17 19:52:44 +0000385
386int
387binary_test (op, arg1, arg2, flags)
388 char *op, *arg1, *arg2;
389 int flags;
390{
391 int patmatch;
392
393 patmatch = (flags & TEST_PATMATCH);
394
395 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
396 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
Jari Aaltocce855b1998-04-17 19:52:44 +0000397 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
Chet Ramey00018032011-11-21 20:51:19 -0500398 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500399#if defined (HAVE_STRCOLL)
Chet Ramey00018032011-11-21 20:51:19 -0500400 if (shell_compatibility_level > 40 && flags & TEST_LOCALE)
401 return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0));
402 else
Chet Rameyac50fba2014-02-26 09:36:43 -0500403#endif
Chet Ramey00018032011-11-21 20:51:19 -0500404 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
405 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000406 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
407 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
Chet Ramey00018032011-11-21 20:51:19 -0500408
Jari Aaltocce855b1998-04-17 19:52:44 +0000409
410 else if (op[2] == 't')
411 {
412 switch (op[1])
413 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000414 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
415 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
Jari Aaltocce855b1998-04-17 19:52:44 +0000416 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
417 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
418 }
419 }
420 else if (op[1] == 'e')
421 {
422 switch (op[2])
423 {
424 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
425 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
426 }
427 }
428 else if (op[2] == 'e')
429 {
430 switch (op[1])
431 {
432 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
433 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
434 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
435 }
436 }
437
438 return (FALSE); /* should never get here */
439}
440
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000441
442static int
443binary_operator ()
444{
445 int value;
446 char *w;
447
448 w = argv[pos + 1];
Jari Aaltocce855b1998-04-17 19:52:44 +0000449 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
450 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
451 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000452 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000453 value = binary_test (w, argv[pos], argv[pos + 2], 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000454 pos += 3;
455 return (value);
456 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000457
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000458#if defined (PATTERN_MATCHING)
459 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
460 {
461 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
462 pos += 3;
463 return (value);
464 }
465#endif
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000466
Jari Aaltocce855b1998-04-17 19:52:44 +0000467 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000468 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000469 test_syntax_error (_("%s: binary operator expected"), w);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000470 /* NOTREACHED */
471 return (FALSE);
472 }
473
Jari Aaltocce855b1998-04-17 19:52:44 +0000474 value = binary_test (w, argv[pos], argv[pos + 2], 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000475 pos += 3;
476 return value;
477}
478
479static int
Jari Aalto726f6381996-08-26 18:22:31 +0000480unary_operator ()
481{
Jari Aaltof73dda02001-11-13 17:56:06 +0000482 char *op;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000483 intmax_t r;
Jari Aaltocce855b1998-04-17 19:52:44 +0000484
485 op = argv[pos];
486 if (test_unop (op) == 0)
487 return (FALSE);
488
489 /* the only tricky case is `-t', which may or may not take an argument. */
490 if (op[1] == 't')
491 {
492 advance (0);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000493 if (pos < argc)
Jari Aaltocce855b1998-04-17 19:52:44 +0000494 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000495 if (legal_number (argv[pos], &r))
496 {
497 advance (0);
498 return (unary_test (op, argv[pos - 1]));
499 }
500 else
501 return (FALSE);
Jari Aaltocce855b1998-04-17 19:52:44 +0000502 }
503 else
504 return (unary_test (op, "1"));
505 }
506
507 /* All of the unary operators take an argument, so we first call
508 unary_advance (), which checks to make sure that there is an
509 argument, and then advances pos right past it. This means that
510 pos - 1 is the location of the argument. */
511 unary_advance ();
512 return (unary_test (op, argv[pos - 1]));
513}
514
515int
516unary_test (op, arg)
517 char *op, *arg;
518{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000519 intmax_t r;
Jari Aalto726f6381996-08-26 18:22:31 +0000520 struct stat stat_buf;
Chet Ramey495aee42011-11-22 19:11:26 -0500521 SHELL_VAR *v;
Jari Aaltocce855b1998-04-17 19:52:44 +0000522
523 switch (op[1])
Jari Aalto726f6381996-08-26 18:22:31 +0000524 {
Jari Aalto726f6381996-08-26 18:22:31 +0000525 case 'a': /* file exists in the file system? */
526 case 'e':
Jari Aalto06285672006-10-10 14:15:34 +0000527 return (sh_stat (arg, &stat_buf) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000528
529 case 'r': /* file is readable? */
Jari Aalto06285672006-10-10 14:15:34 +0000530 return (sh_eaccess (arg, R_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000531
532 case 'w': /* File is writeable? */
Jari Aalto06285672006-10-10 14:15:34 +0000533 return (sh_eaccess (arg, W_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000534
535 case 'x': /* File is executable? */
Jari Aalto06285672006-10-10 14:15:34 +0000536 return (sh_eaccess (arg, X_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000537
538 case 'O': /* File is owned by you? */
Jari Aalto06285672006-10-10 14:15:34 +0000539 return (sh_stat (arg, &stat_buf) == 0 &&
Jari Aaltod166f041997-06-05 14:59:13 +0000540 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
Jari Aalto726f6381996-08-26 18:22:31 +0000541
542 case 'G': /* File is owned by your group? */
Jari Aalto06285672006-10-10 14:15:34 +0000543 return (sh_stat (arg, &stat_buf) == 0 &&
Jari Aaltod166f041997-06-05 14:59:13 +0000544 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
Jari Aalto726f6381996-08-26 18:22:31 +0000545
Jari Aaltocce855b1998-04-17 19:52:44 +0000546 case 'N':
Jari Aalto06285672006-10-10 14:15:34 +0000547 return (sh_stat (arg, &stat_buf) == 0 &&
Jari Aaltocce855b1998-04-17 19:52:44 +0000548 stat_buf.st_atime <= stat_buf.st_mtime);
549
Jari Aalto726f6381996-08-26 18:22:31 +0000550 case 'f': /* File is a file? */
Jari Aalto06285672006-10-10 14:15:34 +0000551 if (sh_stat (arg, &stat_buf) < 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000552 return (FALSE);
553
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000554 /* -f is true if the given file exists and is a regular file. */
Jari Aalto726f6381996-08-26 18:22:31 +0000555#if defined (S_IFMT)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000556 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000557#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000558 return (S_ISREG (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000559#endif /* !S_IFMT */
560
561 case 'd': /* File is a directory? */
Jari Aalto06285672006-10-10 14:15:34 +0000562 return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
Jari Aalto726f6381996-08-26 18:22:31 +0000563
564 case 's': /* File has something in it? */
Jari Aalto06285672006-10-10 14:15:34 +0000565 return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000566
567 case 'S': /* File is a socket? */
568#if !defined (S_ISSOCK)
569 return (FALSE);
570#else
Jari Aalto06285672006-10-10 14:15:34 +0000571 return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000572#endif /* S_ISSOCK */
Jari Aalto726f6381996-08-26 18:22:31 +0000573
574 case 'c': /* File is character special? */
Jari Aalto06285672006-10-10 14:15:34 +0000575 return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000576
577 case 'b': /* File is block special? */
Jari Aalto06285672006-10-10 14:15:34 +0000578 return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000579
580 case 'p': /* File is a named pipe? */
Jari Aalto726f6381996-08-26 18:22:31 +0000581#ifndef S_ISFIFO
582 return (FALSE);
583#else
Jari Aalto06285672006-10-10 14:15:34 +0000584 return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000585#endif /* S_ISFIFO */
Jari Aalto726f6381996-08-26 18:22:31 +0000586
587 case 'L': /* Same as -h */
Jari Aalto726f6381996-08-26 18:22:31 +0000588 case 'h': /* File is a symbolic link? */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000589#if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
Jari Aalto726f6381996-08-26 18:22:31 +0000590 return (FALSE);
591#else
Jari Aaltocce855b1998-04-17 19:52:44 +0000592 return ((arg[0] != '\0') &&
593 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000594#endif /* S_IFLNK && HAVE_LSTAT */
Jari Aalto726f6381996-08-26 18:22:31 +0000595
596 case 'u': /* File is setuid? */
Jari Aalto06285672006-10-10 14:15:34 +0000597 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000598
599 case 'g': /* File is setgid? */
Jari Aalto06285672006-10-10 14:15:34 +0000600 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000601
602 case 'k': /* File has sticky bit set? */
Jari Aalto726f6381996-08-26 18:22:31 +0000603#if !defined (S_ISVTX)
604 /* This is not Posix, and is not defined on some Posix systems. */
605 return (FALSE);
606#else
Jari Aalto06285672006-10-10 14:15:34 +0000607 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000608#endif
609
Jari Aaltocce855b1998-04-17 19:52:44 +0000610 case 't': /* File fd is a terminal? */
611 if (legal_number (arg, &r) == 0)
612 return (FALSE);
Jari Aaltof73dda02001-11-13 17:56:06 +0000613 return ((r == (int)r) && isatty ((int)r));
Jari Aalto726f6381996-08-26 18:22:31 +0000614
615 case 'n': /* True if arg has some length. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000616 return (arg[0] != '\0');
Jari Aalto726f6381996-08-26 18:22:31 +0000617
618 case 'z': /* True if arg has no length. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000619 return (arg[0] == '\0');
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000620
Jari Aaltocce855b1998-04-17 19:52:44 +0000621 case 'o': /* True if option `arg' is set. */
622 return (minus_o_option_value (arg) == 1);
Chet Ramey495aee42011-11-22 19:11:26 -0500623
624 case 'v':
Chet Rameyac50fba2014-02-26 09:36:43 -0500625#if defined (ARRAY_VARS)
Chet Rameyd233b482019-01-07 09:27:52 -0500626 if (valid_array_reference (arg, 0))
Chet Rameyac50fba2014-02-26 09:36:43 -0500627 {
628 char *t;
Chet Rameyd233b482019-01-07 09:27:52 -0500629 int rtype, ret;
630 t = array_value (arg, 0, 0, &rtype, (arrayind_t *)0);
631 ret = t ? TRUE : FALSE;
632 if (rtype > 0) /* subscript is * or @ */
633 free (t);
634 return ret;
Chet Rameyac50fba2014-02-26 09:36:43 -0500635 }
Chet Rameyd233b482019-01-07 09:27:52 -0500636 v = find_variable (arg);
637 if (v && invisible_p (v) == 0 && array_p (v))
Chet Rameyac50fba2014-02-26 09:36:43 -0500638 {
639 char *t;
640 /* [[ -v foo ]] == [[ -v foo[0] ]] */
641 t = array_reference (array_cell (v), 0);
642 return (t ? TRUE : FALSE);
643 }
644 else if (v && invisible_p (v) == 0 && assoc_p (v))
645 {
646 char *t;
647 t = assoc_reference (assoc_cell (v), "0");
648 return (t ? TRUE : FALSE);
649 }
Chet Rameyd233b482019-01-07 09:27:52 -0500650#else
651 v = find_variable (arg);
Chet Rameyac50fba2014-02-26 09:36:43 -0500652#endif
653 return (v && invisible_p (v) == 0 && var_isset (v) ? TRUE : FALSE);
654
655 case 'R':
Chet Rameyf5c82372014-03-28 11:52:47 -0400656 v = find_variable_noref (arg);
657 return ((v && invisible_p (v) == 0 && var_isset (v) && nameref_p (v)) ? TRUE : FALSE);
Jari Aalto726f6381996-08-26 18:22:31 +0000658 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000659
660 /* We can't actually get here, but this shuts up gcc. */
661 return (FALSE);
Jari Aalto726f6381996-08-26 18:22:31 +0000662}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000663
Jari Aaltocce855b1998-04-17 19:52:44 +0000664/* Return TRUE if OP is one of the test command's binary operators. */
665int
666test_binop (op)
667 char *op;
Jari Aalto726f6381996-08-26 18:22:31 +0000668{
Jari Aaltocce855b1998-04-17 19:52:44 +0000669 if (op[0] == '=' && op[1] == '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000670 return (1); /* '=' */
Jari Aaltocce855b1998-04-17 19:52:44 +0000671 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000672 return (1);
Jari Aaltocce855b1998-04-17 19:52:44 +0000673 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000674 return (1); /* `==' and `!=' */
675#if defined (PATTERN_MATCHING)
Jari Aaltocce855b1998-04-17 19:52:44 +0000676 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000677 return (1);
678#endif
Chet Rameyd233b482019-01-07 09:27:52 -0500679 else if (op[0] != '-' || op[1] == '\0' || op[2] == '\0' || op[3] != '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000680 return (0);
681 else
682 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000683 if (op[2] == 't')
684 switch (op[1])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000685 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000686 case 'n': /* -nt */
687 case 'o': /* -ot */
688 case 'l': /* -lt */
689 case 'g': /* -gt */
690 return (1);
691 default:
692 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000693 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000694 else if (op[1] == 'e')
695 switch (op[2])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000696 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000697 case 'q': /* -eq */
698 case 'f': /* -ef */
699 return (1);
700 default:
701 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000702 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000703 else if (op[2] == 'e')
704 switch (op[1])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000705 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000706 case 'n': /* -ne */
707 case 'g': /* -ge */
708 case 'l': /* -le */
709 return (1);
710 default:
711 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000712 }
713 else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000714 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000715 }
Jari Aalto726f6381996-08-26 18:22:31 +0000716}
717
718/* Return non-zero if OP is one of the test command's unary operators. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000719int
720test_unop (op)
721 char *op;
Jari Aalto726f6381996-08-26 18:22:31 +0000722{
Chet Rameyd233b482019-01-07 09:27:52 -0500723 if (op[0] != '-' || (op[1] && op[2] != 0))
Jari Aaltocce855b1998-04-17 19:52:44 +0000724 return (0);
725
726 switch (op[1])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000727 {
728 case 'a': case 'b': case 'c': case 'd': case 'e':
729 case 'f': case 'g': case 'h': case 'k': case 'n':
Jari Aaltocce855b1998-04-17 19:52:44 +0000730 case 'o': case 'p': case 'r': case 's': case 't':
Chet Ramey495aee42011-11-22 19:11:26 -0500731 case 'u': case 'v': case 'w': case 'x': case 'z':
Jari Aaltocce855b1998-04-17 19:52:44 +0000732 case 'G': case 'L': case 'O': case 'S': case 'N':
Chet Rameyf5c82372014-03-28 11:52:47 -0400733 case 'R':
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000734 return (1);
735 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000736
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000737 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000738}
739
740static int
741two_arguments ()
742{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000743 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
744 return (argv[pos + 1][0] == '\0');
Chet Rameyd233b482019-01-07 09:27:52 -0500745 else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000746 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000747 if (test_unop (argv[pos]))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000748 return (unary_operator ());
Jari Aalto726f6381996-08-26 18:22:31 +0000749 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000750 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000751 }
752 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000753 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000754
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000755 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000756}
757
Chet Rameyd233b482019-01-07 09:27:52 -0500758#define ANDOR(s) (s[0] == '-' && (s[1] == 'a' || s[1] == 'o') && s[2] == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000759
Jari Aaltof73dda02001-11-13 17:56:06 +0000760/* This could be augmented to handle `-t' as equivalent to `-t 1', but
761 POSIX requires that `-t' be given an argument. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000762#define ONE_ARG_TEST(s) ((s)[0] != '\0')
763
Jari Aalto726f6381996-08-26 18:22:31 +0000764static int
765three_arguments ()
766{
767 int value;
768
Jari Aaltocce855b1998-04-17 19:52:44 +0000769 if (test_binop (argv[pos+1]))
Jari Aalto726f6381996-08-26 18:22:31 +0000770 {
771 value = binary_operator ();
772 pos = argc;
773 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000774 else if (ANDOR (argv[pos+1]))
775 {
776 if (argv[pos+1][1] == 'a')
Jari Aalto28ef6c32001-04-06 19:14:31 +0000777 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000778 else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000779 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000780 pos = argc;
781 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000782 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000783 {
784 advance (1);
785 value = !two_arguments ();
786 }
787 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
788 {
789 value = ONE_ARG_TEST(argv[pos+1]);
790 pos = argc;
791 }
Jari Aalto726f6381996-08-26 18:22:31 +0000792 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000793 test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000794
Jari Aalto726f6381996-08-26 18:22:31 +0000795 return (value);
796}
797
798/* This is an implementation of a Posix.2 proposal by David Korn. */
799static int
800posixtest ()
801{
802 int value;
803
804 switch (argc - 1) /* one extra passed in */
805 {
806 case 0:
807 value = FALSE;
808 pos = argc;
809 break;
810
811 case 1:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000812 value = ONE_ARG_TEST(argv[1]);
Jari Aalto726f6381996-08-26 18:22:31 +0000813 pos = argc;
814 break;
815
816 case 2:
817 value = two_arguments ();
818 pos = argc;
819 break;
820
821 case 3:
822 value = three_arguments ();
823 break;
824
825 case 4:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000826 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000827 {
828 advance (1);
829 value = !three_arguments ();
830 break;
831 }
Chet Rameya0c0a002016-09-15 16:59:08 -0400832 else if (argv[pos][0] == '(' && argv[pos][1] == '\0' && argv[argc-1][0] == ')' && argv[argc-1][1] == '\0')
833 {
834 advance (1);
835 value = two_arguments ();
836 pos = argc;
837 break;
838 }
Jari Aalto726f6381996-08-26 18:22:31 +0000839 /* FALLTHROUGH */
Jari Aalto726f6381996-08-26 18:22:31 +0000840 default:
841 value = expr ();
842 }
843
844 return (value);
845}
846
847/*
848 * [:
849 * '[' expr ']'
850 * test:
851 * test expr
852 */
853int
Jari Aaltod166f041997-06-05 14:59:13 +0000854test_command (margc, margv)
Jari Aalto726f6381996-08-26 18:22:31 +0000855 int margc;
856 char **margv;
857{
858 int value;
Jari Aalto726f6381996-08-26 18:22:31 +0000859 int code;
860
Jari Aaltof73dda02001-11-13 17:56:06 +0000861 USE_VAR(margc);
862
Chet Rameyac50fba2014-02-26 09:36:43 -0500863 code = setjmp_nosigs (test_exit_buf);
Jari Aalto726f6381996-08-26 18:22:31 +0000864
865 if (code)
866 return (test_error_return);
Jari Aalto726f6381996-08-26 18:22:31 +0000867
868 argv = margv;
869
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000870 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000871 {
872 --margc;
873
Jari Aalto726f6381996-08-26 18:22:31 +0000874 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
Jari Aaltob80f6442004-07-27 13:29:18 +0000875 test_syntax_error (_("missing `]'"), (char *)NULL);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000876
877 if (margc < 2)
878 test_exit (SHELL_BOOLEAN (FALSE));
Jari Aalto726f6381996-08-26 18:22:31 +0000879 }
880
881 argc = margc;
882 pos = 1;
883
884 if (pos >= argc)
885 test_exit (SHELL_BOOLEAN (FALSE));
886
887 noeval = 0;
888 value = posixtest ();
889
890 if (pos != argc)
Jari Aaltob80f6442004-07-27 13:29:18 +0000891 test_syntax_error (_("too many arguments"), (char *)NULL);
Jari Aalto726f6381996-08-26 18:22:31 +0000892
893 test_exit (SHELL_BOOLEAN (value));
894}