2005-01-05 16:01:06 +08:00
|
|
|
/*
|
2008-08-28 11:07:54 +08:00
|
|
|
* parse_num.c - Parse the number of blocks
|
2005-01-05 16:01:06 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2004,2005 Theodore Ts'o <tytso@mit.edu>
|
2008-08-28 11:07:54 +08:00
|
|
|
*
|
2010-05-18 09:31:56 +08:00
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Library
|
|
|
|
* General Public License, version 2.
|
|
|
|
* %End-Header%
|
2005-01-05 16:01:06 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "e2p.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-07-22 07:34:42 +08:00
|
|
|
unsigned long long parse_num_blocks2(const char *arg, int log_block_size)
|
2005-01-05 16:01:06 +08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
unsigned long long num;
|
|
|
|
|
|
|
|
num = strtoull(arg, &p, 0);
|
|
|
|
|
2008-08-28 11:07:54 +08:00
|
|
|
if (p[0] && p[1])
|
2005-01-05 16:01:06 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (*p) { /* Using fall-through logic */
|
2008-08-28 11:07:54 +08:00
|
|
|
case 'T': case 't':
|
2005-01-05 16:01:06 +08:00
|
|
|
num <<= 10;
|
2011-09-17 04:49:23 +08:00
|
|
|
/* fallthrough */
|
2008-08-28 11:07:54 +08:00
|
|
|
case 'G': case 'g':
|
2005-01-05 16:01:06 +08:00
|
|
|
num <<= 10;
|
2011-09-17 04:49:23 +08:00
|
|
|
/* fallthrough */
|
2008-08-28 11:07:54 +08:00
|
|
|
case 'M': case 'm':
|
2005-01-05 16:01:06 +08:00
|
|
|
num <<= 10;
|
2011-09-17 04:49:23 +08:00
|
|
|
/* fallthrough */
|
2008-08-28 11:07:54 +08:00
|
|
|
case 'K': case 'k':
|
|
|
|
num >>= log_block_size;
|
2005-01-05 16:01:06 +08:00
|
|
|
break;
|
2008-08-28 11:07:54 +08:00
|
|
|
case 's':
|
2007-04-03 07:12:17 +08:00
|
|
|
num >>= (1+log_block_size);
|
2005-01-05 16:01:06 +08:00
|
|
|
break;
|
|
|
|
case '\0':
|
|
|
|
break;
|
2008-08-28 11:07:54 +08:00
|
|
|
default:
|
2005-01-05 16:01:06 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2009-07-22 07:34:42 +08:00
|
|
|
unsigned long parse_num_blocks(const char *arg, int log_block_size)
|
|
|
|
{
|
|
|
|
return parse_num_blocks2(arg, log_block_size);
|
|
|
|
}
|
|
|
|
|
2005-01-05 16:01:06 +08:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
unsigned long num;
|
|
|
|
int log_block_size = 0;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
|
|
|
fprintf(stderr, "Usage: %s arg\n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
num = parse_num_blocks(argv[1], log_block_size);
|
|
|
|
|
|
|
|
printf("Parsed number: %lu\n", num);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif
|