Debian Codegraph @

benchmarking/time-output.c

raw symbols SCIP
1 /*
2
3 Pipe through this program to get time information on each line of text.
4
5 Use it like:
6 sudo cowbuilder --update | ./time-output.c [optional: file-to-tee]
7
8 */
9 #include <stdio.h>
10 #include <time.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <termios.h>
15
16 void fix_terminal() {
17 struct termios t;
18
19 if (isatty(1)) {
20 tcgetattr(1, &t);
21 t.c_lflag |= ECHO;
22 tcsetattr(1, TCSANOW, &t);
23 }
24 }
25
26 int main(int argc, char **argv) {
27 time_t start = time(NULL);
28 char b[4096];
29 FILE *f = NULL;
30
31 if (argc == 2) {
32 printf("Output tee to extra file %s\n", argv[1]);
33 f = fopen(argv[1], "w");
34 }
35
36 while (fgets(b, sizeof(b), stdin) != 0) {
37 printf("[%i] %s", (int)difftime(time(NULL), start), b);
38 fflush(stdout);
39 if (f) {
40 fprintf(f, "[%i] %s", (int)difftime(time(NULL), start), b);
41 fflush(f);
42 }
43 }
44
45 if (f) {
46 fclose(f);
47 }
48 fix_terminal();
49 return 0;
50 }