aboutsummaryrefslogtreecommitdiff
path: root/lib/xwrap.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2018-10-21 21:36:51 -0500
committerRob Landley <rob@landley.net>2018-10-21 21:36:51 -0500
commit07a3b9192001369c7bf74cd4f096dc49703b24e9 (patch)
treecfb6f13f8a275f0ae21d6b577baa69d8a05f9ce7 /lib/xwrap.c
parent3e0b077ec236aa26bfeb290f3cd7973ec3fbcb7d (diff)
downloadtoybox-07a3b9192001369c7bf74cd4f096dc49703b24e9.tar.gz
Add % to lib/args.c (long time in milliseconds), add xmillitime(), redo
xparsetime() not to need floating point, adjust callers.
Diffstat (limited to 'lib/xwrap.c')
-rw-r--r--lib/xwrap.c47
1 files changed, 28 insertions, 19 deletions
diff --git a/lib/xwrap.c b/lib/xwrap.c
index 2c3b6041..fba262b9 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -789,36 +789,45 @@ double xstrtod(char *s)
}
// parse fractional seconds with optional s/m/h/d suffix
-long xparsetime(char *arg, long units, long *fraction)
+long xparsetime(char *arg, long zeroes, long *fraction)
{
- double d;
- long l;
+ long l, fr;
char *end;
- if (*arg != '.' && !isdigit(*arg)) error_exit("bad %s", arg);
- if (CFG_TOYBOX_FLOAT) d = strtod(arg, &end);
- else l = strtoul(arg, &end, 10);
-
- if (end == arg) error_exit("Not a number '%s'", arg);
- arg = end;
+ if (*arg != '.' && !isdigit(*arg)) error_exit("Not a number '%s'", arg);
+ l = strtoul(arg, &end, 10);
+ fr = 0;
+ if (*end == '.') {
+ end++;
+ while (zeroes--) {
+ fr *= 10;
+ if (isdigit(*end)) fr += *end++-'0';
+ }
+ }
+ if (fraction) *fraction = fr;
// Parse suffix
- if (*arg) {
- int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg);
+ if (*end) {
+ int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *end);
- if (i == -1 || *(arg+1)) error_exit("Unknown suffix '%s'", arg);
- if (CFG_TOYBOX_FLOAT) d *= ismhd[i];
- else l *= ismhd[i];
+ if (i == -1 || *(end+1)) error_exit("Unknown suffix '%s'", end);
+ l *= ismhd[i];
}
- if (CFG_TOYBOX_FLOAT) {
- l = (long)d;
- if (fraction) *fraction = units*(d-l);
- } else if (fraction) *fraction = 0;
-
return l;
}
+long long xparsemillitime(char *arg)
+{
+ long l, ll;
+
+ l = xparsetime(arg, 3, &ll);
+
+ return (l*1000LL)+ll;
+}
+
+
+
// Compile a regular expression into a regex_t
void xregcomp(regex_t *preg, char *regex, int cflags)
{