blob: e2fd78b359e92804f03b5852397196c4b7f3db70 [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 Ramey8868eda2020-12-06 15:51:17 -05005/* Copyright (C) 1987-2020 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
Chet Ramey8868eda2020-12-06 15:51:17 -0500106extern int sh_stat PARAMS((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
Chet Ramey8868eda2020-12-06 15:51:17 -0500113static void test_syntax_error PARAMS((char *, char *)) __attribute__((__noreturn__));
114static void beyond PARAMS((void)) __attribute__((__noreturn__));
115static void integer_expected_error PARAMS((char *)) __attribute__((__noreturn__));
Jari Aalto726f6381996-08-26 18:22:31 +0000116
Chet Ramey8868eda2020-12-06 15:51:17 -0500117static int unary_operator PARAMS((void));
118static int binary_operator PARAMS((void));
119static int two_arguments PARAMS((void));
120static int three_arguments PARAMS((void));
121static int posixtest PARAMS((void));
Jari Aaltof73dda02001-11-13 17:56:06 +0000122
Chet Ramey8868eda2020-12-06 15:51:17 -0500123static int expr PARAMS((void));
124static int term PARAMS((void));
125static int and PARAMS((void));
126static int or PARAMS((void));
Jari Aaltof73dda02001-11-13 17:56:06 +0000127
Chet Ramey8868eda2020-12-06 15:51:17 -0500128static int filecomp PARAMS((char *, char *, int));
129static int arithcomp PARAMS((char *, char *, int, int));
130static int patcomp PARAMS((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
Chet Ramey8868eda2020-12-06 15:51:17 -0500276 /* Might be a switch type argument -- make sure we have enough arguments for
277 the unary operator and argument */
278 else if ((pos + 2) <= argc && test_unop (argv[pos]))
279 value = unary_operator ();
280
Jari Aalto726f6381996-08-26 18:22:31 +0000281 else
282 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000283 value = argv[pos][0] != '\0';
Jari Aalto726f6381996-08-26 18:22:31 +0000284 advance (0);
285 }
286
287 return (value);
288}
289
290static int
Chet Rameyac50fba2014-02-26 09:36:43 -0500291stat_mtime (fn, st, ts)
292 char *fn;
293 struct stat *st;
294 struct timespec *ts;
295{
296 int r;
297
298 r = sh_stat (fn, st);
299 if (r < 0)
300 return r;
301 *ts = get_stat_mtime (st);
302 return 0;
303}
304
305static int
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000306filecomp (s, t, op)
307 char *s, *t;
308 int op;
Jari Aalto726f6381996-08-26 18:22:31 +0000309{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000310 struct stat st1, st2;
Chet Rameyac50fba2014-02-26 09:36:43 -0500311 struct timespec ts1, ts2;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000312 int r1, r2;
Jari Aalto726f6381996-08-26 18:22:31 +0000313
Chet Rameyac50fba2014-02-26 09:36:43 -0500314 if ((r1 = stat_mtime (s, &st1, &ts1)) < 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000315 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000316 if (op == EF)
317 return (FALSE);
318 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500319 if ((r2 = stat_mtime (t, &st2, &ts2)) < 0)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000320 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000321 if (op == EF)
322 return (FALSE);
323 }
324
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000325 switch (op)
Jari Aalto726f6381996-08-26 18:22:31 +0000326 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500327 case OT: return (r1 < r2 || (r2 == 0 && timespec_cmp (ts1, ts2) < 0));
328 case NT: return (r1 > r2 || (r1 == 0 && timespec_cmp (ts1, ts2) > 0));
Jari Aalto31859422009-01-12 13:36:28 +0000329 case EF: return (same_file (s, t, &st1, &st2));
Jari Aalto726f6381996-08-26 18:22:31 +0000330 }
331 return (FALSE);
332}
333
334static int
Jari Aaltocce855b1998-04-17 19:52:44 +0000335arithcomp (s, t, op, flags)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000336 char *s, *t;
Jari Aaltocce855b1998-04-17 19:52:44 +0000337 int op, flags;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000338{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000339 intmax_t l, r;
Jari Aaltocce855b1998-04-17 19:52:44 +0000340 int expok;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000341
Jari Aaltocce855b1998-04-17 19:52:44 +0000342 if (flags & TEST_ARITHEXP)
343 {
Chet Ramey8868eda2020-12-06 15:51:17 -0500344 l = evalexp (s, EXP_EXPANDED, &expok);
Jari Aaltocce855b1998-04-17 19:52:44 +0000345 if (expok == 0)
346 return (FALSE); /* should probably longjmp here */
Chet Ramey8868eda2020-12-06 15:51:17 -0500347 r = evalexp (t, EXP_EXPANDED, &expok);
Jari Aaltocce855b1998-04-17 19:52:44 +0000348 if (expok == 0)
349 return (FALSE); /* ditto */
350 }
351 else
352 {
353 if (legal_number (s, &l) == 0)
354 integer_expected_error (s);
355 if (legal_number (t, &r) == 0)
356 integer_expected_error (t);
357 }
358
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000359 switch (op)
360 {
361 case EQ: return (l == r);
362 case NE: return (l != r);
363 case LT: return (l < r);
364 case GT: return (l > r);
365 case LE: return (l <= r);
366 case GE: return (l >= r);
367 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000368
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000369 return (FALSE);
370}
371
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000372static int
373patcomp (string, pat, op)
374 char *string, *pat;
375 int op;
376{
377 int m;
378
Jari Aalto95732b42005-12-07 14:08:12 +0000379 m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE);
Jari Aaltocce855b1998-04-17 19:52:44 +0000380 return ((op == EQ) ? (m == 0) : (m != 0));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000381}
Jari Aaltocce855b1998-04-17 19:52:44 +0000382
383int
384binary_test (op, arg1, arg2, flags)
385 char *op, *arg1, *arg2;
386 int flags;
387{
388 int patmatch;
389
390 patmatch = (flags & TEST_PATMATCH);
391
392 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
393 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
Jari Aaltocce855b1998-04-17 19:52:44 +0000394 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
Chet Ramey00018032011-11-21 20:51:19 -0500395 {
Chet Rameyac50fba2014-02-26 09:36:43 -0500396#if defined (HAVE_STRCOLL)
Chet Ramey00018032011-11-21 20:51:19 -0500397 if (shell_compatibility_level > 40 && flags & TEST_LOCALE)
398 return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0));
399 else
Chet Rameyac50fba2014-02-26 09:36:43 -0500400#endif
Chet Ramey00018032011-11-21 20:51:19 -0500401 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
402 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000403 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
404 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
Chet Ramey00018032011-11-21 20:51:19 -0500405
Jari Aaltocce855b1998-04-17 19:52:44 +0000406
407 else if (op[2] == 't')
408 {
409 switch (op[1])
410 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000411 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
412 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
Jari Aaltocce855b1998-04-17 19:52:44 +0000413 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
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000438
439static int
440binary_operator ()
441{
442 int value;
443 char *w;
444
445 w = argv[pos + 1];
Jari Aaltocce855b1998-04-17 19:52:44 +0000446 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')) /* != */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000449 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000450 value = binary_test (w, argv[pos], argv[pos + 2], 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000451 pos += 3;
452 return (value);
453 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000454
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000455#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
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000463
Jari Aaltocce855b1998-04-17 19:52:44 +0000464 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000465 {
Jari Aaltob80f6442004-07-27 13:29:18 +0000466 test_syntax_error (_("%s: binary operator expected"), w);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000467 /* NOTREACHED */
468 return (FALSE);
469 }
470
Jari Aaltocce855b1998-04-17 19:52:44 +0000471 value = binary_test (w, argv[pos], argv[pos + 2], 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000472 pos += 3;
473 return value;
474}
475
476static int
Jari Aalto726f6381996-08-26 18:22:31 +0000477unary_operator ()
478{
Jari Aaltof73dda02001-11-13 17:56:06 +0000479 char *op;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000480 intmax_t r;
Jari Aaltocce855b1998-04-17 19:52:44 +0000481
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);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000490 if (pos < argc)
Jari Aaltocce855b1998-04-17 19:52:44 +0000491 {
Jari Aalto28ef6c32001-04-06 19:14:31 +0000492 if (legal_number (argv[pos], &r))
493 {
494 advance (0);
495 return (unary_test (op, argv[pos - 1]));
496 }
497 else
498 return (FALSE);
Jari Aaltocce855b1998-04-17 19:52:44 +0000499 }
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{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000516 intmax_t r;
Jari Aalto726f6381996-08-26 18:22:31 +0000517 struct stat stat_buf;
Chet Ramey8868eda2020-12-06 15:51:17 -0500518 struct timespec mtime, atime;
Chet Ramey495aee42011-11-22 19:11:26 -0500519 SHELL_VAR *v;
Jari Aaltocce855b1998-04-17 19:52:44 +0000520
521 switch (op[1])
Jari Aalto726f6381996-08-26 18:22:31 +0000522 {
Jari Aalto726f6381996-08-26 18:22:31 +0000523 case 'a': /* file exists in the file system? */
524 case 'e':
Jari Aalto06285672006-10-10 14:15:34 +0000525 return (sh_stat (arg, &stat_buf) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000526
527 case 'r': /* file is readable? */
Jari Aalto06285672006-10-10 14:15:34 +0000528 return (sh_eaccess (arg, R_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000529
530 case 'w': /* File is writeable? */
Jari Aalto06285672006-10-10 14:15:34 +0000531 return (sh_eaccess (arg, W_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000532
533 case 'x': /* File is executable? */
Jari Aalto06285672006-10-10 14:15:34 +0000534 return (sh_eaccess (arg, X_OK) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000535
536 case 'O': /* File is owned by you? */
Jari Aalto06285672006-10-10 14:15:34 +0000537 return (sh_stat (arg, &stat_buf) == 0 &&
Jari Aaltod166f041997-06-05 14:59:13 +0000538 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
Jari Aalto726f6381996-08-26 18:22:31 +0000539
540 case 'G': /* File is owned by your group? */
Jari Aalto06285672006-10-10 14:15:34 +0000541 return (sh_stat (arg, &stat_buf) == 0 &&
Jari Aaltod166f041997-06-05 14:59:13 +0000542 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
Jari Aalto726f6381996-08-26 18:22:31 +0000543
Jari Aaltocce855b1998-04-17 19:52:44 +0000544 case 'N':
Chet Ramey8868eda2020-12-06 15:51:17 -0500545 if (sh_stat (arg, &stat_buf) < 0)
546 return (FALSE);
547 atime = get_stat_atime (&stat_buf);
548 mtime = get_stat_mtime (&stat_buf);
549 return (timespec_cmp (mtime, atime) > 0);
Jari Aaltocce855b1998-04-17 19:52:44 +0000550
Jari Aalto726f6381996-08-26 18:22:31 +0000551 case 'f': /* File is a file? */
Jari Aalto06285672006-10-10 14:15:34 +0000552 if (sh_stat (arg, &stat_buf) < 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000553 return (FALSE);
554
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000555 /* -f is true if the given file exists and is a regular file. */
Jari Aalto726f6381996-08-26 18:22:31 +0000556#if defined (S_IFMT)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000557 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000558#else
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000559 return (S_ISREG (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000560#endif /* !S_IFMT */
561
562 case 'd': /* File is a directory? */
Jari Aalto06285672006-10-10 14:15:34 +0000563 return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
Jari Aalto726f6381996-08-26 18:22:31 +0000564
565 case 's': /* File has something in it? */
Jari Aalto06285672006-10-10 14:15:34 +0000566 return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000567
568 case 'S': /* File is a socket? */
569#if !defined (S_ISSOCK)
570 return (FALSE);
571#else
Jari Aalto06285672006-10-10 14:15:34 +0000572 return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000573#endif /* S_ISSOCK */
Jari Aalto726f6381996-08-26 18:22:31 +0000574
575 case 'c': /* File is character special? */
Jari Aalto06285672006-10-10 14:15:34 +0000576 return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000577
578 case 'b': /* File is block special? */
Jari Aalto06285672006-10-10 14:15:34 +0000579 return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
Jari Aalto726f6381996-08-26 18:22:31 +0000580
581 case 'p': /* File is a named pipe? */
Jari Aalto726f6381996-08-26 18:22:31 +0000582#ifndef S_ISFIFO
583 return (FALSE);
584#else
Jari Aalto06285672006-10-10 14:15:34 +0000585 return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000586#endif /* S_ISFIFO */
Jari Aalto726f6381996-08-26 18:22:31 +0000587
588 case 'L': /* Same as -h */
Jari Aalto726f6381996-08-26 18:22:31 +0000589 case 'h': /* File is a symbolic link? */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000590#if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
Jari Aalto726f6381996-08-26 18:22:31 +0000591 return (FALSE);
592#else
Jari Aaltocce855b1998-04-17 19:52:44 +0000593 return ((arg[0] != '\0') &&
594 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000595#endif /* S_IFLNK && HAVE_LSTAT */
Jari Aalto726f6381996-08-26 18:22:31 +0000596
597 case 'u': /* File is setuid? */
Jari Aalto06285672006-10-10 14:15:34 +0000598 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000599
600 case 'g': /* File is setgid? */
Jari Aalto06285672006-10-10 14:15:34 +0000601 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000602
603 case 'k': /* File has sticky bit set? */
Jari Aalto726f6381996-08-26 18:22:31 +0000604#if !defined (S_ISVTX)
605 /* This is not Posix, and is not defined on some Posix systems. */
606 return (FALSE);
607#else
Jari Aalto06285672006-10-10 14:15:34 +0000608 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
Jari Aalto726f6381996-08-26 18:22:31 +0000609#endif
610
Jari Aaltocce855b1998-04-17 19:52:44 +0000611 case 't': /* File fd is a terminal? */
612 if (legal_number (arg, &r) == 0)
613 return (FALSE);
Jari Aaltof73dda02001-11-13 17:56:06 +0000614 return ((r == (int)r) && isatty ((int)r));
Jari Aalto726f6381996-08-26 18:22:31 +0000615
616 case 'n': /* True if arg has some length. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000617 return (arg[0] != '\0');
Jari Aalto726f6381996-08-26 18:22:31 +0000618
619 case 'z': /* True if arg has no length. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000620 return (arg[0] == '\0');
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000621
Jari Aaltocce855b1998-04-17 19:52:44 +0000622 case 'o': /* True if option `arg' is set. */
623 return (minus_o_option_value (arg) == 1);
Chet Ramey495aee42011-11-22 19:11:26 -0500624
625 case 'v':
Chet Rameyac50fba2014-02-26 09:36:43 -0500626#if defined (ARRAY_VARS)
Chet Rameyd233b482019-01-07 09:27:52 -0500627 if (valid_array_reference (arg, 0))
Chet Rameyac50fba2014-02-26 09:36:43 -0500628 {
629 char *t;
Chet Ramey8868eda2020-12-06 15:51:17 -0500630 int rtype, ret, flags;
631
632 /* Let's assume that this has already been expanded once. */
633 flags = assoc_expand_once ? AV_NOEXPAND : 0;
634 t = array_value (arg, 0, flags, &rtype, (arrayind_t *)0);
Chet Rameyd233b482019-01-07 09:27:52 -0500635 ret = t ? TRUE : FALSE;
636 if (rtype > 0) /* subscript is * or @ */
637 free (t);
638 return ret;
Chet Rameyac50fba2014-02-26 09:36:43 -0500639 }
Chet Ramey8868eda2020-12-06 15:51:17 -0500640 else if (legal_number (arg, &r)) /* -v n == is $n set? */
641 return ((r >= 0 && r <= number_of_args()) ? TRUE : FALSE);
Chet Rameyd233b482019-01-07 09:27:52 -0500642 v = find_variable (arg);
643 if (v && invisible_p (v) == 0 && array_p (v))
Chet Rameyac50fba2014-02-26 09:36:43 -0500644 {
645 char *t;
646 /* [[ -v foo ]] == [[ -v foo[0] ]] */
647 t = array_reference (array_cell (v), 0);
648 return (t ? TRUE : FALSE);
649 }
650 else if (v && invisible_p (v) == 0 && assoc_p (v))
651 {
652 char *t;
653 t = assoc_reference (assoc_cell (v), "0");
654 return (t ? TRUE : FALSE);
655 }
Chet Rameyd233b482019-01-07 09:27:52 -0500656#else
657 v = find_variable (arg);
Chet Rameyac50fba2014-02-26 09:36:43 -0500658#endif
659 return (v && invisible_p (v) == 0 && var_isset (v) ? TRUE : FALSE);
660
661 case 'R':
Chet Rameyf5c82372014-03-28 11:52:47 -0400662 v = find_variable_noref (arg);
663 return ((v && invisible_p (v) == 0 && var_isset (v) && nameref_p (v)) ? TRUE : FALSE);
Jari Aalto726f6381996-08-26 18:22:31 +0000664 }
Jari Aaltof73dda02001-11-13 17:56:06 +0000665
666 /* We can't actually get here, but this shuts up gcc. */
667 return (FALSE);
Jari Aalto726f6381996-08-26 18:22:31 +0000668}
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000669
Jari Aaltocce855b1998-04-17 19:52:44 +0000670/* Return TRUE if OP is one of the test command's binary operators. */
671int
672test_binop (op)
673 char *op;
Jari Aalto726f6381996-08-26 18:22:31 +0000674{
Jari Aaltocce855b1998-04-17 19:52:44 +0000675 if (op[0] == '=' && op[1] == '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000676 return (1); /* '=' */
Jari Aaltocce855b1998-04-17 19:52:44 +0000677 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000678 return (1);
Jari Aaltocce855b1998-04-17 19:52:44 +0000679 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000680 return (1); /* `==' and `!=' */
681#if defined (PATTERN_MATCHING)
Jari Aaltocce855b1998-04-17 19:52:44 +0000682 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000683 return (1);
684#endif
Chet Rameyd233b482019-01-07 09:27:52 -0500685 else if (op[0] != '-' || op[1] == '\0' || op[2] == '\0' || op[3] != '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000686 return (0);
687 else
688 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000689 if (op[2] == 't')
690 switch (op[1])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000691 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000692 case 'n': /* -nt */
693 case 'o': /* -ot */
694 case 'l': /* -lt */
695 case 'g': /* -gt */
696 return (1);
697 default:
698 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000699 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000700 else if (op[1] == 'e')
701 switch (op[2])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000702 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000703 case 'q': /* -eq */
704 case 'f': /* -ef */
705 return (1);
706 default:
707 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000708 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000709 else if (op[2] == 'e')
710 switch (op[1])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000711 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000712 case 'n': /* -ne */
713 case 'g': /* -ge */
714 case 'l': /* -le */
715 return (1);
716 default:
717 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000718 }
719 else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000720 return (0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000721 }
Jari Aalto726f6381996-08-26 18:22:31 +0000722}
723
724/* Return non-zero if OP is one of the test command's unary operators. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000725int
726test_unop (op)
727 char *op;
Jari Aalto726f6381996-08-26 18:22:31 +0000728{
Chet Rameyd233b482019-01-07 09:27:52 -0500729 if (op[0] != '-' || (op[1] && op[2] != 0))
Jari Aaltocce855b1998-04-17 19:52:44 +0000730 return (0);
731
732 switch (op[1])
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000733 {
734 case 'a': case 'b': case 'c': case 'd': case 'e':
735 case 'f': case 'g': case 'h': case 'k': case 'n':
Jari Aaltocce855b1998-04-17 19:52:44 +0000736 case 'o': case 'p': case 'r': case 's': case 't':
Chet Ramey495aee42011-11-22 19:11:26 -0500737 case 'u': case 'v': case 'w': case 'x': case 'z':
Jari Aaltocce855b1998-04-17 19:52:44 +0000738 case 'G': case 'L': case 'O': case 'S': case 'N':
Chet Rameyf5c82372014-03-28 11:52:47 -0400739 case 'R':
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000740 return (1);
741 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000742
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000743 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000744}
745
746static int
747two_arguments ()
748{
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000749 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
750 return (argv[pos + 1][0] == '\0');
Chet Rameyd233b482019-01-07 09:27:52 -0500751 else if (argv[pos][0] == '-' && argv[pos][1] && argv[pos][2] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000752 {
Jari Aaltocce855b1998-04-17 19:52:44 +0000753 if (test_unop (argv[pos]))
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000754 return (unary_operator ());
Jari Aalto726f6381996-08-26 18:22:31 +0000755 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000756 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000757 }
758 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000759 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
Jari Aalto726f6381996-08-26 18:22:31 +0000760
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000761 return (0);
Jari Aalto726f6381996-08-26 18:22:31 +0000762}
763
Chet Rameyd233b482019-01-07 09:27:52 -0500764#define ANDOR(s) (s[0] == '-' && (s[1] == 'a' || s[1] == 'o') && s[2] == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000765
Jari Aaltof73dda02001-11-13 17:56:06 +0000766/* This could be augmented to handle `-t' as equivalent to `-t 1', but
767 POSIX requires that `-t' be given an argument. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000768#define ONE_ARG_TEST(s) ((s)[0] != '\0')
769
Jari Aalto726f6381996-08-26 18:22:31 +0000770static int
771three_arguments ()
772{
773 int value;
774
Jari Aaltocce855b1998-04-17 19:52:44 +0000775 if (test_binop (argv[pos+1]))
Jari Aalto726f6381996-08-26 18:22:31 +0000776 {
777 value = binary_operator ();
778 pos = argc;
779 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000780 else if (ANDOR (argv[pos+1]))
781 {
782 if (argv[pos+1][1] == 'a')
Jari Aalto28ef6c32001-04-06 19:14:31 +0000783 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000784 else
Jari Aalto28ef6c32001-04-06 19:14:31 +0000785 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000786 pos = argc;
787 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000788 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000789 {
790 advance (1);
791 value = !two_arguments ();
792 }
793 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
794 {
795 value = ONE_ARG_TEST(argv[pos+1]);
796 pos = argc;
797 }
Jari Aalto726f6381996-08-26 18:22:31 +0000798 else
Jari Aaltob80f6442004-07-27 13:29:18 +0000799 test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000800
Jari Aalto726f6381996-08-26 18:22:31 +0000801 return (value);
802}
803
804/* This is an implementation of a Posix.2 proposal by David Korn. */
805static int
806posixtest ()
807{
808 int value;
809
810 switch (argc - 1) /* one extra passed in */
811 {
812 case 0:
813 value = FALSE;
814 pos = argc;
815 break;
816
817 case 1:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000818 value = ONE_ARG_TEST(argv[1]);
Jari Aalto726f6381996-08-26 18:22:31 +0000819 pos = argc;
820 break;
821
822 case 2:
823 value = two_arguments ();
824 pos = argc;
825 break;
826
827 case 3:
828 value = three_arguments ();
829 break;
830
831 case 4:
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000832 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000833 {
834 advance (1);
835 value = !three_arguments ();
836 break;
837 }
Chet Rameya0c0a002016-09-15 16:59:08 -0400838 else if (argv[pos][0] == '(' && argv[pos][1] == '\0' && argv[argc-1][0] == ')' && argv[argc-1][1] == '\0')
839 {
840 advance (1);
841 value = two_arguments ();
842 pos = argc;
843 break;
844 }
Jari Aalto726f6381996-08-26 18:22:31 +0000845 /* FALLTHROUGH */
Jari Aalto726f6381996-08-26 18:22:31 +0000846 default:
847 value = expr ();
848 }
849
850 return (value);
851}
852
853/*
854 * [:
855 * '[' expr ']'
856 * test:
857 * test expr
858 */
859int
Jari Aaltod166f041997-06-05 14:59:13 +0000860test_command (margc, margv)
Jari Aalto726f6381996-08-26 18:22:31 +0000861 int margc;
862 char **margv;
863{
864 int value;
Jari Aalto726f6381996-08-26 18:22:31 +0000865 int code;
866
Jari Aaltof73dda02001-11-13 17:56:06 +0000867 USE_VAR(margc);
868
Chet Rameyac50fba2014-02-26 09:36:43 -0500869 code = setjmp_nosigs (test_exit_buf);
Jari Aalto726f6381996-08-26 18:22:31 +0000870
871 if (code)
872 return (test_error_return);
Jari Aalto726f6381996-08-26 18:22:31 +0000873
874 argv = margv;
875
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000876 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
Jari Aalto726f6381996-08-26 18:22:31 +0000877 {
878 --margc;
879
Jari Aalto726f6381996-08-26 18:22:31 +0000880 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
Jari Aaltob80f6442004-07-27 13:29:18 +0000881 test_syntax_error (_("missing `]'"), (char *)NULL);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000882
883 if (margc < 2)
884 test_exit (SHELL_BOOLEAN (FALSE));
Jari Aalto726f6381996-08-26 18:22:31 +0000885 }
886
887 argc = margc;
888 pos = 1;
889
890 if (pos >= argc)
891 test_exit (SHELL_BOOLEAN (FALSE));
892
893 noeval = 0;
894 value = posixtest ();
895
896 if (pos != argc)
Chet Ramey8868eda2020-12-06 15:51:17 -0500897 {
898 if (pos < argc && argv[pos][0] == '-')
899 test_syntax_error (_("syntax error: `%s' unexpected"), argv[pos]);
900 else
901 test_syntax_error (_("too many arguments"), (char *)NULL);
902 }
Jari Aalto726f6381996-08-26 18:22:31 +0000903
904 test_exit (SHELL_BOOLEAN (value));
905}