OSDN Git Service

auto import from //depot/cupcake/@135843
[android-x86/external-e2fsprogs.git] / lib / e2p / parse_num.c
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
10 #include "e2p.h"
11
12 #include <stdlib.h>
13
14 unsigned long parse_num_blocks(const char *arg, int log_block_size)
15 {
16         char *p;
17         unsigned long long num;
18
19         num = strtoull(arg, &p, 0);
20
21         if (p[0] && p[1]) 
22                 return 0;
23
24         switch (*p) {           /* Using fall-through logic */
25         case 'T': case 't': 
26                 num <<= 10;
27         case 'G': case 'g': 
28                 num <<= 10;
29         case 'M': case 'm': 
30                 num <<= 10;
31         case 'K': case 'k': 
32                 num >>= log_block_size; 
33                 break;
34         case 's': 
35                 num >>= (1+log_block_size);
36                 break;
37         case '\0':
38                 break;
39         default: 
40                 return 0;
41         }
42         return num;
43 }
44
45 #ifdef DEBUG
46 #include <unistd.h>
47 #include <stdio.h>
48
49 main(int argc, char **argv)
50 {
51         unsigned long num;
52         int log_block_size = 0;
53
54         if (argc != 2) {
55                 fprintf(stderr, "Usage: %s arg\n", argv[0]);
56                 exit(1);
57         }
58
59         num = parse_num_blocks(argv[1], log_block_size);
60
61         printf("Parsed number: %lu\n", num);
62         exit(0);
63 }
64 #endif