Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / lib / sh / strtoumax.c
1 /* Convert string representation of a number into an uintmax_t value.
2    Copyright 1999, 2001 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.  Modified by Chet Ramey for Bash. */
19
20 #if HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #if HAVE_INTTYPES_H
25 #  include <inttypes.h>
26 #endif
27
28 #if HAVE_STDLIB_H
29 #  include <stdlib.h>
30 #endif
31
32 #include <stdc.h>
33
34 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
35 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
36
37 #ifndef HAVE_DECL_STRTOUL
38 "this configure-time declaration test was not run"
39 #endif
40 #if !HAVE_DECL_STRTOUL
41 extern unsigned long strtoul __P((const char *, char **, int));
42 #endif
43
44 #ifndef HAVE_DECL_STRTOULL
45 "this configure-time declaration test was not run"
46 #endif
47 #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
48 extern unsigned long long strtoull __P((const char *, char **, int));
49 #endif
50
51 uintmax_t
52 strtoumax (ptr, endptr, base)
53      const char *ptr;
54      char **endptr;
55      int base;
56 {
57 #if HAVE_UNSIGNED_LONG_LONG
58   verify (size_is_that_of_unsigned_long_or_unsigned_long_long,
59           (sizeof (uintmax_t) == sizeof (unsigned long) ||
60            sizeof (uintmax_t) == sizeof (unsigned long long)));
61
62   if (sizeof (uintmax_t) != sizeof (unsigned long))
63     return (strtoull (ptr, endptr, base));
64 #else
65   verify (size_is_that_of_unsigned_long, sizeof (uintmax_t) == sizeof (unsigned long));
66 #endif
67
68   return (strtoul (ptr, endptr, base));
69 }
70
71 #ifdef TESTING
72 # include <stdio.h>
73 int
74 main ()
75 {
76   char *p, *endptr;
77   uintmax_t x;
78 #if HAVE_UNSIGNED_LONG_LONG
79   unsigned long long y;
80 #endif
81   unsigned long z;
82
83   printf ("sizeof uintmax_t: %d\n", sizeof (uintmax_t));
84
85 #if HAVE_UNSIGNED_LONG_LONG
86   printf ("sizeof unsigned long long: %d\n", sizeof (unsigned long long));
87 #endif
88   printf ("sizeof unsigned long: %d\n", sizeof (unsigned long));
89
90   x = strtoumax("42", &endptr, 10);
91 #if HAVE_LONG_LONG
92   y = strtoull("42", &endptr, 10);
93 #else
94   y = 0;
95 #endif
96   z = strtoul("42", &endptr, 10);
97
98   printf ("%llu %llu %lu\n", x, y, z);
99
100   exit (0);
101 }
102 #endif