cmdline.c 875 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * From lib/cmdline.c
  4. */
  5. #include <stdlib.h>
  6. #if __has_attribute(__fallthrough__)
  7. # define fallthrough __attribute__((__fallthrough__))
  8. #else
  9. # define fallthrough do {} while (0) /* fallthrough */
  10. #endif
  11. unsigned long long memparse(const char *ptr, char **retptr)
  12. {
  13. char *endptr; /* local pointer to end of parsed string */
  14. unsigned long long ret = strtoll(ptr, &endptr, 0);
  15. switch (*endptr) {
  16. case 'E':
  17. case 'e':
  18. ret <<= 10;
  19. fallthrough;
  20. case 'P':
  21. case 'p':
  22. ret <<= 10;
  23. fallthrough;
  24. case 'T':
  25. case 't':
  26. ret <<= 10;
  27. fallthrough;
  28. case 'G':
  29. case 'g':
  30. ret <<= 10;
  31. fallthrough;
  32. case 'M':
  33. case 'm':
  34. ret <<= 10;
  35. fallthrough;
  36. case 'K':
  37. case 'k':
  38. ret <<= 10;
  39. endptr++;
  40. fallthrough;
  41. default:
  42. break;
  43. }
  44. if (retptr)
  45. *retptr = endptr;
  46. return ret;
  47. }