|
|
1.1 ! root 1: # include <ingres.h> ! 2: # include <sccs.h> ! 3: ! 4: SCCSID(@(#)atoi.c 7.1 2/5/81) ! 5: ! 6: /* ! 7: ** ASCII CHARACTER STRING TO INTEGER CONVERSION ! 8: ** ! 9: ** `a' is a pointer to the character string, `i' is a ! 10: ** pointer to the word which is to contain the result. ! 11: ** The integer is either 16-bit or 32-bit depending upon ! 12: ** the natural word length of the machine. ! 13: ** ! 14: ** The return value of the function is: ! 15: ** zero: succesful conversion; `i' contains the integer ! 16: ** +1: numeric overflow; `i' is unchanged ! 17: ** -1: syntax error; `i' is unchanged ! 18: ** ! 19: ** A valid string is of the form: ! 20: ** <space>* [+-] <space>* <digit>* <space>* ! 21: */ ! 22: ! 23: atoi(a, i) ! 24: register char *a; ! 25: int *i; ! 26: { ! 27: # ifdef PDP11 ! 28: int sign; /* flag to indicate the sign */ ! 29: register int x; /* holds the integer being formed */ ! 30: register char c; ! 31: ! 32: sign = 0; ! 33: /* skip leading blanks */ ! 34: while (*a == ' ') ! 35: a++; ! 36: /* check for sign */ ! 37: switch (*a) ! 38: { ! 39: ! 40: case '-': ! 41: sign = -1; ! 42: ! 43: case '+': ! 44: while (*++a == ' '); ! 45: } ! 46: ! 47: /* at this point everything had better be numeric */ ! 48: x = 0; ! 49: while ((c = *a) <= '9' && c >= '0') ! 50: { ! 51: if (x > 3276 || (x == 3276 && c > '7')) ! 52: return (1); /* overflow */ ! 53: x = x * 10 + c - '0'; ! 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: # else ! 64: return (atol(a, i)); ! 65: # endif ! 66: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.