3
0

parse_num.c 1.0 KB

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