TetaTricks/code/C/slowprint.c

42 lines
769 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;
int slowdown = 300; /* set a default value */
float delay;
/*
* get the desired slowdown from command-line
*/
if (2 == argc) {
if (1 != sscanf(argv[1], "%d", &slowdown)) {
fprintf(stderr, "%s: bad arg, sorry\n", argv[0]);
exit(1);
}
if (slowdown < 1) {
fprintf(stderr, "%d is invalid\n", slowdown);
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()) )
{
putchar(c);
fflush(stdout);
nanosleep(&ts, NULL);
}
return 0;
}