Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 1 | /* strtoumax - convert string representation of a number into an uintmax_t value. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 2 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 3 | /* Copyright 1999-2009 Free Software Foundation, Inc. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 8 | it under the terms of the GNU General Public License as published by |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 20 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 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 | |
Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 31 | #if HAVE_STDINT_H |
| 32 | # include <stdint.h> |
| 33 | #endif |
| 34 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 35 | #if HAVE_STDLIB_H |
| 36 | # include <stdlib.h> |
| 37 | #endif |
| 38 | |
| 39 | #include <stdc.h> |
| 40 | |
| 41 | /* Verify a requirement at compile-time (unlike assert, which is runtime). */ |
| 42 | #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; } |
| 43 | |
| 44 | #ifndef HAVE_DECL_STRTOUL |
| 45 | "this configure-time declaration test was not run" |
| 46 | #endif |
| 47 | #if !HAVE_DECL_STRTOUL |
| 48 | extern unsigned long strtoul __P((const char *, char **, int)); |
| 49 | #endif |
| 50 | |
| 51 | #ifndef HAVE_DECL_STRTOULL |
| 52 | "this configure-time declaration test was not run" |
| 53 | #endif |
| 54 | #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG |
| 55 | extern unsigned long long strtoull __P((const char *, char **, int)); |
| 56 | #endif |
| 57 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 58 | #ifdef strtoumax |
| 59 | #undef strtoumax |
| 60 | #endif |
| 61 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 62 | uintmax_t |
| 63 | strtoumax (ptr, endptr, base) |
| 64 | const char *ptr; |
| 65 | char **endptr; |
| 66 | int base; |
| 67 | { |
| 68 | #if HAVE_UNSIGNED_LONG_LONG |
| 69 | verify (size_is_that_of_unsigned_long_or_unsigned_long_long, |
| 70 | (sizeof (uintmax_t) == sizeof (unsigned long) || |
| 71 | sizeof (uintmax_t) == sizeof (unsigned long long))); |
| 72 | |
| 73 | if (sizeof (uintmax_t) != sizeof (unsigned long)) |
| 74 | return (strtoull (ptr, endptr, base)); |
| 75 | #else |
| 76 | verify (size_is_that_of_unsigned_long, sizeof (uintmax_t) == sizeof (unsigned long)); |
| 77 | #endif |
| 78 | |
| 79 | return (strtoul (ptr, endptr, base)); |
| 80 | } |
| 81 | |
| 82 | #ifdef TESTING |
| 83 | # include <stdio.h> |
| 84 | int |
| 85 | main () |
| 86 | { |
| 87 | char *p, *endptr; |
| 88 | uintmax_t x; |
| 89 | #if HAVE_UNSIGNED_LONG_LONG |
| 90 | unsigned long long y; |
| 91 | #endif |
| 92 | unsigned long z; |
| 93 | |
| 94 | printf ("sizeof uintmax_t: %d\n", sizeof (uintmax_t)); |
| 95 | |
| 96 | #if HAVE_UNSIGNED_LONG_LONG |
| 97 | printf ("sizeof unsigned long long: %d\n", sizeof (unsigned long long)); |
| 98 | #endif |
| 99 | printf ("sizeof unsigned long: %d\n", sizeof (unsigned long)); |
| 100 | |
| 101 | x = strtoumax("42", &endptr, 10); |
| 102 | #if HAVE_LONG_LONG |
| 103 | y = strtoull("42", &endptr, 10); |
| 104 | #else |
| 105 | y = 0; |
| 106 | #endif |
| 107 | z = strtoul("42", &endptr, 10); |
| 108 | |
| 109 | printf ("%llu %llu %lu\n", x, y, z); |
| 110 | |
| 111 | exit (0); |
| 112 | } |
| 113 | #endif |