From 04a47dfb8d96a58352b16658eb12d4aed90a97e8 Mon Sep 17 00:00:00 2001 From: James Turner Date: Sat, 15 Oct 2011 02:06:35 +0100 Subject: [PATCH] Use mkdtemp where available, avoiding linker warnings about dangerous tempnam --- CMakeLists.txt | 2 +- simgear/misc/sg_dir.cxx | 20 ++++++++++++++++++++ simgear/simgear_config_cmake.h.in | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e2e8ebe..922d653f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,7 +130,7 @@ check_function_exists(gettimeofday HAVE_GETTIMEOFDAY) check_function_exists(ftime HAVE_FTIME) check_function_exists(timegm HAVE_TIMEGM) check_function_exists(rint HAVE_RINT) - +check_function_exists(mkdtemp HAVE_MKDTEMP) if(HAVE_UNISTD_H) set(CMAKE_REQUIRED_INCLUDES ${CMAKE_INCLUDE_PATH}) diff --git a/simgear/misc/sg_dir.cxx b/simgear/misc/sg_dir.cxx index e1a2610f..95f9949d 100644 --- a/simgear/misc/sg_dir.cxx +++ b/simgear/misc/sg_dir.cxx @@ -18,6 +18,9 @@ // // $Id$ +#ifdef HAVE_CONFIG_H +# include +#endif #include @@ -74,6 +77,22 @@ Dir Dir::current() Dir Dir::tempDir(const std::string& templ) { +#ifdef HAVE_MKDTEMP + char buf[1024]; + char* tempPath = ::getenv("TMPDIR"); + if (!tempPath) { + tempPath = "/tmp/"; + } + // Mac OS-X / BSD manual says any number of 'X's, but GLibc manual + // says exactly six, so that's what I'm going with + ::snprintf(buf, 1024, "%s%s-XXXXXX", tempPath, templ.c_str()); + if (!mkdtemp(buf)) { + SG_LOG(SG_IO, SG_WARN, "mkdtemp failed:" << strerror(errno)); + return Dir(); + } + + return Dir(SGPath(buf)); +#else SGPath p(tempnam(0, templ.c_str())); Dir t(p); if (!t.create(0700)) { @@ -81,6 +100,7 @@ Dir Dir::tempDir(const std::string& templ) } return t; +#endif } PathList Dir::children(int types, const std::string& nameFilter) const diff --git a/simgear/simgear_config_cmake.h.in b/simgear/simgear_config_cmake.h.in index bfb4b1b9..3d132306 100644 --- a/simgear/simgear_config_cmake.h.in +++ b/simgear/simgear_config_cmake.h.in @@ -11,6 +11,7 @@ #cmakedefine HAVE_TIMEGM #cmakedefine HAVE_ISNAN #cmakedefine HAVE_WINDOWS_H +#cmakedefine HAVE_MKDTEMP #cmakedefine HAVE_SVN_CLIENT_H #cmakedefine HAVE_LIBSVN_CLIENT_1