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
|
|
|
*/
|
|
|
|
|
2011-09-19 05:34:37 +08:00
|
|
|
#include "config.h"
|
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':
|
2013-01-15 08:01:25 +08:00
|
|
|
if (log_block_size < 0)
|
|
|
|
num <<= 10;
|
|
|
|
else
|
|
|
|
num >>= log_block_size;
|
2005-01-05 16:01:06 +08:00
|
|
|
break;
|
2008-08-28 11:07:54 +08:00
|
|
|
case 's':
|
2013-01-15 08:01:25 +08:00
|
|
|
if (log_block_size < 0)
|
2013-02-25 13:55:05 +08:00
|
|
|
num <<= 9;
|
2013-01-15 08:01:25 +08:00
|
|
|
else
|
|
|
|
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;
|
|
|
|
|
2013-01-15 08:01:25 +08:00
|
|
|
if (argc != 2 && argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: %s arg [log_block_size]\n", argv[0]);
|
2005-01-05 16:01:06 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-01-15 08:01:25 +08:00
|
|
|
if (argc == 3) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
log_block_size = strtol(argv[2], &p, 0);
|
|
|
|
if (*p) {
|
|
|
|
fprintf(stderr, "Bad log_block_size: %s\n", argv[2]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-05 16:01:06 +08:00
|
|
|
num = parse_num_blocks(argv[1], log_block_size);
|
|
|
|
|
|
|
|
printf("Parsed number: %lu\n", num);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
#endif
|