1/* vi: set sw=4 ts=4: */ 2/* 3 * parse_num.c - Parse the number of blocks 4 * 5 * Copyright (C) 2004,2005 Theodore Ts'o <tytso@mit.edu> 6 * 7 * This file can be redistributed under the terms of the GNU Library General 8 * Public License 9 */ 10 11#include "e2p.h" 12 13#include <stdlib.h> 14 15unsigned long parse_num_blocks(const char *arg, int log_block_size) 16{ 17 char *p; 18 unsigned long long num; 19 20 num = strtoull(arg, &p, 0); 21 22 if (p[0] && p[1]) 23 return 0; 24 25 switch (*p) { /* Using fall-through logic */ 26 case 'T': case 't': 27 num <<= 10; 28 case 'G': case 'g': 29 num <<= 10; 30 case 'M': case 'm': 31 num <<= 10; 32 case 'K': case 'k': 33 num >>= log_block_size; 34 break; 35 case 's': 36 num >>= 1; 37 break; 38 case '\0': 39 break; 40 default: 41 return 0; 42 } 43 return num; 44} 45 46#ifdef DEBUG 47#include <unistd.h> 48#include <stdio.h> 49 50main(int argc, char **argv) 51{ 52 unsigned long num; 53 int log_block_size = 0; 54 55 if (argc != 2) { 56 fprintf(stderr, "Usage: %s arg\n", argv[0]); 57 exit(1); 58 } 59 60 num = parse_num_blocks(argv[1], log_block_size); 61 62 printf("Parsed number: %lu\n", num); 63 exit(0); 64} 65#endif 66