TetaTricks/code/C/slowprint.c

43 lines
810 B
C
Raw Normal View History

2023-02-11 22:19:00 +11:00
#include <stdio.h>
2024-01-12 13:43:49 +11:00
#include <stdlib.h>
2023-02-11 22:19:00 +11:00
#include <time.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
2024-01-12 13:43:49 +11:00
int c, sec, nano;
struct timespec ts;
2024-03-11 12:20:47 +11:00
int slowdown = 300; /* set a default value */
2024-01-12 13:43:49 +11:00
float delay;
/*
* get the desired slowdown from command-line
*/
if (2 == argc) {
if (1 != sscanf(argv[1], "%d", &slowdown)) {
2024-03-11 12:20:47 +11:00
fprintf(stderr, "%s: bad arg, sorry\n",
argv[0]);
2024-01-12 13:43:49 +11:00
exit(1);
}
if (slowdown < 1) {
2024-03-11 12:20:47 +11:00
fprintf(stderr, "%d is invalid\n",
slowdown);
2024-01-12 13:43:49 +11:00
exit(1);
}
}
/* convert the slowdown value to delay */
delay = 1.0 / (float)slowdown;
sec = (int)delay;
nano = (int)(1000*1000*1000*(delay - (int)delay));
2023-02-11 22:19:00 +11:00
2024-01-12 13:43:49 +11:00
ts.tv_sec = sec;
ts.tv_nsec = nano;
2023-02-11 22:19:00 +11:00
while ( EOF != (c=getchar()) )
{
2024-07-08 04:54:33 +11:00
putchar(c), fflush(stdout);
2023-02-11 22:19:00 +11:00
nanosleep(&ts, NULL);
}
return 0;
}