-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhead.c
58 lines (44 loc) · 1.06 KB
/
head.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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#define OSNLOG_BUFFSZ (20)
#define TEST_FILE "osn.debug"
#define MIN(x,y) (x)<(y)?(x):(y)
int main()
{
int fd;
off_t start_pos, end_pos, pos;
int read_bytes, filesz, idx;
int read_end = 0;
char buf[OSNLOG_BUFFSZ+1], *char_tmp;
int i = 3;
fd = open("test", O_RDONLY);
if (fd == -1)
return -1;
end_pos = lseek(fd, 0, SEEK_END);
if (end_pos <= 0) /* blank file or pipe/FIFP/socket */
goto out;
start_pos = lseek(fd, 0, SEEK_SET);
if (start_pos == -1) /* open pipe, FIFO or socket, do nothing */
goto out;
pos = start_pos;
while (1) {
read_bytes = MIN(end_pos - pos, OSNLOG_BUFFSZ);
if (read_bytes <= 0)
break;
memset(buf, 0, OSNLOG_BUFFSZ+1);
read(fd, buf, read_bytes); /* note: SEEK_CUR's position changed */
/* we need get unbroken lines */
char_tmp = strrchr(buf, '\n');
if (char_tmp)
*char_tmp = 0;
else
continue;
printf("%s\n", buf);
pos = lseek(fd, 0, SEEK_CUR);
idx = read_bytes - (char_tmp - buf) - 1;
pos = lseek(fd, pos-idx, SEEK_SET);
}
out:
close(fd);
}