comparison lib/xwrap.c @ 952:ce0519f6457c

Add timeout, factoring out common code from sleep.
author Rob Landley <rob@landley.net>
date Wed, 17 Jul 2013 17:22:46 -0500
parents 62d59b8aea34
children caa05719070f
comparison
equal deleted inserted replaced
951:62d59b8aea34 952:ce0519f6457c
109 109
110 void xflush(void) 110 void xflush(void)
111 { 111 {
112 if (fflush(stdout)) perror_exit("write");; 112 if (fflush(stdout)) perror_exit("write");;
113 } 113 }
114
115 // Call xexec with a chunk of optargs, starting at skip. (You can't just
116 // call xexec() directly because toy_init() frees optargs.)
117 void xexec_optargs(int skip)
118 {
119 char **s = toys.optargs;
120
121 toys.optargs = 0;
122 xexec(s+skip);
123 }
124
114 125
115 // Die unless we can exec argv[] (or run builtin command). Note that anything 126 // Die unless we can exec argv[] (or run builtin command). Note that anything
116 // with a path isn't a builtin, so /bin/sh won't match the builtin sh. 127 // with a path isn't a builtin, so /bin/sh won't match the builtin sh.
117 void xexec(char **argv) 128 void xexec(char **argv)
118 { 129 {
466 len = xread(in, buf, 4096); 477 len = xread(in, buf, 4096);
467 if (len<1) break; 478 if (len<1) break;
468 xwrite(out, buf, len); 479 xwrite(out, buf, len);
469 } 480 }
470 } 481 }
482
483 // parse fractional seconds with optional s/m/h/d suffix
484 long xparsetime(char *arg, long units, long *fraction)
485 {
486 double d;
487 long l;
488
489 if (CFG_TOYBOX_FLOAT) d = strtod(arg, &arg);
490 else l = strtoul(arg, &arg, 10);
491
492 // Parse suffix
493 if (*arg) {
494 int ismhd[]={1,60,3600,86400}, i = stridx("smhd", *arg);
495
496 if (i == -1) error_exit("Unknown suffix '%c'", *arg);
497 if (CFG_TOYBOX_FLOAT) d *= ismhd[i];
498 else l *= ismhd[i];
499 }
500
501 if (CFG_TOYBOX_FLOAT) {
502 l = (long)d;
503 if (fraction) *fraction = units*(d-l);
504 } else if (fraction) *fraction = 0;
505
506 return l;
507 }