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