stdlib.c 934 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. * stdlib functions
  3. *
  4. * Author: Scott Wood <scottwood@freescale.com>
  5. *
  6. * Copyright (c) 2007 Freescale Semiconductor, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published
  10. * by the Free Software Foundation.
  11. */
  12. #include "stdlib.h"
  13. /* Not currently supported: leading whitespace, sign, 0x prefix, zero base */
  14. unsigned long long int strtoull(const char *ptr, char **end, int base)
  15. {
  16. unsigned long long ret = 0;
  17. if (base > 36)
  18. goto out;
  19. while (*ptr) {
  20. int digit;
  21. if (*ptr >= '0' && *ptr <= '9' && *ptr < '0' + base)
  22. digit = *ptr - '0';
  23. else if (*ptr >= 'A' && *ptr < 'A' + base - 10)
  24. digit = *ptr - 'A' + 10;
  25. else if (*ptr >= 'a' && *ptr < 'a' + base - 10)
  26. digit = *ptr - 'a' + 10;
  27. else
  28. break;
  29. ret *= base;
  30. ret += digit;
  31. ptr++;
  32. }
  33. out:
  34. if (end)
  35. *end = (char *)ptr;
  36. return ret;
  37. }