-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtailf.cpp
More file actions
120 lines (88 loc) · 2.71 KB
/
tailf.cpp
File metadata and controls
120 lines (88 loc) · 2.71 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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// This program approximates the functionality of tail -f using inotify.
// I left in the commented-out garbage because it's somewhat interesting.
#include <cassert>
#include <cstdio>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <poll.h>
#include <sys/inotify.h>
#include <unistd.h>
int main(int argc, char **argv) {
assert(argc == 2);
// std::ifstream stream(argv[1]);
// for (;;) {
// char ch = '\0';
// if (stream >> ch)
// std::cout << '\'' << ch << "' (" << static_cast<int>(ch) << ")\n";
// }
int fd = open(argv[1], O_RDONLY);
assert(fd != -1);
ssize_t status;
int ifd = inotify_init();
assert(ifd != -1);
inotify_add_watch(ifd, argv[1], IN_MODIFY);
pollfd pfd {.fd = ifd, .events = POLLIN, .revents = 0};
inotify_event events[256];
while (0 <= (status = poll(&pfd, 1, -1))) {
std::cout << "status[" << status << "] revents[" << pfd.revents << "]\n";
if (0 < status) {
ssize_t rstatus = read(ifd, events, sizeof(events));
char buf[256] {};
do {
rstatus = read(fd, buf, sizeof(buf));
assert(rstatus != -1);
for (ssize_t i = 0; i < rstatus; ++i) {
char ch = buf[i];
std::cout << '\'' << ch << "' (" << static_cast<int>(ch) << ')' << std::endl;
}
// else
// std::cout << "!!!" << std::endl;
} while (0 < rstatus);
}
}
std::cout << "Done.\n";
// pollfd pfd {.fd = fd, .events = POLLIN, .revents = 0};
// std::cout << std::endl << std::endl << "\e[31mpfd.fd[" << pfd.fd << "]\e[39m" << std::endl;
// while (0 <= (status = poll(&pfd, 1, -1))) {
// // std::cout << "status[" << status << "]" << std::endl;
// if (0 < status) {
// char ch;
// ssize_t rstatus = read(fd, &ch, 1);
// assert(rstatus != -1);
// if (0 < rstatus)
// std::cout << '\'' << ch << "' (" << static_cast<int>(ch) << ')' << std::endl;
// // else
// // std::cout << "!!!" << std::endl;
// }
// }
// fd_set fds;
// FD_ZERO(&fds);
// FD_SET(fd, &fds);
// timeval tv;
// tv.tv_sec = 10;
// tv.tv_usec = 0;
// while (0 <= (status = select(fd + 1, &fds, nullptr, nullptr, &tv))) {
// FD_ZERO(&fds);
// FD_SET(fd, &fds);
// tv.tv_sec = 10;
// tv.tv_usec = 0;
// std::cout << "status[" << status << "] sec[" << tv.tv_sec << "] usec[" << tv.tv_usec << "]\n";
// if (0 < status) {
// char ch;
// ssize_t rstatus = read(fd, &ch, 1);
// assert(rstatus != -1);
// if (0 < rstatus)
// std::cout << '\'' << ch << "' (" << static_cast<int>(ch) << ")\n";
// }
// }
// std::cout << "Done.\n";
// for (;;) {
// char ch;
// ssize_t status = read(fd, &ch, 1);
// assert(status != -1);
// if (0 < status)
// std::cout << '\'' << ch << "' (" << static_cast<int>(ch) << ")\n";
// // Should be using select()
// }
}