diff --git a/examples/pdf-bookmarks.cc b/examples/pdf-bookmarks.cc index 4f878211..802cdbac 100644 --- a/examples/pdf-bookmarks.cc +++ b/examples/pdf-bookmarks.cc @@ -163,14 +163,8 @@ void extract_bookmarks(QPDFObjectHandle outlines, std::vector& numbers) int main(int argc, char* argv[]) { - if ((whoami = strrchr(argv[0], '/')) == NULL) - { - whoami = argv[0]; - } - else - { - ++whoami; - } + whoami = QUtil::getWhoami(argv[0]); + // For libtool's sake.... if (strncmp(whoami, "lt-", 3) == 0) { diff --git a/examples/pdf-mod-info.cc b/examples/pdf-mod-info.cc index 7401a571..4e851316 100644 --- a/examples/pdf-mod-info.cc +++ b/examples/pdf-mod-info.cc @@ -77,14 +77,8 @@ int main(int argc, char* argv[]) bool static_id = false; std::map Keys; - if ((whoami = strrchr(argv[0], '/')) == NULL) - { - whoami = argv[0]; - } - else - { - ++whoami; - } + whoami = QUtil::getWhoami(argv[0]); + // For libtool's sake.... if (strncmp(whoami, "lt-", 3) == 0) { @@ -161,6 +155,9 @@ int main(int argc, char* argv[]) usage(); } + std::string fl_tmp = fl_out; + fl_tmp += ".tmp"; + try { QPDF file; @@ -198,13 +195,21 @@ int main(int argc, char* argv[]) fileinfo.replaceKey(it->first, elt); } } - std::string fl_tmp = fl_out; - fl_tmp += ".tmp"; QPDFWriter w(file, fl_tmp.c_str()); w.setStreamDataMode(QPDFWriter::s_preserve); w.setLinearization(true); w.setStaticID(static_id); w.write(); + } + catch (std::exception& e) + { + std::cerr << e.what() << std::endl; + exit(2); + } + + try + { + (void) unlink(fl_out); QUtil::os_wrapper("rename " + fl_tmp + " " + std::string(fl_out), rename(fl_tmp.c_str(), fl_out)); } diff --git a/examples/pdf-npages.cc b/examples/pdf-npages.cc index b5ef6479..c62d33de 100644 --- a/examples/pdf-npages.cc +++ b/examples/pdf-npages.cc @@ -3,6 +3,7 @@ #include #include +#include static char const* whoami = 0; @@ -15,14 +16,8 @@ void usage() int main(int argc, char* argv[]) { - if ((whoami = strrchr(argv[0], '/')) == NULL) - { - whoami = argv[0]; - } - else - { - ++whoami; - } + whoami = QUtil::getWhoami(argv[0]); + // For libtool's sake.... if (strncmp(whoami, "lt-", 3) == 0) { diff --git a/examples/qtest/mod-info.test b/examples/qtest/mod-info.test index 29a72475..90db6276 100644 --- a/examples/qtest/mod-info.test +++ b/examples/qtest/mod-info.test @@ -18,32 +18,38 @@ cleanup(); $td->runtest("usage #1", {$td->COMMAND => "$prg -in target.pdf"}, {$td->FILE => "usage.out", - $td->EXIT_STATUS => 2}); + $td->EXIT_STATUS => 2}, + $td->NORMALIZE_NEWLINES); $td->runtest("usage #2", {$td->COMMAND => "$prg -key abc -val def"}, {$td->FILE => "usage.out", - $td->EXIT_STATUS => 2}); + $td->EXIT_STATUS => 2}, + $td->NORMALIZE_NEWLINES); $td->runtest("usage #3", {$td->COMMAND => "$prg -key abc -val def abc"}, {$td->FILE => "usage.out", - $td->EXIT_STATUS => 2}); + $td->EXIT_STATUS => 2}, + $td->NORMALIZE_NEWLINES); $td->runtest("usage #4", {$td->COMMAND => "$prg -in source1.pdf -key /date -val 01/01/01 -val 12/12/12"}, {$td->FILE => "usage.out", - $td->EXIT_STATUS => 2}); + $td->EXIT_STATUS => 2}, + $td->NORMALIZE_NEWLINES); $td->runtest("dump #1", {$td->COMMAND => "$prg --dump -in files/source1.pdf"}, {$td->FILE => "dump.out", - $td->EXIT_STATUS => 0}); + $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); $td->runtest("dump #2", {$td->COMMAND => "$prg --dump -in files/no-info.pdf"}, {$td->STRING => "", - $td->EXIT_STATUS => 0}); + $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); $td->runtest("dump #3", {$td->COMMAND => "$prg --dump -in files/empty-info.pdf"}, diff --git a/include/qpdf/QUtil.hh b/include/qpdf/QUtil.hh index f5ff3323..a70db0bd 100644 --- a/include/qpdf/QUtil.hh +++ b/include/qpdf/QUtil.hh @@ -32,8 +32,12 @@ namespace QUtil char* copy_string(std::string const&); - // Set stdout to binary mode + // Set stdin, stdout to binary mode void binary_stdout(); + void binary_stdin(); + + // May modify argv0 + char* getWhoami(char* argv0); // Get the value of an environment variable in a portable fashion. // Returns true iff the variable is defined. If `value' is diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index c3df35da..a000d82e 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -114,6 +114,41 @@ QUtil::binary_stdout() #endif } +void +QUtil::binary_stdin() +{ +#ifdef _WIN32 + _setmode(_fileno(stdin), _O_BINARY); +#endif +} + +char* +QUtil::getWhoami(char* argv0) +{ +#ifdef _WIN32 + char pathsep = '\\'; +#else + char pathsep = '/'; +#endif + char* whoami = 0; + if ((whoami = strrchr(argv0, pathsep)) == NULL) + { + whoami = argv0; + } + else + { + ++whoami; + } +#ifdef _WIN32 + if ((strlen(whoami) > 4) && + (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0)) + { + whoami[strlen(whoami) - 4] = '\0'; + } +#endif + return whoami; +} + bool QUtil::get_env(std::string const& var, std::string* value) { diff --git a/libtests/rc4.cc b/libtests/rc4.cc index 34d779ad..195740ec 100644 --- a/libtests/rc4.cc +++ b/libtests/rc4.cc @@ -7,12 +7,6 @@ #include #include -#ifdef _WIN32 -# include -#else -# include -#endif - int main(int argc, char* argv[]) { if (argc != 4) diff --git a/qpdf/qpdf.cc b/qpdf/qpdf.cc index 7bf41936..12053cbe 100644 --- a/qpdf/qpdf.cc +++ b/qpdf/qpdf.cc @@ -4,10 +4,6 @@ #include #include -#ifdef _WIN32 -# include -#endif - #include #include #include @@ -20,7 +16,7 @@ static int const EXIT_ERROR = 2; static int const EXIT_WARNING = 3; -static char* whoami = 0; +static char const* whoami = 0; // Note: let's not be too noisy about documenting the fact that this // software purposely fails to enforce the distinction between user @@ -434,26 +430,8 @@ parse_encrypt_options( int main(int argc, char* argv[]) { -#ifdef _WIN32 - char pathsep = '\\'; -#else - char pathsep = '/'; -#endif - if ((whoami = strrchr(argv[0], pathsep)) == NULL) - { - whoami = argv[0]; - } - else - { - ++whoami; - } -#ifdef _WIN32 - if ((strlen(whoami) > 4) && - (strcmp(whoami + strlen(whoami) - 4, ".exe") == 0)) - { - whoami[strlen(whoami) - 4] = '\0'; - } -#endif + whoami = QUtil::getWhoami(argv[0]); + // For libtool's sake.... if (strncmp(whoami, "lt-", 3) == 0) { diff --git a/zlib-flate/zlib-flate.cc b/zlib-flate/zlib-flate.cc index b5ba8e5e..67cd925d 100644 --- a/zlib-flate/zlib-flate.cc +++ b/zlib-flate/zlib-flate.cc @@ -7,11 +7,6 @@ #include #include #include -#ifdef _WIN32 -# include -#else -# include -#endif static char const* whoami = 0; @@ -65,6 +60,7 @@ int main(int argc, char* argv[]) } QUtil::binary_stdout(); + QUtil::binary_stdin(); Pl_StdioFile* out = new Pl_StdioFile("stdout", stdout); Pl_Flate* flate = new Pl_Flate("flate", out, action); @@ -74,7 +70,7 @@ int main(int argc, char* argv[]) bool done = false; while (! done) { - int len = read(0, buf, sizeof(buf)); + int len = fread(buf, 1, sizeof(buf), stdin); if (len <= 0) { done = true;