1: /*
   2: **  ASCII CHARACTER STRING TO 16-BIT INTEGER CONVERSION
   3: **
   4: **	`a' is a pointer to the character string, `i' is a
   5: **	pointer to the word which is to contain the result.
   6: **
   7: **	The return value of the function is:
   8: **		zero:	succesful conversion; `i' contains the integer
   9: **		+1:	numeric overflow; `i' is unchanged
  10: **		-1:	syntax error; `i' is unchanged
  11: **
  12: **	A valid string is of the form:
  13: **		<space>* [+-] <space>* <digit>* <space>*
  14: */
  15: 
  16: atoi(a1, i)
  17: char    *a1;
  18: int *i;
  19: {
  20:     int     sign;       /* flag to indicate the sign */
  21:     register int    x;      /* holds the integer being formed */
  22:     register char   c;
  23:     register char   *a;
  24: 
  25:     a = a1;
  26:     sign = 0;
  27:     /* skip leading blanks */
  28:     while (*a == ' ')
  29:         a++;
  30:     /* check for sign */
  31:     switch (*a)
  32:     {
  33: 
  34:       case '-':
  35:         sign = -1;
  36: 
  37:       case '+':
  38:         while (*++a == ' ');
  39:     }
  40: 
  41:     /* at this point everything had better be numeric */
  42:     x = 0;
  43:     while ((c = *a) <= '9' && c >= '0')
  44:     {
  45:         if (x > 3276 || (x == 3276 && c > '7'))
  46:             return (1);     /* overflow */
  47:         x = x * 10 + c - '0';
  48:         a++;
  49:     }
  50: 
  51:     /* eaten all the numerics; better be all blanks */
  52:     while (c = *a++)
  53:         if(c != ' ')            /* syntax error */
  54:             return (-1);
  55:     *i = sign ? -x : x;
  56:     return (0);     /* successful termination */
  57: }
Last modified: 1980-12-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 431
Valid CSS Valid XHTML 1.0 Strict