tst_test: Add option parsing helpers.
Add two helpers for parsing integers and floats, these are intended to
be used in the test setup to parse options from struct tst_option.
static char *str_threads;
static int threads;
...
static struct tst_option options[] = {
{"t:", &str_threads, "Number of threads"},
...
{NULL, NULL, NULL}
};
static void setup(void)
{
if (tst_parse_int(str_threads, &threads, 1, INT_MAX))
tst_brk(TBROK, "Invalid number of threads '%s'", str_threads);
...
}
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
Reviewed-by: Jan Stancek <jstancek@redhat.com>
diff --git a/lib/tst_test.c b/lib/tst_test.c
index 9eb1393..6c93152 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -443,6 +443,53 @@
}
}
+int tst_parse_int(const char *str, int *val, int min, int max)
+{
+ long rval;
+ char *end;
+
+ if (!str)
+ return 0;
+
+ errno = 0;
+ rval = strtol(str, &end, 10);
+
+ if (str == end || *end != '\0')
+ return EINVAL;
+
+ if (errno)
+ return errno;
+
+ if (rval > (long)max || rval < (long)min)
+ return ERANGE;
+
+ *val = (int)rval;
+ return 0;
+}
+
+int tst_parse_float(const char *str, float *val, float min, float max)
+{
+ double rval;
+ char *end;
+
+ if (!str)
+ return 0;
+
+ errno = 0;
+ rval = strtod(str, &end);
+
+ if (str == end || *end != '\0')
+ return EINVAL;
+
+ if (errno)
+ return errno;
+
+ if (rval > (double)max || rval < (double)min)
+ return ERANGE;
+
+ *val = (float)rval;
+ return 0;
+}
static void do_exit(int ret)
{