#include <errno.h>
#include <stdio.h>
#include <string.h>
+#include <sys/time.h>
+#include <time.h>
#include <stdlib.h>
#include <sys/ioctl.h>
}
return v;
}
+
+static void gettod(struct timeval *now_r) {
+ int r;
+ r= gettimeofday(now_r,0);
+ if (r) { perror("gettimeofday failed"); exit(127); }
+}
+
+void usleep_gettod(unsigned long us) {
+ struct timeval start, end, now;
+ ldiv_t ld;
+
+ gettod(&start);
+
+ ld= ldiv(start.tv_usec + us, 1000000);
+ end.tv_sec= start.tv_sec + ld.quot;
+ end.tv_usec= ld.rem;
+
+ do {
+ gettod(&now);
+ } while (timercmp(&now,&end,<));
+}
#define LIB_H
void doioctl(int ioctlnum, void *vp, unsigned long vpv);
+void usleep_gettod(unsigned long us);
unsigned long number(const char *a);
extern int fd;
" t<value> PPWDATA value\n"
" w<value> usleep(value)\n"
"options:\n"
+ " n<value> do whole list n times\n"
" m<value> do every item m times\n");
}
int main(int argc, const char *const *argv) {
- int ii, io=0, m=1;
+ int ii, io=0, m=1, n=-1, loops_count, loops_n, loops_incr;
+ unsigned long ul;
unsigned char v;
const char *a;
argc--;
argv++;
- if (argc>=1 && (a=argv[0]) && a[0]=='m') {
- m= number(a+1);
+ while (argc>=1 && (a=argv[0])) {
+ switch (a[0]) {
+ case 'm': m= number(a+1); break;
+ case 'n': n= number(a+1); break;
+ default: goto loop_break_options;
+ }
argc--;
argv++;
}
+loop_break_options:
if (argc<1) badusage();
- for (ii=0; ; ii++, ii%=(argc*m)) {
+
+ if (n==-1) {
+ loops_n= 1;
+ loops_incr= 0;
+ } else {
+ loops_n= n * argc * m;
+ loops_incr= 1;
+ }
+ for (ii=0, loops_count=0;
+ loops_count < loops_n;
+ ii++, ii%=(argc*m), loops_count += loops_incr) {
a= argv[ii/m];
switch (a[0]) {
case 'k': io= PPWCONTROL; break;
case 'w': io= -1; break;
default: badusage();
}
- v= number(a+1);
+ ul= number(a+1);
if (io==-1) {
- usleep(v);
+ usleep_gettod(ul);
} else {
+ v= ul;
doioctl(io, &v, v);
}
}
+ return 0;
}