1 | #include <cstdio> |
---|---|
2 | #include <string> |
3 | #include <unistd.h> |
4 | |
5 | std::string getline() { |
6 | std::string result; |
7 | while (true) { |
8 | int r; |
9 | char c; |
10 | do |
11 | r = read(fd: fileno(stdin), buf: &c, nbytes: 1); |
12 | while (r == -1 && errno == EINTR); |
13 | if (r <= 0 || c == '\n') |
14 | return result; |
15 | result += c; |
16 | } |
17 | } |
18 | |
19 | void input_copy_loop() { |
20 | std::string str; |
21 | while (str = getline(), !str.empty()) |
22 | printf(format: "read: %s\n", str.c_str()); |
23 | } |
24 | |
25 | int main() { |
26 | input_copy_loop(); |
27 | return 0; |
28 | } |
29 |