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-2010 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
__P((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
__P((char *, char *)) __attribute__((__noreturn__
));
114 static void beyond
__P((void)) __attribute__((__noreturn__
));
115 static void integer_expected_error
__P((char *)) __attribute__((__noreturn__
));
117 static int unary_operator
__P((void));
118 static int binary_operator
__P((void));
119 static int two_arguments
__P((void));
120 static int three_arguments
__P((void));
121 static int posixtest
__P((void));
123 static int expr
__P((void));
124 static int term
__P((void));
125 static int and __P((void));
126 static int or __P((void));
128 static int filecomp
__P((char *, char *, int));
129 static int arithcomp
__P((char *, char *, int, int));
130 static int patcomp
__P((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 */
277 else if (argv
[pos
][0] == '-' && argv
[pos
][2] == '\0')
279 if (test_unop (argv
[pos
]))
280 value
= unary_operator ();
282 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
286 value
= argv
[pos
][0] != '\0';
294 stat_mtime (fn
, st
, ts
)
301 r
= sh_stat (fn
, st
);
304 *ts
= get_stat_mtime (st
);
313 struct stat st1
, st2
;
314 struct timespec ts1
, ts2
;
317 if ((r1
= stat_mtime (s
, &st1
, &ts1
)) < 0)
322 if ((r2
= stat_mtime (t
, &st2
, &ts2
)) < 0)
330 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));
332 case EF
: return (same_file (s
, t
, &st1
, &st2
));
338 arithcomp (s
, t
, op
, flags
)
345 if (flags
& TEST_ARITHEXP
)
347 l
= evalexp (s
, &expok
);
349 return (FALSE
); /* should probably longjmp here */
350 r
= evalexp (t
, &expok
);
352 return (FALSE
); /* ditto */
356 if (legal_number (s
, &l
) == 0)
357 integer_expected_error (s
);
358 if (legal_number (t
, &r
) == 0)
359 integer_expected_error (t
);
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
);
376 patcomp (string
, pat
, op
)
382 m
= strmatch (pat
, string
, FNMATCH_EXTFLAG
|FNMATCH_IGNCASE
);
383 return ((op
== EQ
) ? (m
== 0) : (m
!= 0));
387 binary_test (op
, arg1
, arg2
, flags
)
388 char *op
, *arg1
, *arg2
;
393 patmatch
= (flags
& TEST_PATMATCH
);
395 if (op
[0] == '=' && (op
[1] == '\0' || (op
[1] == '=' && op
[2] == '\0')))
396 return (patmatch
? patcomp (arg1
, arg2
, EQ
) : STREQ (arg1
, arg2
));
397 else if ((op
[0] == '>' || op
[0] == '<') && op
[1] == '\0')
399 #if defined (HAVE_STRCOLL)
400 if (shell_compatibility_level
> 40 && flags
& TEST_LOCALE
)
401 return ((op
[0] == '>') ? (strcoll (arg1
, arg2
) > 0) : (strcoll (arg1
, arg2
) < 0));
404 return ((op
[0] == '>') ? (strcmp (arg1
, arg2
) > 0) : (strcmp (arg1
, arg2
) < 0));
406 else if (op
[0] == '!' && op
[1] == '=' && op
[2] == '\0')
407 return (patmatch
? patcomp (arg1
, arg2
, NE
) : (STREQ (arg1
, arg2
) == 0));
410 else if (op
[2] == 't')
414 case 'n': return (filecomp (arg1
, arg2
, NT
)); /* -nt */
415 case 'o': return (filecomp (arg1
, arg2
, OT
)); /* -ot */
416 case 'l': return (arithcomp (arg1
, arg2
, LT
, flags
)); /* -lt */
417 case 'g': return (arithcomp (arg1
, arg2
, GT
, flags
)); /* -gt */
420 else if (op
[1] == 'e')
424 case 'f': return (filecomp (arg1
, arg2
, EF
)); /* -ef */
425 case 'q': return (arithcomp (arg1
, arg2
, EQ
, flags
)); /* -eq */
428 else if (op
[2] == 'e')
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 */
438 return (FALSE
); /* should never get here */
449 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')) /* != */
453 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], 0);
458 #if defined (PATTERN_MATCHING)
459 if ((w
[0] == '=' || w
[0] == '!') && w
[1] == '~' && w
[2] == '\0')
461 value
= patcomp (argv
[pos
], argv
[pos
+ 2], w
[0] == '=' ? EQ
: NE
);
467 if ((w
[0] != '-' || w
[3] != '\0') || test_binop (w
) == 0)
469 test_syntax_error (_("%s: binary operator expected"), w
);
474 value
= binary_test (w
, argv
[pos
], argv
[pos
+ 2], 0);
486 if (test_unop (op
) == 0)
489 /* the only tricky case is `-t', which may or may not take an argument. */
495 if (legal_number (argv
[pos
], &r
))
498 return (unary_test (op
, argv
[pos
- 1]));
504 return (unary_test (op
, "1"));
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. */
512 return (unary_test (op
, argv
[pos
- 1]));
520 struct stat stat_buf
;
525 case 'a': /* file exists in the file system? */
527 return (sh_stat (arg
, &stat_buf
) == 0);
529 case 'r': /* file is readable? */
530 return (sh_eaccess (arg
, R_OK
) == 0);
532 case 'w': /* File is writeable? */
533 return (sh_eaccess (arg
, W_OK
) == 0);
535 case 'x': /* File is executable? */
536 return (sh_eaccess (arg
, X_OK
) == 0);
538 case 'O': /* File is owned by you? */
539 return (sh_stat (arg
, &stat_buf
) == 0 &&
540 (uid_t
) current_user
.euid
== (uid_t
) stat_buf
.st_uid
);
542 case 'G': /* File is owned by your group? */
543 return (sh_stat (arg
, &stat_buf
) == 0 &&
544 (gid_t
) current_user
.egid
== (gid_t
) stat_buf
.st_gid
);
547 return (sh_stat (arg
, &stat_buf
) == 0 &&
548 stat_buf
.st_atime
<= stat_buf
.st_mtime
);
550 case 'f': /* File is a file? */
551 if (sh_stat (arg
, &stat_buf
) < 0)
554 /* -f is true if the given file exists and is a regular file. */
556 return (S_ISREG (stat_buf
.st_mode
) || (stat_buf
.st_mode
& S_IFMT
) == 0);
558 return (S_ISREG (stat_buf
.st_mode
));
561 case 'd': /* File is a directory? */
562 return (sh_stat (arg
, &stat_buf
) == 0 && (S_ISDIR (stat_buf
.st_mode
)));
564 case 's': /* File has something in it? */
565 return (sh_stat (arg
, &stat_buf
) == 0 && stat_buf
.st_size
> (off_t
) 0);
567 case 'S': /* File is a socket? */
568 #if !defined (S_ISSOCK)
571 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISSOCK (stat_buf
.st_mode
));
572 #endif /* S_ISSOCK */
574 case 'c': /* File is character special? */
575 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISCHR (stat_buf
.st_mode
));
577 case 'b': /* File is block special? */
578 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISBLK (stat_buf
.st_mode
));
580 case 'p': /* File is a named pipe? */
584 return (sh_stat (arg
, &stat_buf
) == 0 && S_ISFIFO (stat_buf
.st_mode
));
585 #endif /* S_ISFIFO */
587 case 'L': /* Same as -h */
588 case 'h': /* File is a symbolic link? */
589 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
592 return ((arg
[0] != '\0') &&
593 (lstat (arg
, &stat_buf
) == 0) && S_ISLNK (stat_buf
.st_mode
));
594 #endif /* S_IFLNK && HAVE_LSTAT */
596 case 'u': /* File is setuid? */
597 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISUID
) != 0);
599 case 'g': /* File is setgid? */
600 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISGID
) != 0);
602 case 'k': /* File has sticky bit set? */
603 #if !defined (S_ISVTX)
604 /* This is not Posix, and is not defined on some Posix systems. */
607 return (sh_stat (arg
, &stat_buf
) == 0 && (stat_buf
.st_mode
& S_ISVTX
) != 0);
610 case 't': /* File fd is a terminal? */
611 if (legal_number (arg
, &r
) == 0)
613 return ((r
== (int)r
) && isatty ((int)r
));
615 case 'n': /* True if arg has some length. */
616 return (arg
[0] != '\0');
618 case 'z': /* True if arg has no length. */
619 return (arg
[0] == '\0');
621 case 'o': /* True if option `arg' is set. */
622 return (minus_o_option_value (arg
) == 1);
625 v
= find_variable (arg
);
626 #if defined (ARRAY_VARS)
627 if (v
== 0 && valid_array_reference (arg
, 0))
630 t
= array_value (arg
, 0, 0, (int *)0, (arrayind_t
*)0);
631 return (t
? TRUE
: FALSE
);
633 else if (v
&& invisible_p (v
) == 0 && array_p (v
))
636 /* [[ -v foo ]] == [[ -v foo[0] ]] */
637 t
= array_reference (array_cell (v
), 0);
638 return (t
? TRUE
: FALSE
);
640 else if (v
&& invisible_p (v
) == 0 && assoc_p (v
))
643 t
= assoc_reference (assoc_cell (v
), "0");
644 return (t
? TRUE
: FALSE
);
647 return (v
&& invisible_p (v
) == 0 && var_isset (v
) ? TRUE
: FALSE
);
650 v
= find_variable_noref (arg
);
651 return ((v
&& invisible_p (v
) == 0 && var_isset (v
) && nameref_p (v
)) ? TRUE
: FALSE
);
654 /* We can't actually get here, but this shuts up gcc. */
658 /* Return TRUE if OP is one of the test command's binary operators. */
663 if (op
[0] == '=' && op
[1] == '\0')
664 return (1); /* '=' */
665 else if ((op
[0] == '<' || op
[0] == '>') && op
[1] == '\0') /* string <, > */
667 else if ((op
[0] == '=' || op
[0] == '!') && op
[1] == '=' && op
[2] == '\0')
668 return (1); /* `==' and `!=' */
669 #if defined (PATTERN_MATCHING)
670 else if (op
[2] == '\0' && op
[1] == '~' && (op
[0] == '=' || op
[0] == '!'))
673 else if (op
[0] != '-' || op
[2] == '\0' || op
[3] != '\0')
688 else if (op
[1] == 'e')
697 else if (op
[2] == 'e')
712 /* Return non-zero if OP is one of the test command's unary operators. */
717 if (op
[0] != '-' || op
[2] != 0)
722 case 'a': case 'b': case 'c': case 'd': case 'e':
723 case 'f': case 'g': case 'h': case 'k': case 'n':
724 case 'o': case 'p': case 'r': case 's': case 't':
725 case 'u': case 'v': case 'w': case 'x': case 'z':
726 case 'G': case 'L': case 'O': case 'S': case 'N':
737 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
738 return (argv
[pos
+ 1][0] == '\0');
739 else if (argv
[pos
][0] == '-' && argv
[pos
][2] == '\0')
741 if (test_unop (argv
[pos
]))
742 return (unary_operator ());
744 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
747 test_syntax_error (_("%s: unary operator expected"), argv
[pos
]);
752 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
754 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
755 POSIX requires that `-t' be given an argument. */
756 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
763 if (test_binop (argv
[pos
+1]))
765 value
= binary_operator ();
768 else if (ANDOR (argv
[pos
+1]))
770 if (argv
[pos
+1][1] == 'a')
771 value
= ONE_ARG_TEST(argv
[pos
]) && ONE_ARG_TEST(argv
[pos
+2]);
773 value
= ONE_ARG_TEST(argv
[pos
]) || ONE_ARG_TEST(argv
[pos
+2]);
776 else if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
779 value
= !two_arguments ();
781 else if (argv
[pos
][0] == '(' && argv
[pos
+2][0] == ')')
783 value
= ONE_ARG_TEST(argv
[pos
+1]);
787 test_syntax_error (_("%s: binary operator expected"), argv
[pos
+1]);
792 /* This is an implementation of a Posix.2 proposal by David Korn. */
798 switch (argc
- 1) /* one extra passed in */
806 value
= ONE_ARG_TEST(argv
[1]);
811 value
= two_arguments ();
816 value
= three_arguments ();
820 if (argv
[pos
][0] == '!' && argv
[pos
][1] == '\0')
823 value
= !three_arguments ();
826 else if (argv
[pos
][0] == '(' && argv
[pos
][1] == '\0' && argv
[argc
-1][0] == ')' && argv
[argc
-1][1] == '\0')
829 value
= two_arguments ();
848 test_command (margc
, margv
)
857 code
= setjmp_nosigs (test_exit_buf
);
860 return (test_error_return
);
864 if (margv
[0] && margv
[0][0] == '[' && margv
[0][1] == '\0')
868 if (margv
[margc
] && (margv
[margc
][0] != ']' || margv
[margc
][1]))
869 test_syntax_error (_("missing `]'"), (char *)NULL
);
872 test_exit (SHELL_BOOLEAN (FALSE
));
879 test_exit (SHELL_BOOLEAN (FALSE
));
882 value
= posixtest ();
885 test_syntax_error (_("too many arguments"), (char *)NULL
);
887 test_exit (SHELL_BOOLEAN (value
));