Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / test.c
diff --git a/test.c b/test.c
index a906f62..449c5d3 100644 (file)
--- a/test.c
+++ b/test.c
@@ -2,7 +2,7 @@
 
 /* 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.
 
@@ -32,9 +32,7 @@
 
 #include "bashtypes.h"
 
-#if defined (HAVE_LIMITS_H)
-#  include <limits.h>
-#else
+#if !defined (HAVE_LIMITS_H)
 #  include <sys/param.h>
 #endif
 
@@ -58,7 +56,7 @@ extern int errno;
 #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)
@@ -115,31 +113,32 @@ 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 test_stat __P((char *, struct stat *));
+
+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);
 }
 
@@ -177,14 +176,17 @@ test_stat (path, finfo)
   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);
-       }
+      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.
@@ -279,7 +281,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 +302,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 ();
@@ -388,24 +390,23 @@ filecomp (s, t, op)
      int op;
 {
   struct stat st1, st2;
+  int r1, r2;
 
-  if (test_stat (s, &st1) < 0)
+  if ((r1 = test_stat (s, &st1)) < 0)
     {
-      st1.st_mtime = 0;
       if (op == EF)
        return (FALSE);
     }
-  if (test_stat (t, &st2) < 0)
+  if ((r2 = test_stat (t, &st2)) < 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 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);
@@ -416,7 +417,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 +457,7 @@ patcomp (string, pat, op)
 {
   int m;
 
-  m = fnmatch (pat, string, FNMATCH_EXTFLAG);
+  m = strmatch (pat, string, FNMATCH_EXTFLAG);
   return ((op == EQ) ? (m == 0) : (m != 0));
 }
 
@@ -550,8 +551,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,7 +588,7 @@ int
 unary_test (op, arg)
      char *op, *arg;
 {
-  long r;
+  intmax_t r;
   struct stat stat_buf;
      
   switch (op[1])
@@ -680,7 +681,7 @@ unary_test (op, arg)
     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');
@@ -691,6 +692,9 @@ unary_test (op, arg)
     case 'o':                  /* True if option `arg' is set. */
       return (minus_o_option_value (arg) == 1);
     }
+
+  /* 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. */
@@ -788,6 +792,8 @@ two_arguments ()
 
 #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
@@ -878,9 +884,10 @@ test_command (margc, margv)
      char **margv;
 {
   int value;
-
   int code;
 
+  USE_VAR(margc);
+
   code = setjmp (test_exit_buf);
 
   if (code)