Skip to content

Commit

Permalink
base64: freopen(stdout)
Browse files Browse the repository at this point in the history
  • Loading branch information
aklomp committed Jan 29, 2024
1 parent ff031d2 commit e940861
Showing 1 changed file with 2 additions and 30 deletions.
32 changes: 2 additions & 30 deletions bin/base64.c
Expand Up @@ -359,41 +359,13 @@ encode_inner (const struct config *config, struct buffer *buf)
static bool
encode (const struct config *config, struct buffer *buf)
{
#ifdef WIN
DWORD dwMode;

// Get the standard output handle.
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

// Check for errors.
if (hOut == INVALID_HANDLE_VALUE) {
fprintf(stderr, "GetStdHandle: err#%d\n", GetLastError());
return false;
}

// Save the current output mode.
if (!GetConsoleMode(hOut, &dwMode)) {
fprintf(stderr, "GetConsoleMode: err#%d\n", GetLastError());
if (freopen(NULL, "wb", stdout) == 0) {
fprintf(stderr, "freopen(stdout)\n");
return false;
}

// Disable newline mangling.
if (!SetConsoleMode(hOut, dwMode | DISABLE_NEWLINE_AUTO_RETURN)) {
fprintf(stderr, "SetConsoleMode: err#%d\n", GetLastError());
return false;
}
#endif

const bool ret = encode_inner(config, buf);

#ifdef WIN

// Restore the original console settings.
if (!SetConsoleMode(hOut, dwMode)) {
fprintf(stderr, "SetConsoleMode: err#%d\n", GetLastError());
}
#endif

return ret;
}

Expand Down

0 comments on commit e940861

Please sign in to comment.