xref: /netbsd-src/external/gpl2/diffutils/dist/lib/strtoimax.c (revision deb6f0161a9109e7de9b519dc8dfb9478668dcdd)
1 /*	$NetBSD: strtoimax.c,v 1.1.1.1 2016/01/13 03:15:30 christos Exp $	*/
2 
3 /* Convert string representation of a number into an intmax_t value.
4 
5    Copyright (C) 1999, 2001 Free Software Foundation, Inc.
6 
7    This program 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 2, or (at your option)
10    any later version.
11 
12    This program 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 this program; if not, write to the Free Software Foundation,
19    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20 
21 /* Written by Paul Eggert. */
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 #ifndef PARAMS
36 # if defined PROTOTYPES || defined __STDC__
37 #  define PARAMS(Args) Args
38 # else
39 #  define PARAMS(Args) ()
40 # endif
41 #endif
42 
43 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
44 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
45 
46 #ifdef UNSIGNED
47 # ifndef HAVE_DECL_STRTOUL
48 "this configure-time declaration test was not run"
49 # endif
50 # if !HAVE_DECL_STRTOUL
51 unsigned long strtoul PARAMS ((char const *, char **, int));
52 # endif
53 # ifndef HAVE_DECL_STRTOULL
54 "this configure-time declaration test was not run"
55 # endif
56 # if !HAVE_DECL_STRTOULL && HAVE_UNSIGNED_LONG_LONG
57 unsigned long long strtoull PARAMS ((char const *, char **, int));
58 # endif
59 
60 #else
61 
62 # ifndef HAVE_DECL_STRTOL
63 "this configure-time declaration test was not run"
64 # endif
65 # if !HAVE_DECL_STRTOL
66 long strtol PARAMS ((char const *, char **, int));
67 # endif
68 # ifndef HAVE_DECL_STRTOLL
69 "this configure-time declaration test was not run"
70 # endif
71 # if !HAVE_DECL_STRTOLL && HAVE_UNSIGNED_LONG_LONG
72 long long strtoll PARAMS ((char const *, char **, int));
73 # endif
74 #endif
75 
76 #ifdef UNSIGNED
77 # undef HAVE_LONG_LONG
78 # define HAVE_LONG_LONG HAVE_UNSIGNED_LONG_LONG
79 # define INT uintmax_t
80 # define strtoimax strtoumax
81 # define strtol strtoul
82 # define strtoll strtoull
83 #else
84 # define INT intmax_t
85 #endif
86 
87 INT
88 strtoimax (char const *ptr, char **endptr, int base)
89 {
90 #if HAVE_LONG_LONG
91   verify (size_is_that_of_long_or_long_long,
92 	  (sizeof (INT) == sizeof (long)
93 	   || sizeof (INT) == sizeof (long long)));
94 
95   if (sizeof (INT) != sizeof (long))
96     return strtoll (ptr, endptr, base);
97 #else
98   verify (size_is_that_of_long,
99 	  sizeof (INT) == sizeof (long));
100 #endif
101 
102   return strtol (ptr, endptr, base);
103 }
104