|
|
1.1 ! root 1: # include <sccs.h> ! 2: ! 3: SCCSID(@(#)atol.c 7.1 2/5/81) ! 4: ! 5: /* ! 6: ** ASCII CHARACTER STRING TO 32-BIT INTEGER CONVERSION ! 7: ** ! 8: ** `a' is a pointer to the character string, `i' is a ! 9: ** pointer to the doubleword which is to contain the result. ! 10: ** ! 11: ** The return value of the function is: ! 12: ** zero: succesful conversion; `i' contains the integer ! 13: ** +1: numeric overflow; `i' is unchanged ! 14: ** -1: syntax error; `i' is unchanged ! 15: ** ! 16: ** A valid string is of the form: ! 17: ** <space>* [+-] <space>* <digit>* <space>* ! 18: */ ! 19: ! 20: atol(a, i) ! 21: register char *a; ! 22: long *i; ! 23: { ! 24: register int sign; /* flag to indicate the sign */ ! 25: long x; /* holds the integer being formed */ ! 26: register char c; ! 27: ! 28: sign = 0; ! 29: /* skip leading blanks */ ! 30: while (*a == ' ') ! 31: a++; ! 32: /* check for sign */ ! 33: switch (*a) ! 34: { ! 35: ! 36: case '-': ! 37: sign = -1; ! 38: ! 39: case '+': ! 40: while (*++a == ' '); ! 41: } ! 42: ! 43: /* at this point everything had better be numeric */ ! 44: x = 0; ! 45: while ((c = *a) <= '9' && c >= '0') ! 46: { ! 47: /* check for overflow */ ! 48: /* 2 ** 31 = 2147483648 */ ! 49: if (x > 0x7fffffffL / 10) ! 50: return (1); ! 51: x = x * 10 + (c - '0'); ! 52: if (x < 0) /* check if new digit caused overflow */ ! 53: return (1); ! 54: a++; ! 55: } ! 56: ! 57: /* eaten all the numerics; better be all blanks */ ! 58: while (c = *a++) ! 59: if(c != ' ') /* syntax error */ ! 60: return (-1); ! 61: *i = sign ? -x : x; ! 62: return (0); /* successful termination */ ! 63: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.