|
|
1.1 root 1: /*
2: * strtol : convert a string to long.
3: *
4: * Andy Wilson, 2-Oct-89.
5: */
6:
7: #include <errno.h>
8: #include <ctype.h>
9: #include <stdio.h>
10: #include "ansidecl.h"
11:
12: /* FIXME: It'd be nice to configure around these, but the include files are too
13: painful. These macros should at least be more portable than hardwired hex
14: constants. */
15:
16: #define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF */
17: #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */
18: #define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */
19:
20: extern int errno;
21:
22: long
23: strtol(s, ptr, base)
24: CONST char *s; char **ptr; int base;
25: {
26: extern unsigned long strtoul();
27: int minus=0;
28: unsigned long tmp;
29: CONST char *start=s, *eptr;
30:
31: if (s==NULL)
32: {
33: errno = ERANGE;
34: if (!ptr)
35: *ptr = (char *)start;
36: return 0L;
37: }
38: while (isspace(*s))
39: s++;
40: if (*s == '-') {
41: s++;
42: minus = 1;
43: }
44: else if (*s == '+')
45: s++;
46:
47: /*
48: * let strtoul do the hard work.
49: */
50: tmp = strtoul(s, &eptr, base);
51: if (ptr != NULL)
52: *ptr = (char *)((eptr==s) ? (char *)start : eptr);
53: if (tmp > (minus ? - (unsigned long) LONG_MIN : (unsigned long) LONG_MAX))
54: {
55: errno = ERANGE;
56: return (minus ? LONG_MIN : LONG_MAX);
57: }
58: return (minus ? (long) -tmp : (long) tmp);
59: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.