diff --git a/src/cmd_compact.cpp b/src/cmd_compact.cpp new file mode 100644 index 0000000..11604f6 --- /dev/null +++ b/src/cmd_compact.cpp @@ -0,0 +1,30 @@ +#include +#include + +#include +#include "golpe.h" + + +static const char USAGE[] = +R"( + Usage: + compact +)"; + + +void cmd_compact(const std::vector &subArgs) { + std::map args = docopt::docopt(USAGE, subArgs, true, ""); + + std::string outputFile = args[""].asString(); + + if (outputFile == "-") { + env.copy_fd(1); + } else { + if (access(outputFile.c_str(), F_OK) == 0) throw herr("output file '", outputFile, "' exists, not overwriting"); + + auto *f = ::fopen(outputFile.c_str(), "w"); + if (!f) throw herr("opening output file '", outputFile, "' failed: ", strerror(errno)); + + env.copy_fd(::fileno(f)); + } +}