From: Eric Biggers Date: Sat, 6 Feb 2016 15:47:08 +0000 (-0600) Subject: extract.c: replace tempnam() with mkstemp() on non-Windows X-Git-Tag: v1.9.1~52 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=011502c8756394066172b3164567a8b9835cc280 extract.c: replace tempnam() with mkstemp() on non-Windows 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. --- diff --git a/include/wimlib_tchar.h b/include/wimlib_tchar.h index fffcd5c4..5a2038f0 100644 --- a/include/wimlib_tchar.h +++ b/include/wimlib_tchar.h @@ -56,7 +56,6 @@ typedef wchar_t tchar; # 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 @@ -117,7 +116,6 @@ typedef char tchar; # define tstrtoull strtoull # define tmkdir mkdir # define tstrdup strdup -# define ttempnam tempnam # define tgetenv getenv # define TSTRDUP STRDUP # define tstrerror_r strerror_r diff --git a/src/extract.c b/src/extract.c index 39da4bee..496d9c98 100644 --- a/src/extract.c +++ b/src/extract.c @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -315,27 +316,33 @@ static int create_temporary_file(struct filedes *fd_ret, tchar **name_ret) { tchar *name; - int open_flags; int raw_fd; +#ifdef __WIN32__ 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; } - - 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 (errno == EEXIST) { - FREE(name); - goto retry; - } ERROR_WITH_ERRNO("Failed to create temporary file " "\"%"TS"\"", name); FREE(name);