155f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o/*
2efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o * parse_num.c		- Parse the number of blocks
355f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o *
455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o * Copyright (C) 2004,2005  Theodore Ts'o <tytso@mit.edu>
5efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o *
6543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * %Begin-Header%
7543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * This file may be redistributed under the terms of the GNU Library
8543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * General Public License, version 2.
9543547a52a20cb7e69d74921b2f691078fd55d83Theodore Ts'o * %End-Header%
1055f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o */
1155f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
12d1154eb460efe588eaed3d439c1caaca149fa362Theodore Ts'o#include "config.h"
1355f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o#include "e2p.h"
1455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
1555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o#include <stdlib.h>
1655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
17b4e0163d53613ae47f79c661f7c1e2009a271c79Valerie Aurora Hensonunsigned long long parse_num_blocks2(const char *arg, int log_block_size)
1855f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o{
1955f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	char *p;
2055f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	unsigned long long num;
2155f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
2255f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	num = strtoull(arg, &p, 0);
2355f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
24efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	if (p[0] && p[1])
2555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		return 0;
2655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
2755f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	switch (*p) {		/* Using fall-through logic */
28efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	case 'T': case 't':
2955f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		num <<= 10;
309e30fb23ef85d6b2a58527048cc9208405a38299Eric Sandeen		/* fallthrough */
31efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	case 'G': case 'g':
3255f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		num <<= 10;
339e30fb23ef85d6b2a58527048cc9208405a38299Eric Sandeen		/* fallthrough */
34efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	case 'M': case 'm':
3555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		num <<= 10;
369e30fb23ef85d6b2a58527048cc9208405a38299Eric Sandeen		/* fallthrough */
37efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	case 'K': case 'k':
3835d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		if (log_block_size < 0)
3935d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o			num <<= 10;
4035d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		else
4135d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o			num >>= log_block_size;
4255f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		break;
43efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	case 's':
4435d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		if (log_block_size < 0)
459e20cf223ad6e44536f7e3116413cc8a445618c7Jan Kara			num <<= 9;
4635d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		else
4735d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o			num >>= (1+log_block_size);
4855f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		break;
4955f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	case '\0':
5055f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		break;
51efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o	default:
5255f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		return 0;
5355f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	}
5455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	return num;
5555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o}
5655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
57b4e0163d53613ae47f79c661f7c1e2009a271c79Valerie Aurora Hensonunsigned long parse_num_blocks(const char *arg, int log_block_size)
58b4e0163d53613ae47f79c661f7c1e2009a271c79Valerie Aurora Henson{
59b4e0163d53613ae47f79c661f7c1e2009a271c79Valerie Aurora Henson	return parse_num_blocks2(arg, log_block_size);
60b4e0163d53613ae47f79c661f7c1e2009a271c79Valerie Aurora Henson}
61b4e0163d53613ae47f79c661f7c1e2009a271c79Valerie Aurora Henson
6255f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o#ifdef DEBUG
6355f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o#include <unistd.h>
6455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o#include <stdio.h>
6555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
6655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'omain(int argc, char **argv)
6755f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o{
6855f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	unsigned long num;
6955f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	int log_block_size = 0;
7055f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
7135d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o	if (argc != 2 && argc != 3) {
7235d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		fprintf(stderr, "Usage: %s arg [log_block_size]\n", argv[0]);
7355f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o		exit(1);
7455f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	}
7555f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
7635d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o	if (argc == 3) {
7735d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		char *p;
7835d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o
7935d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		log_block_size = strtol(argv[2], &p, 0);
8035d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		if (*p) {
8135d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o			fprintf(stderr, "Bad log_block_size: %s\n", argv[2]);
8235d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o			exit(1);
8335d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o		}
8435d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o	}
8535d886b67947dcb802da09306b6a85f1e9f99865Theodore Ts'o
8655f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	num = parse_num_blocks(argv[1], log_block_size);
8755f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o
8855f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	printf("Parsed number: %lu\n", num);
8955f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o	exit(0);
9055f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o}
9155f4cbd96e0029f0ff67c4913192d87bf52fd149Theodore Ts'o#endif
92