atol.c 308 B

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