summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVedang Manerikar <ved.manerikar@gmail.com>2022-05-16 13:00:18 -0400
committerVedang Manerikar <ved.manerikar@gmail.com>2022-05-23 22:11:19 -0400
commit8d51e55b5f0ae7bdebfaa606ab61263be2829cc2 (patch)
treed087e45f058448f9905817415960a42ee002ac1c
parentf9ccdf99e560bae70d3a13325cec9dc0e3cc45b0 (diff)
Use mkstemp instead of tempnamfeature/replace-tempnam
This reverts commit fedd930a09a497c03df3ce5204ccbd80da724662. It should be possible to use the filename as generated by `mkstemp`, the problem was that we were using the wrong path-separator. So the diff from fedd930a09a497c03df3ce5204ccbd80da724662 is as follows: @@ -338,7 +338,11 @@ strchomp (char *str) return str; } - +#if HAVE_W32 +#define MKTEMP_SEPARATOR "\\" +#else +#define MKTEMP_SEPARATOR "/" +#endif /** * Create a new, temporary file and returns its name. * @@ -347,7 +351,7 @@ strchomp (char *str) static char* mktempfile() { - char template[] = P_tmpdir "epdfinfo_XXXXXX"; + char template[] = P_tmpdir MKTEMP_SEPARATOR "epdfinfo_XXXXXX"; I have tested the change on Mac OSX and Linux. It is tested on Windows 10 by @ShuguangSun. Closes: #110
-rw-r--r--server/epdfinfo.c34
1 files changed, 16 insertions, 18 deletions
diff --git a/server/epdfinfo.c b/server/epdfinfo.c
index 061df61..7faeb89 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>
@@ -338,6 +339,11 @@ strchomp (char *str)
return str;
}
+#if HAVE_W32
+#define MKTEMP_SEPARATOR "\\"
+#else
+#define MKTEMP_SEPARATOR "/"
+#endif
/**
* Create a new, temporary file and returns its name.
*
@@ -346,26 +352,18 @@ strchomp (char *str)
static char*
mktempfile()
{
- char *filename = NULL;
- int tries = 3;
- while (! filename && tries-- > 0)
+ char template[] = P_tmpdir MKTEMP_SEPARATOR "epdfinfo_XXXXXX";
+ 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;
}