illumos-port-bash/test.c

895 lines
20 KiB
C
Raw Permalink Normal View History

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