Use mkostemp instead of mktemp where available

This commit is contained in:
ridiculousfish 2014-05-03 15:27:58 -07:00
parent cbef88a593
commit 333fb1bf97
2 changed files with 9 additions and 1 deletions

View file

@ -523,7 +523,7 @@ fi
AC_CHECK_FUNCS( wcsdup wcsndup wcslen wcscasecmp wcsncasecmp fwprintf )
AC_CHECK_FUNCS( futimes wcwidth wcswidth wcstok fputwc fgetwc )
AC_CHECK_FUNCS( wcstol wcslcat wcslcpy lrand48_r killpg )
AC_CHECK_FUNCS( wcstol wcslcat wcslcpy lrand48_r killpg mkostemp )
AC_CHECK_FUNCS( backtrace backtrace_symbols sysconf getifaddrs getpeerucred getpeereid )
if test x$local_gettext != xno; then

View file

@ -1380,12 +1380,20 @@ bool history_t::save_internal_via_rewrite()
for (size_t attempt = 0; attempt < 10 && out_fd == -1; attempt++)
{
char *narrow_str = wcs2str(tmp_name_template.c_str());
#if HAVE_MKOSTEMP
out_fd = mkostemp(narrow_str, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC);
if (out_fd >= 0)
{
tmp_name = str2wcstring(narrow_str);
}
#else
if (narrow_str && mktemp(narrow_str))
{
/* It was successfully templated; try opening it atomically */
tmp_name = str2wcstring(narrow_str);
out_fd = wopen_cloexec(tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0644);
}
#endif
free(narrow_str);
}