xref: /llvm-project/lldb/test/API/iohandler/sigint/cat.cpp (revision a4d6de2031ad2f92d399fc8d1b1301229ed8255b)
1*a4d6de20SPavel Labath #include <cstdio>
2*a4d6de20SPavel Labath #include <string>
3*a4d6de20SPavel Labath #include <unistd.h>
4*a4d6de20SPavel Labath 
getline()5*a4d6de20SPavel Labath std::string getline() {
6*a4d6de20SPavel Labath   std::string result;
7*a4d6de20SPavel Labath   while (true) {
8*a4d6de20SPavel Labath     int r;
9*a4d6de20SPavel Labath     char c;
10*a4d6de20SPavel Labath     do
11*a4d6de20SPavel Labath       r = read(fileno(stdin), &c, 1);
12*a4d6de20SPavel Labath     while (r == -1 && errno == EINTR);
13*a4d6de20SPavel Labath     if (r <= 0 || c == '\n')
14*a4d6de20SPavel Labath       return result;
15*a4d6de20SPavel Labath     result += c;
16*a4d6de20SPavel Labath   }
17*a4d6de20SPavel Labath }
1814bd14f9SPavel Labath 
input_copy_loop()1914bd14f9SPavel Labath void input_copy_loop() {
2014bd14f9SPavel Labath   std::string str;
21*a4d6de20SPavel Labath   while (str = getline(), !str.empty())
22*a4d6de20SPavel Labath     printf("read: %s\n", str.c_str());
2314bd14f9SPavel Labath }
2414bd14f9SPavel Labath 
main()2514bd14f9SPavel Labath int main() {
2614bd14f9SPavel Labath   input_copy_loop();
2714bd14f9SPavel Labath   return 0;
2814bd14f9SPavel Labath }
29