btrfs-progs: check: introduce traversal function for fsck
[platform/upstream/btrfs-progs.git] / utils-lib.c
1 #include "kerncompat.h"
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <limits.h>
5
6 #if BTRFS_FLAT_INCLUDES
7 #else
8 #endif /* BTRFS_FLAT_INCLUDES */
9
10 /*
11  * This function should be only used when parsing command arg, it won't return
12  * error to its caller and rather exit directly just like usage().
13  */
14 u64 arg_strtou64(const char *str)
15 {
16         u64 value;
17         char *ptr_parse_end = NULL;
18
19         value = strtoull(str, &ptr_parse_end, 0);
20         if (ptr_parse_end && *ptr_parse_end != '\0') {
21                 fprintf(stderr, "ERROR: %s is not a valid numeric value.\n",
22                         str);
23                 exit(1);
24         }
25
26         /*
27          * if we pass a negative number to strtoull, it will return an
28          * unexpected number to us, so let's do the check ourselves.
29          */
30         if (str[0] == '-') {
31                 fprintf(stderr, "ERROR: %s: negative value is invalid.\n",
32                         str);
33                 exit(1);
34         }
35         if (value == ULLONG_MAX) {
36                 fprintf(stderr, "ERROR: %s is too large.\n", str);
37                 exit(1);
38         }
39         return value;
40 }