} test_exit (SHELL_BOOLEAN (value)); test_syntax_error (_("too many arguments"), (char *)NULL); if (pos != argc) value = posixtest (); noeval = 0; test_exit (SHELL_BOOLEAN (FALSE)); if (pos >= argc) pos = 1; argc = margc; } test_exit (SHELL_BOOLEAN (FALSE)); if (margc < 2) test_syntax_error (_("missing `]'"), (char *)NULL); if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1])) --margc; { if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0') argv = margv; return (test_error_return); if (code) code = setjmp (test_exit_buf); USE_VAR(margc); int code; int value; { char **margv; int margc; test_command (margc, margv) int */ * test expr * test: * '[' expr ']' * [: /* } return (value); } value = expr (); default: /* FALLTHROUGH */ } break; value = !three_arguments (); advance (1); { if (argv[pos][0] == '!' && argv[pos][1] == '\0') case 4: break; value = three_arguments (); case 3: break; pos = argc; value = two_arguments (); case 2: break; pos = argc; value = ONE_ARG_TEST(argv[1]); case 1: break; pos = argc; value = FALSE; case 0: { switch (argc - 1) /* one extra passed in */ int value; { posixtest () static int /* This is an implementation of a Posix.2 proposal by David Korn. */ } return (value); test_syntax_error (_("%s: binary operator expected"), argv[pos+1]); else } pos = argc; value = ONE_ARG_TEST(argv[pos+1]); { else if (argv[pos][0] == '(' && argv[pos+2][0] == ')') } value = !two_arguments (); advance (1); { else if (argv[pos][0] == '!' && argv[pos][1] == '\0') } pos = argc; value = ONE_ARG_TEST(argv[pos]) || ONE_ARG_TEST(argv[pos+2]); else value = ONE_ARG_TEST(argv[pos]) && ONE_ARG_TEST(argv[pos+2]); if (argv[pos+1][1] == 'a') { else if (ANDOR (argv[pos+1])) } pos = argc; value = binary_operator (); { if (test_binop (argv[pos+1])) int value; { three_arguments () static int #define ONE_ARG_TEST(s) ((s)[0] != '\0') POSIX requires that `-t' be given an argument. */ /* This could be augmented to handle `-t' as equivalent to `-t 1', but #define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o')) } return (0); test_syntax_error (_("%s: unary operator expected"), argv[pos]); else } test_syntax_error (_("%s: unary operator expected"), argv[pos]); else return (unary_operator ()); if (test_unop (argv[pos])) { else if (argv[pos][0] == '-' && argv[pos][2] == '\0') return (argv[pos + 1][0] == '\0'); if (argv[pos][0] == '!' && argv[pos][1] == '\0') { two_arguments () static int } return (0); } return (1); case 'G': case 'L': case 'O': case 'S': case 'N': case 'u': case 'v': case 'w': case 'x': case 'z': case 'o': case 'p': case 'r': case 's': case 't': case 'f': case 'g': case 'h': case 'k': case 'n': case 'a': case 'b': case 'c': case 'd': case 'e': { switch (op[1]) return (0); if (op[0] != '-' || op[2] != 0) { char *op; test_unop (op) int /* Return non-zero if OP is one of the test command's unary operators. */ } } return (0); else } return (0); default: return (1); case 'l': /* -le */ case 'g': /* -ge */ case 'n': /* -ne */ { switch (op[1]) else if (op[2] == 'e') } return (0); default: return (1); case 'f': /* -ef */ case 'q': /* -eq */ { switch (op[2]) else if (op[1] == 'e') } return (0); default: return (1); case 'g': /* -gt */ case 'l': /* -lt */ case 'o': /* -ot */ case 'n': /* -nt */ { switch (op[1]) if (op[2] == 't') { else return (0); else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0') #endif return (1); else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!')) #if defined (PATTERN_MATCHING) return (1); /* `==' and `!=' */ else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0') return (1); else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */ return (1); /* '=' */ if (op[0] == '=' && op[1] == '\0') { char *op; test_binop (op) int /* Return TRUE if OP is one of the test command's binary operators. */ } return (FALSE); /* We can't actually get here, but this shuts up gcc. */ } return (v && var_isset (v) ? TRUE : FALSE); v = find_variable (arg); case 'v': return (minus_o_option_value (arg) == 1); case 'o': /* True if option `arg' is set. */ return (arg[0] == '\0'); case 'z': /* True if arg has no length. */ return (arg[0] != '\0'); case 'n': /* True if arg has some length. */ return ((r == (int)r) && isatty ((int)r)); return (FALSE); if (legal_number (arg, &r) == 0) case 't': /* File fd is a terminal? */ #endif return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0); #else return (FALSE); /* This is not Posix, and is not defined on some Posix systems. */ #if !defined (S_ISVTX) case 'k': /* File has sticky bit set? */ return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0); case 'g': /* File is setgid? */ return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0); case 'u': /* File is setuid? */ #endif /* S_IFLNK && HAVE_LSTAT */ (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode)); return ((arg[0] != '\0') && #else return (FALSE); #if !defined (S_ISLNK) || !defined (HAVE_LSTAT) case 'h': /* File is a symbolic link? */ case 'L': /* Same as -h */ #endif /* S_ISFIFO */ return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode)); #else return (FALSE); #ifndef S_ISFIFO case 'p': /* File is a named pipe? */ return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode)); case 'b': /* File is block special? */ return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode)); case 'c': /* File is character special? */ #endif /* S_ISSOCK */ return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode)); #else return (FALSE); #if !defined (S_ISSOCK) case 'S': /* File is a socket? */ return (sh_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0); case 's': /* File has something in it? */ return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode))); case 'd': /* File is a directory? */ #endif /* !S_IFMT */ return (S_ISREG (stat_buf.st_mode)); #else return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0); #if defined (S_IFMT) /* -f is true if the given file exists and is a regular file. */ return (FALSE); if (sh_stat (arg, &stat_buf) < 0) case 'f': /* File is a file? */ stat_buf.st_atime <= stat_buf.st_mtime); return (sh_stat (arg, &stat_buf) == 0 && case 'N': (gid_t) current_user.egid == (gid_t) stat_buf.st_gid); return (sh_stat (arg, &stat_buf) == 0 && case 'G': /* File is owned by your group? */ (uid_t) current_user.euid == (uid_t) stat_buf.st_uid); return (sh_stat (arg, &stat_buf) == 0 && case 'O': /* File is owned by you? */ return (sh_eaccess (arg, X_OK) == 0); case 'x': /* File is executable? */ return (sh_eaccess (arg, W_OK) == 0); case 'w': /* File is writeable? */ return (sh_eaccess (arg, R_OK) == 0); case 'r': /* file is readable? */ return (sh_stat (arg, &stat_buf) == 0); case 'e': case 'a': /* file exists in the file system? */ { switch (op[1]) SHELL_VAR *v; struct stat stat_buf; intmax_t r; { char *op, *arg; unary_test (op, arg) int } return (unary_test (op, argv[pos - 1])); unary_advance (); pos - 1 is the location of the argument. */ argument, and then advances pos right past it. This means that unary_advance (), which checks to make sure that there is an /* All of the unary operators take an argument, so we first call } return (unary_test (op, "1")); else } return (FALSE); else } return (unary_test (op, argv[pos - 1])); advance (0); { if (legal_number (argv[pos], &r)) { if (pos < argc) advance (0); { if (op[1] == 't') /* the only tricky case is `-t', which may or may not take an argument. */ return (FALSE); if (test_unop (op) == 0) op = argv[pos]; intmax_t r; char *op; { unary_operator () static int } return value; pos += 3; value = binary_test (w, argv[pos], argv[pos + 2], 0); } return (FALSE); /* NOTREACHED */ test_syntax_error (_("%s: binary operator expected"), w); { if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0) #endif } return (value); pos += 3; value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE); { if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0') #if defined (PATTERN_MATCHING) } return (value); pos += 3; value = binary_test (w, argv[pos], argv[pos + 2], 0); { (w[0] == '!' && w[1] == '=' && w[2] == '\0')) /* != */ ((w[0] == '>' || w[0] == '<') && w[1] == '\0') || /* <, > */ if ((w[0] == '=' && (w[1] == '\0' || (w[1] == '=' && w[2] == '\0'))) || /* =, == */ w = argv[pos + 1]; char *w; int value; { binary_operator () static int } return (FALSE); /* should never get here */ } } case 'l': return (arithcomp (arg1, arg2, LE, flags)); /* -le */ case 'g': return (arithcomp (arg1, arg2, GE, flags)); /* -ge */ case 'n': return (arithcomp (arg1, arg2, NE, flags)); /* -ne */ { switch (op[1]) { else if (op[2] == 'e') } } case 'q': return (arithcomp (arg1, arg2, EQ, flags)); /* -eq */ case 'f': return (filecomp (arg1, arg2, EF)); /* -ef */ { switch (op[2]) { else if (op[1] == 'e') } } case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */ case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */ case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */ case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */ { switch (op[1]) { else if (op[2] == 't') return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0)); else if (op[0] == '!' && op[1] == '=' && op[2] == '\0') } return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0)); else return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0)); if (shell_compatibility_level > 40 && flags & TEST_LOCALE) { else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0') return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2)); if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0'))) patmatch = (flags & TEST_PATMATCH); int patmatch; { int flags; char *op, *arg1, *arg2; binary_test (op, arg1, arg2, flags) int } return ((op == EQ) ? (m == 0) : (m != 0)); m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE); int m; { int op; char *string, *pat; patcomp (string, pat, op) static int } return (FALSE); } case GE: return (l >= r); case LE: return (l <= r); case GT: return (l > r); case LT: return (l < r); case NE: return (l != r); case EQ: return (l == r); { switch (op) } integer_expected_error (t); if (legal_number (t, &r) == 0) integer_expected_error (s); if (legal_number (s, &l) == 0) { else } return (FALSE); /* ditto */ if (expok == 0) r = evalexp (t, &expok); return (FALSE); /* should probably longjmp here */ if (expok == 0) l = evalexp (s, &expok); { if (flags & TEST_ARITHEXP) int expok; intmax_t l, r; { int op, flags; char *s, *t; arithcomp (s, t, op, flags) static int } return (FALSE); } case EF: return (same_file (s, t, &st1, &st2)); case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime)); case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime)); { switch (op) } return (FALSE); if (op == EF) { if ((r2 = sh_stat (t, &st2)) < 0) } return (FALSE); if (op == EF) { if ((r1 = sh_stat (s, &st1)) < 0) int r1, r2; struct stat st1, st2; { int op; char *s, *t; filecomp (s, t, op) static int } return (value); } advance (0); value = argv[pos][0] != '\0'; { else } test_syntax_error (_("%s: unary operator expected"), argv[pos]); else value = unary_operator (); if (test_unop (argv[pos])) { else if (argv[pos][0] == '-' && argv[pos][2] == '\0') /* Might be a switch type argument */ value = binary_operator (); if ((pos + 3 <= argc) && test_binop (argv[pos + 1])) /* are there enough arguments left that this could be dyadic? */ } return (value); advance (0); test_syntax_error (_("`)' expected, found %s"), argv[pos]); else if (argv[pos][0] != ')' || argv[pos][1]) /* ( */ test_syntax_error (_("`)' expected"), (char *)NULL); if (argv[pos] == 0) /* ( */ value = expr (); advance (1); { if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */ /* A paren-bracketed argument. */ } return (value ? !term() : term()); } value = 1 - value; advance (1); { while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0') value = 0; { if (argv[pos][0] == '!' && argv[pos][1] == '\0') /* Deal with leading `not's. */ beyond (); if (pos >= argc) int value; { term () static int */ * positive and negative integers * int ::= * '(' ')' * file '-'(nt|ot|ef) file * '-'(eq|ne|le|lt|ge|gt) * string ('!='|'='|'==') string * string * '-o' option * '-'('z'|'n') string * '-t' [int] * '-'('G'|'L'|'O'|'S'|'N') filename * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename * term ::= * * evaluates to true or false, respectively. * term - parse a term and return 1 or 0 depending on whether the term /* } return (value); } return (value && v2); v2 = and (); advance (0); { if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2]) value = term (); int value, v2; { and () static int */ * term '-a' and * term * and: /* } return (value); } return (value || v2); v2 = or (); advance (0); { if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2]) value = and (); int value, v2; { or () static int */ * and '-o' or * and * or: /* } return (FALSE ^ or ()); /* Same with this. */ beyond (); if (pos >= argc) { expr () static int */ * or * expr: /* #define unary_advance() do { advance (1); ++pos; } while (0) #define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0) argument is FALSE. Made a macro for efficiency. */ past the end of the argument list. This check is supressed if the /* Increment our position in the argument list. Check that we're not } test_syntax_error (_("%s: integer expression expected"), pch); { char *pch; integer_expected_error (pch) static void something else was found. */ /* Syntax error for when an integer argument was expected, but } test_syntax_error (_("argument expected"), (char *)NULL); { beyond () static void */ * error condition) * beyond - call when we're beyond the end of the argument list (an /* } test_exit (TEST_ERREXIT_STATUS); builtin_error (format, arg); { char *format, *arg; test_syntax_error (format, arg) static void static int patcomp __P((char *, char *, int)); static int arithcomp __P((char *, char *, int, int)); static int filecomp __P((char *, char *, int)); static int or __P((void)); static int and __P((void)); static int term __P((void)); static int expr __P((void)); static int posixtest __P((void)); static int three_arguments __P((void)); static int two_arguments __P((void)); static int binary_operator __P((void)); static int unary_operator __P((void)); static void integer_expected_error __P((char *)) __attribute__((__noreturn__)); static void beyond __P((void)) __attribute__((__noreturn__)); static void test_syntax_error __P((char *, char *)) __attribute__((__noreturn__)); static int noeval; static char **argv; /* The argument list. */ static int argc; /* The number of arguments present in ARGV. */ static int pos; /* The offset of the current argument in ARGV. */ extern int sh_stat __P((const char *, struct stat *)); do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0) #define test_exit(val) \ static int test_error_return; static procenv_t test_exit_buf; #define TEST_ERREXIT_STATUS 2 #define SHELL_BOOLEAN(value) (!(value)) #define FALSE 0 #define TRUE 1 Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */ SHELL_BOOLEAN is the form which returns truth or falseness in shell terms. TRUE and FALSE are what we use to compute the final output value. /* The following few defines control the truth and false output of each stage. #define EF 2 #define OT 1 #define NT 0 #define GE 5 #define LE 4 #define GT 3 #define LT 2 #define NE 1 #define EQ 0 #endif /* R_OK */ #define F_OK 0 #define X_OK 1 #define W_OK 2 #define R_OK 4 #if !defined (R_OK) #define STRCOLLEQ(a, b) ((a)[0] == (b)[0] && strcoll ((a), (b)) == 0) #endif /* !STREQ */ # define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0) #if !defined (STREQ) #endif # define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0) #if !defined (STRLEN) #include #include "builtins/common.h" #include "test.h" #include "pathexp.h" #include "shell.h" #include "bashintl.h" #include "filecntl.h" #include "posixstat.h" #endif /* !_POSIX_VERSION */ # include #if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H) #endif /* !errno */ extern int errno; #if !defined (errno) #include #endif # include #if defined (HAVE_UNISTD_H) #endif # include #if !defined (HAVE_LIMITS_H) #include "bashtypes.h" #include #endif # include #if defined (HAVE_CONFIG_H) /* #define PATTERN_MATCHING */ binary operators. */ /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching */ along with Bash. If not, see . You should have received a copy of the GNU General Public License GNU General Public License for more details. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the but WITHOUT ANY WARRANTY; without even the implied warranty of Bash is distributed in the hope that it will be useful, (at your option) any later version. the Free Software Foundation, either version 3 of the License, or it under the terms of the GNU General Public License as published by Bash is free software: you can redistribute it and/or modify This file is part of GNU Bash, the Bourne Again SHell. /* Copyright (C) 1987-2010 Free Software Foundation, Inc. /* Modified to run with the GNU shell Apr 25, 1988 by bfox. */ /* test.c - GNU test program (ksb and mjb) */