add mkstemp for msvc builds
authorBrent Cook <[email protected]>
Tue, 7 Oct 2025 10:38:30 +0000 (7 05:38 -0500)
committerBrent Cook <[email protected]>
Tue, 7 Oct 2025 10:38:30 +0000 (7 05:38 -0500)
include/compat/unistd.h
tests/CMakeLists.txt
tests/compat/mkstemp.c [new file with mode: 0644]

index 63c07fc..544cb27 100644 (file)
@@ -45,6 +45,8 @@ static inline unsigned int sleep(unsigned int seconds)
        Sleep(seconds * 1000);
        return seconds;
 }
+
+int mkstemp(char *template);
 #endif
 
 int ftruncate(int fd, off_t length);
index 55529cd..7f9b93c 100644 (file)
@@ -82,6 +82,9 @@ endfunction()
 
 # XXX - should probably be in their own static lib
 set(TEST_HELPER_SRC test.c test_util.c)
+if(WIN32)
+       set(TEST_HELPER_SRC ${TEST_HELPER_SRC} compat/mkstemp.c)
+endif()
 
 # aeadtest
 add_executable(aeadtest aeadtest.c)
@@ -555,12 +558,9 @@ prepare_emscripten_test_target(lhash_test)
 add_platform_test(lhash_test lhash_test)
 
 # md_test
-# XXX - ftruncate and mkstemp missing from Windows
-if(NOT WIN32)
-       add_executable(md_test md_test.c ${TEST_HELPER_SRC})
-       target_link_libraries(md_test ${OPENSSL_TEST_LIBS})
-       add_platform_test(md_test md_test)
-endif()
+add_executable(md_test md_test.c ${TEST_HELPER_SRC})
+target_link_libraries(md_test ${OPENSSL_TEST_LIBS})
+add_platform_test(md_test md_test)
 
 # mlkem_tests
 add_executable(mlkem_tests mlkem_tests.c parse_test_file.c)
@@ -742,12 +742,9 @@ endif()
 set_tests_properties(servertest PROPERTIES ENVIRONMENT "srcdir=${TEST_SOURCE_DIR}")
 
 # sha_test
-# XXX - ftruncate and mkstemp missing from Windows
-if(NOT WIN32)
-       add_executable(sha_test sha_test.c ${TEST_HELPER_SRC})
-       target_link_libraries(sha_test ${OPENSSL_TEST_LIBS})
-       add_platform_test(sha_test sha_test)
-endif()
+add_executable(sha_test sha_test.c ${TEST_HELPER_SRC})
+target_link_libraries(sha_test ${OPENSSL_TEST_LIBS})
+add_platform_test(sha_test sha_test)
 
 # shutdowntest
 set(SHUTDOWNTEST_SRC shutdowntest.c)
diff --git a/tests/compat/mkstemp.c b/tests/compat/mkstemp.c
new file mode 100644 (file)
index 0000000..fe3d15a
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+musl as a whole is licensed under the following standard MIT license:
+
+----------------------------------------------------------------------
+Copyright © 2005-2020 Rich Felker, et al.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+----------------------------------------------------------------------
+*/
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
+
+#include <io.h>
+
+int mkstemp(char *template)
+{
+       int fd;
+retry:
+       if (!_mktemp(template)) return -1;
+       fd = open(template, O_RDWR | O_CREAT | O_EXCL, 0600);
+       if (fd >= 0) return fd;
+       if (errno == EEXIST) {
+               /* this is safe because mktemp verified
+                * that we have a valid template string */
+               strcpy(template+strlen(template)-6, "XXXXXX");
+               goto retry;
+       }
+       return -1;
+}