File: time-output.c

package info (click to toggle)
cowdancer 0.73
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 752 kB
  • ctags: 269
  • sloc: ansic: 3,822; sh: 481; makefile: 191; cpp: 6
file content (58 lines) | stat: -rwxr-xr-x 989 bytes parent folder | download | duplicates (4)
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
/*BINFMTC:

  Pipe through this program to get time information on each line of text.

    Use it like:
    sudo cowbuilder --update  | ./time-output.c [optional: file-to-tee]

 */
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>

void fix_terminal() 
{
  struct termios t; 

  if (isatty(1)) 
    {
      tcgetattr(1, &t); 
      t.c_lflag |= ECHO;
      tcsetattr(1, TCSANOW, &t); 
    }
}

int main(int argc, char** argv)
{
  time_t start = time(NULL);
  char b[4096];
  FILE* f = NULL;  

  if (argc == 2) 
    {
      printf("Output tee to extra file %s\n", argv[1]);
      f = fopen(argv[1], "w");
    }
  
  while(fgets(b,sizeof(b),stdin)!=0)
    {
      printf("[%i] %s", 
	     (int)difftime(time(NULL), start), 
	     b);
      fflush(stdout);
      if (f) 
	{
	  fprintf(f, "[%i] %s", 
		  (int)difftime(time(NULL), start), 
		  b);
	  fflush(f);
	}
    }

  if (f) fclose(f);
  fix_terminal();
  return 0;
}