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

Defined functions

atol defined in line 4; never used
Last modified: 1986-04-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 651
Valid CSS Valid XHTML 1.0 Strict