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