1: /*
   2: **  ASCII CHARACTER STRING TO 32-BIT INTEGER CONVERSION
   3: **
   4: **	`a' is a pointer to the character string, `i' is a
   5: **	pointer to the doubleword 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: atol(a1, i)
  17: char    *a1;
  18: long    *i;
  19: {
  20:     register int    sign;   /* flag to indicate the sign */
  21:     long        x;  /* holds the integer being formed */
  22:     register char   c;
  23:     register char   *a;
  24:     long        longconst();
  25: 
  26:     a = a1;
  27:     sign = 0;
  28:     /* skip leading blanks */
  29:     while (*a == ' ')
  30:         a++;
  31:     /* check for sign */
  32:     switch (*a)
  33:     {
  34: 
  35:       case '-':
  36:         sign = -1;
  37: 
  38:       case '+':
  39:         while (*++a == ' ');
  40:     }
  41: 
  42:     /* at this point everything had better be numeric */
  43:     x = 0;
  44:     while ((c = *a) <= '9' && c >= '0')
  45:     {
  46:         /* since most c compilers don't support long constants, */
  47:         /* the code below is the contortion for: */
  48:         /* if (x > 2147483647 / 10) */
  49:         if (x > longconst(06314, 0146314))  /* check if mult by 10 will overflow */
  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: }
Last modified: 1980-12-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 767
Valid CSS Valid XHTML 1.0 Strict