From f11ebb00466c139e2b29779946ff138e327c0f0f Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 15 Jan 2018 18:30:52 +0100 Subject: [PATCH] New function, strtobytes(), allows k/M/G ISO size modifiers Signed-off-by: Joachim Nilsson --- src/util.c | 30 ++++++++++++++++++++++++++++++ src/util.h | 1 + 2 files changed, 31 insertions(+) diff --git a/src/util.c b/src/util.c index 460d5f4a..2f4735b8 100644 --- a/src/util.c +++ b/src/util.c @@ -25,6 +25,7 @@ #include /* isprint() */ #include #include +#include #include #include /* sysinfo() */ #include /* strlcat() */ @@ -45,6 +46,35 @@ char *progname(char *arg0) return prognm; } +int strtobytes(char *arg) +{ + int mod = 0, bytes; + size_t pos; + + if (!arg) + return -1; + + pos = strspn(arg, "0123456789"); + if (arg[pos] != 0) { + if (arg[pos] == 'G') + mod = 3; + else if (arg[pos] == 'M') + mod = 2; + else if (arg[pos] == 'k') + mod = 1; + else + return -1; + + arg[pos] = 0; + } + + bytes = atoi(arg); + while (mod--) + bytes *= 1000; + + return bytes; +} + void do_sleep(unsigned int sec) { while ((sec = sleep(sec))) diff --git a/src/util.h b/src/util.h index 952f410e..26659b33 100644 --- a/src/util.h +++ b/src/util.h @@ -39,6 +39,7 @@ extern char *prognm; #include char *progname (char *arg0); +int strtobytes (char *arg); void do_sleep (unsigned int sec); long jiffies (void);