xref: /freebsd-src/contrib/diff/lib/strtoimax.c (revision 18fd37a72c3a7549d2d4f6c6ea00bdcd2bdaca01)
1*18fd37a7SXin LI /* Convert string representation of a number into an intmax_t value.
2*18fd37a7SXin LI    Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
3*18fd37a7SXin LI 
4*18fd37a7SXin LI    This program is free software; you can redistribute it and/or modify
5*18fd37a7SXin LI    it under the terms of the GNU General Public License as published by
6*18fd37a7SXin LI    the Free Software Foundation; either version 2, or (at your option)
7*18fd37a7SXin LI    any later version.
8*18fd37a7SXin LI 
9*18fd37a7SXin LI    This program is distributed in the hope that it will be useful,
10*18fd37a7SXin LI    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*18fd37a7SXin LI    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*18fd37a7SXin LI    GNU General Public License for more details.
13*18fd37a7SXin LI 
14*18fd37a7SXin LI    You should have received a copy of the GNU General Public License
15*18fd37a7SXin LI    along with this program; if not, write to the Free Software Foundation,
16*18fd37a7SXin LI    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17*18fd37a7SXin LI 
18*18fd37a7SXin LI /* Written by Paul Eggert. */
19*18fd37a7SXin LI 
20*18fd37a7SXin LI #if HAVE_CONFIG_H
21*18fd37a7SXin LI # include <config.h>
22*18fd37a7SXin LI #endif
23*18fd37a7SXin LI 
24*18fd37a7SXin LI #if HAVE_INTTYPES_H
25*18fd37a7SXin LI # include <inttypes.h>
26*18fd37a7SXin LI #elif HAVE_STDINT_H
27*18fd37a7SXin LI # include <stdint.h>
28*18fd37a7SXin LI #endif
29*18fd37a7SXin LI 
30*18fd37a7SXin LI #include <stdlib.h>
31*18fd37a7SXin LI 
32*18fd37a7SXin LI /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
33*18fd37a7SXin LI #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
34*18fd37a7SXin LI 
35*18fd37a7SXin LI #ifdef UNSIGNED
36*18fd37a7SXin LI # ifndef HAVE_DECL_STRTOULL
37*18fd37a7SXin LI "this configure-time declaration test was not run"
38*18fd37a7SXin LI # endif
39*18fd37a7SXin LI # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
40*18fd37a7SXin LI unsigned long long strtoull (char const *, char **, int);
41*18fd37a7SXin LI # endif
42*18fd37a7SXin LI 
43*18fd37a7SXin LI #else
44*18fd37a7SXin LI 
45*18fd37a7SXin LI # ifndef HAVE_DECL_STRTOLL
46*18fd37a7SXin LI "this configure-time declaration test was not run"
47*18fd37a7SXin LI # endif
48*18fd37a7SXin LI # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
49*18fd37a7SXin LI long long strtoll (char const *, char **, int);
50*18fd37a7SXin LI # endif
51*18fd37a7SXin LI #endif
52*18fd37a7SXin LI 
53*18fd37a7SXin LI #ifdef UNSIGNED
54*18fd37a7SXin LI # undef HAVE_LONG_LONG
55*18fd37a7SXin LI # define HAVE_LONG_LONG HAVE_UNSIGNED_LONG_LONG
56*18fd37a7SXin LI # define INT uintmax_t
57*18fd37a7SXin LI # define strtoimax strtoumax
58*18fd37a7SXin LI # define strtol strtoul
59*18fd37a7SXin LI # define strtoll strtoull
60*18fd37a7SXin LI #else
61*18fd37a7SXin LI # define INT intmax_t
62*18fd37a7SXin LI #endif
63*18fd37a7SXin LI 
64*18fd37a7SXin LI INT
strtoimax(char const * ptr,char ** endptr,int base)65*18fd37a7SXin LI strtoimax (char const *ptr, char **endptr, int base)
66*18fd37a7SXin LI {
67*18fd37a7SXin LI #if HAVE_LONG_LONG
68*18fd37a7SXin LI   verify (size_is_that_of_long_or_long_long,
69*18fd37a7SXin LI 	  (sizeof (INT) == sizeof (long)
70*18fd37a7SXin LI 	   || sizeof (INT) == sizeof (long long)));
71*18fd37a7SXin LI 
72*18fd37a7SXin LI   if (sizeof (INT) != sizeof (long))
73*18fd37a7SXin LI     return strtoll (ptr, endptr, base);
74*18fd37a7SXin LI #else
75*18fd37a7SXin LI   verify (size_is_that_of_long,
76*18fd37a7SXin LI 	  sizeof (INT) == sizeof (long));
77*18fd37a7SXin LI #endif
78*18fd37a7SXin LI 
79*18fd37a7SXin LI   return strtol (ptr, endptr, base);
80*18fd37a7SXin LI }
81