From 72756670274dac9562b869761c50c59ed57b7295 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 17 Jul 2013 17:22:46 -0500 Subject: Add timeout, factoring out common code from sleep. --- lib/lib.h | 4 +++- lib/xwrap.c | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/lib.h b/lib/lib.h index 907d96eb..98f4aadd 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -85,7 +85,7 @@ struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); void show_help(void); -// xfuncs.c +// xwrap.c void xstrncpy(char *dest, char *src, size_t size); void xexit(void) noreturn; void *xmalloc(size_t size); @@ -98,6 +98,7 @@ void xprintf(char *format, ...); void xputs(char *s); void xputc(char c); void xflush(void); +void xexec_optargs(int skip); void xexec(char **argv); void xaccess(char *path, int flags); void xunlink(char *path); @@ -120,6 +121,7 @@ void xchdir(char *path); void xmkpath(char *path, int mode); void xsetuid(uid_t uid); char *xreadlink(char *name); +long xparsetime(char *arg, long units, long *fraction); // lib.c void verror_msg(char *msg, int err, va_list va); diff --git a/lib/xwrap.c b/lib/xwrap.c index b0029eef..a0f64fd2 100644 --- a/lib/xwrap.c +++ b/lib/xwrap.c @@ -112,6 +112,17 @@ void xflush(void) if (fflush(stdout)) perror_exit("write");; } +// Call xexec with a chunk of optargs, starting at skip. (You can't just +// call xexec() directly because toy_init() frees optargs.) +void xexec_optargs(int skip) +{ + char **s = toys.optargs; + + toys.optargs = 0; + xexec(s+skip); +} + + // Die unless we can exec argv[] (or run builtin command). Note that anything // with a path isn't a builtin, so /bin/sh won't match the builtin sh. void xexec(char **argv) @@ -468,3 +479,29 @@ void xsendfile(int in, int out) xwrite(out, buf, len); } } + +// parse fractional seconds with optional s/m/h/d suffix +long xparsetime(char *arg, long units, long *fraction) +{ + double d; + long l; + + if (CFG_TOYBOX_FLOAT) d = strtod(arg, &arg); + else l = strtoul(arg, &arg, 10); + + // Parse suffix + if (*arg) { + int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg); + + if (i == -1) error_exit("Unknown suffix '%c'", *arg); + if (CFG_TOYBOX_FLOAT) d *= ismhd[i]; + else l *= ismhd[i]; + } + + if (CFG_TOYBOX_FLOAT) { + l = (long)d; + if (fraction) *fraction = units*(d-l); + } else if (fraction) *fraction = 0; + + return l; +} -- cgit v1.2.3