-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtail.c
86 lines (77 loc) · 2.1 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
#include <stdio.h>
#include <stdlib.h>
#include <bsd/string.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <time.h>
void print_last_n_lines(FILE* stream, int n){
char* line = NULL;
size_t len = 0;
ssize_t read;
int line_count = 0;
int i = 0, j = 0;
char** lines = (char**)malloc(n * sizeof(char*));
while ((read = getline(&line, &len, stream)) != -1) {
int line_len = read + 1;
if (line_count >= n)
free(lines[i]);
lines[i] = (char*)malloc(line_len);
strlcpy(lines[i], line, line_len);
i = (i + 1) % n;
line_count++;
}
if (line)
free(line);
if (line_count < n){
for (j = 0; j < line_count; j++){
printf("%s", lines[j]);
free(lines[j]);
}
} else {
for (j = 0; j < n; j++){
char *_line = lines[(i + j) % n];
printf("%s", _line);
free(_line);
}
}
free(lines);
}
int main(int argc, char *argv[]){
FILE* stream = stdin;
int i = 0, n = 10;
bool follow = false;
struct timespec ts;
char *endptr;
for (i = 1; i < argc; i++){
if(strcmp(argv[i], "-n") == 0){
n = strtol(argv[i + 1], &endptr, 10);
if (*endptr){
fprintf(stderr, "Invalid number of lines: %s\n", argv[i + 1]);
return 1;
}
i++;
} else if( strcmp(argv[i], "-f") == 0){
follow = true;
} else {
stream = fopen(argv[i], "r");
if (stream == NULL){
fprintf(stderr, "Error opening file %s, %s\n", argv[1], strerror(errno));
return 1;
}
}
}
print_last_n_lines(stream, n);
if (follow){
int ret;
char buffer[64];
memset(&ts, 0, sizeof(struct timespec));
ts.tv_nsec = 125000000;
while (true){
nanosleep(&ts, NULL);
while((ret = fread(buffer, 1, sizeof(buffer), stream)) > 0){
fwrite(buffer, 1, ret, stdout);
}
}
}
}