strtol.c 927 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. #include <stdlib.h>
  7. #include <ctype.h>
  8. long int
  9. strtol(register const char *p, char **pp, int base)
  10. {
  11. register long val, v;
  12. register int c;
  13. int sign = 1;
  14. if (pp) *pp = p;
  15. while (isspace(*p)) p++;
  16. c = *p;
  17. switch (c) {
  18. case '-':
  19. sign = -1;
  20. /* fallthrough */
  21. case '+':
  22. p++;
  23. }
  24. /* this is bizare */
  25. if (base==16 && *p=='0' && (*(p+1)=='x' || *(p+1)=='X'))
  26. p += 2;
  27. while (isdigit(c = *p++) || isalpha(c)) {
  28. if (isalpha(c))
  29. v = 10 + (isupper(c) ? c - 'A' : c - 'a');
  30. else
  31. v = c - '0';
  32. if (v >= base) {
  33. p--;
  34. break;
  35. }
  36. val = (val * base) + v;
  37. }
  38. if (pp) *pp = p-1;
  39. return sign * val;
  40. }
  41. unsigned long int
  42. strtoul(register const char *p, char **pp, int base)
  43. {
  44. return (unsigned long)strtol(p, pp, base);
  45. }