• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- FDReader.cpp ------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "FDReader.h"
10 #include <cassert>
11 #include <cstring>
12 #include <iostream>
13 #include <unistd.h>
14 
15 namespace __llvm_libc {
16 namespace testutils {
17 
FDReader()18 FDReader::FDReader() {
19   if (::pipe(pipefd)) {
20     std::cerr << "pipe(2) failed";
21     abort();
22   }
23 }
24 
~FDReader()25 FDReader::~FDReader() {
26   ::close(pipefd[0]);
27   ::close(pipefd[1]);
28 }
29 
matchWritten(const char * str)30 bool FDReader::matchWritten(const char *str) {
31 
32   ::close(pipefd[1]);
33 
34   constexpr ssize_t ChunkSize = 4096 * 4;
35 
36   char Buffer[ChunkSize];
37   std::string PipeStr;
38   std::string InputStr(str);
39 
40   for (int BytesRead; (BytesRead = ::read(pipefd[0], Buffer, ChunkSize));) {
41     if (BytesRead > 0) {
42       PipeStr.insert(PipeStr.size(), Buffer, BytesRead);
43     } else {
44       assert(0 && "Error reading from pipe");
45       return false;
46     }
47   }
48 
49   return PipeStr == InputStr;
50 }
51 
52 } // namespace testutils
53 } // namespace __llvm_libc
54