X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Fstrtol.c;h=8fc95b0fce7c66d842bcb8d325e371f211f2cac9;hb=0d54a75af25ca44411e7c4190cc2a93a390e61a2;hp=2ee1e5f3fab52aa72af6b963120373569618efa0;hpb=fed7bd2546a24c265b7a4c74b2b2829a1738a1db;p=pdclib.old diff --git a/functions/stdlib/strtol.c b/functions/stdlib/strtol.c index 2ee1e5f..8fc95b0 100644 --- a/functions/stdlib/strtol.c +++ b/functions/stdlib/strtol.c @@ -1,33 +1,32 @@ /* $Id$ */ -/* Release $Name$ */ - /* strtol( const char *, char * *, int ) This file is part of the Public Domain C Library (PDCLib). Permission is granted to use, modify, and / or redistribute at will. */ -#define _PDCLIB_INT_H _PDCLIB_INT_H -#include <_PDCLIB_int.h> #include #include #ifndef REGTEST +#include + long int strtol( const char * s, char ** endptr, int base ) { long int rc; char sign = '+'; const char * p = _PDCLIB_strtox_prelim( s, &sign, &base ); + if ( base < 2 || base > 36 ) return 0; if ( sign == '+' ) { - rc = _PDCLIB_strtox_main( &p, base, LONG_MAX, LONG_MAX / base, LONG_MAX % base, &sign ); + rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MAX, (uintmax_t)( LONG_MAX / base ), (int)( LONG_MAX % base ), &sign ); } else { /* FIXME: This breaks on some machines that round negatives wrongly */ - rc = _PDCLIB_strtox_main( &p, base, LONG_MIN, LONG_MIN / -base, -( LONG_MIN % base ), &sign ); + rc = (long int)_PDCLIB_strtox_main( &p, (unsigned)base, (uintmax_t)LONG_MIN, (uintmax_t)( LONG_MIN / -base ), (int)( -( LONG_MIN % base ) ), &sign ); } if ( endptr != NULL ) *endptr = ( p != NULL ) ? (char *) p : (char *) s; return ( sign == '+' ) ? rc : -rc; @@ -37,14 +36,19 @@ long int strtol( const char * s, char ** endptr, int base ) #ifdef TEST #include <_PDCLIB_test.h> + +#ifndef _PDCLIB_INT_H +#define _PDCLIB_INT_H +#include <_PDCLIB_int.h> +#endif + #include -int main() +int main( void ) { char * endptr; /* this, to base 36, overflows even a 256 bit integer */ char overflow[] = "-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ_"; - BEGIN_TESTS; errno = 0; /* basic functionality */ TESTCASE( strtol( "123", NULL, 10 ) == 123 ); @@ -88,7 +92,7 @@ int main() TESTCASE( strtol( "0x80000000", NULL, 0 ) == LONG_MAX ); TESTCASE( errno == ERANGE ); errno = 0; - TESTCASE( strtol( "-0x7FFFFFFF", NULL, 0 ) == 0x80000001 ); + TESTCASE( strtol( "-0x7FFFFFFF", NULL, 0 ) == (long)0x80000001 ); TESTCASE( errno == 0 ); TESTCASE( strtol( "-0x80000000", NULL, 0 ) == LONG_MIN ); TESTCASE( errno == 0 ); @@ -102,7 +106,7 @@ int main() TESTCASE( strtol( "0x8000000000000000", NULL, 0 ) == LONG_MAX ); TESTCASE( errno == ERANGE ); errno = 0; - TESTCASE( strtol( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == -0x8000000000000001 ); + TESTCASE( strtol( "-0x7FFFFFFFFFFFFFFF", NULL, 0 ) == (long)0x8000000000000001 ); TESTCASE( errno == 0 ); TESTCASE( strtol( "-0x8000000000000000", NULL, 0 ) == LONG_MIN ); TESTCASE( errno == 0 );