-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdlineparse.c
77 lines (64 loc) · 1.39 KB
/
cmdlineparse.c
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
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "cmdlineparse.h"
uint64_t ipow(unsigned int a, unsigned int b)
{
uint64_t ans = 1;
while(b--)
ans *= (uint64_t)a;
return ans;
}
/* Get a positive integer out of the current optarg (option @c) or exit */
uint64_t parse_num(int c)
{
char *p;
long int n;
unsigned long int mult;
errno = 0;
n = strtol(optarg, &p, 10);
if(errno != 0 || n < 0) {
fprintf(stderr, "Error -%c requires a positive integer\n", c);
exit(EXIT_FAILURE);
}
switch(*p) {
case '\0':
return n;
case 'K':
mult = ipow(10, 3); break;
case 'k':
mult = ipow(2, 10); break;
case 'M':
mult = ipow(10, 6); break;
case 'm':
mult = ipow(2, 20); break;
case 'G':
mult = ipow(10, 9); break;
case 'g':
mult = ipow(2, 30); break;
break;
default:
fprintf(stderr,
"Invalid multiplier character found for -%c: %c, "
"must be K/k/M/m/G/g\n", c, *p);
exit(EXIT_FAILURE);
}
if(*(p + 1) == '\0' || (*(p + 1) == 'b' && *(p + 2) == '\0'))
return n * mult;
fprintf(stderr, "Invalid character found after multiplier for"
" -%c: %c\n", c, *(p+1));
exit(EXIT_FAILURE);
}
double parse_dbl(int c)
{
char *p;
double d;
errno = 0;
d = strtod(optarg, &p);
if(errno != 0 || d < 0) {
fprintf(stderr, "Error -%c requires a (possibly float) num of seconds\n", c);
exit(EXIT_FAILURE);
}
return d;
}