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)
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"
59 #include "builtins/common.h"
61 #include <glob/strmatch.h>
64 # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
68 # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0)
70 #define STRCOLLEQ(a, b) ((a)[0] == (b)[0] && strcoll ((a), (b)) == 0)
90 /* The following few defines control the truth and false output of each stage.
91 TRUE and FALSE are what we use to compute the final output value.
92 SHELL_BOOLEAN is the form which returns truth or falseness in shell terms.
93 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
96 #define SHELL_BOOLEAN(value) (!(value))
98 #define TEST_ERREXIT_STATUS 2
100 static procenv_t test_exit_buf;
101 static int test_error_return;
102 #define test_exit(val) \
103 do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
105 extern int sh_stat __P((const char *, struct stat *));
107 static int pos; /* The offset of the current argument in ARGV. */
108 static int argc; /* The number of arguments present in ARGV. */
109 static char **argv; /* The argument list. */
112 static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__));
113 static void beyond __P((void)) __attribute__((__noreturn__));
114 static void integer_expected_error __P((char *)) __attribute__((__noreturn__));
116 static int unary_operator __P((void));
117 static int binary_operator __P((void));
118 static int two_arguments __P((void));
119 static int three_arguments __P((void));
120 static int posixtest __P((void));
122 static int expr __P((void));
123 static int term __P((void));
124 static int and __P((void));
125 static int or __P((void));
127 static int filecomp __P((char *, char *, int));
128 static int arithcomp __P((char *, char *, int, int));
129 static int patcomp __P((char *, char *, int));
132 test_syntax_error (format, 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 (pch)
155 test_syntax_error (_("%s: integer expression expected"), pch);
158 /* Increment our position in the argument list. Check that we're not
159 past the end of the argument list. This check is supressed if the
160 argument is FALSE. Made a macro for efficiency. */
161 #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
162 #define unary_advance() do { advance (1); ++pos; } while (0)
174 return (FALSE ^ or ()); /* Same with this. */
188 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
192 return (value || v2);
209 if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
213 return (value && v2);
219 * term - parse a term and return 1 or 0 depending on whether the term
220 * evaluates to true or false, respectively.
223 * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
224 * '-'('G'|'L'|'O'|'S'|'N') filename
226 * '-'('z'|'n') string
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 */
275 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
277 if (test_unop (argv[pos]))
278 value = unary_operator ();
280 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
284 value = argv[pos][0] != '\0';
296 struct stat st1, st2;
299 if ((r1 = sh_stat (s, &st1)) < 0)
304 if ((r2 = sh_stat (t, &st2)) < 0)
312 case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime));
313 case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime));
314 case EF: return (same_file (s, t, &st1, &st2));
320 arithcomp (s, t, op, flags)
327 if (flags & TEST_ARITHEXP)
329 l = evalexp (s, &expok);
331 return (FALSE); /* should probably longjmp here */
332 r = evalexp (t, &expok);
334 return (FALSE); /* ditto */
338 if (legal_number (s, &l) == 0)
339 integer_expected_error (s);
340 if (legal_number (t, &r) == 0)
341 integer_expected_error (t);
346 case EQ: return (l == r);
347 case NE: return (l != r);
348 case LT: return (l < r);
349 case GT: return (l > r);
350 case LE: return (l <= r);
351 case GE: return (l >= r);
358 patcomp (string, pat, op)
364 m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE);
365 return ((op == EQ) ? (m == 0) : (m != 0));
369 binary_test (op, arg1, arg2, flags)
370 char *op, *arg1, *arg2;
375 patmatch = (flags & TEST_PATMATCH);
377 if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
378 return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
379 else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
381 if (shell_compatibility_level > 40 && flags & TEST_LOCALE)
382 return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0));
384 return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
386 else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
387 return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
390 else if (op[2] == 't')
394 case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
395 case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
396 case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
397 case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
400 else if (op[1] == 'e')
404 case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */
405 case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */
408 else if (op[2] == 'e')
412 case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */
413 case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */
414 case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */
418 return (FALSE); /* should never get here */
429 if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */
430 ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */
431 (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */
433 value = binary_test (w, argv[pos], argv[pos + 2], 0);
438 #if defined (PATTERN_MATCHING)
439 if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
441 value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
447 if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
449 test_syntax_error (_("%s: binary operator expected"), w);
454 value = binary_test (w, argv[pos], argv[pos + 2], 0);
466 if (test_unop (op) == 0)
469 /* the only tricky case is `-t', which may or may not take an argument. */
475 if (legal_number (argv[pos], &r))
478 return (unary_test (op, argv[pos - 1]));
484 return (unary_test (op, "1"));
487 /* All of the unary operators take an argument, so we first call
488 unary_advance (), which checks to make sure that there is an
489 argument, and then advances pos right past it. This means that
490 pos - 1 is the location of the argument. */
492 return (unary_test (op, argv[pos - 1]));
500 struct stat stat_buf;
505 case 'a': /* file exists in the file system? */
507 return (sh_stat (arg, &stat_buf) == 0);
509 case 'r': /* file is readable? */
510 return (sh_eaccess (arg, R_OK) == 0);
512 case 'w': /* File is writeable? */
513 return (sh_eaccess (arg, W_OK) == 0);
515 case 'x': /* File is executable? */
516 return (sh_eaccess (arg, X_OK) == 0);
518 case 'O': /* File is owned by you? */
519 return (sh_stat (arg, &stat_buf) == 0 &&
520 (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
522 case 'G': /* File is owned by your group? */
523 return (sh_stat (arg, &stat_buf) == 0 &&
524 (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
527 return (sh_stat (arg, &stat_buf) == 0 &&
528 stat_buf.st_atime <= stat_buf.st_mtime);
530 case 'f': /* File is a file? */
531 if (sh_stat (arg, &stat_buf) < 0)
534 /* -f is true if the given file exists and is a regular file. */
536 return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
538 return (S_ISREG (stat_buf.st_mode));
541 case 'd': /* File is a directory? */
542 return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
544 case 's': /* File has something in it? */
545 return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
547 case 'S': /* File is a socket? */
548 #if !defined (S_ISSOCK)
551 return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
552 #endif /* S_ISSOCK */
554 case 'c': /* File is character special? */
555 return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
557 case 'b': /* File is block special? */
558 return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
560 case 'p': /* File is a named pipe? */
564 return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
565 #endif /* S_ISFIFO */
567 case 'L': /* Same as -h */
568 case 'h': /* File is a symbolic link? */
569 #if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
572 return ((arg[0] != '\0') &&
573 (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
574 #endif /* S_IFLNK && HAVE_LSTAT */
576 case 'u': /* File is setuid? */
577 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
579 case 'g': /* File is setgid? */
580 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
582 case 'k': /* File has sticky bit set? */
583 #if !defined (S_ISVTX)
584 /* This is not Posix, and is not defined on some Posix systems. */
587 return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
590 case 't': /* File fd is a terminal? */
591 if (legal_number (arg, &r) == 0)
593 return ((r == (int)r) && isatty ((int)r));
595 case 'n': /* True if arg has some length. */
596 return (arg[0] != '\0');
598 case 'z': /* True if arg has no length. */
599 return (arg[0] == '\0');
601 case 'o': /* True if option `arg' is set. */
602 return (minus_o_option_value (arg) == 1);
605 v = find_variable (arg);
606 return (v && var_isset (v) ? TRUE : FALSE);
609 /* We can't actually get here, but this shuts up gcc. */
613 /* Return TRUE if OP is one of the test command's binary operators. */
618 if (op[0] == '=' && op[1] == '\0')
619 return (1); /* '=' */
620 else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
622 else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
623 return (1); /* `==' and `!=' */
624 #if defined (PATTERN_MATCHING)
625 else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
628 else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
643 else if (op[1] == 'e')
652 else if (op[2] == 'e')
667 /* Return non-zero if OP is one of the test command's unary operators. */
672 if (op[0] != '-' || op[2] != 0)
677 case 'a': case 'b': case 'c': case 'd': case 'e':
678 case 'f': case 'g': case 'h': case 'k': case 'n':
679 case 'o': case 'p': case 'r': case 's': case 't':
680 case 'u': case 'v': case 'w': case 'x': case 'z':
681 case 'G': case 'L': case 'O': case 'S': case 'N':
691 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
692 return (argv[pos + 1][0] == '\0');
693 else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
695 if (test_unop (argv[pos]))
696 return (unary_operator ());
698 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
701 test_syntax_error (_("%s: unary operator expected"), argv[pos]);
706 #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
708 /* This could be augmented to handle `-t' as equivalent to `-t 1', but
709 POSIX requires that `-t' be given an argument. */
710 #define ONE_ARG_TEST(s) ((s)[0] != '\0')
717 if (test_binop (argv[pos+1]))
719 value = binary_operator ();
722 else if (ANDOR (argv[pos+1]))
724 if (argv[pos+1][1] == 'a')
725 value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]);
727 value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]);
730 else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
733 value = !two_arguments ();
735 else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
737 value = ONE_ARG_TEST(argv[pos+1]);
741 test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
746 /* This is an implementation of a Posix.2 proposal by David Korn. */
752 switch (argc - 1) /* one extra passed in */
760 value = ONE_ARG_TEST(argv[1]);
765 value = two_arguments ();
770 value = three_arguments ();
774 if (argv[pos][0] == '!' && argv[pos][1] == '\0')
777 value = !three_arguments ();
795 test_command (margc, margv)
804 code = setjmp (test_exit_buf);
807 return (test_error_return);
811 if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
815 if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
816 test_syntax_error (_("missing `]'"), (char *)NULL);
819 test_exit (SHELL_BOOLEAN (FALSE));
826 test_exit (SHELL_BOOLEAN (FALSE));
829 value = posixtest ();
832 test_syntax_error (_("too many arguments"), (char *)NULL);
834 test_exit (SHELL_BOOLEAN (value));