-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtail.c
More file actions
54 lines (43 loc) · 1.06 KB
/
tail.c
File metadata and controls
54 lines (43 loc) · 1.06 KB
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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define BUFFER_SIZE 8192
static char buffer[BUFFER_SIZE];
static char line_starts[1024];
int main(int argc, char **argv) {
int fd;
struct stat st;
ssize_t bytes_read;
off_t file_size, pos;
int newlines = 0, lines_to_show = 10;
char *p;
if (argc > 1) {
fd = open(argv[1], O_RDONLY);
if (fd == -1) return 1;
} else {
fd = 0;
}
if (fstat(fd, &st) == -1) {
if (fd != 0) close(fd);
return 1;
}
file_size = st.st_size;
if (file_size == 0) {
if (fd != 0) close(fd);
return 0;
}
pos = file_size - BUFFER_SIZE;
if (pos < 0) pos = 0;
lseek(fd, pos, SEEK_SET);
bytes_read = read(fd, buffer, BUFFER_SIZE);
p = buffer + bytes_read - 1;
while (p >= buffer && newlines < lines_to_show) {
if (*p == '\n') newlines++;
p--;
}
p += 2;
write(1, p, buffer + bytes_read - p);
if (fd != 0) close(fd);
return 0;
}