1 /* $NetBSD: strtoumax.c,v 1.1.1.1 2016/01/10 21:36:19 christos Exp $ */
2
3 /* Convert string representation of a number into an uintmax_t value.
4 Copyright 1999 Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 /* Written by Paul Eggert. */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #if HAVE_INTTYPES_H
27 # include <inttypes.h>
28 #endif
29
30 #if HAVE_STDLIB_H
31 # include <stdlib.h>
32 #endif
33
34 #ifndef PARAMS
35 # if defined PROTOTYPES || defined __STDC__
36 # define PARAMS(Args) Args
37 # else
38 # define PARAMS(Args) ()
39 # endif
40 #endif
41
42 #ifndef HAVE_DECL_STRTOUL
43 "this configure-time declaration test was not run"
44 #endif
45 #if !HAVE_DECL_STRTOUL
46 unsigned long strtoul PARAMS ((char const *, char **, int));
47 #endif
48
49 #ifndef HAVE_DECL_STRTOULL
50 "this configure-time declaration test was not run"
51 #endif
52 #if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
53 unsigned long long strtoull PARAMS ((char const *, char **, int));
54 #endif
55
56 uintmax_t
strtoumax(char const * ptr,char ** endptr,int base)57 strtoumax (char const *ptr, char **endptr, int base)
58 {
59 #define USE_IF_EQUIVALENT(function) \
60 if (sizeof (uintmax_t) == sizeof function (ptr, endptr, base)) \
61 return function (ptr, endptr, base);
62
63 #if HAVE_UNSIGNED_LONG_LONG
64 USE_IF_EQUIVALENT (strtoull)
65 #endif
66
67 USE_IF_EQUIVALENT (strtoul)
68
69 abort ();
70 }
71
72 #ifdef TESTING
73 # include <stdio.h>
74 int
main()75 main ()
76 {
77 char *p, *endptr;
78 printf ("sizeof uintmax_t: %d\n", sizeof (uintmax_t));
79 printf ("sizeof strtoull(): %d\n", sizeof strtoull(p, &endptr, 10));
80 printf ("sizeof strtoul(): %d\n", sizeof strtoul(p, &endptr, 10));
81 exit (0);
82 }
83 #endif
84