[libc-commits] [libc] [libc] Implement mkdtemp (PR #222106)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Wed Sep 9 07:51:15 PDT 2026
================
@@ -0,0 +1,96 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Shared helper functions for creating unique temporary files and directories.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDLIB_LINUX_MKTEMP_UTIL_H
+#define LLVM_LIBC_SRC_STDLIB_LINUX_MKTEMP_UTIL_H
+
+#include "hdr/errno_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/CPP/array.h"
+#include "src/__support/CPP/span.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getrandom.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+// POSIX portable filename character set, sorted by ASCII value.
+// See
+// https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_265
+LIBC_INLINE_VAR constexpr cpp::string_view MKTEMP_CHARSET =
+ "-._0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz";
+
+/// Core helper function for creating temporary files and directories.
+///
+/// \param tmpl Template string ending in at least six 'X' characters.
+/// \param create_fn Callable taking `const char *path` and returning
+/// `ErrorOr<T>`.
+/// \return `ErrorOr<T>` with the result of `create_fn` on success, or Error on
+/// failure.
+template <typename CreateFn>
+LIBC_INLINE auto mktemp_core(char *tmpl, CreateFn create_fn)
+ -> decltype(create_fn(tmpl)) {
----------------
kaladron wrote:
That's cleaner, good call. Thanks.
https://github.com/llvm/llvm-project/pull/222106
More information about the libc-commits
mailing list