-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytail.c
61 lines (52 loc) · 1.09 KB
/
mytail.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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
void usage(const char *str);
int main(int ac, char *av[]) {
int fd;
int nlines;
int cnt = 0;
char buf[BUFSIZ];
int offset = -BUFSIZ;
int rlen;
int total = 0;
int curp = 0;
/*
while (ac-- > 0) {
printf("%s\n", *av++);
}
*/
if (*++av && av[0][0] == '-') {
if (!sscanf(*av, "-%d\0", &nlines))
usage("invalid args");
} else {
nlines = 10;
}
printf("file name is %s\n", *++av);
fd = open(*av, O_RDONLY);
if (fd == -1) {
perror(*av);
exit(2);
}
lseek(fd, offset, SEEK_END);
/*
while (read(fd, buf, BUFSIZ)) {
printf("%s", buf);
}
*/
rlen = read(fd, buf, BUFSIZ);
printf("%d %c %cheh\n", rlen, buf[BUFSIZ-2], buf[BUFSIZ-1]);
while (rlen-- > 0) {
if (buf[curp++] == '\n') {
total++;
}
}
printf("%s", buf);
printf("%d %d\n", total, nlines);
return 0;
}
void usage(const char *str) {
printf("%s\n", str);
exit(1);
}