15793c849SAlex Brachet //===-- FDReader.cpp ------------------------------------------------------===// 25793c849SAlex Brachet // 35793c849SAlex Brachet // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 45793c849SAlex Brachet // See https://llvm.org/LICENSE.txt for license information. 55793c849SAlex Brachet // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65793c849SAlex Brachet // 75793c849SAlex Brachet //===----------------------------------------------------------------------===// 85793c849SAlex Brachet 95793c849SAlex Brachet #include "FDReader.h" 105793c849SAlex Brachet #include <cassert> 115793c849SAlex Brachet #include <cstring> 12*8a4ee355SMichael Jones #include <iostream> 135793c849SAlex Brachet #include <unistd.h> 145793c849SAlex Brachet 155793c849SAlex Brachet namespace __llvm_libc { 165793c849SAlex Brachet namespace testutils { 175793c849SAlex Brachet 185793c849SAlex Brachet FDReader::FDReader() { 19*8a4ee355SMichael Jones if (::pipe(pipefd)) { 20*8a4ee355SMichael Jones std::cerr << "pipe(2) failed"; 21*8a4ee355SMichael Jones abort(); 22*8a4ee355SMichael Jones } 235793c849SAlex Brachet } 245793c849SAlex Brachet 255793c849SAlex Brachet FDReader::~FDReader() { 265793c849SAlex Brachet ::close(pipefd[0]); 275793c849SAlex Brachet ::close(pipefd[1]); 285793c849SAlex Brachet } 295793c849SAlex Brachet 305793c849SAlex Brachet bool FDReader::matchWritten(const char *str) { 31*8a4ee355SMichael Jones 32*8a4ee355SMichael Jones ::close(pipefd[1]); 33*8a4ee355SMichael Jones 34*8a4ee355SMichael Jones constexpr ssize_t ChunkSize = 4096 * 4; 35*8a4ee355SMichael Jones 36*8a4ee355SMichael Jones char Buffer[ChunkSize]; 37*8a4ee355SMichael Jones std::string PipeStr; 38*8a4ee355SMichael Jones std::string InputStr(str); 39*8a4ee355SMichael Jones 40*8a4ee355SMichael Jones for (int BytesRead; (BytesRead = ::read(pipefd[0], Buffer, ChunkSize));) { 41*8a4ee355SMichael Jones if (BytesRead > 0) { 42*8a4ee355SMichael Jones PipeStr.insert(PipeStr.size(), Buffer, BytesRead); 43*8a4ee355SMichael Jones } else { 445793c849SAlex Brachet assert(0 && "Error reading from pipe"); 455793c849SAlex Brachet return false; 465793c849SAlex Brachet } 47*8a4ee355SMichael Jones } 48*8a4ee355SMichael Jones 49*8a4ee355SMichael Jones return PipeStr == InputStr; 505793c849SAlex Brachet } 515793c849SAlex Brachet 525793c849SAlex Brachet } // namespace testutils 535793c849SAlex Brachet } // namespace __llvm_libc 54