[libc-commits] [libc] [libc] implement mkstemp (PR #199220)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Fri May 22 12:17:33 PDT 2026
================
@@ -0,0 +1,86 @@
+//===-- Implementation of mkstemp -----------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/mkstemp.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getrandom.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/open.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, mkstemp, (char *tmpl)) {
+ LIBC_CRASH_ON_NULLPTR(tmpl);
+
+ // keep walking until we find the null terminator
+ int len = 0;
+ char *walk = tmpl;
+ while (*walk != '\0') {
+ walk++;
+ len++;
+ }
+
+ // invalid string length check
+ if (len < 6) {
+ libc_errno = EINVAL;
+ return -1;
+ }
+
+ walk--;
+
+ // when we find it, backtrack six positions
+ int counter = 0;
+ while (*walk == 'X') {
+ counter++;
+ if (counter == 6)
+ break;
+ walk--;
+ }
+
+ // if there aren't six Xs, throw error
+ if (counter != 6) {
+ libc_errno = EINVAL;
+ return -1;
+ }
+
+ char *suffix = tmpl + len - 6;
+
+ const char charset[] = "qwertyuiopasdfghjklzxcvbnm"
+ "QWERTYUIOPASDFGHJKLZXCVBNM"
+ "1234567890";
----------------
michaelrj-google wrote:
The posix standard (here: https://pubs.opengroup.org/onlinepubs/9799919799/functions/mkdtemp.html) says this function should use the portable filename character set, defined here: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282
https://github.com/llvm/llvm-project/pull/199220
More information about the libc-commits
mailing list