-
Notifications
You must be signed in to change notification settings - Fork 1
/
tail.c
87 lines (78 loc) · 2 KB
/
tail.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "sleep.h"
/* Open a file, seek at the end, and store in '*len' the file length */
static FILE *vi_openAtEnd(char *filename, long *len)
{
FILE *fp = NULL;
if ((fp = fopen(filename, "rb")) == NULL) goto err; /* open */
if ((fseek(fp, 0, SEEK_END)) == -1) goto err; /* seek at end */
if ((*len = ftell(fp)) == -1) goto err; /* read the file length */
return fp;
err:
if (fp != NULL) fclose(fp);
return NULL;
}
/* Output 'len' bytes of file 'fp' starting from 'offset'.
* The function returns 0 on success, -1 on error. */
#define TAILOUT_BUFLEN 1024
static int vi_tailOutput(FILE *fp, long offset, long len)
{
char buf[TAILOUT_BUFLEN];
if (fseek(fp, offset, SEEK_SET) == -1) return -1;
while(len) {
unsigned int min = (len > TAILOUT_BUFLEN) ? TAILOUT_BUFLEN : len;
if (fread(buf, 1, min, fp) != min) return -1;
fwrite(buf, 1, min, stdout);
fflush(stdout);
len -= min;
}
return 0;
}
/* An interation for the 'tail -f' simulation. Open the
* file at every iteration in order to continue to work
* when files are rotated. */
static void vi_tailIteration(char *filename, long *len)
{
long newlen, datalen;
FILE *fp = NULL;
fp = vi_openAtEnd(filename, &newlen);
if (fp != NULL) {
if (*len == -1) {
/* Initialization */
*len = newlen;
} else if (newlen < *len) {
/* Shorter file condition */
*len = 0; /* the next iteration will read
the new data */
} else if (newlen > *len) {
/* Data ready condition */
datalen = newlen - *len;
if (vi_tailOutput(fp, *len, datalen) != -1)
*len = newlen;
}
}
if (fp != NULL) fclose(fp);
}
void vi_tail(int filec, char **filev)
{
long *len;
int i;
if (filec <= 0) {
fprintf(stderr, "No files specified in tail-mode\n");
exit(1);
}
len = malloc(filec);
if (!len) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
for (i = 0; i < filec; i++)
len[i] = -1;
while(1) {
for (i = 0; i < filec; i++)
vi_tailIteration(filev[i], &len[i]);
vi_sleep(1);
}
}