[libc-commits] [libc] [libc] Implement mkostemp (PR #224112)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Wed Sep 16 15:00:29 PDT 2026


================
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Implementation of mkostemp, a POSIX function that creates a unique temporary
+/// file from a template string ending in at least six 'X' characters with
+/// additional open flags.
+///
+/// Replaces the trailing X's with random characters from the POSIX portable
+/// filename character set, opens the file exclusively with the specified flags,
+/// and returns an open file descriptor, retrying automatically on name
+/// collision. See:
+/// https://pubs.opengroup.org/onlinepubs/9799919799/functions/mkdtemp.html
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/mkostemp.h"
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.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"
+#include "src/stdlib/linux/mktemp_util.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, mkostemp, (char *tmpl, int flags)) {
+  LIBC_CRASH_ON_NULLPTR(tmpl);
+
+  // POSIX.1-2024 restricts flags to the following open constants.
+  constexpr int ALLOWED_FLAGS = O_APPEND | O_CLOEXEC | O_DSYNC | O_SYNC;
----------------
kaladron wrote:

O_RSYNC isn't supported by Linux.  glibc just defines to to O_SYNC, so I left it out.  https://man7.org/linux/man-pages/man2/open.2.html says that Linux doesn't support O_CLOFORK, so I left that off too.

https://github.com/llvm/llvm-project/pull/224112


More information about the libc-commits mailing list