scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

atol.c (365B)


      1 #include <ctype.h>
      2 #include <stdlib.h>
      3 
      4 #include "../libc.h"
      5 
      6 #undef atol
      7 
      8 long
      9 atol(const char *s)
     10 {
     11 	int d, sign = -1;
     12 	long n;
     13 
     14 	while (isspace(*s))
     15 		++s;
     16 
     17 	switch (*s) {
     18 	case '-':
     19 		sign = 1;
     20 	case '+':
     21 		++s;
     22 	}
     23 
     24 	/* Compute n as a negative number to avoid overflow on LONG_MIN */
     25 	for (n = 0; (d = _dtoi(*s)) < 10; ++s)
     26 		n = n*10 - d;
     27 
     28 	return sign * n;
     29 }