No specific user configuration
[platform/upstream/bash.git] / test.c
diff --git a/test.c b/test.c
index a906f62..ab7bec7 100644 (file)
--- a/test.c
+++ b/test.c
@@ -1,24 +1,24 @@
-/* GNU test program (ksb and mjb) */
+/* test.c - GNU test program (ksb and mjb) */
 
 /* 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-2010 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
-   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 2, or (at your option) any later
-   version.
+   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.
 
-   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.
+   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.
 
-   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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 /* Define PATTERN_MATCHING to get the csh-like =~ and !~ pattern-matching
    binary operators. */
@@ -32,9 +32,7 @@
 
 #include "bashtypes.h"
 
-#if defined (HAVE_LIMITS_H)
-#  include <limits.h>
-#else
+#if !defined (HAVE_LIMITS_H) && defined (HAVE_SYS_PARAM_H)
 #  include <sys/param.h>
 #endif
 
 extern int errno;
 #endif /* !errno */
 
-#if !defined (_POSIX_VERSION)
+#if !defined (_POSIX_VERSION) && defined (HAVE_SYS_FILE_H)
 #  include <sys/file.h>
 #endif /* !_POSIX_VERSION */
 #include "posixstat.h"
 #include "filecntl.h"
+#include "stat-time.h"
+
+#include "bashintl.h"
 
 #include "shell.h"
 #include "pathexp.h"
 #include "test.h"
 #include "builtins/common.h"
 
-#include <glob/fnmatch.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)
+#  define STREQ(a, b) ((a)[0] == (b)[0] && strcmp ((a), (b)) == 0)
 #endif /* !STREQ */
+#define STRCOLLEQ(a, b) ((a)[0] == (b)[0] && strcoll ((a), (b)) == 0)
 
 #if !defined (R_OK)
 #define R_OK 4
@@ -101,45 +103,37 @@ static int test_error_return;
 #define test_exit(val) \
        do { test_error_return = val; longjmp (test_exit_buf, 1); } while (0)
 
-/* 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)
-#  define EACCESS(path, mode)  access(path, mode)
-#else
-#  define EACCESS(path, mode)  test_eaccess(path, mode)
-#endif /* AFS */
+extern int sh_stat __P((const char *, struct stat *));
 
 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;
 
-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 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 ();
-static int term ();
-static int and ();
-static int or ();
+static int expr __P((void));
+static int term __P((void));
+static int and __P((void));
+static int or __P((void));
 
-static void beyond ();
+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;
 {
-  extern int interactive_shell;
-  extern char *get_name_for_error ();
-  if (interactive_shell == 0)
-    fprintf (stderr, "%s: ", get_name_for_error ());
-  fprintf (stderr, "%s: ", argv[0]);
-  fprintf (stderr, format, arg);
-  fprintf (stderr, "\n");
-  fflush (stderr);
+  builtin_error (format, arg);
   test_exit (TEST_ERREXIT_STATUS);
 }
 
@@ -150,7 +144,7 @@ test_syntax_error (format, arg)
 static void
 beyond ()
 {
-  test_syntax_error ("argument expected", (char *)NULL);
+  test_syntax_error (_("argument expected"), (char *)NULL);
 }
 
 /* Syntax error for when an integer argument was expected, but
@@ -159,98 +153,11 @@ 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
-   and handles /dev/fd emulation on systems that don't have it. */
-static int
-test_stat (path, finfo)
-     char *path;
-     struct stat *finfo;
-{
-  if (*path == '\0')
-    {
-      errno = ENOENT;
-      return (-1);
-    }
-  if (path[0] == '/' && path[1] == 'd' && strncmp (path, "/dev/fd/", 8) == 0)
-    {
-#if !defined (HAVE_DEV_FD)
-      long fd;
-      if (legal_number (path + 8, &fd))
-       return (fstat ((int)fd, finfo));
-      else
-       {
-         errno = EBADF;
-         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
-       return (stat (path, finfo));
-    }
-#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. */
-int
-test_eaccess (path, mode)
-     char *path;
-     int mode;
-{
-  struct stat st;
-
-  if (test_stat (path, &st) < 0)
-    return (-1);
-
-  if (current_user.euid == 0)
-    {
-      /* Root can read or write any file. */
-      if (mode != X_OK)
-       return (0);
-
-      /* Root can execute any file that has any one of the execute
-        bits set. */
-      if (st.st_mode & S_IXUGO)
-       return (0);
-    }
-
-  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);
+  test_syntax_error (_("%s: integer expression expected"), pch);
 }
 
 /* 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
+   past the end of the argument list.  This check is suppressed if the
    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)
@@ -279,7 +186,7 @@ or ()
   int value, v2;
 
   value = and ();
-  while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
+  if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'o' && !argv[pos][2])
     {
       advance (0);
       v2 = or ();
@@ -300,7 +207,7 @@ and ()
   int value, v2;
 
   value = term ();
-  while (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
+  if (pos < argc && argv[pos][0] == '-' && argv[pos][1] == 'a' && !argv[pos][2])
     {
       advance (0);
       v2 = and ();
@@ -349,14 +256,14 @@ term ()
     }
 
   /* A paren-bracketed argument. */
-  if (argv[pos][0] == '(' && argv[pos][1] == '\0')
+  if (argv[pos][0] == '(' && argv[pos][1] == '\0') /* ) */
     {
       advance (1);
       value = expr ();
-      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]);
+      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]);
       advance (0);
       return (value);
     }
@@ -371,7 +278,7 @@ term ()
       if (test_unop (argv[pos]))
        value = unary_operator ();
       else
-       test_syntax_error ("%s: unary operator expected", argv[pos]);
+       test_syntax_error (_("%s: unary operator expected"), argv[pos]);
     }
   else
     {
@@ -383,30 +290,45 @@ term ()
 }
 
 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;
+}
+
+static int
 filecomp (s, t, op)
      char *s, *t;
      int op;
 {
   struct stat st1, st2;
+  struct timespec ts1, ts2;
+  int r1, r2;
 
-  if (test_stat (s, &st1) < 0)
+  if ((r1 = stat_mtime (s, &st1, &ts1)) < 0)
     {
-      st1.st_mtime = 0;
       if (op == EF)
        return (FALSE);
     }
-  if (test_stat (t, &st2) < 0)
+  if ((r2 = stat_mtime (t, &st2, &ts2)) < 0)
     {
-      st2.st_mtime = 0;
       if (op == EF)
        return (FALSE);
     }
   
   switch (op)
     {
-    case OT: return (st1.st_mtime < st2.st_mtime);
-    case NT: return (st1.st_mtime > st2.st_mtime);
-    case EF: return ((st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino));
+    case OT: return (r1 < r2 || (r2 == 0 && timespec_cmp (ts1, ts2) < 0));
+    case NT: return (r1 > r2 || (r1 == 0 && timespec_cmp (ts1, ts2) > 0));
+    case EF: return (same_file (s, t, &st1, &st2));
     }
   return (FALSE);
 }
@@ -416,7 +338,7 @@ arithcomp (s, t, op, flags)
      char *s, *t;
      int op, flags;
 {
-  long l, r;
+  intmax_t l, r;
   int expok;
 
   if (flags & TEST_ARITHEXP)
@@ -456,7 +378,7 @@ patcomp (string, pat, op)
 {
   int m;
 
-  m = fnmatch (pat, string, FNMATCH_EXTFLAG);
+  m = strmatch (pat, string, FNMATCH_EXTFLAG|FNMATCH_IGNCASE);
   return ((op == EQ) ? (m == 0) : (m != 0));
 }
 
@@ -471,12 +393,18 @@ binary_test (op, arg1, arg2, flags)
 
   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')
-    return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
-
+    {
+#if defined (HAVE_STRCOLL)
+      if (shell_compatibility_level > 40 && flags & TEST_LOCALE)
+       return ((op[0] == '>') ? (strcoll (arg1, arg2) > 0) : (strcoll (arg1, arg2) < 0));
+      else
+#endif
+       return ((op[0] == '>') ? (strcmp (arg1, arg2) > 0) : (strcmp (arg1, arg2) < 0));
+    }
   else if (op[0] == '!' && op[1] == '=' && op[2] == '\0')
     return (patmatch ? patcomp (arg1, arg2, NE) : (STREQ (arg1, arg2) == 0));
+    
 
   else if (op[2] == 't')
     {
@@ -537,7 +465,7 @@ binary_operator ()
 
   if ((w[0] != '-' || w[3] != '\0') || test_binop (w) == 0)
     {
-      test_syntax_error ("%s: binary operator expected", w);
+      test_syntax_error (_("%s: binary operator expected"), w);
       /* NOTREACHED */
       return (FALSE);
     }
@@ -550,8 +478,8 @@ binary_operator ()
 static int
 unary_operator ()
 {
-  char *op, *arg;
-  long r;
+  char *op;
+  intmax_t r;
 
   op = argv[pos];
   if (test_unop (op) == 0)
@@ -587,38 +515,39 @@ int
 unary_test (op, arg)
      char *op, *arg;
 {
-  long r;
+  intmax_t r;
   struct stat stat_buf;
+  SHELL_VAR *v;
      
   switch (op[1])
     {
     case 'a':                  /* file exists in the file system? */
     case 'e':
-      return (test_stat (arg, &stat_buf) == 0);
+      return (sh_stat (arg, &stat_buf) == 0);
 
     case 'r':                  /* file is readable? */
-      return (EACCESS (arg, R_OK) == 0);
+      return (sh_eaccess (arg, R_OK) == 0);
 
     case 'w':                  /* File is writeable? */
-      return (EACCESS (arg, W_OK) == 0);
+      return (sh_eaccess (arg, W_OK) == 0);
 
     case 'x':                  /* File is executable? */
-      return (EACCESS (arg, X_OK) == 0);
+      return (sh_eaccess (arg, X_OK) == 0);
 
     case 'O':                  /* File is owned by you? */
-      return (test_stat (arg, &stat_buf) == 0 &&
+      return (sh_stat (arg, &stat_buf) == 0 &&
              (uid_t) current_user.euid == (uid_t) stat_buf.st_uid);
 
     case 'G':                  /* File is owned by your group? */
-      return (test_stat (arg, &stat_buf) == 0 &&
+      return (sh_stat (arg, &stat_buf) == 0 &&
              (gid_t) current_user.egid == (gid_t) stat_buf.st_gid);
 
     case 'N':
-      return (test_stat (arg, &stat_buf) == 0 &&
+      return (sh_stat (arg, &stat_buf) == 0 &&
              stat_buf.st_atime <= stat_buf.st_mtime);
 
     case 'f':                  /* File is a file? */
-      if (test_stat (arg, &stat_buf) < 0)
+      if (sh_stat (arg, &stat_buf) < 0)
        return (FALSE);
 
       /* -f is true if the given file exists and is a regular file. */
@@ -629,29 +558,29 @@ unary_test (op, arg)
 #endif /* !S_IFMT */
 
     case 'd':                  /* File is a directory? */
-      return (test_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
+      return (sh_stat (arg, &stat_buf) == 0 && (S_ISDIR (stat_buf.st_mode)));
 
     case 's':                  /* File has something in it? */
-      return (test_stat (arg, &stat_buf) == 0 && stat_buf.st_size > (off_t) 0);
+      return (sh_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
-      return (test_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
+      return (sh_stat (arg, &stat_buf) == 0 && S_ISSOCK (stat_buf.st_mode));
 #endif /* S_ISSOCK */
 
     case 'c':                  /* File is character special? */
-      return (test_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
+      return (sh_stat (arg, &stat_buf) == 0 && S_ISCHR (stat_buf.st_mode));
 
     case 'b':                  /* File is block special? */
-      return (test_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
+      return (sh_stat (arg, &stat_buf) == 0 && S_ISBLK (stat_buf.st_mode));
 
     case 'p':                  /* File is a named pipe? */
 #ifndef S_ISFIFO
       return (FALSE);
 #else
-      return (test_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
+      return (sh_stat (arg, &stat_buf) == 0 && S_ISFIFO (stat_buf.st_mode));
 #endif /* S_ISFIFO */
 
     case 'L':                  /* Same as -h  */
@@ -664,23 +593,23 @@ unary_test (op, arg)
 #endif /* S_IFLNK && HAVE_LSTAT */
 
     case 'u':                  /* File is setuid? */
-      return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
+      return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISUID) != 0);
 
     case 'g':                  /* File is setgid? */
-      return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
+      return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISGID) != 0);
 
     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
-      return (test_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
+      return (sh_stat (arg, &stat_buf) == 0 && (stat_buf.st_mode & S_ISVTX) != 0);
 #endif
 
     case 't':  /* File fd is a terminal? */
       if (legal_number (arg, &r) == 0)
        return (FALSE);
-      return (isatty ((int)r));
+      return ((r == (int)r) && isatty ((int)r));
 
     case 'n':                  /* True if arg has some length. */
       return (arg[0] != '\0');
@@ -690,7 +619,39 @@ unary_test (op, arg)
 
     case 'o':                  /* True if option `arg' is set. */
       return (minus_o_option_value (arg) == 1);
+
+    case 'v':
+      v = find_variable (arg);
+#if defined (ARRAY_VARS)
+      if (v == 0 && valid_array_reference (arg))
+       {
+         char *t;
+         t = array_value (arg, 0, 0, (int *)0, (arrayind_t *)0);
+         return (t ? TRUE : FALSE);
+       }
+     else 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);
+       }
+#endif
+      return (v && invisible_p (v) == 0 && var_isset (v) ? TRUE : FALSE);
+
+    case 'R':
+      v = find_variable_noref (arg);
+      return ((v && invisible_p (v) == 0 && var_isset (v) && nameref_p (v)) ? TRUE : FALSE);
     }
+
+  /* We can't actually get here, but this shuts up gcc. */
+  return (FALSE);
 }
 
 /* Return TRUE if OP is one of the test command's binary operators. */
@@ -752,7 +713,7 @@ int
 test_unop (op)
      char *op;
 {
-  if (op[0] != '-')
+  if (op[0] != '-' || op[2] != 0)
     return (0);
 
   switch (op[1])
@@ -760,8 +721,9 @@ test_unop (op)
     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 'u': case 'v': case 'w': case 'x': case 'z':
     case 'G': case 'L': case 'O': case 'S': case 'N':
+    case 'R':
       return (1);
     }
 
@@ -778,16 +740,18 @@ two_arguments ()
       if (test_unop (argv[pos]))
        return (unary_operator ());
       else
-       test_syntax_error ("%s: unary operator expected", argv[pos]);
+       test_syntax_error (_("%s: unary operator expected"), argv[pos]);
     }
   else
-    test_syntax_error ("%s: unary operator expected", argv[pos]);
+    test_syntax_error (_("%s: unary operator expected"), argv[pos]);
 
   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
@@ -819,7 +783,7 @@ three_arguments ()
       pos = argc;
     }
   else
-    test_syntax_error ("%s: binary operator expected", argv[pos+1]);
+    test_syntax_error (_("%s: binary operator expected"), argv[pos+1]);
 
   return (value);
 }
@@ -878,10 +842,11 @@ test_command (margc, margv)
      char **margv;
 {
   int value;
-
   int code;
 
-  code = setjmp (test_exit_buf);
+  USE_VAR(margc);
+
+  code = setjmp_nosigs (test_exit_buf);
 
   if (code)
     return (test_error_return);
@@ -893,7 +858,7 @@ test_command (margc, margv)
       --margc;
 
       if (margv[margc] && (margv[margc][0] != ']' || margv[margc][1]))
-       test_syntax_error ("missing `]'", (char *)NULL);
+       test_syntax_error (_("missing `]'"), (char *)NULL);
 
       if (margc < 2)
        test_exit (SHELL_BOOLEAN (FALSE));
@@ -909,7 +874,7 @@ test_command (margc, margv)
   value = posixtest ();
 
   if (pos != argc)
-    test_syntax_error ("too many arguments", (char *)NULL);
+    test_syntax_error (_("too many arguments"), (char *)NULL);
 
   test_exit (SHELL_BOOLEAN (value));
 }