]> wimlib.net Git - wimlib/commitdiff
extract.c: replace tempnam() with mkstemp() on non-Windows
authorEric Biggers <ebiggers3@gmail.com>
Sat, 6 Feb 2016 15:47:08 +0000 (09:47 -0600)
committerEric Biggers <ebiggers3@gmail.com>
Tue, 9 Feb 2016 01:43:11 +0000 (19:43 -0600)
tempnam() produces an annoying warning when linking, even though wimlib
was using it safely (with O_EXCL to make it race-free).  Just replace it
with mkstemp() anyway.

include/wimlib_tchar.h
src/extract.c

index fffcd5c4bf156f568df92f5796e6fed69f0b827d..5a2038f07508de2904492d1a19f8efc3febf053b 100644 (file)
@@ -56,7 +56,6 @@ typedef wchar_t tchar;
 #  define tstrerror    _wcserror
 #  define taccess      _waccess
 #  define tstrdup      wcsdup
 #  define tstrerror    _wcserror
 #  define taccess      _waccess
 #  define tstrdup      wcsdup
-#  define ttempnam     _wtempnam
 #  define tgetenv      _wgetenv
 /* The following "tchar" functions do not have exact wide-character equivalents
  * on Windows so require parameter rearrangement or redirection to a replacement
 #  define tgetenv      _wgetenv
 /* The following "tchar" functions do not have exact wide-character equivalents
  * on Windows so require parameter rearrangement or redirection to a replacement
@@ -117,7 +116,6 @@ typedef char tchar;
 #  define tstrtoull    strtoull
 #  define tmkdir       mkdir
 #  define tstrdup      strdup
 #  define tstrtoull    strtoull
 #  define tmkdir       mkdir
 #  define tstrdup      strdup
-#  define ttempnam     tempnam
 #  define tgetenv      getenv
 #  define TSTRDUP      STRDUP
 #  define tstrerror_r  strerror_r
 #  define tgetenv      getenv
 #  define TSTRDUP      STRDUP
 #  define tstrerror_r  strerror_r
index 39da4bee378b1afedd0c8ab34f56c69ed45bc260..496d9c9870664ba050902139a942363197956605 100644 (file)
@@ -41,6 +41,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
 
 #include <errno.h>
 #include <fcntl.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include <unistd.h>
 
 #include <sys/stat.h>
 #include <unistd.h>
 
@@ -315,27 +316,33 @@ static int
 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
 {
        tchar *name;
 create_temporary_file(struct filedes *fd_ret, tchar **name_ret)
 {
        tchar *name;
-       int open_flags;
        int raw_fd;
 
        int raw_fd;
 
+#ifdef __WIN32__
 retry:
 retry:
-       name = ttempnam(NULL, T("wimlib"));
+       name = _wtempnam(NULL, L"wimlib");
        if (!name) {
                ERROR_WITH_ERRNO("Failed to create temporary filename");
                return WIMLIB_ERR_NOMEM;
        }
        if (!name) {
                ERROR_WITH_ERRNO("Failed to create temporary filename");
                return WIMLIB_ERR_NOMEM;
        }
-
-       open_flags = O_WRONLY | O_CREAT | O_EXCL | O_BINARY;
-#ifdef __WIN32__
-       open_flags |= _O_SHORT_LIVED;
-#endif
-       raw_fd = topen(name, open_flags, 0600);
+       raw_fd = _wopen(name, O_WRONLY | O_CREAT | O_EXCL | O_BINARY |
+                       _O_SHORT_LIVED, 0600);
+       if (raw_fd < 0 && errno == EEXIST) {
+               FREE(name);
+               goto retry;
+       }
+#else /* __WIN32__ */
+       const char *tmpdir = getenv("TMPDIR");
+       if (!tmpdir)
+               tmpdir = P_tmpdir;
+       name = MALLOC(strlen(tmpdir) + 1 + 6 + 6 + 1);
+       if (!name)
+               return WIMLIB_ERR_NOMEM;
+       sprintf(name, "%s/wimlibXXXXXX", tmpdir);
+       raw_fd = mkstemp(name);
+#endif /* !__WIN32__ */
 
        if (raw_fd < 0) {
 
        if (raw_fd < 0) {
-               if (errno == EEXIST) {
-                       FREE(name);
-                       goto retry;
-               }
                ERROR_WITH_ERRNO("Failed to create temporary file "
                                 "\"%"TS"\"", name);
                FREE(name);
                ERROR_WITH_ERRNO("Failed to create temporary file "
                                 "\"%"TS"\"", name);
                FREE(name);