[libc-commits] [libc] 7f3c40a - [libc] implement pathconf/fpathconf (#87165)

via libc-commits libc-commits at lists.llvm.org
Sun Jul 7 11:20:53 PDT 2024


Author: Nhat Nguyen
Date: 2024-07-07T11:20:49-07:00
New Revision: 7f3c40a6613346a4ea856c1462de4fca12dc4fef

URL: https://github.com/llvm/llvm-project/commit/7f3c40a6613346a4ea856c1462de4fca12dc4fef
DIFF: https://github.com/llvm/llvm-project/commit/7f3c40a6613346a4ea856c1462de4fca12dc4fef.diff

LOG: [libc] implement pathconf/fpathconf (#87165)

Added: 
    libc/hdr/limits_macros.h
    libc/hdr/sys_stat_macros.h
    libc/hdr/unistd_macros.h
    libc/src/unistd/fpathconf.h
    libc/src/unistd/linux/fpathconf.cpp
    libc/src/unistd/linux/pathconf.cpp
    libc/src/unistd/linux/pathconf_utils.cpp
    libc/src/unistd/linux/pathconf_utils.h
    libc/src/unistd/pathconf.h
    libc/test/src/unistd/fpathconf_test.cpp
    libc/test/src/unistd/pathconf_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/hdr/CMakeLists.txt
    libc/include/llvm-libc-macros/limits-macros.h
    libc/include/llvm-libc-macros/linux/unistd-macros.h
    libc/src/sys/statvfs/linux/CMakeLists.txt
    libc/src/unistd/CMakeLists.txt
    libc/src/unistd/linux/CMakeLists.txt
    libc/test/src/unistd/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 6c67e4bbadee7e..5891d7a0eabfa0 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -290,6 +290,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.dup3
     libc.src.unistd.execve
     libc.src.unistd.fchdir
+    libc.src.unistd.fpathconf
     libc.src.unistd.fsync
     libc.src.unistd.ftruncate
     libc.src.unistd.getcwd
@@ -301,6 +302,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
+    libc.src.unistd.pathconf
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2b7e3d0256fc3a..172f5ad24ecb41 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -289,6 +289,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.dup3
     libc.src.unistd.execve
     libc.src.unistd.fchdir
+    libc.src.unistd.fpathconf
     libc.src.unistd.fsync
     libc.src.unistd.ftruncate
     libc.src.unistd.getcwd
@@ -300,6 +301,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
+    libc.src.unistd.pathconf
     libc.src.unistd.pread
     libc.src.unistd.pwrite
     libc.src.unistd.read

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 2ca8f00d2de50a..7c27cac1a09809 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -308,6 +308,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.dup3
     libc.src.unistd.execve
     libc.src.unistd.fchdir
+    libc.src.unistd.fpathconf
     libc.src.unistd.fsync
     libc.src.unistd.ftruncate
     libc.src.unistd.getcwd
@@ -319,6 +320,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.link
     libc.src.unistd.linkat
     libc.src.unistd.lseek
+    libc.src.unistd.pathconf
     libc.src.unistd.pipe
     libc.src.unistd.pread
     libc.src.unistd.pwrite

diff  --git a/libc/hdr/CMakeLists.txt b/libc/hdr/CMakeLists.txt
index 66b82c84dac499..1303280c2c5ef8 100644
--- a/libc/hdr/CMakeLists.txt
+++ b/libc/hdr/CMakeLists.txt
@@ -78,6 +78,24 @@ add_proxy_header_library(
     libc.include.llvm-libc-macros.sys_epoll_macros
 )
 
+add_proxy_header_library(
+  sys_stat_macros
+  HDRS
+    sys_stat_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.sys_stat
+    libc.include.llvm-libc-macros.sys_stat_macros
+)
+
+add_proxy_header_library(
+  unistd_macros
+  HDRS
+    unistd_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.unistd
+    libc.include.llvm-libc-macros.unistd_macros
+)
+
 add_proxy_header_library(
   time_macros
   HDRS
@@ -97,4 +115,13 @@ add_proxy_header_library(
     libc.include.float
 )
 
+add_proxy_header_library(
+  limits_macros
+  HDRS
+    limits_macros.h
+  FULL_BUILD_DEPENDS
+    libc.include.limits
+    libc.include.llvm-libc-macros.limits_macros
+)
+
 add_subdirectory(types)

diff  --git a/libc/hdr/limits_macros.h b/libc/hdr/limits_macros.h
new file mode 100644
index 00000000000000..2dc13b0cca60df
--- /dev/null
+++ b/libc/hdr/limits_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from limits.h --------------------------------===//
+//
+// 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_HDR_LIMITS_MACROS_H
+#define LLVM_LIBC_HDR_LIMITS_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/limits-macros.h"
+
+#else // Overlay mode
+
+#include <limits.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_LIMITS_MACROS_H

diff  --git a/libc/hdr/sys_stat_macros.h b/libc/hdr/sys_stat_macros.h
new file mode 100644
index 00000000000000..cb58d62e1ffb72
--- /dev/null
+++ b/libc/hdr/sys_stat_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from sys/stat.h ------------------------------===//
+//
+// 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_HDR_SYS_STAT_MACROS_H
+#define LLVM_LIBC_HDR_SYS_STAT_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/sys-stat-macros.h"
+
+#else // Overlay mode
+
+#include <sys/stat.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_SYS_STAT_MACROS_H

diff  --git a/libc/hdr/unistd_macros.h b/libc/hdr/unistd_macros.h
new file mode 100644
index 00000000000000..132e123280139f
--- /dev/null
+++ b/libc/hdr/unistd_macros.h
@@ -0,0 +1,22 @@
+//===-- Definition of macros from unistd.h --------------------------------===//
+//
+// 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_HDR_UNISTD_MACROS_H
+#define LLVM_LIBC_HDR_UNISTD_MACROS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-macros/unistd-macros.h"
+
+#else // Overlay mode
+
+#include <unistd.h>
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_UNISTD_MACROS_H

diff  --git a/libc/include/llvm-libc-macros/limits-macros.h b/libc/include/llvm-libc-macros/limits-macros.h
index 3fab996b61ac9c..456487e603b254 100644
--- a/libc/include/llvm-libc-macros/limits-macros.h
+++ b/libc/include/llvm-libc-macros/limits-macros.h
@@ -225,4 +225,16 @@
 #define ULLONG_MIN 0ULL
 #endif // ULLONG_MIN
 
+#ifndef _POSIX_MAX_CANON
+#define _POSIX_MAX_CANON 255
+#endif
+
+#ifndef _POSIX_MAX_INPUT
+#define _POSIX_MAX_INPUT 255
+#endif
+
+#ifndef _POSIX_NAME_MAX
+#define _POSIX_PATH_MAX 256
+#endif
+
 #endif // LLVM_LIBC_MACROS_LIMITS_MACROS_H

diff  --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
index c5109df435e68d..a4c8e3cd91f7e0 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -18,6 +18,33 @@
 #define _SC_PAGESIZE 1
 #define _SC_PAGE_SIZE _SC_PAGESIZE
 
+#define _PC_FILESIZEBITS 0
+#define _PC_LINK_MAX 1
+#define _PC_MAX_CANON 2
+#define _PC_MAX_INPUT 3
+#define _PC_NAME_MAX 4
+#define _PC_PATH_MAX 5
+#define _PC_PIPE_BUF 6
+#define _PC_2_SYMLINKS 7
+#define _PC_ALLOC_SIZE_MIN 8
+#define _PC_REC_INCR_XFER_SIZE 9
+#define _PC_REC_MAX_XFER_SIZE 10
+#define _PC_REC_MIN_XFER_SIZE 11
+#define _PC_REC_XFER_ALIGN 12
+#define _PC_SYMLINK_MAX 13
+#define _PC_CHOWN_RESTRICTED 14
+#define _PC_NO_TRUNC 15
+#define _PC_VDISABLE 16
+#define _PC_ASYNC_IO 17
+#define _PC_PRIO_IO 18
+#define _PC_SYNC_IO 19
+
+// TODO: Move these limit macros to a separate file
+#define _POSIX_CHOWN_RESTRICTED 1
+#define _POSIX_PIPE_BUF 512
+#define _POSIX_NO_TRUNC 1
+#define _POSIX_VDISABLE '\0'
+
 // Macro to set up the call to the __llvm_libc_syscall function
 // This is to prevent the call from having fewer than 6 arguments, since six
 // arguments are always passed to the syscall. Unnecessary arguments are

diff  --git a/libc/src/sys/statvfs/linux/CMakeLists.txt b/libc/src/sys/statvfs/linux/CMakeLists.txt
index f818863bb4707f..a6660c02badf78 100644
--- a/libc/src/sys/statvfs/linux/CMakeLists.txt
+++ b/libc/src/sys/statvfs/linux/CMakeLists.txt
@@ -8,6 +8,7 @@ add_header_library(
     libc.src.__support.common
     libc.src.__support.CPP.optional
     libc.include.sys_syscall
+    libc.include.llvm-libc-types.struct_statvfs
 )
 
 add_entrypoint_object(

diff  --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 77db76518350c7..ddafcd7c92f210 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -69,6 +69,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.fork
 )
 
+add_entrypoint_object(
+  fpathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.fpathconf
+)
+
 add_entrypoint_object(
   execv
   ALIAS
@@ -160,6 +167,14 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.lseek
 )
 
+add_entrypoint_object(
+  pathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.pathconf
+)
+
+
 add_entrypoint_object(
   pipe
   ALIAS

diff  --git a/libc/src/unistd/fpathconf.h b/libc/src/unistd/fpathconf.h
new file mode 100644
index 00000000000000..f3182c8123618b
--- /dev/null
+++ b/libc/src/unistd/fpathconf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for fpathconf ---------------------*- 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_UNISTD_FPATHCONF_H
+#define LLVM_LIBC_SRC_UNISTD_FPATHCONF_H
+
+namespace LIBC_NAMESPACE {
+
+long fpathconf(int fd, int name);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_FSYNC_H

diff  --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 7d831f9c29c74f..7e733d7f002c32 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -105,6 +105,20 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  fpathconf
+  SRCS
+    fpathconf.cpp
+  HDRS
+    ../fpathconf.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+    libc.src.unistd.linux.pathconf_utils
+)
+
 add_entrypoint_object(
   execv
   SRCS
@@ -273,6 +287,34 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  pathconf
+  SRCS
+    pathconf.cpp
+  HDRS
+    ../pathconf.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+    libc.src.unistd.linux.pathconf_utils
+)
+
+add_object_library(
+  pathconf_utils
+  SRCS
+    pathconf_utils.cpp
+  HDRS
+    pathconf_utils.h
+  DEPENDS
+    libc.hdr.limits_macros
+    libc.hdr.unistd_macros
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+    libc.src.sys.statvfs.linux.statfs_utils
+)
+
 add_entrypoint_object(
   pipe
   SRCS

diff  --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
new file mode 100644
index 00000000000000..6643ed1cc9e882
--- /dev/null
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -0,0 +1,24 @@
+//===-- Linux implementation of fpathconf ---------------------------------===//
+//
+// 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/unistd/fpathconf.h"
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/sys/statvfs/linux/statfs_utils.h"
+#include "src/unistd/linux/pathconf_utils.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
+  if (cpp::optional<statfs_utils::LinuxStatFs> result =
+          statfs_utils::linux_fstatfs(fd))
+    return pathconfig(result.value(), name);
+  return -1;
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
new file mode 100644
index 00000000000000..11427698232b5b
--- /dev/null
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -0,0 +1,23 @@
+//===-- Linux implementation of pathconf ----------------------------------===//
+//
+// 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/unistd/pathconf.h"
+#include "src/errno/libc_errno.h"
+#include "src/sys/statvfs/linux/statfs_utils.h"
+#include "src/unistd/linux/pathconf_utils.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(long, pathconf, (const char *path, int name)) {
+  if (cpp::optional<statfs_utils::LinuxStatFs> result =
+          statfs_utils::linux_statfs(path))
+    return pathconfig(result.value(), name);
+  return -1;
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/unistd/linux/pathconf_utils.cpp b/libc/src/unistd/linux/pathconf_utils.cpp
new file mode 100644
index 00000000000000..3f963ab5aaaf71
--- /dev/null
+++ b/libc/src/unistd/linux/pathconf_utils.cpp
@@ -0,0 +1,127 @@
+//===-- Linux implementation of pathconf_utils ----------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// This header must go before limits_macros.h otherwise libc header may choose
+// to undefine LINK_MAX.
+#include <linux/limits.h> // For LINK_MAX and other limits
+
+#include "hdr/limits_macros.h"
+#include "hdr/unistd_macros.h"
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
+#include "src/sys/statvfs/linux/statfs_utils.h"
+
+// other linux specific includes
+#include <linux/bfs_fs.h>
+#if __has_include(<linux/ufs_fs.h>)
+#include <linux/ufs_fs.h>
+#else
+// from https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/
+#define UFS_MAGIC 0x00011954
+#endif
+#include <linux/magic.h> // For common FS magics
+
+namespace LIBC_NAMESPACE {
+
+long filesizebits(const statfs_utils::LinuxStatFs &s) {
+  switch (s.f_type) {
+  case JFFS2_SUPER_MAGIC:
+  case MSDOS_SUPER_MAGIC:
+  case NCP_SUPER_MAGIC:
+    return 32;
+  }
+  return 64;
+}
+
+long link_max(const statfs_utils::LinuxStatFs &s) {
+  switch (s.f_type) {
+  case EXT2_SUPER_MAGIC:
+    return 32000;
+  case MINIX_SUPER_MAGIC:
+    return 250;
+  case MINIX2_SUPER_MAGIC:
+    return 65530;
+  case REISERFS_SUPER_MAGIC:
+    return 0xffff - 1000;
+  case UFS_MAGIC:
+    return 32000;
+  }
+  return LINK_MAX;
+}
+
+long symlinks(const statfs_utils::LinuxStatFs &s) {
+  switch (s.f_type) {
+  case ADFS_SUPER_MAGIC:
+  case BFS_MAGIC:
+  case CRAMFS_MAGIC:
+  case EFS_SUPER_MAGIC:
+  case MSDOS_SUPER_MAGIC:
+  case QNX4_SUPER_MAGIC:
+    return 0;
+  }
+  return 1;
+}
+
+long pathconfig(const statfs_utils::LinuxStatFs &s, int name) {
+  switch (name) {
+  case _PC_LINK_MAX:
+    return link_max(s);
+
+  case _PC_FILESIZEBITS:
+    return filesizebits(s);
+
+  case _PC_2_SYMLINKS:
+    return symlinks(s);
+
+  case _PC_REC_MIN_XFER_SIZE:
+    return s.f_bsize;
+
+  case _PC_ALLOC_SIZE_MIN:
+  case _PC_REC_XFER_ALIGN:
+    return s.f_frsize;
+
+  case _PC_MAX_CANON:
+    return _POSIX_MAX_CANON;
+
+  case _PC_MAX_INPUT:
+    return _POSIX_MAX_INPUT;
+
+  case _PC_NAME_MAX:
+    return s.f_namelen;
+
+  case _PC_PATH_MAX:
+    return _POSIX_PATH_MAX;
+
+  case _PC_PIPE_BUF:
+    return _POSIX_PIPE_BUF;
+
+  case _PC_CHOWN_RESTRICTED:
+    return _POSIX_CHOWN_RESTRICTED;
+
+  case _PC_NO_TRUNC:
+    return _POSIX_NO_TRUNC;
+
+  case _PC_VDISABLE:
+    return _POSIX_VDISABLE;
+
+  case _PC_ASYNC_IO:
+  case _PC_PRIO_IO:
+  case _PC_REC_INCR_XFER_SIZE:
+  case _PC_REC_MAX_XFER_SIZE:
+  case _PC_SYMLINK_MAX:
+  case _PC_SYNC_IO:
+    return -1;
+
+  default:
+    libc_errno = EINVAL;
+    return -1;
+  }
+}
+
+} // namespace LIBC_NAMESPACE

diff  --git a/libc/src/unistd/linux/pathconf_utils.h b/libc/src/unistd/linux/pathconf_utils.h
new file mode 100644
index 00000000000000..2c0ec0ea292f42
--- /dev/null
+++ b/libc/src/unistd/linux/pathconf_utils.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for pathconf_utils ----------------*- 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_UNISTD_PATHCONF_UTILS_H
+#define LLVM_LIBC_SRC_UNISTD_PATHCONF_UTILS_H
+
+#include "src/sys/statvfs/linux/statfs_utils.h"
+
+namespace LIBC_NAMESPACE {
+
+long pathconfig(const statfs_utils::LinuxStatFs &s, int name);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_PREAD_H

diff  --git a/libc/src/unistd/pathconf.h b/libc/src/unistd/pathconf.h
new file mode 100644
index 00000000000000..6543d738a9b227
--- /dev/null
+++ b/libc/src/unistd/pathconf.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for pathconf ----------------------*- 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_UNISTD_PATHCONF_H
+#define LLVM_LIBC_SRC_UNISTD_PATHCONF_H
+
+namespace LIBC_NAMESPACE {
+
+long pathconf(const char *path, int name);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_PREAD_H

diff  --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index de3e8d9ccbb626..1a1e01e50f4e88 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -446,6 +446,36 @@ add_libc_unittest(
     libc.src.unistd.sysconf
 )
 
+add_libc_unittest(
+  fpathconf_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    fpathconf_test.cpp
+  DEPENDS
+    libc.hdr.limits_macros
+    libc.hdr.unistd_macros
+    libc.hdr.sys_stat_macros
+    libc.src.unistd.fpathconf
+    libc.src.fcntl.open
+    libc.src.unistd.close
+)
+
+add_libc_unittest(
+  pathconf_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    pathconf_test.cpp
+  DEPENDS
+    libc.hdr.limits_macros
+    libc.hdr.unistd_macros
+    libc.hdr.sys_stat_macros
+    libc.src.unistd.pathconf
+    libc.src.fcntl.open
+    libc.src.unistd.close
+)
+
 add_libc_test(
   getopt_test
   HERMETIC_TEST_ONLY # Uses libc's own stderr

diff  --git a/libc/test/src/unistd/fpathconf_test.cpp b/libc/test/src/unistd/fpathconf_test.cpp
new file mode 100644
index 00000000000000..fe63e5e0859731
--- /dev/null
+++ b/libc/test/src/unistd/fpathconf_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for fpathconf -------------------------------------------===//
+//
+// 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 "hdr/fcntl_macros.h"
+#include "hdr/limits_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/unistd_macros.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/fpathconf.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+TEST(LlvmLibcPipeTest, SmokeTest) {
+  constexpr const char *FILENAME = "fpathconf.test";
+  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  EXPECT_EQ(LIBC_NAMESPACE::fpathconf(fd, _PC_SYNC_IO), -1l);
+  EXPECT_EQ(LIBC_NAMESPACE::fpathconf(fd, _PC_PATH_MAX),
+            static_cast<long>(_POSIX_PATH_MAX));
+  LIBC_NAMESPACE::close(fd);
+}
+
+// TODO: Functionality tests

diff  --git a/libc/test/src/unistd/pathconf_test.cpp b/libc/test/src/unistd/pathconf_test.cpp
new file mode 100644
index 00000000000000..3dc4b2c6027cba
--- /dev/null
+++ b/libc/test/src/unistd/pathconf_test.cpp
@@ -0,0 +1,30 @@
+//===-- Unittests for pathconf --------------------------------------------===//
+//
+// 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 "hdr/fcntl_macros.h"
+#include "hdr/limits_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/unistd_macros.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/pathconf.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+
+TEST(LlvmLibcPipeTest, SmokeTest) {
+  constexpr const char *FILENAME = "pathconf.test";
+  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  EXPECT_EQ(LIBC_NAMESPACE::pathconf(FILENAME, _PC_SYNC_IO), -1l);
+  EXPECT_EQ(LIBC_NAMESPACE::pathconf(FILENAME, _PC_PATH_MAX),
+            static_cast<long>(_POSIX_PATH_MAX));
+  LIBC_NAMESPACE::close(fd);
+}
+
+// TODO: Functionality tests


        


More information about the libc-commits mailing list