[libc-commits] [libc] [libc] Implement basename and dirname in libgen.h (PR #204554)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Thu Jun 18 07:25:21 PDT 2026


================
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 basename.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/libgen/basename.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(char *, basename, (char *path)) {
+  if (path == nullptr || path[0] == '\0') {
+    static char dot[] = ".";
----------------
kaladron wrote:

The problem is that we return a char *, not a const char *, and the function modified its argument.  But if we have to return something when we've been handed a null, we can either leak, or have a static buffer that we reset to what we want.

I thought about having a single buffer for the dot and the slash, but that would be weird if it changed underneath someone when they called a different function.

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


More information about the libc-commits mailing list