--- /dev/null
+//===-- Linux implementation of remove ------------------------------------===//
+//
+// 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/stdio/remove.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include <errno.h>
+#include <fcntl.h> // For AT_* macros.
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace __llvm_libc {
+
+LLVM_LIBC_FUNCTION(int, remove, (const char *path)) {
+ // We first try unlinking it as a file. If it is ia file, it will succeed. If
+ // it fails with EISDIR, we will try unlinking it as a directory.
+ int ret = __llvm_libc::syscall(SYS_unlinkat, AT_FDCWD, path, 0);
+ if (ret == -EISDIR)
+ ret = __llvm_libc::syscall(SYS_unlinkat, AT_FDCWD, path, AT_REMOVEDIR);
+ if (ret >= 0)
+ return 0;
+ errno = -ret;
+ return -1;
+}
+
+} // namespace __llvm_libc
--- /dev/null
+//===-- Implementation header of remove -------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_REMOVE_H
+#define LLVM_LIBC_SRC_STDIO_REMOVE_H
+
+#include <stdio.h>
+
+namespace __llvm_libc {
+
+int remove(const char *path);
+
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_SRC_STDIO_REMOVE_H
--- /dev/null
+//===-- Unittests for remove ----------------------------------------------===//
+//
+// 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/fcntl/open.h"
+#include "src/stdio/remove.h"
+#include "src/sys/stat/mkdirat.h"
+#include "src/unistd/access.h"
+#include "src/unistd/close.h"
+#include "test/ErrnoSetterMatcher.h"
+#include "utils/UnitTest/Test.h"
+#include "utils/testutils/FDReader.h"
+
+#include <errno.h>
+#include <unistd.h>
+
+TEST(LlvmLibcRemoveTest, CreateAndRemoveFile) {
+ // The test strategy is to create a file and remove it, and also verify that
+ // it was removed.
+ errno = 0;
+ using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
+ using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *TEST_FILE = "testdata/remove.test.file";
+ int fd = __llvm_libc::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_EQ(errno, 0);
+ ASSERT_GT(fd, 0);
+ ASSERT_THAT(__llvm_libc::close(fd), Succeeds(0));
+
+ ASSERT_THAT(__llvm_libc::access(TEST_FILE, F_OK), Succeeds(0));
+ ASSERT_THAT(__llvm_libc::remove(TEST_FILE), Succeeds(0));
+ ASSERT_THAT(__llvm_libc::access(TEST_FILE, F_OK), Fails(ENOENT));
+}
+
+TEST(LlvmLibcRemoveTest, CreateAndRemoveDir) {
+ // The test strategy is to create a dir and remove it, and also verify that
+ // it was removed.
+ errno = 0;
+ using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
+ using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *TEST_DIR = "testdata/remove.test.dir";
+ ASSERT_THAT(__llvm_libc::mkdirat(AT_FDCWD, TEST_DIR, S_IRWXU), Succeeds(0));
+
+ ASSERT_THAT(__llvm_libc::access(TEST_DIR, F_OK), Succeeds(0));
+ ASSERT_THAT(__llvm_libc::remove(TEST_DIR), Succeeds(0));
+ ASSERT_THAT(__llvm_libc::access(TEST_DIR, F_OK), Fails(ENOENT));
+}
+
+TEST(LlvmLibcRemoveTest, RemoveNonExistent) {
+ using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
+ ASSERT_THAT(__llvm_libc::remove("testdata/non-existent"), Fails(ENOENT));
+}