]> git.ipfire.org Git - thirdparty/bash.git/blame_incremental - lib/sh/strtoumax.c
Bash-5.3 distribution sources and documentation
[thirdparty/bash.git] / lib / sh / strtoumax.c
... / ...
CommitLineData
1/* strtoumax - convert string representation of a number into an uintmax_t value. */
2
3/* Copyright 1999-2020,2022 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_STDINT_H
32# include <stdint.h>
33#endif
34
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
48extern unsigned long strtoul (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_INT
55extern unsigned long long strtoull (const char *, char **, int);
56#endif
57
58#ifdef strtoumax
59#undef strtoumax
60#endif
61
62uintmax_t
63strtoumax (const char *ptr, char **endptr, int base)
64{
65#if HAVE_UNSIGNED_LONG_LONG_INT
66 verify (size_is_that_of_unsigned_long_or_unsigned_long_long,
67 (sizeof (uintmax_t) == sizeof (unsigned long) ||
68 sizeof (uintmax_t) == sizeof (unsigned long long)));
69
70 if (sizeof (uintmax_t) != sizeof (unsigned long))
71 return (strtoull (ptr, endptr, base));
72#else
73 verify (size_is_that_of_unsigned_long, sizeof (uintmax_t) == sizeof (unsigned long));
74#endif
75
76 return (strtoul (ptr, endptr, base));
77}
78
79#ifdef TESTING
80# include <stdio.h>
81int
82main ()
83{
84 char *p, *endptr;
85 uintmax_t x;
86#if HAVE_UNSIGNED_LONG_LONG_INT
87 unsigned long long y;
88#endif
89 unsigned long z;
90
91 printf ("sizeof uintmax_t: %d\n", sizeof (uintmax_t));
92
93#if HAVE_UNSIGNED_LONG_LONG_INT
94 printf ("sizeof unsigned long long: %d\n", sizeof (unsigned long long));
95#endif
96 printf ("sizeof unsigned long: %d\n", sizeof (unsigned long));
97
98 x = strtoumax("42", &endptr, 10);
99#if HAVE_LONG_LONG_INT
100 y = strtoull("42", &endptr, 10);
101#else
102 y = 0;
103#endif
104 z = strtoul("42", &endptr, 10);
105
106 printf ("%llu %llu %lu\n", x, y, z);
107
108 exit (0);
109}
110#endif