(IN_CTYPE_DOMAIN): Define.
[gnulib.git] / lib / strtol.c
index ad08049..6d5f4ac 100644 (file)
@@ -1,24 +1,23 @@
-/* Copyright (C) 1991, 92, 94, 95, 96 Free Software Foundation, Inc.
-
-NOTE: The canonical source of this file is maintained with the GNU C Library.
-Bugs can be reported to bug-glibc@prep.ai.mit.edu.
-
-This program 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.
-
-This program 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 this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
-USA.  */
-
-#ifdef HAVE_CONFIG_H
+/* strtol - Convert string representation of a number into an integer value.
+   Copyright (C) 1991, 92, 94, 95, 96, 97 Free Software Foundation, Inc.
+   NOTE: The canonical source of this file is maintained with the GNU C
+   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
+
+   This program 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.
+
+   This program 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 this program; if not, write to the Free Software Foundation,
+   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+
+#if HAVE_CONFIG_H
 # include <config.h>
 #endif
 
@@ -33,6 +32,9 @@ USA.  */
 #ifndef errno
 extern int errno;
 #endif
+#ifndef __set_errno
+# define __set_errno(Val) errno = (Val)
+#endif
 
 #ifdef HAVE_LIMITS_H
 # include <limits.h>
@@ -41,6 +43,7 @@ extern int errno;
 #ifdef STDC_HEADERS
 # include <stddef.h>
 # include <stdlib.h>
+# include <string.h>
 #else
 # ifndef NULL
 #  define NULL 0
@@ -108,40 +111,43 @@ extern int errno;
 #else
 # define LONG long
 
-# ifndef ULONG_MAX
-#  define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
-# endif
-# ifndef LONG_MAX
-#  define LONG_MAX ((long int) (ULONG_MAX >> 1))
-# endif
+#ifndef ULONG_MAX
+# define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
+#endif
+#ifndef LONG_MAX
+# define LONG_MAX ((long int) (ULONG_MAX >> 1))
+#endif
 #endif
 
 #ifdef USE_WIDE_CHAR
 # include <wchar.h>
 # include <wctype.h>
-# define L_(ch) L##ch
+# define L_(Ch) L##Ch
 # define UCHAR_TYPE wint_t
 # define STRING_TYPE wchar_t
-# define ISSPACE(ch) iswspace (ch)
-# define ISALPHA(ch) iswalpha (ch)
-# define TOUPPER(ch) towupper (ch)
+# define ISSPACE(Ch) iswspace (Ch)
+# define ISALPHA(Ch) iswalpha (Ch)
+# define TOUPPER(Ch) towupper (Ch)
 #else
-# define L_(ch) ch
+# if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
+#  define IN_CTYPE_DOMAIN(c) 1
+# else
+#  define IN_CTYPE_DOMAIN(c) isascii(c)
+# endif
+# define L_(Ch) Ch
 # define UCHAR_TYPE unsigned char
 # define STRING_TYPE char
-# define ISSPACE(ch) isspace (ch)
-# define ISALPHA(ch) isalpha (ch)
-# define TOUPPER(ch) toupper (ch)
+# define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
+# define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
+# define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
 #endif
 
 #ifdef __STDC__
-# define INTERNAL(x) INTERNAL1(x)
-# define INTERNAL1(x) __##x##_internal
-# define WEAKNAME(x) WEAKNAME1(x)
-# define WEAKNAME1(x) __##x
+# define INTERNAL(X) INTERNAL1(X)
+# define INTERNAL1(X) __##X##_internal
+# define WEAKNAME(X) WEAKNAME1(X)
 #else
-# define INTERNAL(x) __/**/x/**/_internal
-# define WEAKNAME(x) __/**/x
+# define INTERNAL(X) __/**/X/**/_internal
 #endif
 
 #ifdef USE_NUMBER_GROUPING
@@ -200,7 +206,10 @@ INTERNAL (strtol) (nptr, endptr, base, group)
 #endif
 
   if (base < 0 || base == 1 || base > 36)
-    base = 10;
+    {
+      __set_errno (EINVAL);
+      return 0;
+    }
 
   save = s = nptr;
 
@@ -251,8 +260,9 @@ INTERNAL (strtol) (nptr, endptr, base, group)
       /* Find the end of the digit string and check its grouping.  */
       end = s;
       for (c = *end; c != L_('\0'); c = *++end)
-       if (c != thousands && (c < L_('0') || c > L_('9'))
-           && (!ISALPHA (c) || TOUPPER (c) - L_('A') + 10 >= base))
+       if ((wchar_t) c != thousands
+           && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
+           && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
          break;
       if (*s == thousands)
        end = s;
@@ -278,7 +288,7 @@ INTERNAL (strtol) (nptr, endptr, base, group)
        c = TOUPPER (c) - L_('A') + 10;
       else
        break;
-      if (c >= base)
+      if ((int) c >= base)
        break;
       /* Check for overflow.  */
       if (i > cutoff || (i == cutoff && c > cutlim))
@@ -307,11 +317,13 @@ INTERNAL (strtol) (nptr, endptr, base, group)
              ? -((unsigned LONG int) (LONG_MIN + 1)) + 1
              : (unsigned LONG int) LONG_MAX))
     overflow = 1;
+#else
+  overflow |= negative;
 #endif
 
   if (overflow)
     {
-      errno = ERANGE;
+      __set_errno (ERANGE);
 #if UNSIGNED
       return ULONG_MAX;
 #else
@@ -324,7 +336,7 @@ INTERNAL (strtol) (nptr, endptr, base, group)
 
 noconv:
   /* We must handle a special case here: the base is 0 or 16 and the
-     first two characters and '0' and 'x', but the rest are no
+     first two characters are '0' and 'x', but the rest are no
      hexadecimal digits.  This is no error case.  We return 0 and
      ENDPTR points to the `x`.  */
   if (endptr != NULL)
@@ -340,19 +352,23 @@ noconv:
 \f
 /* External user entry point.  */
 
-#undef __P
-#if defined (__STDC__) && __STDC__
-# define __P(args) args
-#else
-# define __P(args) ()
-#endif
+#if _LIBC - 0 == 0
+# undef PARAMS
+# if defined (__STDC__) && __STDC__
+#  define PARAMS(Args) Args
+# else
+#  define PARAMS(Args) ()
+# endif
 
 /* Prototype.  */
-INT strtol __P ((const STRING_TYPE *nptr, STRING_TYPE **endptr,
-                           int base));
+INT strtol PARAMS ((const STRING_TYPE *nptr, STRING_TYPE **endptr, int base));
+#endif
 
 
 INT
+#ifdef weak_function
+weak_function
+#endif
 strtol (nptr, endptr, base)
      const STRING_TYPE *nptr;
      STRING_TYPE **endptr;