(my_strtoumax): Fix typo in computing
[pspp] / lib / xstrtoumax.c
1 /* xstrtoumax.c -- A more useful interface to strtoumax.
2    Copyright 1999 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Paul Eggert. */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #if HAVE_STRTOUMAX
25 # define __strtol strtoumax
26 #else
27 # define __strtol my_strtoumax
28
29   /* Define a temporary implementation, intended to hold the fort only
30      until glibc has an implementation of the C9x standard function
31      strtoumax.  When glibc implements strtoumax, we can remove the
32      following function in favor of a replacement file strtoumax.c taken
33      from glibc.  */
34
35 # if HAVE_INTTYPES_H
36 #  include <inttypes.h>
37 # endif
38
39 # if HAVE_STDLIB_H
40 #  include <stdlib.h>
41 # endif
42
43 # ifndef PARAMS
44 #  if defined PROTOTYPES || defined __STDC__
45 #   define PARAMS(Args) Args
46 #  else
47 #   define PARAMS(Args) ()
48 #  endif
49 # endif
50
51 static uintmax_t my_strtoumax PARAMS ((char const *, char **, int));
52 #endif
53
54 #define __strtol_t uintmax_t
55 #define __xstrtol xstrtoumax
56 #include "xstrtol.c"
57
58 #if !HAVE_STRTOUMAX
59
60 static uintmax_t
61 my_strtoumax (char const *ptr, char **endptr, int base)
62 {
63 # define USE_IF_EQUIVALENT(function) \
64     if (sizeof (uintmax_t) == sizeof function (ptr, endptr, base)) \
65       return function (ptr, endptr, base);
66
67   USE_IF_EQUIVALENT (strtoul)
68
69 # if HAVE_STRTOULL
70     USE_IF_EQUIVALENT (strtoull)
71 # endif
72
73 # if HAVE_STRTOUQ
74     USE_IF_EQUIVALENT (strtouq)
75 # endif
76
77   {
78     /* An implementation with uintmax_t longer than long, but with no
79        known way to convert it.  Do it by hand.  Assume base 10.  */
80     uintmax_t n = 0;
81     uintmax_t overflow = 0;
82     for (;  '0' <= *ptr && *ptr <= '9';  ptr++)
83       {
84         uintmax_t n10 = n * 10;
85         int digit = *ptr - '0';
86         overflow |= n ^ (n10 + digit) / 10;
87         n = n10 + digit;
88       }
89     if (endptr)
90       *endptr = (char *) ptr;
91     if (overflow)
92       {
93         errno = ERANGE;
94         n = (uintmax_t) -1;
95       }
96     return n;
97   }
98 }
99 #endif /* HAVE_STRTOUMAX */