From e6955747567830648528824fde7491bdd0171015 Mon Sep 17 00:00:00 2001 From: Eyal Rozenberg Date: Fri, 27 May 2022 23:29:15 +0300 Subject: [PATCH] Fixes #342: Using `tmpnam()` instead of `mkstemp()` when the latter is not known to be available. --- .../simpleDrvRuntimePTX/simpleDrvRuntimePTX.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/modified_cuda_samples/simpleDrvRuntimePTX/simpleDrvRuntimePTX.cpp b/examples/modified_cuda_samples/simpleDrvRuntimePTX/simpleDrvRuntimePTX.cpp index 68bc605e..7b9b6c23 100644 --- a/examples/modified_cuda_samples/simpleDrvRuntimePTX/simpleDrvRuntimePTX.cpp +++ b/examples/modified_cuda_samples/simpleDrvRuntimePTX/simpleDrvRuntimePTX.cpp @@ -78,12 +78,18 @@ std::string create_ptx_file() } )"; +#if _POSIX_C_SOURCE >= 200809L char temp_filename[] = "caw-simple-drv-runtime-ptx-XXXXXX"; - int file_descriptor = mkstemp(temp_filename); + int file_descriptor = mkstemp(temp_filename); if (file_descriptor == -1) { - throw std::runtime_error(std::string("Failed creating a temporary file using mkstemp(): ") + std::strerror(errno) + '\n'); - } + throw std::runtime_error(std::string("Failed creating a temporary file using mkstemp(): ") + std::strerror(errno) + '\n'); + } FILE* ptx_file = fdopen(file_descriptor, "w"); +#else + char temp_filename[L_tmpnam]; + std::tmpnam(temp_filename); + FILE* ptx_file = fopen(temp_filename, "w"); +#endif if (ptx_file == nullptr) { throw std::runtime_error(std::string("Failed converting temporay file descriptor into a C library FILE structure: ") + std::strerror(errno) + '\n'); } @@ -99,7 +105,7 @@ std::string create_ptx_file() // Host code int main(int argc, char** argv) { - std::cout << "simpleDrvRuntime - PTX version..\n"; + std::cout << "simpleDrvRuntime - PTX version.\n"; int N = 50000; size_t size = N * sizeof(float);