File: logprn.cpp

package info (click to toggle)
logtools 0.13e%2Bnmu3
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 324 kB
  • sloc: cpp: 1,110; sh: 116; makefile: 78
file content (133 lines) | stat: -rw-r--r-- 2,940 bytes parent folder | download | duplicates (8)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>

#include "logtools.h"

void usage()
{
  fprintf(stderr, "Usage: logprn logfile idle-time[:max-wait] command\n"
  "\nVersion: " VERSION "\n");
  exit(ERR_PARAM);
}

int main(int argc, char **argv)
{
  if(argc != 4)
    usage();

  struct stat sBuf;
  int rc = stat(argv[1], &sBuf);
  if(rc)
  {
    printf("Can't stat \"%s\".\n", argv[1]);
    return ERR_PARAM;
  }

  bool changed = false;
  time_t mtime = sBuf.st_mtime;
  ino_t inode = sBuf.st_ino;
  off_t size = sBuf.st_size;
  off_t print_size = size;

  char *pbuf = strdup(argv[2]);
  strtok(pbuf, ":");
  char *maxBuf = strtok(NULL, ":");
  time_t delay = atoi(pbuf);
  time_t maxWait = 0;
  if(maxBuf)
    maxWait = atoi(maxBuf);
  free(pbuf);
  if(maxWait && maxWait < delay)
    usage();

  if(delay < 1)
    usage();
  time_t last_change = time(NULL);
  time_t first_unwritten_change = 0;

  while(1)
  {
    sleep(1);
    rc = stat(argv[1], &sBuf);
    if(rc)
    {
      // if failed then try again 1 second later in case of link changes etc.
      sleep(1);
      rc = stat(argv[1], &sBuf);
    }
    if(rc)
    {
      fprintf(stderr, "File disappeared or became unreadable.\n");
      return ERR_INPUT;
    }
    if(inode != sBuf.st_ino)
    {
      inode = sBuf.st_ino;
      changed = true;
      mtime = sBuf.st_mtime;
      print_size = 0;
      size = sBuf.st_size;
      last_change = time(NULL);
    }
    else if(mtime != sBuf.st_mtime || size != sBuf.st_size)
    {
      if(size > sBuf.st_size)
        print_size = 0;
      size = sBuf.st_size;
      mtime = sBuf.st_mtime;
      changed = true;
      last_change = time(NULL);
      if(first_unwritten_change == 0)
        first_unwritten_change = last_change;
    }
    time_t now = time(NULL);
    if(changed)
    {
      if((now - last_change) > delay
       || (maxWait && (now - first_unwritten_change ) > maxWait) )
      {
        int fd = open(argv[1], O_RDONLY);
        if(fd == -1)
        {
          fprintf(stderr, "Can't open file \"%s\"\n", argv[1]);
          return ERR_INPUT;
        }
        rc = lseek(fd, print_size, SEEK_SET);
        if(rc == -1)
        {
          fprintf(stderr, "Can't lseek().\n");
        }
        FILE *fp = popen(argv[3], "w");
        if(!fp)
        {
          fprintf(stderr, "Can't run \"%s\"\n", argv[3]);
          return ERR_OUTPUT;
        }
        char buf[4096];
        while( (rc = read(fd, buf, sizeof(buf))) > 0)
        {
          if(int(fwrite(buf, 1, rc, fp)) != rc)
          {
            fprintf(stderr, "Short write to pipe.\n");
            break;
          }
          print_size += rc;
        }

        pclose(fp);
        close(fd);
        changed = false;
        first_unwritten_change = 0;
      }
    }
  }
  return 0; // to make gcc happy
}