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-2020 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
PARAMS((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
PARAMS((char *, char *)) __attribute__((__noreturn__
));
114 static void beyond
PARAMS((void)) __attribute__((__noreturn__
));
115 static void integer_expected_error
PARAMS((char *)) __attribute__((__noreturn__
));
117 static int unary_operator
PARAMS((void));
118 static int binary_operator
PARAMS((void));
119 static int two_arguments
PARAMS((void));
120 static int three_arguments
PARAMS((void));
121 static int posixtest
PARAMS((void));
123 static int expr
PARAMS((void));
124 static int term
PARAMS((void));
125 static int and PARAMS((void));
126 static int or PARAMS((void));
128 static int filecomp
PARAMS((char *, char *, int));
129 static int arithcomp
PARAMS((char *, char *, int, int));
130 static int patcomp
PARAMS((char *, char *, int));
133 test_syntax_error (format
, arg
)
136 builtin_error (format
, arg
);
137 test_exit (TEST_ERREXIT_STATUS
);
141 * beyond - call when we're beyond the end of the argument list (an
147 test_syntax_error (_("argument expected"), (char *)NULL
);
150 /* Syntax error for when an integer argument was expected, but
151 something else was found. */
153 integer_expected_error (pch
)
156 test_syntax_error (_("%s: integer expression expected"), pch
);
159 /* Increment our position in the argument list. Check that we're not
160 past the end of the argument list. This check is suppressed if the
161 argument is FALSE. Made a macro for efficiency. */
162 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
163 #define unary_advance() do { advance (1); ++pos; } while (0)
175 return (FALSE
^ or ()); /* Same with this. */
189 if (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'o' && !argv
[pos
][2])
193 return (value
|| v2
);
210 if (pos
< argc
&& argv
[pos
][0] == '-' && argv
[pos
][1] == 'a' && !argv
[pos
][2])
214 return (value
&& v2
);
220 * term - parse a term and return 1 or 0 depending on whether the term
221 * evaluates to true or false, respectively.
224 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
225 * '-'('G'|'L'|'O'|'S'|'N') filename
227 * '-'('z'|'n') string
228 * '-'('v'|'R') varname
231 * string ('!='|'='|'==') string
232 * <int> '-'(eq|ne|le|lt|ge|gt) <int>
233 * file '-'(nt|ot|ef) file
236 * positive and negative integers
246 /* Deal with leading `not's. */
247 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
250 while (pos
< argc
&& argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
256 return (value
? !term() : term());
259 /* A paren-bracketed argument. */
260 if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0') /* ) */
264 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
]);
272 /* are there enough arguments left that this could be dyadic? */
273 if ((pos
+ 3 <= argc
) && test_binop (argv
[pos
+ 1]))
274 value
= binary_operator ();
276 /* 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 ();
283 value
= argv
[pos
][0] != '\0';
291 stat_mtime (fn
, st
, ts
)
298 r
= sh_stat (fn
, st
);
301 *ts
= get_stat_mtime (st
);
310 struct stat st1
, st2
;
311 struct timespec ts1
, ts2
;
314 if ((r1
= stat_mtime (s
, &st1
, &ts1
)) < 0)
319 if ((r2
= stat_mtime (t
, &st2
, &ts2
)) < 0)
327 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));
329 case EF
: return (same_file (s
, t
, &st1
, &st2
));
335 arithcomp (s
, t
, op
, flags
)
342 if (flags
& TEST_ARITHEXP
)
344 l
= evalexp (s
, EXP_EXPANDED
, &expok
);
346 return (FALSE
); /* should probably longjmp here */
347 r
= evalexp (t
, EXP_EXPANDED
, &expok
);
349 return (FALSE
); /* ditto */
353 if (legal_number (s
, &l
) == 0)
354 integer_expected_error (s
);
355 if (legal_number (t
, &r
) == 0)
356 integer_expected_error (t
);
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
);
373 patcomp (string
, pat
, op
)
379 m
= strmatch (pat
, string
, FNMATCH_EXTFLAG
|FNMATCH_IGNCASE
);
380 return ((op
== EQ
) ? (m
== 0) : (m
!= 0));
384 binary_test (op
, arg1
, arg2
, flags
)
385 char *op
, *arg1
, *arg2
;
390 patmatch
= (flags
& TEST_PATMATCH
);
392 if (op
[0] == '=' && (op
[1] == '\0' || (op
[1] == '=' && op
[2] == '\0')))
393 return (patmatch
? patcomp (arg1
, arg2
, EQ
) : STREQ (arg1
, arg2
));
394 else if ((op
[0] == '>' || op
[0] == '<') && op
[1] == '\0')
396 #if defined (HAVE_STRCOLL)
397 if (shell_compatibility_level
> 40 && flags
& TEST_LOCALE
)
398 return ((op
[0] == '>') ? (strcoll (arg1
, arg2
) > 0) : (strcoll (arg1
, arg2
) < 0));
401 return ((op
[0] == '>') ? (strcmp (arg1
, arg2
) > 0) : (strcmp (arg1
, arg2
) < 0));
403 else if (op
[0] == '!' && op
[1] == '=' && op
[2] == '\0')
404 return (patmatch
? patcomp (arg1
, arg2
, NE
) : (STREQ (arg1
, arg2
) == 0));
407 else if (op
[2] == 't')
411 case 'n': return (filecomp (arg1
, arg2
, NT
)); /* -nt */
412 case 'o': return (filecomp (arg1
, arg2
, OT
)); /* -ot */
413 case 'l': return (arithcomp (arg1
, arg2
, LT
, flags
)); /* -lt */
414 case 'g': return (arithcomp (arg1
, arg2
, GT
, flags
)); /* -gt */
417 else if (op
[1] == 'e')
421 case 'f': return (filecomp (arg1
, arg2
, EF
)); /* -ef */
422 case 'q': return (arithcomp (arg1
, arg2
, EQ
, flags
)); /* -eq */
425 else if (op
[2] == 'e')
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 */
435 return (FALSE
); /* should never get here */
446 if ((w
[0] == '=' && (w
[1] == '\0' || (w
[1] == '=' && w
[2] == '\0'))) || /* =, == */
447 ((w
[0] == '>' || w
[0] == '<') && w
[1] == '\0') || /* <, > */
448 (w
[0] == '!' && w
[1] == '=' && w
[2] == '\0')) /* != */
450 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], 0);
455 #if defined (PATTERN_MATCHING)
456 if ((w
[0] == '=' || w
[0] == '!') && w
[1] == '~' && w
[2] == '\0')
458 value
= patcomp (argv
[pos
], argv
[pos
+ 2], w
[0] == '=' ? EQ
: NE
);
464 if ((w
[0] != '-' || w
[3] != '\0') || test_binop (w
) == 0)
466 test_syntax_error (_("%s: binary operator expected"), w
);
471 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], 0);
483 if (test_unop (op
) == 0)
486 /* the only tricky case is `-t', which may or may not take an argument. */
492 if (legal_number (argv
[pos
], &r
))
495 return (unary_test (op
, argv
[pos
- 1]));
501 return (unary_test (op
, "1"));
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. */
509 return (unary_test (op
, argv
[pos
- 1]));
517 struct stat stat_buf
;
518 struct timespec mtime
, atime
;
523 case 'a': /* file exists in the file system? */
525 return (sh_stat (arg
, &stat_buf
) == 0);
527 case 'r': /* file is readable? */
528 return (sh_eaccess (arg
, R_OK
) == 0);
530 case 'w': /* File is writeable? */
531 return (sh_eaccess (arg
, W_OK
) == 0);
533 case 'x': /* File is executable? */
534 return (sh_eaccess (arg
, X_OK
) == 0);
536 case 'O': /* File is owned by you? */
537 return (sh_stat (arg
, &stat_buf
) == 0 &&
538 (uid_t
) current_user
.euid
== (uid_t
) stat_buf
.st_uid
);
540 case 'G': /* File is owned by your group? */
541 return (sh_stat (arg
, &stat_buf
) == 0 &&
542 (gid_t
) current_user
.egid
== (gid_t
) stat_buf
.st_gid
);
545 if (sh_stat (arg
, &stat_buf
) < 0)
547 atime
= get_stat_atime (&stat_buf
);
548 mtime
= get_stat_mtime (&stat_buf
);
549 return (timespec_cmp (mtime
, atime
) > 0);
551 case 'f': /* File is a file? */
552 if (sh_stat (arg
, &stat_buf
) < 0)
555 /* -f is true if the given file exists and is a regular file. */
557 return (S_ISREG (stat_buf
.st_mode
) || (stat_buf
.st_mode
& S_IFMT
) == 0);
559 return (S_ISREG (stat_buf
.st_mode
));
562 case 'd': /* File is a directory? */
563 return (sh_stat (arg
, &stat_buf
) == 0 && (S_ISDIR (stat_buf
.st_mode
)));
565 case 's': /* File has something in it? */
566 return (sh_stat (arg
, &stat_buf
) == 0 && stat_buf
.st_size
> (off_t
) 0);
568 case 'S': /* File is a socket? */
569 #if !defined (S_ISSOCK)
572 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISSOCK (stat_buf
.st_mode
));
573 #endif /* S_ISSOCK */
575 case 'c': /* File is character special? */
576 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISCHR (stat_buf
.st_mode
));
578 case 'b': /* File is block special? */
579 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISBLK (stat_buf
.st_mode
));
581 case 'p': /* File is a named pipe? */
585 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISFIFO (stat_buf
.st_mode
));
586 #endif /* S_ISFIFO */
588 case 'L': /* Same as -h */
589 case 'h': /* File is a symbolic link? */
590 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
593 return ((arg
[0] != '\0') &&
594 (lstat (arg
, &stat_buf
) == 0) && S_ISLNK (stat_buf
.st_mode
));
595 #endif /* S_IFLNK && HAVE_LSTAT */
597 case 'u': /* File is setuid? */
598 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISUID
) != 0);
600 case 'g': /* File is setgid? */
601 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISGID
) != 0);
603 case 'k': /* File has sticky bit set? */
604 #if !defined (S_ISVTX)
605 /* This is not Posix, and is not defined on some Posix systems. */
608 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISVTX
) != 0);
611 case 't': /* File fd is a terminal? */
612 if (legal_number (arg
, &r
) == 0)
614 return ((r
== (int)r
) && isatty ((int)r
));
616 case 'n': /* True if arg has some length. */
617 return (arg
[0] != '\0');
619 case 'z': /* True if arg has no length. */
620 return (arg
[0] == '\0');
622 case 'o': /* True if option `arg' is set. */
623 return (minus_o_option_value (arg
) == 1);
626 #if defined (ARRAY_VARS)
627 if (valid_array_reference (arg
, 0))
630 int rtype
, ret
, flags
;
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);
635 ret
= t
? TRUE
: FALSE
;
636 if (rtype
> 0) /* subscript is * or @ */
640 else if (legal_number (arg
, &r
)) /* -v n == is $n set? */
641 return ((r
>= 0 && r
<= number_of_args()) ? TRUE
: FALSE
);
642 v
= find_variable (arg
);
643 if (v
&& invisible_p (v
) == 0 && array_p (v
))
646 /* [[ -v foo ]] == [[ -v foo[0] ]] */
647 t
= array_reference (array_cell (v
), 0);
648 return (t
? TRUE
: FALSE
);
650 else if (v
&& invisible_p (v
) == 0 && assoc_p (v
))
653 t
= assoc_reference (assoc_cell (v
), "0");
654 return (t
? TRUE
: FALSE
);
657 v
= find_variable (arg
);
659 return (v
&& invisible_p (v
) == 0 && var_isset (v
) ? TRUE
: FALSE
);
662 v
= find_variable_noref (arg
);
663 return ((v
&& invisible_p (v
) == 0 && var_isset (v
) && nameref_p (v
)) ? TRUE
: FALSE
);
666 /* We can't actually get here, but this shuts up gcc. */
670 /* Return TRUE if OP is one of the test command's binary operators. */
675 if (op
[0] == '=' && op
[1] == '\0')
676 return (1); /* '=' */
677 else if ((op
[0] == '<' || op
[0] == '>') && op
[1] == '\0') /* string <, > */
679 else if ((op
[0] == '=' || op
[0] == '!') && op
[1] == '=' && op
[2] == '\0')
680 return (1); /* `==' and `!=' */
681 #if defined (PATTERN_MATCHING)
682 else if (op
[2] == '\0' && op
[1] == '~' && (op
[0] == '=' || op
[0] == '!'))
685 else if (op
[0] != '-' || op
[1] == '\0' || op
[2] == '\0' || op
[3] != '\0')
700 else if (op
[1] == 'e')
709 else if (op
[2] == 'e')
724 /* Return non-zero if OP is one of the test command's unary operators. */
729 if (op
[0] != '-' || (op
[1] && op
[2] != 0))
734 case 'a': case 'b': case 'c': case 'd': case 'e':
735 case 'f': case 'g': case 'h': case 'k': case 'n':
736 case 'o': case 'p': case 'r': case 's': case 't':
737 case 'u': case 'v': case 'w': case 'x': case 'z':
738 case 'G': case 'L': case 'O': case 'S': case 'N':
749 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
750 return (argv
[pos
+ 1][0] == '\0');
751 else if (argv
[pos
][0] == '-' && argv
[pos
][1] && argv
[pos
][2] == '\0')
753 if (test_unop (argv
[pos
]))
754 return (unary_operator ());
756 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
759 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
764 #define ANDOR(s) (s[0] == '-' && (s[1] == 'a' || s[1] == 'o') && s[2] == 0)
766 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
767 POSIX requires that `-t' be given an argument. */
768 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
775 if (test_binop (argv
[pos
+1]))
777 value
= binary_operator ();
780 else if (ANDOR (argv
[pos
+1]))
782 if (argv
[pos
+1][1] == 'a')
783 value
= ONE_ARG_TEST(argv
[pos
]) && ONE_ARG_TEST(argv
[pos
+2]);
785 value
= ONE_ARG_TEST(argv
[pos
]) || ONE_ARG_TEST(argv
[pos
+2]);
788 else if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
791 value
= !two_arguments ();
793 else if (argv
[pos
][0] == '(' && argv
[pos
+2][0] == ')')
795 value
= ONE_ARG_TEST(argv
[pos
+1]);
799 test_syntax_error (_("%s: binary operator expected"), argv
[pos
+1]);
804 /* This is an implementation of a Posix.2 proposal by David Korn. */
810 switch (argc
- 1) /* one extra passed in */
818 value
= ONE_ARG_TEST(argv
[1]);
823 value
= two_arguments ();
828 value
= three_arguments ();
832 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
835 value
= !three_arguments ();
838 else if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0' && argv
[argc
-1][0] == ')' && argv
[argc
-1][1] == '\0')
841 value
= two_arguments ();
860 test_command (margc
, 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
));