"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 2 | /* |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 3 | * parse_num.c - Parse the number of blocks |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2004,2005 Theodore Ts'o <tytso@mit.edu> |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 6 | * |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 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 | |
| 15 | unsigned 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 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 22 | if (p[0] && p[1]) |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 23 | return 0; |
| 24 | |
| 25 | switch (*p) { /* Using fall-through logic */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 26 | case 'T': case 't': |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 27 | num <<= 10; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 28 | case 'G': case 'g': |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 29 | num <<= 10; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 30 | case 'M': case 'm': |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 31 | num <<= 10; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 32 | case 'K': case 'k': |
| 33 | num >>= log_block_size; |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 34 | break; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 35 | case 's': |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 36 | num >>= 1; |
| 37 | break; |
| 38 | case '\0': |
| 39 | break; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 40 | default: |
Mike Frysinger | d89e629 | 2005-04-24 05:07:59 +0000 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | return num; |
| 44 | } |
| 45 | |
| 46 | #ifdef DEBUG |
| 47 | #include <unistd.h> |
| 48 | #include <stdio.h> |
| 49 | |
| 50 | main(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 |