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