-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy paththrottle.c
61 lines (52 loc) · 1.02 KB
/
throttle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "delayms.h"
void printUsage(FILE *stream, const char *argv0);
int main(int argc, char *argv[])
{
int ms = 50;
int bytes = 1;
FILE *fp = stdin;
if (argc >= 5) {
printUsage(stderr, argv[0]);
return 2;
}
if (argc >= 2) {
if (strcmp(argv[1], "--help") == 0) {
printUsage(stdout, argv[0]);
return 0;
} else {
ms = atoi(argv[1]);
}
}
if (argc >= 3) {
bytes = atoi(argv[2]);
}
if (argc == 4) {
fp = fopen(argv[3], "r");
if (!fp) {
fprintf(stderr, "%s: error opening '%s': %s\n", argv[0], argv[3], strerror(errno));
return 1;
}
}
while (1) {
int i; for (i=0; i<bytes; ++i) {
int ch = fgetc(fp);
if (feof(fp)) {
break;
} else {
putchar(ch);
}
}
fflush(stdout);
delayms(ms);
}
if (fp != stdin) fclose(fp);
return 0;
}
void printUsage(FILE *stream, const char *argv0)
{
fprintf(stream, "Usage: %s [milliseconds between steps] [bytes to output per step] [filename]\n", argv0);
}