branch: elpa/pdf-tools commit 37bbe861755bc60c7cc333359fee3e2a5d919c77 Author: Vedang Manerikar <ved.maneri...@gmail.com> Commit: Vedang Manerikar <ved.maneri...@gmail.com>
Use mkstemp instead of tempnam This reverts commit 8ee31220a6ae3e41549bfffca7a89c481d270004 and brings in @JunyuanChen's change once again. We modify the commit to fix the problem on Windows by changing the code as follows: --- a/server/epdfinfo.c +++ b/server/epdfinfo.c @@ -347,6 +347,6 @@ static char* mktempfile() { - char template[] = "/tmp/epdfinfoXXXXXX"; + char template[] = P_tmpdir "/epdfinfoXXXXXX"; char *filename = malloc(sizeof(template)); memcpy(filename, template, sizeof(template)); This works correctly for Windows (as confirmed by @ShuguangSun), Mac (tested by @vedang) and Linux (tested by @Junyuanchen) Relates to: #101, #94 Closes: #110 --- server/epdfinfo.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/server/epdfinfo.c b/server/epdfinfo.c index 3e0e7c1166..906bdb486b 100644 --- a/server/epdfinfo.c +++ b/server/epdfinfo.c @@ -35,6 +35,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> +#include <unistd.h> #include <errno.h> #include <png.h> #include <math.h> @@ -346,26 +347,18 @@ strchomp (char *str) static char* mktempfile() { - char *filename = NULL; - int tries = 3; - while (! filename && tries-- > 0) + char template[] = P_tmpdir "/epdfinfoXXXXXX"; + char *filename = malloc(sizeof(template)); + memcpy(filename, template, sizeof(template)); + int fd = mkstemp(filename); + if (fd == -1) { - - 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; - } - } + fprintf (stderr, "Unable to create tempfile"); + free(filename); + filename = NULL; } - if (! filename) - fprintf (stderr, "Unable to create tempfile"); + else + close(fd); return filename; }