home *** CD-ROM | disk | FTP | other *** search
- #ifdef __STDC__
- static char sccs_id[] = "@(#) strtol.c 1.3 " __DATE__ " HJR";
- #else
- static char sccs_id[] = "@(#) strtol.c 1.3 26/9/90 HJR";
- #endif
-
- /* strtol.c (c) Copyright 1990 H.Rogers */
-
- #include <ctype.h>
- #ifdef __STDC__
- #include <stdlib.h>
- #else
- extern int atoi ();
- extern long atol ();
- extern long strtol ();
- extern unsigned long strtoul ();
- #endif
-
- #ifdef __STDC__
- int (atoi) (register const char *s)
- #else
- int (atoi) (s)
- register const char *s;
- #endif
- {
- return (atoi (s));
- }
- #ifdef __STDC__
- long (atol) (register const char *s)
- #else
- long (atol) (s)
- register const char *s;
- #endif
- {
- return (atol (s));
- }
-
- #define digit(x) (((x) > '9') ? (((x) & 31) + 9) : ((x) - '0'))
-
- #ifdef __STDC__
- long
- strtol (register const char *s, char **end, register int b)
- #else
- long
- strtol (s, end, b)
- register const char *s;
- char **end;
- register int b;
- #endif
- {
- register long r = 0L;
- register int r_ = 0, d;
-
- if (!s)
- return (r);
-
- while (isspace (*s))
- s++;
-
- if (*s == '+' || *s == '-')
- {
- if (*s == '-')
- r_ = 1;
- s++;
- }
-
- if (!b)
- {
- if (*s == '0')
- {
- s++;
- b = 010;
- if (*s == 'x')
- {
- s++;
- b = 0x10;
- }
- }
- else
- b = 10;
- }
- else if (b == 16)
- if (*s == '0' && *++s == 'x')
- s++;
-
- while (d = digit (*s), d >= 0 && d < b)
- {
- r = r * (long) b + (long) d;
- s++;
- }
-
- if (end)
- *end = (char *) s;
-
- return (r_ ? -r : r);
- }
-
- #ifdef __STDC__
- unsigned long
- strtoul (register const char *s, char **end, register int b)
- #else
- unsigned long
- strtoul (s, end, b)
- register const char *s;
- char **end;
- register int b;
- #endif
- {
- register unsigned long r = 0;
- register int d;
-
- if (!s)
- return (r);
-
- while (isspace (*s))
- s++;
-
- if (!b)
- {
- if (*s == '0')
- {
- s++;
- b = 010;
- if (*s == 'x')
- {
- s++;
- b = 0x10;
- }
- }
- else
- b = 10;
- }
- else if (b == 16)
- if (*s == '0' && *++s == 'x')
- s++;
-
- while (d = digit (*s), d >= 0 && d < b)
- {
- r = r * (unsigned long) b + (unsigned long) d;
- s++;
- }
-
- if (end)
- *end = (char *) s;
-
- return (r);
- }
-