summaryrefslogtreecommitdiff
path: root/release/src/router/busybox/e2fsprogs/old_e2fsprogs/e2p/parse_num.c
blob: 6db076f9cd34440b9bd10e72d526a797b94518c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* vi: set sw=4 ts=4: */
/*
 * parse_num.c		- Parse the number of blocks
 *
 * Copyright (C) 2004,2005  Theodore Ts'o <tytso@mit.edu>
 *
 * 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);

	if (p[0] && p[1])
		return 0;

	switch (*p) {		/* Using fall-through logic */
	case 'T': case 't':
		num <<= 10;
	case 'G': case 'g':
		num <<= 10;
	case 'M': case 'm':
		num <<= 10;
	case 'K': case 'k':
		num >>= log_block_size;
		break;
	case 's':
		num >>= 1;
		break;
	case '\0':
		break;
	default:
		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