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