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