diff options
| author | Vedang Manerikar <ved.manerikar@gmail.com> | 2022-05-09 21:10:06 -0400 |
|---|---|---|
| committer | Vedang Manerikar <ved.manerikar@gmail.com> | 2022-05-09 21:10:06 -0400 |
| commit | 8ee31220a6ae3e41549bfffca7a89c481d270004 (patch) | |
| tree | 9ab77ae731fbc9f6b3e2c1274b95d39a477c3547 | |
| parent | 3e35dfa8bc66fc563ad4bcd6a5cd6338969a9fc9 (diff) | |
Revert "Use mkstemp instead of tempnam"
This reverts commit d63a1e7d87f9b0a19209f2eeb170bcf64612aa2f.
In d63a1e7, @JunyuanChen fixed a long standing compilation warning by
replacing `tempnam` with `mkstemp` in `epdfinfo`. However, the
`mkstemp` implementation does not work correctly on MS
Windows (probably because the path template is "wrong" for Windows).
I am reverting the commit and opening a new issue #110 to track the
correct implementation of `mkstemp` (or equivalent) on MS Windows.
It would also be great to add a test against Windows CI to open a PDF
and check that the operation completes successfully.
Closes: #101
| -rw-r--r-- | server/epdfinfo.c | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/server/epdfinfo.c b/server/epdfinfo.c index 95a7f16..3e0e7c1 100644 --- a/server/epdfinfo.c +++ b/server/epdfinfo.c @@ -35,7 +35,6 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> -#include <unistd.h> #include <errno.h> #include <png.h> #include <math.h> @@ -347,18 +346,26 @@ strchomp (char *str) static char* mktempfile() { - char template[] = "/tmp/epdfinfoXXXXXX"; - char *filename = malloc(sizeof(template)); - memcpy(filename, template, sizeof(template)); - int fd = mkstemp(filename); - if (fd == -1) + char *filename = NULL; + int tries = 3; + while (! filename && tries-- > 0) { - fprintf (stderr, "Unable to create tempfile"); - free(filename); - filename = NULL; + + filename = tempnam(NULL, "epdfinfo"); + if (filename) + { + int fd = open(filename, O_CREAT | O_EXCL | O_RDONLY, S_IRWXU); + if (fd > 0) + close (fd); + else + { + free (filename); + filename = NULL; + } + } } - else - close(fd); + if (! filename) + fprintf (stderr, "Unable to create tempfile"); return filename; } |
