throttle.c (562B)
1 #include <u.h> 2 #include <libc.h> 3 4 void 5 usage(void) 6 { 7 fprint(2, "usage: %s [-t millisec] [-b bytes]\n", argv0); 8 exits("usage"); 9 } 10 11 void 12 main(int argc, char **argv) 13 { 14 long b = 44 * 4; 15 long t = 1; 16 long T; 17 char *buf; 18 ARGBEGIN{ 19 case 't': 20 t = strtol(EARGF(usage()), nil, 10); 21 break; 22 case 'b': 23 b = strtol(EARGF(usage()), nil, 10); 24 break; 25 default: 26 usage(); 27 }ARGEND 28 29 buf = sbrk(b); 30 long n; 31 T = nsec() / 1000000; 32 while ((n = read(0, buf, b)) > 0) { 33 write(1, buf, n); 34 T += t; 35 sleep(T - (nsec()/1000000)); 36 } 37 if (n != 0) fprint(2, "%r\n"); 38 }