New function, strtobytes(), allows k/M/G ISO size modifiers

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2018-01-15 19:42:33 +01:00
parent b5e260b9e0
commit f11ebb0046
2 changed files with 31 additions and 0 deletions
+30
View File
@@ -25,6 +25,7 @@
#include <ctype.h> /* isprint() */
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/sysinfo.h> /* sysinfo() */
#include <lite/lite.h> /* 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)))
+1
View File
@@ -39,6 +39,7 @@ extern char *prognm;
#include <lite/conio.h>
char *progname (char *arg0);
int strtobytes (char *arg);
void do_sleep (unsigned int sec);
long jiffies (void);