1 /* test.c - GNU test program (ksb and mjb) */
3 /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
5 /* Copyright (C) 1987-2023 Free Software Foundation, Inc.
7 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
23 /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
25 /* #define PATTERN_MATCHING */
27 #if defined (HAVE_CONFIG_H)
33 #include "bashtypes.h"
35 #if !defined (HAVE_LIMITS_H) && defined (HAVE_SYS_PARAM_H)
36 # include <sys/param.h>
39 #if defined (HAVE_UNISTD_H)
48 #if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H)
49 # include <sys/file.h>
50 #endif /* !_POSIX_VERSION */
51 #include "posixstat.h"
53 #include "stat-time.h"
60 #include "builtins/common.h"
62 #include <glob/strmatch.h>
65 # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
69 # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0)
71 #define STRCOLLEQ(a, b) ((a)[0] == (b)[0] && strcoll ((a), (b)) == 0)
91 /* The following few defines control the truth and false output of each stage.
92 TRUE and FALSE are what we use to compute the final output value.
93 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
94 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
97 #define SHELL_BOOLEAN(value) (!(value))
99 #define TEST_ERREXIT_STATUS 2
101 static procenv_t test_exit_buf
;
102 static int test_error_return
;
103 #define test_exit(val) \
104 do { test_error_return = val; sh_longjmp (test_exit_buf, 1); } while (0)
106 extern int sh_stat (const char *, struct stat
*);
108 static int pos
; /* The offset of the current argument in ARGV. */
109 static int argc
; /* The number of arguments present in ARGV. */
110 static char **argv
; /* The argument list. */
113 static void test_syntax_error (char *, char *) __attribute__((__noreturn__
));
114 static void beyond (void) __attribute__((__noreturn__
));
115 static void integer_expected_error (char *) __attribute__((__noreturn__
));
117 static int unary_operator (void);
118 static int binary_operator (void);
119 static int two_arguments (void);
120 static int three_arguments (void);
121 static int posixtest (void);
123 static int expr (void);
124 static int term (void);
125 static int and (void);
126 static int or (void);
128 static int filecomp (const char *, const char *, int);
129 static int arithcomp (char *, char *, int, int);
130 static int patcomp (char *, char *, int);
133 test_syntax_error (char *format
, char *arg
)
135 builtin_error (format
, arg
);
136 test_exit (TEST_ERREXIT_STATUS
);
140 * beyond - call when we're beyond the end of the argument list (an
146 test_syntax_error (_("argument expected"), (char *)NULL
);
149 /* Syntax error for when an integer argument was expected, but
150 something else was found. */
152 integer_expected_error (char *pch
)
154 test_syntax_error (_("%s: integer expected"), pch
);
157 /* Increment our position in the argument list. Check that we're not
158 past the end of the argument list. This check is suppressed if the
159 argument is FALSE. Made a macro for efficiency. */
160 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
161 #define unary_advance() do { advance (1); ++pos; } while (0)
173 return (FALSE
^ or ()); /* Same with this. */
187 if (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'o' && !argv
[pos
][2])
191 return (value
|| v2
);
208 if (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'a' && !argv
[pos
][2])
212 return (value
&& v2
);
218 * term - parse a term and return 1 or 0 depending on whether the term
219 * evaluates to true or false, respectively.
222 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
223 * '-'('G'|'L'|'O'|'S'|'N') filename
225 * '-'('z'|'n') string
226 * '-'('v'|'R') varname
229 * string ('!='|'='|'==') string
230 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
231 * file '-'(nt|ot|ef) file
234 * positive and negative integers
244 /* Deal with leading `not's. */
245 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
248 while (pos
< argc
&& argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
254 return (value
? !term() : term());
257 /* A paren-bracketed argument. */
258 if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0') /* ) */
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
]);
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 ();
274 /* Might be a switch type argument -- make sure we have enough arguments for
275 the unary operator and argument */
276 else if ((pos
+ 2) <= argc
&& test_unop (argv
[pos
]))
277 value
= unary_operator ();
281 value
= argv
[pos
][0] != '\0';
289 stat_mtime (const char *fn
, struct stat
*st
, struct timespec
*ts
)
293 r
= sh_stat (fn
, st
);
296 *ts
= get_stat_mtime (st
);
301 filecomp (const char *s
, const char *t
, int op
)
303 struct stat st1
, st2
;
304 struct timespec ts1
, ts2
;
307 if ((r1
= stat_mtime (s
, &st1
, &ts1
)) < 0)
312 if ((r2
= stat_mtime (t
, &st2
, &ts2
)) < 0)
320 case OT
: return (r1
< r2
|| (r2
== 0 && timespec_cmp (ts1
, ts2
) < 0));
321 case NT
: return (r1
> r2
|| (r1
== 0 && timespec_cmp (ts1
, ts2
) > 0));
322 case EF
: return (same_file (s
, t
, &st1
, &st2
));
328 arithcomp (char *s
, char *t
, int op
, int flags
)
333 if (flags
& TEST_ARITHEXP
) /* conditional command */
337 eflag
= (shell_compatibility_level
> 51) ? 0 : EXP_EXPANDED
;
338 l
= evalexp (s
, eflag
, &expok
);
340 return (FALSE
); /* should probably longjmp here */
341 r
= evalexp (t
, eflag
, &expok
);
343 return (FALSE
); /* ditto */
347 if (legal_number (s
, &l
) == 0)
348 integer_expected_error (s
);
349 if (legal_number (t
, &r
) == 0)
350 integer_expected_error (t
);
355 case EQ
: return (l
== r
);
356 case NE
: return (l
!= r
);
357 case LT
: return (l
< r
);
358 case GT
: return (l
> r
);
359 case LE
: return (l
<= r
);
360 case GE
: return (l
>= r
);
367 patcomp (char *string
, char *pat
, int op
)
371 m
= strmatch (pat
, string
, FNMATCH_EXTFLAG
|FNMATCH_IGNCASE
);
372 return ((op
== EQ
) ? (m
== 0) : (m
!= 0));
376 binary_test (char *op
, char *arg1
, char *arg2
, int flags
)
380 patmatch
= (flags
& TEST_PATMATCH
);
382 if (op
[0] == '=' && (op
[1] == '\0' || (op
[1] == '=' && op
[2] == '\0')))
383 return (patmatch
? patcomp (arg1
, arg2
, EQ
) : STREQ (arg1
, arg2
));
384 else if ((op
[0] == '>' || op
[0] == '<') && op
[1] == '\0')
386 #if defined (HAVE_STRCOLL)
387 if (shell_compatibility_level
> 40 && (flags
& TEST_LOCALE
))
388 return ((op
[0] == '>') ? (strcoll (arg1
, arg2
) > 0) : (strcoll (arg1
, arg2
) < 0));
391 return ((op
[0] == '>') ? (strcmp (arg1
, arg2
) > 0) : (strcmp (arg1
, arg2
) < 0));
393 else if (op
[0] == '!' && op
[1] == '=' && op
[2] == '\0')
394 return (patmatch
? patcomp (arg1
, arg2
, NE
) : (STREQ (arg1
, arg2
) == 0));
397 else if (op
[2] == 't')
401 case 'n': return (filecomp (arg1
, arg2
, NT
)); /* -nt */
402 case 'o': return (filecomp (arg1
, arg2
, OT
)); /* -ot */
403 case 'l': return (arithcomp (arg1
, arg2
, LT
, flags
)); /* -lt */
404 case 'g': return (arithcomp (arg1
, arg2
, GT
, flags
)); /* -gt */
407 else if (op
[1] == 'e')
411 case 'f': return (filecomp (arg1
, arg2
, EF
)); /* -ef */
412 case 'q': return (arithcomp (arg1
, arg2
, EQ
, flags
)); /* -eq */
415 else if (op
[2] == 'e')
419 case 'n': return (arithcomp (arg1
, arg2
, NE
, flags
)); /* -ne */
420 case 'g': return (arithcomp (arg1
, arg2
, GE
, flags
)); /* -ge */
421 case 'l': return (arithcomp (arg1
, arg2
, LE
, flags
)); /* -le */
425 return (FALSE
); /* should never get here */
429 binary_operator (void)
435 if ((w
[0] == '=' && (w
[1] == '\0' || (w
[1] == '=' && w
[2] == '\0'))) || /* =, == */
436 ((w
[0] == '>' || w
[0] == '<') && w
[1] == '\0') || /* <, > */
437 (w
[0] == '!' && w
[1] == '=' && w
[2] == '\0')) /* != */
439 #if 1 /* POSIX interp 375 11/9/2022 */
440 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], (posixly_correct
? TEST_LOCALE
: 0));
442 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], 0);
448 #if defined (PATTERN_MATCHING)
449 if ((w
[0] == '=' || w
[0] == '!') && w
[1] == '~' && w
[2] == '\0')
451 value
= patcomp (argv
[pos
], argv
[pos
+ 2], w
[0] == '=' ? EQ
: NE
);
457 if ((w
[0] != '-' || w
[3] != '\0') || test_binop (w
) == 0)
459 test_syntax_error (_("%s: binary operator expected"), w
);
464 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], 0);
470 unary_operator (void)
476 if (test_unop (op
) == 0)
479 /* the only tricky case is `-t', which may or may not take an argument. */
485 if (legal_number (argv
[pos
], &r
))
488 return (unary_test (op
, argv
[pos
- 1], 0));
494 return (unary_test (op
, "1", 0));
497 /* All of the unary operators take an argument, so we first call
498 unary_advance (), which checks to make sure that there is an
499 argument, and then advances pos right past it. This means that
500 pos - 1 is the location of the argument. */
502 return (unary_test (op
, argv
[pos
- 1], 0));
506 unary_test (char *op
, char *arg
, int flags
)
509 struct stat stat_buf
;
510 struct timespec mtime
, atime
;
516 case 'a': /* file exists in the file system? */
518 return (sh_stat (arg
, &stat_buf
) == 0);
520 case 'r': /* file is readable? */
521 return (sh_eaccess (arg
, R_OK
) == 0);
523 case 'w': /* File is writeable? */
524 return (sh_eaccess (arg
, W_OK
) == 0);
526 case 'x': /* File is executable? */
527 return (sh_eaccess (arg
, X_OK
) == 0);
529 case 'O': /* File is owned by you? */
530 return (sh_stat (arg
, &stat_buf
) == 0 &&
531 (uid_t
) current_user
.euid
== (uid_t
) stat_buf
.st_uid
);
533 case 'G': /* File is owned by your group? */
534 return (sh_stat (arg
, &stat_buf
) == 0 &&
535 (gid_t
) current_user
.egid
== (gid_t
) stat_buf
.st_gid
);
538 if (sh_stat (arg
, &stat_buf
) < 0)
540 atime
= get_stat_atime (&stat_buf
);
541 mtime
= get_stat_mtime (&stat_buf
);
542 return (timespec_cmp (mtime
, atime
) > 0);
544 case 'f': /* File is a file? */
545 if (sh_stat (arg
, &stat_buf
) < 0)
548 /* -f is true if the given file exists and is a regular file. */
550 return (S_ISREG (stat_buf
.st_mode
) || (stat_buf
.st_mode
& S_IFMT
) == 0);
552 return (S_ISREG (stat_buf
.st_mode
));
555 case 'd': /* File is a directory? */
556 return (sh_stat (arg
, &stat_buf
) == 0 && (S_ISDIR (stat_buf
.st_mode
)));
558 case 's': /* File has something in it? */
559 return (sh_stat (arg
, &stat_buf
) == 0 && stat_buf
.st_size
> (off_t
) 0);
561 case 'S': /* File is a socket? */
562 #if !defined (S_ISSOCK)
565 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISSOCK (stat_buf
.st_mode
));
566 #endif /* S_ISSOCK */
568 case 'c': /* File is character special? */
569 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISCHR (stat_buf
.st_mode
));
571 case 'b': /* File is block special? */
572 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISBLK (stat_buf
.st_mode
));
574 case 'p': /* File is a named pipe? */
578 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISFIFO (stat_buf
.st_mode
));
579 #endif /* S_ISFIFO */
581 case 'L': /* Same as -h */
582 case 'h': /* File is a symbolic link? */
583 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
586 return ((arg
[0] != '\0') &&
587 (lstat (arg
, &stat_buf
) == 0) && S_ISLNK (stat_buf
.st_mode
));
588 #endif /* S_IFLNK && HAVE_LSTAT */
590 case 'u': /* File is setuid? */
591 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISUID
) != 0);
593 case 'g': /* File is setgid? */
594 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISGID
) != 0);
596 case 'k': /* File has sticky bit set? */
597 #if !defined (S_ISVTX)
598 /* This is not Posix, and is not defined on some Posix systems. */
601 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISVTX
) != 0);
604 case 't': /* File fd is a terminal? */
605 if (legal_number (arg
, &r
) == 0)
607 return ((r
== (int)r
) && isatty ((int)r
));
609 case 'n': /* True if arg has some length. */
610 return (arg
[0] != '\0');
612 case 'z': /* True if arg has no length. */
613 return (arg
[0] == '\0');
615 case 'o': /* True if option `arg' is set. */
616 return (minus_o_option_value (arg
) == 1);
619 #if defined (ARRAY_VARS)
620 aflags
= array_expand_once
? AV_NOEXPAND
: 0;
621 if (valid_array_reference (arg
, aflags
))
627 /* Let's assume that this has already been expanded once. */
628 /* XXX - TAG:bash-5.2 fix with corresponding fix to execute_cmd.c:
629 execute_cond_node() that passes TEST_ARRAYEXP in FLAGS */
631 if (shell_compatibility_level
> 51)
632 /* Allow associative arrays to use `test -v array[@]' to look for
634 aflags
|= AV_ATSTARKEYS
; /* XXX */
636 t
= get_array_value (arg
, aflags
|AV_ALLOWALL
, &es
);
637 ret
= t
? TRUE
: FALSE
;
638 if (es
.subtype
> 0) /* subscript is * or @ */
640 flush_eltstate (&es
);
643 else if (legal_number (arg
, &r
)) /* -v n == is $n set? */
644 return ((r
>= 0 && r
<= number_of_args()) ? TRUE
: FALSE
);
645 v
= find_variable (arg
);
646 if (v
&& invisible_p (v
) == 0 && array_p (v
))
649 /* [[ -v foo ]] == [[ -v foo[0] ]] */
650 t
= array_reference (array_cell (v
), 0);
651 return (t
? TRUE
: FALSE
);
653 else if (v
&& invisible_p (v
) == 0 && assoc_p (v
))
656 t
= assoc_reference (assoc_cell (v
), "0");
657 return (t
? TRUE
: FALSE
);
660 v
= find_variable (arg
);
662 return (v
&& invisible_p (v
) == 0 && var_isset (v
) ? TRUE
: FALSE
);
665 v
= find_variable_noref (arg
);
666 return ((v
&& invisible_p (v
) == 0 && var_isset (v
) && nameref_p (v
)) ? TRUE
: FALSE
);
669 /* We can't actually get here, but this shuts up gcc. */
673 /* Return TRUE if OP is one of the test command's binary operators. */
675 test_binop (char *op
)
677 if (op
[0] == '=' && op
[1] == '\0')
678 return (1); /* '=' */
679 else if ((op
[0] == '<' || op
[0] == '>') && op
[1] == '\0') /* string <, > */
681 else if ((op
[0] == '=' || op
[0] == '!') && op
[1] == '=' && op
[2] == '\0')
682 return (1); /* `==' and `!=' */
683 #if defined (PATTERN_MATCHING)
684 else if (op
[2] == '\0' && op
[1] == '~' && (op
[0] == '=' || op
[0] == '!'))
687 else if (op
[0] != '-' || op
[1] == '\0' || op
[2] == '\0' || op
[3] != '\0')
702 else if (op
[1] == 'e')
711 else if (op
[2] == 'e')
726 /* Return non-zero if OP is one of the test command's unary operators. */
730 if (op
[0] != '-' || (op
[1] && op
[2] != 0))
735 case 'a': case 'b': case 'c': case 'd': case 'e':
736 case 'f': case 'g': case 'h': case 'k': case 'n':
737 case 'o': case 'p': case 'r': case 's': case 't':
738 case 'u': case 'v': case 'w': case 'x': case 'z':
739 case 'G': case 'L': case 'O': case 'S': case 'N':
750 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
751 return (argv
[pos
+ 1][0] == '\0');
752 else if (argv
[pos
][0] == '-' && argv
[pos
][1] && argv
[pos
][2] == '\0')
754 if (test_unop (argv
[pos
]))
755 return (unary_operator ());
757 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
760 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
765 #define ANDOR(s) (s[0] == '-' && (s[1] == 'a' || s[1] == 'o') && s[2] == 0)
767 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
768 POSIX requires that `-t' be given an argument. */
769 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
772 three_arguments (void)
776 if (test_binop (argv
[pos
+1]))
778 value
= binary_operator ();
781 else if (ANDOR (argv
[pos
+1]))
783 if (argv
[pos
+1][1] == 'a')
784 value
= ONE_ARG_TEST(argv
[pos
]) && ONE_ARG_TEST(argv
[pos
+2]);
786 value
= ONE_ARG_TEST(argv
[pos
]) || ONE_ARG_TEST(argv
[pos
+2]);
789 else if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
792 value
= !two_arguments ();
795 else if (argv
[pos
][0] == '(' && argv
[pos
+2][0] == ')')
797 value
= ONE_ARG_TEST(argv
[pos
+1]);
801 test_syntax_error (_("%s: binary operator expected"), argv
[pos
+1]);
806 /* This is an implementation of a Posix.2 proposal by David Korn. */
812 switch (argc
- 1) /* one extra passed in */
820 value
= ONE_ARG_TEST(argv
[1]);
825 value
= two_arguments ();
830 value
= three_arguments ();
834 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
837 value
= !three_arguments ();
840 else if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0' && argv
[argc
-1][0] == ')' && argv
[argc
-1][1] == '\0')
843 value
= two_arguments ();
862 test_command (int margc
, char **margv
)
869 code
= setjmp_nosigs (test_exit_buf
);
872 return (test_error_return
);
876 if (margv
[0] && margv
[0][0] == '[' && margv
[0][1] == '\0')
880 if (margv
[margc
] && (margv
[margc
][0] != ']' || margv
[margc
][1]))
881 test_syntax_error (_("missing `]'"), (char *)NULL
);
884 test_exit (SHELL_BOOLEAN (FALSE
));
891 test_exit (SHELL_BOOLEAN (FALSE
));
894 value
= posixtest ();
898 if (pos
< argc
&& argv
[pos
][0] == '-')
899 test_syntax_error (_("syntax error: `%s' unexpected"), argv
[pos
]);
901 test_syntax_error (_("too many arguments"), (char *)NULL
);
904 test_exit (SHELL_BOOLEAN (value
));