branch: elpa/pdf-tools
commit 0adc845fc2e0110dab0c7fc401a7bcf890d0610a
Author: Jimmy Yuen Ho Wong <[email protected]>
Commit: GitHub <[email protected]>

    fix(server): replace tempnam with mkstemp/_tempnam
    
    Use platform-appropriate temp file creation:
    - _tempnam() on Windows (Microsoft-recommended)
    - mkstemp() on Unix-like systems
    
    Fixes #110, addresses #316
    
    Supersedes #235
---
 server/configure.ac |  2 +-
 server/epdfinfo.c   | 18 ++++++++++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/server/configure.ac b/server/configure.ac
index 19369f64098..ec1862124d6 100644
--- a/server/configure.ac
+++ b/server/configure.ac
@@ -84,7 +84,7 @@ AC_C_BIGENDIAN
 # Checks for library functions.
 AC_FUNC_ERROR_AT_LINE
 AC_FUNC_STRTOD
-AC_CHECK_FUNCS([strcspn strtol getline])
+AC_CHECK_FUNCS([strcspn strtol getline _tempnam])
 
 AC_CONFIG_FILES([Makefile])
 AC_OUTPUT
diff --git a/server/epdfinfo.c b/server/epdfinfo.c
index b3925d0ef73..29793d6b42d 100644
--- a/server/epdfinfo.c
+++ b/server/epdfinfo.c
@@ -346,12 +346,13 @@ strchomp (char *str)
 static char*
 mktempfile()
 {
+#if defined (HAVE__TEMPNAM)
   char *filename = NULL;
   int tries = 3;
   while (! filename && tries-- > 0)
     {
 
-      filename =  tempnam(NULL, "epdfinfo");
+      filename =  _tempnam(NULL, "epdfinfo");
       if (filename)
         {
           int fd = open(filename, O_CREAT | O_EXCL | O_RDONLY, S_IRUSR | 
S_IWUSR);
@@ -366,7 +367,20 @@ mktempfile()
     }
   if (! filename)
     fprintf (stderr, "Unable to create tempfile");
-
+#else
+  char template[] = P_tmpdir "/epdfinfoXXXXXX";
+  char *filename = malloc(sizeof(template));
+  memcpy(filename, template, sizeof(template));
+  int fd = mkstemp(filename);
+  if (fd == -1)
+    {
+      fprintf (stderr, "Unable to create tempfile");
+      free(filename);
+      filename = NULL;
+    }
+  else
+    close(fd);
+#endif
   return filename;
 }
 

Reply via email to