blob: 81c160544a419ce57574b8eb405a4f3f1f30f866 [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002 * parse_num.c - Parse the number of blocks
Mike Frysingerd89e6292005-04-24 05:07:59 +00003 *
4 * Copyright (C) 2004,2005 Theodore Ts'o <tytso@mit.edu>
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00005 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00006 * 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
14unsigned 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
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000021 if (p[0] && p[1])
Mike Frysingerd89e6292005-04-24 05:07:59 +000022 return 0;
23
24 switch (*p) { /* Using fall-through logic */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000025 case 'T': case 't':
Mike Frysingerd89e6292005-04-24 05:07:59 +000026 num <<= 10;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000027 case 'G': case 'g':
Mike Frysingerd89e6292005-04-24 05:07:59 +000028 num <<= 10;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000029 case 'M': case 'm':
Mike Frysingerd89e6292005-04-24 05:07:59 +000030 num <<= 10;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000031 case 'K': case 'k':
32 num >>= log_block_size;
Mike Frysingerd89e6292005-04-24 05:07:59 +000033 break;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000034 case 's':
Mike Frysingerd89e6292005-04-24 05:07:59 +000035 num >>= 1;
36 break;
37 case '\0':
38 break;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000039 default:
Mike Frysingerd89e6292005-04-24 05:07:59 +000040 return 0;
41 }
42 return num;
43}
44
45#ifdef DEBUG
46#include <unistd.h>
47#include <stdio.h>
48
49main(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