Skip to content

Commit

Permalink
Squash an extremely insidious bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Slackadays committed Feb 14, 2023
1 parent ac3b2a5 commit 1d3655e
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/clipboard/src/clipboard.cpp
Expand Up @@ -94,21 +94,40 @@ TerminalSize getTerminalSize() {

std::string fileContents(const fs::path& path) {
std::stringstream buffer;
buffer << std::ifstream(path).rdbuf();
buffer << std::ifstream(path, std::ios::binary).rdbuf();
return buffer.str();
}

std::string pipedInContent() {
std::string content;
std::string line;
while (std::getline(std::cin, line)) {
content.append(line);
#if !defined(_WIN32) && !defined(_WIN64)
std::string buffer(65535, '\0');
int len = -1;
while (len != 0) {
len = read(fileno(stdin), buffer.data(), buffer.size());
content.append(buffer.data(), len);
}
#elif defined(_WIN32) || defined(_WIN64)
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));

This comment has been minimized.

Copy link
@supsm

supsm Feb 14, 2023

probably should set this one back to what it was originally

This comment has been minimized.

Copy link
@Slackadays

Slackadays Feb 14, 2023

Author Owner

is there a reason to do so?

This comment has been minimized.

Copy link
@supsm

supsm Feb 14, 2023

setting this flag to false would make user input not show up on the console... so yes

This comment has been minimized.

Copy link
@Slackadays

Slackadays Feb 14, 2023

Author Owner

ouch, so SetConsoleMode should be gone altogether?

This comment has been minimized.

Copy link
@supsm

supsm Feb 14, 2023

probably, unless the piped content is also echoed (I don't think it should be though)


DWORD dwRead;
CHAR chBuf[1024];
BOOL bSuccess = FALSE;

while (true) {
bSuccess = ReadFile(hStdin, chBuf, 1024, &dwRead, NULL);
if (!bSuccess || dwRead == 0) break;
content.append(chBuf, dwRead);
}
#endif
return content;
}

void writeToFile(const fs::path& path, const std::string& content, bool append = false) {
std::ofstream file(path, append ? std::ios::app : std::ios::trunc);
std::ofstream file(path, append ? std::ios::app : std::ios::trunc | std::ios::binary);
file << content;
}

Expand Down Expand Up @@ -255,7 +274,13 @@ void pipeIn() {
void pipeOut() {
for (const auto& entry : fs::recursive_directory_iterator(path.main)) {
std::string content(fileContents(entry.path()));
printf("%s", content.data());
#if !defined(_WIN32) && !defined(_WIN64)
int len = write(fileno(stdout), content.data(), content.size());
if (len < 0)
throw std::runtime_error("write() failed");
#elif defined(_WIN32) || defined(_WIN64)
fwrite(content.data(), sizeof(char), content.size(), stdout);
#endif
fflush(stdout);
successes.bytes += content.size();
}
Expand Down

0 comments on commit 1d3655e

Please sign in to comment.