blob: 6db076f9cd34440b9bd10e72d526a797b94518c8 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerd89e6292005-04-24 05:07:59 +00002/*
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003 * parse_num.c - Parse the number of blocks
Mike Frysingerd89e6292005-04-24 05:07:59 +00004 *
5 * Copyright (C) 2004,2005 Theodore Ts'o <tytso@mit.edu>
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00006 *
Mike Frysingerd89e6292005-04-24 05:07:59 +00007 * This file can be redistributed under the terms of the GNU Library General
8 * Public License
9 */
10
11#include "e2p.h"
12
13#include <stdlib.h>
14
15unsigned long parse_num_blocks(const char *arg, int log_block_size)
16{
17 char *p;
18 unsigned long long num;
19
20 num = strtoull(arg, &p, 0);
21
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000022 if (p[0] && p[1])
Mike Frysingerd89e6292005-04-24 05:07:59 +000023 return 0;
24
25 switch (*p) { /* Using fall-through logic */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000026 case 'T': case 't':
Mike Frysingerd89e6292005-04-24 05:07:59 +000027 num <<= 10;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028 case 'G': case 'g':
Mike Frysingerd89e6292005-04-24 05:07:59 +000029 num <<= 10;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000030 case 'M': case 'm':
Mike Frysingerd89e6292005-04-24 05:07:59 +000031 num <<= 10;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000032 case 'K': case 'k':
33 num >>= log_block_size;
Mike Frysingerd89e6292005-04-24 05:07:59 +000034 break;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000035 case 's':
Mike Frysingerd89e6292005-04-24 05:07:59 +000036 num >>= 1;
37 break;
38 case '\0':
39 break;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000040 default:
Mike Frysingerd89e6292005-04-24 05:07:59 +000041 return 0;
42 }
43 return num;
44}
45
46#ifdef DEBUG
47#include <unistd.h>
48#include <stdio.h>
49
50main(int argc, char **argv)
51{
52 unsigned long num;
53 int log_block_size = 0;
54
55 if (argc != 2) {
56 fprintf(stderr, "Usage: %s arg\n", argv[0]);
57 exit(1);
58 }
59
60 num = parse_num_blocks(argv[1], log_block_size);
61
62 printf("Parsed number: %lu\n", num);
63 exit(0);
64}
65#endif