busybox/e2fsprogs/e2p/parse_num.c

65 lines
1.0 KiB
C
Raw Normal View History

2005-04-24 10:37:59 +05:30
/*
2006-01-25 05:38:53 +05:30
* parse_num.c - Parse the number of blocks
2005-04-24 10:37:59 +05:30
*
* Copyright (C) 2004,2005 Theodore Ts'o <tytso@mit.edu>
2006-01-25 05:38:53 +05:30
*
2005-04-24 10:37:59 +05:30
* This file can be redistributed under the terms of the GNU Library General
* Public License
*/
#include "e2p.h"
#include <stdlib.h>
unsigned long parse_num_blocks(const char *arg, int log_block_size)
{
char *p;
unsigned long long num;
num = strtoull(arg, &p, 0);
2006-01-25 05:38:53 +05:30
if (p[0] && p[1])
2005-04-24 10:37:59 +05:30
return 0;
switch (*p) { /* Using fall-through logic */
2006-01-25 05:38:53 +05:30
case 'T': case 't':
2005-04-24 10:37:59 +05:30
num <<= 10;
2006-01-25 05:38:53 +05:30
case 'G': case 'g':
2005-04-24 10:37:59 +05:30
num <<= 10;
2006-01-25 05:38:53 +05:30
case 'M': case 'm':
2005-04-24 10:37:59 +05:30
num <<= 10;
2006-01-25 05:38:53 +05:30
case 'K': case 'k':
num >>= log_block_size;
2005-04-24 10:37:59 +05:30
break;
2006-01-25 05:38:53 +05:30
case 's':
2005-04-24 10:37:59 +05:30
num >>= 1;
break;
case '\0':
break;
2006-01-25 05:38:53 +05:30
default:
2005-04-24 10:37:59 +05:30
return 0;
}
return num;
}
#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