/* ratelimit.c by Michael Thorpe 2020-08-06 */ #include #include #include #include #include #define BUF_SIZE 2048 /* (BUF_SIZE+1)*1000000-1 must fit in a signed long */ #undef BUF_SIZE #define BUF_SIZE 1024 /* This can yield better packetization on ethernet */ char buf[BUF_SIZE]; int main(int argc,char **argv) { struct timeval a,b; int i,r=0; size_t k; if(argc==2) r=atoi(argv[1]); if(argc != 2 || r<=0) { fprintf(stderr,"usage: ratelimit \n"); return(1); } if(gettimeofday(&a,0)) { perror("gettimeofday"); return(-1); } do { k=read(0,buf,BUF_SIZE); if(k==0) return(0); if(k==-1) { perror("read"); return(-1); } if(0>=write(1,buf,k)) { perror("write"); return(-1); } if(gettimeofday(&b,0)) { perror("gettimeofday"); return(-1); } a.tv_usec+=1000000*k/r; i=a.tv_usec/1000000; a.tv_sec+=i; a.tv_usec-=1000000*i; if(a.tv_sec