/* Modified to run with the GNU shell Apr 25, 1988 by bfox. */
-/* Copyright (C) 1987, 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
- Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
-/* Define STANDALONE to get the /bin/test version. Otherwise, you get
- the shell builtin version. */
-/* #define STANDALONE */
+/* 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
#include <stdio.h>
+
#include "bashtypes.h"
-#if !defined (STANDALONE)
-# if !defined (_POSIX_VERSION)
-# include <sys/file.h>
-# endif /* !_POSIX_VERSION */
-# include "posixstat.h"
-# include "filecntl.h"
-# include "shell.h"
-#else /* STANDALONE */
-# include "system.h"
-# if !defined (S_IXUGO)
-# define S_IXUGO 0111
-# endif
-# if defined (HAVE_UNISTD_H)
-# include <unistd.h>
-# endif /* HAVE_UNISTD_H */
-# define whitespace(c) (((c) == ' ') || ((c) == '\t'))
-# define digit(c) ((c) >= '0' && (c) <= '9')
-# define digit_value(c) ((c) - '0')
-#endif /* STANDALONE */
+#if !defined (HAVE_LIMITS_H)
+# include <sys/param.h>
+#endif
-#if !defined (STRLEN)
-# define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
+#if defined (HAVE_UNISTD_H)
+# include <unistd.h>
#endif
#include <errno.h>
extern int errno;
#endif /* !errno */
+#if !defined (_POSIX_VERSION)
+# include <sys/file.h>
+#endif /* !_POSIX_VERSION */
+#include "posixstat.h"
+#include "filecntl.h"
+
+#include "shell.h"
+#include "pathexp.h"
+#include "test.h"
+#include "builtins/common.h"
+
+#include <glob/strmatch.h>
+
+#if !defined (STRLEN)
+# define STRLEN(s) ((s)[0] ? ((s)[1] ? ((s)[2] ? strlen(s) : 2) : 1) : 0)
+#endif
+
#if !defined (STREQ)
# define STREQ(a, b) ((a)[0] == (b)[0] && strcmp (a, b) == 0)
#endif /* !STREQ */
-#if !defined (member)
-# define member(c, s) (int)((c) ? (char *)strchr ((s), (c)) : 0)
-#endif /* !member */
-
-/* Make gid_t and uid_t mean something for non-posix systems. */
-#if !defined (_POSIX_VERSION) && !defined (HAVE_UID_T)
-# if !defined (gid_t)
-# define gid_t int
-# endif
-# if !defined (uid_t)
-# define uid_t int
-# endif
-#endif /* !_POSIX_VERSION */
-
-/* What type are the user and group ids? GID_T is actually the type of
- the members of the array that getgroups(3) fills in from its second
- argument. */
-#if defined (INT_GROUPS_ARRAY)
-# define GID_T int
-# define UID_T int
-#else /* !INT_GROUPS_ARRAY */
-# define GID_T gid_t
-# define UID_T uid_t
-#endif /* !INT_GROUPS_ARRAY */
-
-#if !defined (Linux) && !defined (USGr4_2) && !defined (SunOS5)
-extern gid_t getegid ();
-extern uid_t geteuid ();
-# if !defined (sony)
-extern gid_t getgid ();
-# endif /* !sony */
-#endif /* !Linux && !USGr4_2 && !SunOS5 */
-
#if !defined (R_OK)
#define R_OK 4
#define W_OK 2
#define F_OK 0
#endif /* R_OK */
+#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
+
/* 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.
- TRUTH_OR is how to do logical or with TRUE and FALSE.
- TRUTH_AND is how to do logical and with TRUE and FALSE..
- Default is TRUE = 1, FALSE = 0, TRUTH_OR = a | b, TRUTH_AND = a & b,
- SHELL_BOOLEAN = (!value). */
+ Default is TRUE = 1, FALSE = 0, SHELL_BOOLEAN = (!value). */
#define TRUE 1
#define FALSE 0
#define SHELL_BOOLEAN(value) (!(value))
-#define TRUTH_OR(a, b) ((a) | (b))
-#define TRUTH_AND(a, b) ((a) & (b))
-#if defined (STANDALONE)
-# define test_exit(val) exit (val)
-#else
- static jmp_buf test_exit_buf;
- static int test_error_return = 0;
-# define test_exit(val) \
+#define TEST_ERREXIT_STATUS 2
+
+static procenv_t test_exit_buf;
+static int test_error_return;
+#define test_exit(val) \
do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
-#endif /* STANDALONE */
+/* We have to use access(2) for machines running AFS, because it's
+ not a Unix file system. This may produce incorrect answers for
+ non-AFS files. I hate AFS. */
#if defined (AFS)
- /* We have to use access(2) for machines running AFS, because it's
- not a Unix file system. This may produce incorrect answers for
- non-AFS files. I hate AFS. */
# define EACCESS(path, mode) access(path, mode)
#else
-# define EACCESS(path, mode) eaccess(path, mode)
+# define EACCESS(path, mode) test_eaccess(path, mode)
#endif /* AFS */
static int pos; /* The offset of the current argument in ARGV. */
static char **argv; /* The argument list. */
static int noeval;
-static int isint ();
-static int unop ();
-static int binop ();
-static int unary_operator ();
-static int binary_operator ();
-static int two_arguments ();
-static int three_arguments ();
-static int posixtest ();
+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 test_stat __P((char *, struct stat *));
-static int expr ();
-static int term ();
-static int and ();
-static int or ();
+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));
+
+static int expr __P((void));
+static int term __P((void));
+static int and __P((void));
+static int or __P((void));
+
+static int filecomp __P((char *, char *, int));
+static int arithcomp __P((char *, char *, int, int));
+static int patcomp __P((char *, char *, int));
static void
test_syntax_error (format, arg)
char *format, *arg;
{
-#if !defined (STANDALONE)
- extern int interactive_shell;
- extern char *get_name_for_error ();
- if (!interactive_shell)
- fprintf (stderr, "%s: ", get_name_for_error ());
-#endif
- fprintf (stderr, "%s: ", argv[0]);
- fprintf (stderr, format, arg);
- fflush (stderr);
- test_exit (SHELL_BOOLEAN (FALSE));
+ builtin_error (format, arg);
+ test_exit (TEST_ERREXIT_STATUS);
+}
+
+/*
+ * beyond - call when we're beyond the end of the argument list (an
+ * error condition)
+ */
+static void
+beyond ()
+{
+ test_syntax_error ("argument expected", (char *)NULL);
+}
+
+/* Syntax error for when an integer argument was expected, but
+ something else was found. */
+static void
+integer_expected_error (pch)
+ char *pch;
+{
+ test_syntax_error ("%s: integer expression expected", pch);
}
/* A wrapper for stat () which disallows pathnames that are empty strings
errno = ENOENT;
return (-1);
}
-#if !defined (HAVE_DEV_FD)
if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
{
- int fd;
- if (isint (path + 8, &fd))
- return (fstat (fd, finfo));
+#if !defined (HAVE_DEV_FD)
+ intmax_t fd;
+ int r;
+
+ if (legal_number (path + 8, &fd) && fd == (int)fd)
+ {
+ r = fstat ((int)fd, finfo);
+ if (r == 0 || errno != EBADF)
+ return (r);
+ }
+ errno = ENOENT;
+ return (-1);
+#else
+ /* If HAVE_DEV_FD is defined, DEV_FD_PREFIX is defined also, and has a
+ trailing slash. Make sure /dev/fd/xx really uses DEV_FD_PREFIX/xx.
+ On most systems, with the notable exception of linux, this is
+ effectively a no-op. */
+ char pbuf[32];
+ strcpy (pbuf, DEV_FD_PREFIX);
+ strcat (pbuf, path + 8);
+ return (stat (pbuf, finfo));
+#endif /* !HAVE_DEV_FD */
+ }
+#if !defined (HAVE_DEV_STDIN)
+ else if (STREQN (path, "/dev/std", 8))
+ {
+ if (STREQ (path+8, "in"))
+ return (fstat (0, finfo));
+ else if (STREQ (path+8, "out"))
+ return (fstat (1, finfo));
+ else if (STREQ (path+8, "err"))
+ return (fstat (2, finfo));
else
- {
- errno = EBADF;
- return (-1);
- }
+ return (stat (path, finfo));
}
-#endif /* !HAVE_DEV_FD */
+#endif /* !HAVE_DEV_STDIN */
return (stat (path, finfo));
}
/* Do the same thing access(2) does, but use the effective uid and gid,
and don't make the mistake of telling root that any file is
executable. */
-static int
-eaccess (path, mode)
+int
+test_eaccess (path, mode)
char *path;
int mode;
{
struct stat st;
- static int euid = -1;
if (test_stat (path, &st) < 0)
return (-1);
- if (euid == -1)
-#if defined (SHELL)
- euid = current_user.euid;
-#else
- euid = geteuid ();
-#endif
-
- if (euid == 0)
+ if (current_user.euid == 0)
{
/* Root can read or write any file. */
if (mode != X_OK)
return (0);
}
- if (st.st_uid == euid) /* owner */
+ if (st.st_uid == current_user.euid) /* owner */
mode <<= 6;
else if (group_member (st.st_gid))
mode <<= 3;
if (st.st_mode & mode)
return (0);
+ errno = EACCES;
return (-1);
}
-#if defined (HAVE_GETGROUPS)
-/* The number of groups that this user is a member of. */
-static int ngroups = 0;
-static GID_T *group_array = (GID_T *)NULL;
-static int default_group_array_size = 0;
-#endif /* HAVE_GETGROUPS */
-
-#if !defined (NOGROUP)
-# define NOGROUP (GID_T) -1
-#endif
-
-/* Return non-zero if GID is one that we have in our groups list. */
-int
-group_member (gid)
- GID_T gid;
-{
- static GID_T pgid = (GID_T)NOGROUP;
- static GID_T egid = (GID_T)NOGROUP;
-
- if (pgid == (GID_T)NOGROUP)
-#if defined (SHELL)
- pgid = (GID_T) current_user.gid;
-#else /* !SHELL */
- pgid = (GID_T) getgid ();
-#endif /* !SHELL */
-
- if (egid == (GID_T)NOGROUP)
-#if defined (SHELL)
- egid = (GID_T) current_user.egid;
-#else /* !SHELL */
- egid = (GID_T) getegid ();
-#endif /* !SHELL */
-
- if (gid == pgid || gid == egid)
- return (1);
-
-#if defined (HAVE_GETGROUPS)
- /* getgroups () returns the number of elements that it was able to
- place into the array. We simply continue to call getgroups ()
- until the number of elements placed into the array is smaller than
- the physical size of the array. */
-
- while (ngroups == default_group_array_size)
- {
- default_group_array_size += 64;
-
- group_array = (GID_T *)
- xrealloc (group_array, default_group_array_size * sizeof (GID_T));
-
- ngroups = getgroups (default_group_array_size, group_array);
- }
-
- /* In case of error, the user loses. */
- if (ngroups < 0)
- return (0);
-
- /* Search through the list looking for GID. */
- {
- register int i;
-
- for (i = 0; i < ngroups; i++)
- if (gid == group_array[i])
- return (1);
- }
-#endif /* HAVE_GETGROUPS */
-
- return (0);
-}
-
/* Increment our position in the argument list. Check that we're not
past the end of the argument list. This check is supressed if the
argument is FALSE. Made a macro for efficiency. */
-#if !defined (lint)
#define advance(f) do { ++pos; if (f && pos >= argc) beyond (); } while (0)
-#endif
-
-#if !defined (advance)
-static int
-advance (f)
- int f;
-{
- ++pos;
-
- if (f && pos >= argc)
- beyond ();
-}
-#endif /* advance */
-
#define unary_advance() do { advance (1); ++pos; } while (0)
/*
- * beyond - call when we're beyond the end of the argument list (an
- * error condition)
+ * expr:
+ * or
*/
static int
-beyond ()
+expr ()
{
- test_syntax_error ("argument expected\n", (char *)NULL);
-}
+ if (pos >= argc)
+ beyond ();
-/* Syntax error for when an integer argument was expected, but
- something else was found. */
-static void
-integer_expected_error (pch)
- char *pch;
-{
- test_syntax_error ("integer expression expected %s\n", pch);
+ return (FALSE ^ or ()); /* Same with this. */
}
-/* Return non-zero if the characters pointed to by STRING constitute a
- valid number. Stuff the converted number into RESULT if RESULT is
- a non-null pointer to a long. */
+/*
+ * or:
+ * and
+ * and '-o' or
+ */
static int
-isint (string, result)
- register char *string;
- long *result;
+or ()
{
- int sign;
- long value;
-
- sign = 1;
- value = 0;
-
- if (result)
- *result = 0;
+ int value, v2;
- /* Skip leading whitespace characters. */
- while (whitespace (*string))
- string++;
-
- if (!*string)
- return (0);
-
- /* We allow leading `-' or `+'. */
- if (*string == '-' || *string == '+')
- {
- if (!digit (string[1]))
- return (0);
-
- if (*string == '-')
- sign = -1;
-
- string++;
- }
-
- while (digit (*string))
- {
- if (result)
- value = (value * 10) + digit_value (*string);
- string++;
- }
-
- /* Skip trailing whitespace, if any. */
- while (whitespace (*string))
- string++;
-
- /* Error if not at end of string. */
- if (*string)
- return (0);
-
- if (result)
+ value = and ();
+ if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
{
- value *= sign;
- *result = value;
+ advance (0);
+ v2 = or ();
+ return (value || v2);
}
- return (1);
+ return (value);
}
-/* Find the modification time of FILE, and stuff it into AGE, a pointer
- to a long. Return non-zero if successful, else zero. */
+/*
+ * and:
+ * term
+ * term '-a' and
+ */
static int
-age_of (filename, age)
- char *filename;
- long *age;
+and ()
{
- struct stat finfo;
-
- if (test_stat (filename, &finfo) < 0)
- return (0);
-
- if (age)
- *age = finfo.st_mtime;
+ int value, v2;
- return (1);
+ value = term ();
+ if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
+ {
+ advance (0);
+ v2 = and ();
+ return (value && v2);
+ }
+ return (value);
}
/*
* evaluates to true or false, respectively.
*
* term ::=
- * '-'('h'|'d'|'f'|'r'|'s'|'w'|'c'|'b'|'p'|'u'|'g'|'k') filename
- * '-'('L'|'x') filename
- * '-t' [ int ]
+ * '-'('a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'k'|'p'|'r'|'s'|'u'|'w'|'x') filename
+ * '-'('G'|'L'|'O'|'S'|'N') filename
+ * '-t' [int]
* '-'('z'|'n') string
+ * '-o' option
* string
- * string ('!='|'=') string
+ * string ('!='|'='|'==') string
* <int> '-'(eq|ne|le|lt|ge|gt) <int>
* file '-'(nt|ot|ef) file
* '(' <expr> ')'
* int ::=
- * '-l' string
* positive and negative integers
*/
static int
if (pos >= argc)
beyond ();
- /* Deal with leading "not"'s. */
- if ('!' == argv[pos][0] && '\000' == argv[pos][1])
+ /* Deal with leading `not's. */
+ if (argv[pos][0] == '!' && argv[pos][1] == '\0')
{
- value = FALSE;
- while (pos < argc && '!' == argv[pos][0] && '\000' == argv[pos][1])
+ value = 0;
+ while (pos < argc && argv[pos][0] == '!' && argv[pos][1] == '\0')
{
advance (1);
- value ^= (TRUE);
+ value = 1 - value;
}
- return (value ^ (term ()));
+ return (value ? !term() : term());
}
- /* A paren-bracketed argument. */
- if (argv[pos][0] == '(' && !argv[pos][1])
+ /* A paren-bracketed argument. */
+ if (argv[pos][0] == '(' && argv[pos][1] == '\0')
{
advance (1);
value = expr ();
if (argv[pos] == 0)
- test_syntax_error ("`)' expected\n");
+ test_syntax_error ("`)' expected", (char *)NULL);
else if (argv[pos][0] != ')' || argv[pos][1])
- test_syntax_error ("`)' expected, found %s\n", argv[pos]);
+ test_syntax_error ("`)' expected, found %s", argv[pos]);
advance (0);
- return (TRUE == (value));
+ return (value);
}
/* are there enough arguments left that this could be dyadic? */
- if (((pos + 3 <= argc) && binop (argv[pos + 1])) ||
- ((pos + 4 <= argc && STREQ (argv[pos], "-l") && binop (argv[pos + 2]))))
+ if ((pos + 3 <= argc) && test_binop (argv[pos + 1]))
value = binary_operator ();
/* Might be a switch type argument */
- else if ('-' == argv[pos][0] && 0 == argv[pos][2])
+ else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
{
- if (unop (argv[pos][1]))
+ if (test_unop (argv[pos]))
value = unary_operator ();
else
- test_syntax_error ("%s: unary operator expected\n", argv[pos]);
+ test_syntax_error ("%s: unary operator expected", argv[pos]);
}
else
{
- value = (argv[pos][0] != '\0');
+ value = argv[pos][0] != '\0';
advance (0);
}
}
static int
-binary_operator ()
+filecomp (s, t, op)
+ char *s, *t;
+ int op;
{
- register int op;
- struct stat stat_buf, stat_spare;
- long int l, r, value;
- /* Are the left and right integer expressions of the form '-l string'? */
- int l_is_l, r_is_l;
+ struct stat st1, st2;
+ int r1, r2;
- if (argv[pos][0] == '-' && argv[pos][1] == 'l' && !argv[pos][2])
+ if ((r1 = test_stat (s, &st1)) < 0)
{
- l_is_l = 1;
- op = pos + 2;
-
- /* Make sure that OP is still a valid binary operator. */
- if ((op >= argc - 1) || (binop (argv[op]) == 0))
- test_syntax_error ("%s: binary operator expected\n", argv[op]);
-
- advance (0);
+ if (op == EF)
+ return (FALSE);
}
- else
+ if ((r2 = test_stat (t, &st2)) < 0)
{
- l_is_l = 0;
- op = pos + 1;
+ if (op == EF)
+ return (FALSE);
}
+
+ switch (op)
+ {
+ case OT: return (r1 < r2 || (r2 == 0 && st1.st_mtime < st2.st_mtime));
+ case NT: return (r1 > r2 || (r1 == 0 && st1.st_mtime > st2.st_mtime));
+ case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
+ }
+ return (FALSE);
+}
+
+static int
+arithcomp (s, t, op, flags)
+ char *s, *t;
+ int op, flags;
+{
+ intmax_t l, r;
+ int expok;
- if ((op < argc - 2) &&
- (argv[op + 1][0] == '-' && argv[op + 1][1] == 'l' && !argv[op + 1][2]))
+ if (flags & TEST_ARITHEXP)
{
- r_is_l = 1;
- advance (0);
+ l = evalexp (s, &expok);
+ if (expok == 0)
+ return (FALSE); /* should probably longjmp here */
+ r = evalexp (t, &expok);
+ if (expok == 0)
+ return (FALSE); /* ditto */
}
else
- r_is_l = 0;
+ {
+ if (legal_number (s, &l) == 0)
+ integer_expected_error (s);
+ if (legal_number (t, &r) == 0)
+ integer_expected_error (t);
+ }
- if (argv[op][0] == '-')
+ switch (op)
{
- /* check for eq, nt, and stuff */
- switch (argv[op][1])
- {
- default:
- break;
+ 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);
+ }
- case 'l':
- if (argv[op][2] == 't' && !argv[op][3])
- {
- /* lt */
- if (l_is_l)
- l = strlen (argv[op - 1]);
- else
- {
- if (!isint (argv[op - 1], &l))
- integer_expected_error ("before -lt");
- }
-
- if (r_is_l)
- r = strlen (argv[op + 2]);
- else
- {
- if (!isint (argv[op + 1], &r))
- integer_expected_error ("after -lt");
- }
- pos += 3;
- return (TRUE == (l < r));
- }
+ return (FALSE);
+}
- if (argv[op][2] == 'e' && !argv[op][3])
- {
- /* le */
- if (l_is_l)
- l = strlen (argv[op - 1]);
- else
- {
- if (!isint (argv[op - 1], &l))
- integer_expected_error ("before -le");
- }
- if (r_is_l)
- r = strlen (argv[op + 2]);
- else
- {
- if (!isint (argv[op + 1], &r))
- integer_expected_error ("after -le");
- }
- pos += 3;
- return (TRUE == (l <= r));
- }
- break;
+static int
+patcomp (string, pat, op)
+ char *string, *pat;
+ int op;
+{
+ int m;
- case 'g':
- if (argv[op][2] == 't' && !argv[op][3])
- {
- /* gt integer greater than */
- if (l_is_l)
- l = strlen (argv[op - 1]);
- else
- {
- if (!isint (argv[op - 1], &l))
- integer_expected_error ("before -gt");
- }
- if (r_is_l)
- r = strlen (argv[op + 2]);
- else
- {
- if (!isint (argv[op + 1], &r))
- integer_expected_error ("after -gt");
- }
- pos += 3;
- return (TRUE == (l > r));
- }
+ m = strmatch (pat, string, FNMATCH_EXTFLAG);
+ return ((op == EQ) ? (m == 0) : (m != 0));
+}
- if (argv[op][2] == 'e' && !argv[op][3])
- {
- /* ge - integer greater than or equal to */
- if (l_is_l)
- l = strlen (argv[op - 1]);
- else
- {
- if (!isint (argv[op - 1], &l))
- integer_expected_error ("before -ge");
- }
- if (r_is_l)
- r = strlen (argv[op + 2]);
- else
- {
- if (!isint (argv[op + 1], &r))
- integer_expected_error ("after -ge");
- }
- pos += 3;
- return (TRUE == (l >= r));
- }
- break;
+int
+binary_test (op, arg1, arg2, flags)
+ char *op, *arg1, *arg2;
+ int flags;
+{
+ int patmatch;
- case 'n':
- if (argv[op][2] == 't' && !argv[op][3])
- {
- /* nt - newer than */
- pos += 3;
- if (l_is_l || r_is_l)
- test_syntax_error ("-nt does not accept -l\n", (char *)NULL);
- if (age_of (argv[op - 1], &l) && age_of (argv[op + 1], &r))
- return (TRUE == (l > r));
- else
- return (FALSE);
- }
+ patmatch = (flags & TEST_PATMATCH);
- if (argv[op][2] == 'e' && !argv[op][3])
- {
- /* ne - integer not equal */
- if (l_is_l)
- l = strlen (argv[op - 1]);
- else
- {
- if (!isint (argv[op - 1], &l))
- integer_expected_error ("before -ne");
- }
- if (r_is_l)
- r = strlen (argv[op + 2]);
- else
- {
- if (!isint (argv[op + 1], &r))
- integer_expected_error ("after -ne");
- }
- pos += 3;
- return (TRUE == (l != r));
- }
- break;
+ if (op[0] == '=' && (op[1] == '\0' || (op[1] == '=' && op[2] == '\0')))
+ return (patmatch ? patcomp (arg1, arg2, EQ) : STREQ (arg1, arg2));
- case 'e':
- if (argv[op][2] == 'q' && !argv[op][3])
- {
- /* eq - integer equal */
- if (l_is_l)
- l = strlen (argv[op - 1]);
- else
- {
- if (!isint (argv[op - 1], &l))
- integer_expected_error ("before -eq");
- }
- if (r_is_l)
- r = strlen (argv[op + 2]);
- else
- {
- if (!isint (argv[op + 1], &r))
- integer_expected_error ("after -eq");
- }
- pos += 3;
- return (TRUE == (l == r));
- }
+ else if ((op[0] == '>' || op[0] == '<') && op[1] == '\0')
+ return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
- if (argv[op][2] == 'f' && !argv[op][3])
- {
- /* ef - hard link? */
- pos += 3;
- if (l_is_l || r_is_l)
- test_syntax_error ("-ef does not accept -l\n", (char *)NULL);
- if (test_stat (argv[op - 1], &stat_buf) < 0)
- return (FALSE);
- if (test_stat (argv[op + 1], &stat_spare) < 0)
- return (FALSE);
- return (TRUE ==
- (stat_buf.st_dev == stat_spare.st_dev &&
- stat_buf.st_ino == stat_spare.st_ino));
- }
- break;
+ else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
+ return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
- case 'o':
- if ('t' == argv[op][2] && '\000' == argv[op][3])
- {
- /* ot - older than */
- pos += 3;
- if (l_is_l || r_is_l)
- test_syntax_error ("-nt does not accept -l\n", (char *)NULL);
- if (age_of (argv[op - 1], &l) && age_of (argv[op + 1], &r))
- return (TRUE == (l < r));
- return (FALSE);
- }
- break;
+ else if (op[2] == 't')
+ {
+ switch (op[1])
+ {
+ case 'n': return (filecomp (arg1, arg2, NT)); /* -nt */
+ case 'o': return (filecomp (arg1, arg2, OT)); /* -ot */
+ case 'l': return (arithcomp (arg1, arg2, LT, flags)); /* -lt */
+ case 'g': return (arithcomp (arg1, arg2, GT, flags)); /* -gt */
}
- test_syntax_error ("%s: unknown binary operator", argv[op]);
}
+ else if (op[1] == 'e')
+ {
+ 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 */
+ }
+ }
+
+ return (FALSE); /* should never get here */
+}
+
- if (argv[op][0] == '=' && !argv[op][1])
+static int
+binary_operator ()
+{
+ int value;
+ char *w;
+
+ w = argv[pos + 1];
+ 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')) /* != */
{
- value = (argv[pos][0] == argv[pos+2][0]) &&
- (strcmp (argv[pos], argv[pos + 2]) == 0);
+ value = binary_test (w, argv[pos], argv[pos + 2], 0);
pos += 3;
- return (TRUE == value);
+ return (value);
}
- if (argv[op][0] == '!' && argv[op][1] == '=' && !argv[op][2])
+#if defined (PATTERN_MATCHING)
+ if ((w[0] == '=' || w[0] == '!') && w[1] == '~' && w[2] == '\0')
{
- value = (argv[pos][0] != argv[pos + 2][0]) ||
- (strcmp (argv[pos], argv[pos + 2]) != 0);
+ value = patcomp (argv[pos], argv[pos + 2], w[0] == '=' ? EQ : NE);
pos += 3;
- return (TRUE == value);
+ return (value);
}
- return (FALSE);
+#endif
+
+ if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
+ {
+ test_syntax_error ("%s: binary operator expected", w);
+ /* NOTREACHED */
+ return (FALSE);
+ }
+
+ value = binary_test (w, argv[pos], argv[pos + 2], 0);
+ pos += 3;
+ return value;
}
static int
unary_operator ()
{
- long r, value;
- struct stat stat_buf;
+ char *op;
+ intmax_t r;
+
+ op = argv[pos];
+ if (test_unop (op) == 0)
+ return (FALSE);
- switch (argv[pos][1])
+ /* the only tricky case is `-t', which may or may not take an argument. */
+ if (op[1] == 't')
{
- default:
- return (FALSE);
+ advance (0);
+ if (pos < argc)
+ {
+ if (legal_number (argv[pos], &r))
+ {
+ advance (0);
+ return (unary_test (op, argv[pos - 1]));
+ }
+ else
+ return (FALSE);
+ }
+ else
+ return (unary_test (op, "1"));
+ }
- /* All of the following unary operators use 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. */
+ /* 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]));
+}
+int
+unary_test (op, arg)
+ char *op, *arg;
+{
+ intmax_t r;
+ struct stat stat_buf;
+
+ switch (op[1])
+ {
case 'a': /* file exists in the file system? */
case 'e':
- unary_advance ();
- value = -1 != test_stat (argv[pos - 1], &stat_buf);
- return (TRUE == value);
+ return (test_stat (arg, &stat_buf) == 0);
case 'r': /* file is readable? */
- unary_advance ();
- value = -1 != EACCESS (argv[pos - 1], R_OK);
- return (TRUE == value);
+ return (EACCESS (arg, R_OK) == 0);
case 'w': /* File is writeable? */
- unary_advance ();
- value = -1 != EACCESS (argv[pos - 1], W_OK);
- return (TRUE == value);
+ return (EACCESS (arg, W_OK) == 0);
case 'x': /* File is executable? */
- unary_advance ();
- value = -1 != EACCESS (argv[pos - 1], X_OK);
- return (TRUE == value);
+ return (EACCESS (arg, X_OK) == 0);
case 'O': /* File is owned by you? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
-#if defined (SHELL)
- return (TRUE == ((UID_T) current_user.euid == (UID_T) stat_buf.st_uid));
-#else
- return (TRUE == ((UID_T) geteuid () == (UID_T) stat_buf.st_uid));
-#endif /* !SHEL */
+ return (test_stat (arg, &stat_buf) == 0 &&
+ (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
case 'G': /* File is owned by your group? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
+ return (test_stat (arg, &stat_buf) == 0 &&
+ (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
- return (TRUE == ((GID_T) getegid () == (GID_T) stat_buf.st_gid));
+ case 'N':
+ return (test_stat (arg, &stat_buf) == 0 &&
+ stat_buf.st_atime <= stat_buf.st_mtime);
case 'f': /* File is a file? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
+ if (test_stat (arg, &stat_buf) < 0)
return (FALSE);
- /* Under POSIX, -f is true if the given file exists
- and is a regular file. */
+ /* -f is true if the given file exists and is a regular file. */
#if defined (S_IFMT)
- return (TRUE == ((S_ISREG (stat_buf.st_mode)) ||
- (0 == (stat_buf.st_mode & S_IFMT))));
+ return (S_ISREG (stat_buf.st_mode) || (stat_buf.st_mode & S_IFMT) == 0);
#else
- return (TRUE == (S_ISREG (stat_buf.st_mode)));
+ return (S_ISREG (stat_buf.st_mode));
#endif /* !S_IFMT */
case 'd': /* File is a directory? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
- return (TRUE == (S_ISDIR (stat_buf.st_mode)));
+ return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
case 's': /* File has something in it? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
- return (TRUE == (stat_buf.st_size > (off_t) 0));
+ return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
case 'S': /* File is a socket? */
#if !defined (S_ISSOCK)
return (FALSE);
#else
- unary_advance ();
-
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
- return (TRUE == (S_ISSOCK (stat_buf.st_mode)));
-#endif /* S_ISSOCK */
+ return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
+#endif /* S_ISSOCK */
case 'c': /* File is character special? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
- return (TRUE == (S_ISCHR (stat_buf.st_mode)));
+ return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
case 'b': /* File is block special? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
- return (TRUE == (S_ISBLK (stat_buf.st_mode)));
+ return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
case 'p': /* File is a named pipe? */
- unary_advance ();
#ifndef S_ISFIFO
return (FALSE);
#else
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
- return (TRUE == (S_ISFIFO (stat_buf.st_mode)));
-#endif /* S_ISFIFO */
+ return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
+#endif /* S_ISFIFO */
case 'L': /* Same as -h */
- /*FALLTHROUGH*/
-
case 'h': /* File is a symbolic link? */
- unary_advance ();
-#ifndef S_ISLNK
+#if !defined (S_ISLNK) || !defined (HAVE_LSTAT)
return (FALSE);
#else
- /* An empty filename is not a valid pathname. */
- if ((argv[pos - 1][0] == '\0') ||
- (lstat (argv[pos - 1], &stat_buf) < 0))
- return (FALSE);
-
- return (TRUE == (S_ISLNK (stat_buf.st_mode)));
-#endif /* S_IFLNK */
+ return ((arg[0] != '\0') &&
+ (lstat (arg, &stat_buf) == 0) && S_ISLNK (stat_buf.st_mode));
+#endif /* S_IFLNK && HAVE_LSTAT */
case 'u': /* File is setuid? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
- return (TRUE == (0 != (stat_buf.st_mode & S_ISUID)));
+ return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
case 'g': /* File is setgid? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
-
- return (TRUE == (0 != (stat_buf.st_mode & S_ISGID)));
+ return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
case 'k': /* File has sticky bit set? */
- unary_advance ();
- if (test_stat (argv[pos - 1], &stat_buf) < 0)
- return (FALSE);
#if !defined (S_ISVTX)
/* This is not Posix, and is not defined on some Posix systems. */
return (FALSE);
#else
- return (TRUE == (0 != (stat_buf.st_mode & S_ISVTX)));
+ return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
#endif
- case 't': /* File (fd) is a terminal? (fd) defaults to stdout. */
- advance (0);
- if (pos < argc && isint (argv[pos], &r))
- {
- advance (0);
- return (TRUE == (isatty ((int) r)));
- }
- return (TRUE == (isatty (1)));
+ case 't': /* File fd is a terminal? */
+ if (legal_number (arg, &r) == 0)
+ return (FALSE);
+ return ((r == (int)r) && isatty ((int)r));
case 'n': /* True if arg has some length. */
- unary_advance ();
- return (TRUE == (argv[pos - 1][0] != 0));
+ return (arg[0] != '\0');
case 'z': /* True if arg has no length. */
- unary_advance ();
- return (TRUE == (argv[pos - 1][0] == '\0'));
- }
-}
-
-/*
- * and:
- * term
- * term '-a' and
- */
-static int
-and ()
-{
- int value;
+ return (arg[0] == '\0');
- value = term ();
- while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
- {
- advance (0);
- value = TRUTH_AND (value, and ());
+ case 'o': /* True if option `arg' is set. */
+ return (minus_o_option_value (arg) == 1);
}
- return (TRUE == value);
+
+ /* We can't actually get here, but this shuts up gcc. */
+ return (FALSE);
}
-/*
- * or:
- * and
- * and '-o' or
- */
-static int
-or ()
+/* Return TRUE if OP is one of the test command's binary operators. */
+int
+test_binop (op)
+ char *op;
{
- int value;
-
- value = and ();
-
- while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
+ if (op[0] == '=' && op[1] == '\0')
+ return (1); /* '=' */
+ else if ((op[0] == '<' || op[0] == '>') && op[1] == '\0') /* string <, > */
+ return (1);
+ else if ((op[0] == '=' || op[0] == '!') && op[1] == '=' && op[2] == '\0')
+ return (1); /* `==' and `!=' */
+#if defined (PATTERN_MATCHING)
+ else if (op[2] == '\0' && op[1] == '~' && (op[0] == '=' || op[0] == '!'))
+ return (1);
+#endif
+ else if (op[0] != '-' || op[2] == '\0' || op[3] != '\0')
+ return (0);
+ else
{
- advance (0);
- value = TRUTH_OR (value, or ());
+ if (op[2] == 't')
+ switch (op[1])
+ {
+ case 'n': /* -nt */
+ case 'o': /* -ot */
+ case 'l': /* -lt */
+ case 'g': /* -gt */
+ return (1);
+ default:
+ return (0);
+ }
+ else if (op[1] == 'e')
+ switch (op[2])
+ {
+ case 'q': /* -eq */
+ case 'f': /* -ef */
+ return (1);
+ default:
+ return (0);
+ }
+ else if (op[2] == 'e')
+ switch (op[1])
+ {
+ case 'n': /* -ne */
+ case 'g': /* -ge */
+ case 'l': /* -le */
+ return (1);
+ default:
+ return (0);
+ }
+ else
+ return (0);
}
-
- return (TRUE == value);
}
-/*
- * expr:
- * or
- */
-static int
-expr ()
+/* Return non-zero if OP is one of the test command's unary operators. */
+int
+test_unop (op)
+ char *op;
{
- if (pos >= argc)
- beyond ();
-
- return (FALSE ^ (or ())); /* Same with this. */
-}
+ if (op[0] != '-')
+ return (0);
-/* Return TRUE if S is one of the test command's binary operators. */
-static int
-binop (s)
- char *s;
-{
- return ((STREQ (s, "=")) || (STREQ (s, "!=")) || (STREQ (s, "-nt")) ||
- (STREQ (s, "-ot")) || (STREQ (s, "-ef")) || (STREQ (s, "-eq")) ||
- (STREQ (s, "-ne")) || (STREQ (s, "-lt")) || (STREQ (s, "-le")) ||
- (STREQ (s, "-gt")) || (STREQ (s, "-ge")));
-}
+ switch (op[1])
+ {
+ case 'a': case 'b': case 'c': case 'd': case 'e':
+ case 'f': case 'g': case 'h': case 'k': case 'n':
+ case 'o': case 'p': case 'r': case 's': case 't':
+ case 'u': case 'w': case 'x': case 'z':
+ case 'G': case 'L': case 'O': case 'S': case 'N':
+ return (1);
+ }
-/* Return non-zero if OP is one of the test command's unary operators. */
-static int
-unop (op)
- int op;
-{
- return (member (op, "abcdefgkLhprsStuwxOGnz"));
+ return (0);
}
static int
two_arguments ()
{
- int value;
-
- if (argv[pos][0] == '!' && !argv[pos][1])
- value = argv[pos + 1][0] == '\0';
- else if ((argv[pos][0] == '-') && (argv[pos][2] == '\0'))
+ if (argv[pos][0] == '!' && argv[pos][1] == '\0')
+ return (argv[pos + 1][0] == '\0');
+ else if (argv[pos][0] == '-' && argv[pos][2] == '\0')
{
- if (unop (argv[pos][1]))
- value = unary_operator ();
+ if (test_unop (argv[pos]))
+ return (unary_operator ());
else
- test_syntax_error ("%s: unary operator expected\n", argv[pos]);
+ test_syntax_error ("%s: unary operator expected", argv[pos]);
}
else
- test_syntax_error ("%s: unary operator expected\n", argv[pos]);
+ test_syntax_error ("%s: unary operator expected", argv[pos]);
- return (value);
+ return (0);
}
+#define ANDOR(s) (s[0] == '-' && !s[2] && (s[1] == 'a' || s[1] == 'o'))
+
+/* This could be augmented to handle `-t' as equivalent to `-t 1', but
+ POSIX requires that `-t' be given an argument. */
+#define ONE_ARG_TEST(s) ((s)[0] != '\0')
+
static int
three_arguments ()
{
int value;
- if (argv[pos][0] == '!' && !argv[pos][1])
+ if (test_binop (argv[pos+1]))
+ {
+ value = binary_operator ();
+ pos = argc;
+ }
+ else if (ANDOR (argv[pos+1]))
+ {
+ if (argv[pos+1][1] == 'a')
+ 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]);
+ pos = argc;
+ }
+ else if (argv[pos][0] == '!' && argv[pos][1] == '\0')
{
advance (1);
value = !two_arguments ();
}
- else if (binop (argv[pos+1]))
+ else if (argv[pos][0] == '(' && argv[pos+2][0] == ')')
{
- value = binary_operator ();
+ value = ONE_ARG_TEST(argv[pos+1]);
pos = argc;
}
- /* Check for -a or -o or a parenthesized subexpression. */
- else if ((argv[pos+1][0] == '-' && !argv[pos+1][2] &&
- (argv[pos+1][1] == 'a' || argv[pos+1][1] == 'o')) ||
- (argv[pos][0] == '('))
- value = expr ();
else
- test_syntax_error ("%s: binary operator expected\n", argv[pos+1]);
+ test_syntax_error ("%s: binary operator expected", argv[pos+1]);
+
return (value);
}
break;
case 1:
- value = argv[1][0] != '\0';
+ value = ONE_ARG_TEST(argv[1]);
pos = argc;
break;
break;
case 4:
- if (STREQ (argv[pos], "!"))
+ if (argv[pos][0] == '!' && argv[pos][1] == '\0')
{
advance (1);
value = !three_arguments ();
break;
}
/* FALLTHROUGH */
- case 5:
default:
value = expr ();
}
* test expr
*/
int
-#if defined (STANDALONE)
-main (margc, margv)
-#else
test_command (margc, margv)
-#endif /* STANDALONE */
int margc;
char **margv;
{
int value;
-
-#if !defined (STANDALONE)
int code;
+ USE_VAR(margc);
+
code = setjmp (test_exit_buf);
if (code)
return (test_error_return);
-#endif /* STANDALONE */
argv = margv;
- if (margv[0] && margv[0][0] == '[' && !margv[0][1])
+ if (margv[0] && margv[0][0] == '[' && margv[0][1] == '\0')
{
--margc;
+ if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
+ test_syntax_error ("missing `]'", (char *)NULL);
+
if (margc < 2)
test_exit (SHELL_BOOLEAN (FALSE));
-
- if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
- test_syntax_error ("missing `]'\n", (char *)NULL);
}
argc = margc;
value = posixtest ();
if (pos != argc)
- test_syntax_error ("too many arguments\n", (char *)NULL);
+ test_syntax_error ("too many arguments", (char *)NULL);
test_exit (SHELL_BOOLEAN (value));
}