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