home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_10_05
/
1005016a
< prev
next >
Wrap
Text File
|
1992-03-06
|
639b
|
33 lines
Listing 3 -- strtol.c
/* strtol function */
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
long (strtol)(const char *s, char **endptr, int base)
{ /* convert string to long, with checking */
const char *sc;
unsigned long x;
for (sc = s; isspace(*sc); ++sc)
;
x = _Stoul(s, endptr, base); /* not sc! */
if (*sc == '-' && x <= LONG_MAX)
{ /* negative number overflowed */
errno = ERANGE;
return (LONG_MIN);
}
else if (*sc != '-' && LONG_MAX < x)
{ /* positive number overflowed */
errno = ERANGE;
return (LONG_MAX);
}
else
return ((long)x);
}