Skip to content

Commit

Permalink
allow redirecting outfile to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
tihmstar committed Jul 25, 2024
1 parent 50a2ded commit bf2289d
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions img4tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ void saveToFile(const char *filePath, const void *buf, size_t bufSize){
fclose(f);
}
});

retassure(f = fopen(filePath, "wb"), "failed to create file");
retassure(fwrite(buf, 1, bufSize, f) == bufSize, "failed to write to file");

if (strcmp(filePath, "-") == 0) {
write(STDERR_FILENO, buf, bufSize);
}else{
retassure(f = fopen(filePath, "wb"), "failed to create file");
retassure(fwrite(buf, 1, bufSize, f) == bufSize, "failed to write to file");
}
}

void cmd_help(){
Expand Down Expand Up @@ -301,6 +305,19 @@ int main_r(int argc, const char * argv[]) {
return -1;
}
}

if (outFile && strcmp(outFile, "-") == 0) {
int s_out = -1;
int s_err = -1;
cleanup([&]{
safeClose(s_out);
safeClose(s_err);
});
s_out = dup(STDOUT_FILENO);
s_err = dup(STDERR_FILENO);
dup2(s_out, STDERR_FILENO);
dup2(s_err, STDOUT_FILENO);
}

if (argc-optind == 1) {
argc -= optind;
Expand Down

0 comments on commit bf2289d

Please sign in to comment.