Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / lib / sh / strtoimax.c
1 /* strtoimax - convert string representation of a number into an intmax_t value. */
2
3 /* Copyright 1999-2005 Free Software Foundation, Inc.
4
5    This file is part of GNU Bash, the Bourne Again SHell.
6    
7    Bash is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    Bash is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* Written by Paul Eggert.  Modified by Chet Ramey for Bash. */
22
23 #if HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #if HAVE_INTTYPES_H
28 #  include <inttypes.h>
29 #endif
30
31 #if HAVE_STDLIB_H
32 #  include <stdlib.h>
33 #endif
34
35 #include <stdc.h>
36
37 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
38 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
39
40 #ifndef HAVE_DECL_STRTOL
41 "this configure-time declaration test was not run"
42 #endif
43 #if !HAVE_DECL_STRTOL
44 extern long strtol __P((const char *, char **, int));
45 #endif
46
47 #ifndef HAVE_DECL_STRTOLL
48 "this configure-time declaration test was not run"
49 #endif
50 #if !HAVE_DECL_STRTOLL && HAVE_LONG_LONG
51 extern long long strtoll __P((const char *, char **, int));
52 #endif
53
54 #ifdef strtoimax
55 #undef strtoimax
56 #endif
57
58 intmax_t
59 strtoimax (ptr, endptr, base)
60      const char *ptr;
61      char **endptr;
62      int base;
63 {
64 #if HAVE_LONG_LONG
65   verify(size_is_that_of_long_or_long_long,
66          (sizeof (intmax_t) == sizeof (long) ||
67           sizeof (intmax_t) == sizeof (long long)));
68
69   if (sizeof (intmax_t) != sizeof (long))
70     return (strtoll (ptr, endptr, base));
71 #else
72   verify (size_is_that_of_long, sizeof (intmax_t) == sizeof (long));
73 #endif
74
75   return (strtol (ptr, endptr, base));
76 }
77
78 #ifdef TESTING
79 # include <stdio.h>
80 int
81 main ()
82 {
83   char *p, *endptr;
84   intmax_t x;
85 #if HAVE_LONG_LONG
86   long long y;
87 #endif
88   long z;
89   
90   printf ("sizeof intmax_t: %d\n", sizeof (intmax_t));
91
92 #if HAVE_LONG_LONG
93   printf ("sizeof long long: %d\n", sizeof (long long));
94 #endif
95   printf ("sizeof long: %d\n", sizeof (long));
96
97   x = strtoimax("42", &endptr, 10);
98 #if HAVE_LONG_LONG
99   y = strtoll("42", &endptr, 10);
100 #else
101   y = -1;
102 #endif
103   z = strtol("42", &endptr, 10);
104
105   printf ("%lld %lld %ld\n", x, y, z);
106
107   exit (0);
108 }
109 #endif