Refactored getuint() for better error handling and use of strtoul()
[netsniff-ng.git] / src / parser.c
blob554b203ce06761d9b78770b0d0484cd67d16c554
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Subject to the GPL.
6 */
8 #include <string.h>
9 #include <ctype.h>
10 #include <stdint.h>
11 #include <assert.h>
13 #include "parser.h"
14 #include "die.h"
16 char *getuint(char *in, uint32_t *out)
18 char * endptr = NULL;
20 assert(in);
21 assert(out);
23 errno = 0;
25 *out = strtoul(in, &endptr, 0);
27 if ((endptr != NULL && *endptr != '\0') || errno != 0) {
28 panic("Syntax error!\n");
31 return in;
34 char *strtrim_right(register char *p, register char c)
36 register char *end;
37 register int len;
39 len = strlen(p);
40 while (*p && len) {
41 end = p + len - 1;
42 if (c == *end)
43 *end = 0;
44 else
45 break;
46 len = strlen(p);
49 return p;
52 char *strtrim_left(register char *p, register char c)
54 register int len;
56 len = strlen(p);
57 while (*p && len--) {
58 if (c == *p)
59 p++;
60 else
61 break;
64 return p;