parse_num.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * parse_num.c - Parse the number of blocks
  3. *
  4. * Copyright (C) 2004,2005 Theodore Ts'o <tytso@mit.edu>
  5. *
  6. * This file can be redistributed under the terms of the GNU Library General
  7. * Public License
  8. */
  9. #include "e2p.h"
  10. #include <stdlib.h>
  11. unsigned long parse_num_blocks(const char *arg, int log_block_size)
  12. {
  13. char *p;
  14. unsigned long long num;
  15. num = strtoull(arg, &p, 0);
  16. if (p[0] && p[1])
  17. return 0;
  18. switch (*p) { /* Using fall-through logic */
  19. case 'T': case 't':
  20. num <<= 10;
  21. case 'G': case 'g':
  22. num <<= 10;
  23. case 'M': case 'm':
  24. num <<= 10;
  25. case 'K': case 'k':
  26. num >>= log_block_size;
  27. break;
  28. case 's':
  29. num >>= 1;
  30. break;
  31. case '\0':
  32. break;
  33. default:
  34. return 0;
  35. }
  36. return num;
  37. }
  38. #ifdef DEBUG
  39. #include <unistd.h>
  40. #include <stdio.h>
  41. main(int argc, char **argv)
  42. {
  43. unsigned long num;
  44. int log_block_size = 0;
  45. if (argc != 2) {
  46. fprintf(stderr, "Usage: %s arg\n", argv[0]);
  47. exit(1);
  48. }
  49. num = parse_num_blocks(argv[1], log_block_size);
  50. printf("Parsed number: %lu\n", num);
  51. exit(0);
  52. }
  53. #endif