Created
July 9, 2012 23:14
-
-
Save tylor/3079697 to your computer and use it in GitHub Desktop.
Simply code to rate limit your stdout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simply code to rate limit your stdout. | |
* | |
* Compile: | |
* - $ gcc limit.c | |
* Fun uses: | |
* - $ find ~/Sites/thisisourstop/ -name "*.js" -exec cat {} \; | ./a.out 1000 | |
* - $ ./a.out < /dev/random | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
int main(int argc, char**argv){ | |
int c; | |
useconds_t stime=10000; // defaults to 100 Hz | |
if (argc>1) { // Argument is interperted as Hz | |
stime=1000000/atoi(argv[1]); | |
} | |
setvbuf(stdout,NULL,_IONBF,0); | |
while ((c=fgetc(stdin)) != EOF){ | |
fputc(c,stdout); | |
usleep(stime); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment