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

Nhat Nguyen via libc-commits libc-commits at lists.llvm.org
Mon Jun 10 22:00:25 PDT 2024


https://github.com/changkhothuychung updated https://github.com/llvm/llvm-project/pull/87165

>From f428a360c32273516999042bb18416fa1a1db35a Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:10:47 -0400
Subject: [PATCH 01/13] intiial attempt

---
 libc/config/linux/aarch64/entrypoints.txt |  2 +
 libc/config/linux/riscv/entrypoints.txt   |  2 +
 libc/config/linux/x86_64/entrypoints.txt  |  2 +
 libc/src/unistd/CMakeLists.txt            | 14 +++++
 libc/src/unistd/linux/CMakeLists.txt      | 26 +++++++++
 libc/src/unistd/linux/pathconf.cpp        | 68 +++++++++++++++++++++++
 libc/src/unistd/pathconf.h                | 20 +++++++
 7 files changed, 134 insertions(+)
 create mode 100644 libc/src/unistd/linux/pathconf.cpp
 create mode 100644 libc/src/unistd/pathconf.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 78da7f0b334b1..be0c4bebfd95a 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -282,6 +282,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
@@ -293,6 +294,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 5aae4e246cfb3..2d8f9b1f6dd82 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -287,6 +287,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
@@ -298,6 +299,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 5b428e51aee62..0f00f264ea149 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -297,6 +297,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
@@ -308,6 +309,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/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index e22b0e1872caa..20c87f668b381 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -58,6 +58,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.fork
 )
 
+add_entrypoint_object(
+  fpathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.fpathconf
+)
+
 add_entrypoint_object(
   execv
   ALIAS
@@ -149,6 +156,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.lseek
 )
 
+add_entrypoint_object(
+  pathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.pathconf
+)
+
 add_entrypoint_object(
   pread
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index df85d44e9e9ed..0ee59d5391ac5 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -105,6 +105,19 @@ 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
+)
+
 add_entrypoint_object(
   execv
   SRCS
@@ -273,6 +286,19 @@ 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
+)
+
 add_entrypoint_object(
   pread
   SRCS
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
new file mode 100644
index 0000000000000..fed94e7d42371
--- /dev/null
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -0,0 +1,68 @@
+//===-- 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/pread.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+#include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
+#include "src/errno/libc_errno.h"
+#include <stdint.h> // For uint64_t.
+#include <sys/statvfs.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+static long pathconfig(const struct statvfs &s, int name) {
+  switch (name) {
+  case _PC_LINK_MAX:
+    return _POSIX_LINK_MAX;
+
+  case _PC_MAX_CANON:
+    return _POSIX_MAX_CANON;
+
+  case _PC_MAX_INPUT:
+    return _POSIX_MAX_INPUT;
+
+  case _PC_NAME_MAX:
+    return _POSIX_NAME_MAX;
+
+  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;
+  }
+}
+
+LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
+  struct statvfs sb;
+  if (statvfs(path, &sb) == -1) {
+    return -1;
+  }
+  return pathconfig(sb, name);
+}
+
+LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
+  struct statvfs sb;
+  if (statvfs(path, &sb) == -1) {
+    return -1;
+  }
+  return pathconfig(sb, name);
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/pathconf.h b/libc/src/unistd/pathconf.h
new file mode 100644
index 0000000000000..a397cd9323bbb
--- /dev/null
+++ b/libc/src/unistd/pathconf.h
@@ -0,0 +1,20 @@
+//===-- 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_PREAD_H
+#define LLVM_LIBC_SRC_UNISTD_PREAD_H
+
+#include <unistd.h>
+
+namespace LIBC_NAMESPACE {
+
+long pathconf(char *path, int name);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_PREAD_H

>From 480690f84a39ac8dbeb06402928decf792a46f36 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:22:00 -0400
Subject: [PATCH 02/13] remove fpathconf

---
 libc/src/unistd/CMakeLists.txt       |  7 -------
 libc/src/unistd/linux/CMakeLists.txt | 13 -------------
 2 files changed, 20 deletions(-)

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 20c87f668b381..9e81d5904447d 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -58,13 +58,6 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.fork
 )
 
-add_entrypoint_object(
-  fpathconf
-  ALIAS
-  DEPENDS
-    .${LIBC_TARGET_OS}.fpathconf
-)
-
 add_entrypoint_object(
   execv
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 0ee59d5391ac5..dfea71ed3d3dc 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -105,19 +105,6 @@ 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
-)
-
 add_entrypoint_object(
   execv
   SRCS

>From c585630cab9f7e1d4ed80b36a4f6b57a8af6a7cf Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:31:36 -0400
Subject: [PATCH 03/13] add fpathconf

---
 libc/src/unistd/CMakeLists.txt       |  7 ++++
 libc/src/unistd/fpathconf.h          | 18 +++++++++
 libc/src/unistd/linux/CMakeLists.txt | 13 ++++++
 libc/src/unistd/linux/fpathconf.cpp  | 60 ++++++++++++++++++++++++++++
 libc/src/unistd/linux/pathconf.cpp   |  8 ----
 5 files changed, 98 insertions(+), 8 deletions(-)
 create mode 100644 libc/src/unistd/fpathconf.h
 create mode 100644 libc/src/unistd/linux/fpathconf.cpp

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 9e81d5904447d..20c87f668b381 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -58,6 +58,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.fork
 )
 
+add_entrypoint_object(
+  fpathconf
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.fpathconf
+)
+
 add_entrypoint_object(
   execv
   ALIAS
diff --git a/libc/src/unistd/fpathconf.h b/libc/src/unistd/fpathconf.h
new file mode 100644
index 0000000000000..d80d94ca3c2ed
--- /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_FSYNC_H
+#define LLVM_LIBC_SRC_UNISTD_FSYNC_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 dfea71ed3d3dc..0ee59d5391ac5 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -105,6 +105,19 @@ 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
+)
+
 add_entrypoint_object(
   execv
   SRCS
diff --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
new file mode 100644
index 0000000000000..16bb0b6334e2c
--- /dev/null
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -0,0 +1,60 @@
+//===-- 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/fsync.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+static long pathconfig(const struct statvfs &s, int name) {
+  switch (name) {
+  case _PC_LINK_MAX:
+    return _POSIX_LINK_MAX;
+
+  case _PC_MAX_CANON:
+    return _POSIX_MAX_CANON;
+
+  case _PC_MAX_INPUT:
+    return _POSIX_MAX_INPUT;
+
+  case _PC_NAME_MAX:
+    return _POSIX_NAME_MAX;
+
+  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;
+  }
+}
+
+LLVM_LIBC_FUNCTION(int, fsync, (int fd)) {
+  LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
+    struct statvfs sb;
+    if (statvfs(path, &sb) == -1) {
+      return -1;
+    }
+    return pathconfig(sb, name);
+  }
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index fed94e7d42371..b1ecc4c80c3d4 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -57,12 +57,4 @@ LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
   return pathconfig(sb, name);
 }
 
-LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
-  struct statvfs sb;
-  if (statvfs(path, &sb) == -1) {
-    return -1;
-  }
-  return pathconfig(sb, name);
-}
-
 } // namespace LIBC_NAMESPACE

>From b9436a33b4545f19f4043be4ebb71c855d51f104 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 30 Mar 2024 14:43:13 -0400
Subject: [PATCH 04/13] fstatvfs

---
 libc/src/unistd/linux/pathconf.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index b1ecc4c80c3d4..a4ffb11a5acce 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -13,7 +13,7 @@
 #include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
 #include "src/errno/libc_errno.h"
 #include <stdint.h> // For uint64_t.
-#include <sys/statvfs.h>
+#include <sys/fstatvfs.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE {
@@ -51,7 +51,7 @@ static long pathconfig(const struct statvfs &s, int name) {
 
 LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
   struct statvfs sb;
-  if (statvfs(path, &sb) == -1) {
+  if (fstatvfs(path, &sb) == -1) {
     return -1;
   }
   return pathconfig(sb, name);

>From 001e9a25308b3f82e80c2c0027763c5177160cb8 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sun, 5 May 2024 12:51:25 -0400
Subject: [PATCH 05/13] some fix

---
 libc/src/unistd/linux/fpathconf.cpp | 14 +++++---------
 libc/src/unistd/linux/pathconf.cpp  | 16 ++++++++++------
 2 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
index 16bb0b6334e2c..f3909c857762c 100644
--- a/libc/src/unistd/linux/fpathconf.cpp
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -6,8 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/unistd/fsync.h"
-
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 
@@ -47,14 +45,12 @@ static long pathconfig(const struct statvfs &s, int name) {
   }
 }
 
-LLVM_LIBC_FUNCTION(int, fsync, (int fd)) {
-  LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
-    struct statvfs sb;
-    if (statvfs(path, &sb) == -1) {
-      return -1;
-    }
-    return pathconfig(sb, name);
+LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
+  struct statvfs sb;
+  if (statvfs(path, &sb) == -1) {
+    return -1;
   }
+  return pathconfig(sb, name);
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index a4ffb11a5acce..a79a4dcd781f9 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -18,10 +18,10 @@
 
 namespace LIBC_NAMESPACE {
 
-static long pathconfig(const struct statvfs &s, int name) {
+static long pathconfig(const struct statfs &s, int name) {
   switch (name) {
   case _PC_LINK_MAX:
-    return _POSIX_LINK_MAX;
+    return s.f_type;
 
   case _PC_MAX_CANON:
     return _POSIX_MAX_CANON;
@@ -30,7 +30,7 @@ static long pathconfig(const struct statvfs &s, int name) {
     return _POSIX_MAX_INPUT;
 
   case _PC_NAME_MAX:
-    return _POSIX_NAME_MAX;
+    return s.f_namemax;
 
   case _PC_PATH_MAX:
     return _POSIX_PATH_MAX;
@@ -50,11 +50,15 @@ static long pathconfig(const struct statvfs &s, int name) {
 }
 
 LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
-  struct statvfs sb;
-  if (fstatvfs(path, &sb) == -1) {
+  // struct statfs sb;
+  // if (fstatfs(PathFD(path), &sb) == -1) {
+  //   return -1;
+  // }
+  cpp::optional<LinuxStatFs> result = linux_statfs(const char *path);
+  if (!result.has_value()) {
     return -1;
   }
-  return pathconfig(sb, name);
+  return pathconfig(result.value(), name);
 }
 
 } // namespace LIBC_NAMESPACE

>From ff1969213ca496f1ea9cfce4aca2f31ac3e355b5 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sat, 11 May 2024 08:55:15 -0400
Subject: [PATCH 06/13] some fix and add test

---
 libc/src/unistd/linux/fpathconf.cpp     | 74 +++++++++++++++++++++++--
 libc/src/unistd/linux/pathconf.cpp      | 67 ++++++++++++++++++++--
 libc/test/src/unistd/fpathconf_test.cpp | 25 +++++++++
 libc/test/src/unistd/pathconf_test.cpp  | 25 +++++++++
 4 files changed, 180 insertions(+), 11 deletions(-)
 create mode 100644 libc/test/src/unistd/fpathconf_test.cpp
 create mode 100644 libc/test/src/unistd/pathconf_test.cpp

diff --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
index f3909c857762c..d2a44ca0b8b30 100644
--- a/libc/src/unistd/linux/fpathconf.cpp
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -14,10 +14,62 @@
 
 namespace LIBC_NAMESPACE {
 
-static long pathconfig(const struct statvfs &s, int name) {
+static long filesizebits(const struct statfs &s) {
+  switch (s.f_type) {
+  case JFFS2_SUPER_MAGIC:
+  case MSDOS_SUPER_MAGIC:
+  case NCP_SUPER_MAGIC:
+    return 32;
+  }
+  return 64;
+}
+
+static long link_max(const struct statfs &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;
+}
+
+static long _2_symlinks(const struct statfs &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;
+}
+
+static long fpathconfig(const struct fstatfs &s, int name) {
   switch (name) {
   case _PC_LINK_MAX:
-    return _POSIX_LINK_MAX;
+    return link_max(s);
+
+  case _PC_FILESIZEBITS:
+    return filesizebits(s);
+
+  case _PC_2_SYMLINKS:
+    return _2_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;
@@ -26,7 +78,7 @@ static long pathconfig(const struct statvfs &s, int name) {
     return _POSIX_MAX_INPUT;
 
   case _PC_NAME_MAX:
-    return _POSIX_NAME_MAX;
+    return s.f_namemax;
 
   case _PC_PATH_MAX:
     return _POSIX_PATH_MAX;
@@ -42,15 +94,25 @@ static long pathconfig(const struct statvfs &s, int name) {
 
   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:
+  default:
+    return -1;
   }
 }
 
 LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
-  struct statvfs sb;
-  if (statvfs(path, &sb) == -1) {
+  struct fstatfs sb;
+  cpp::optional<LinuxStatFs> result = linux_fstatfs(fd);
+  if (!result.has_value()) {
     return -1;
   }
-  return pathconfig(sb, name);
+  return fpathconfig(sb, name);
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index a79a4dcd781f9..89c018855f8ce 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -18,10 +18,62 @@
 
 namespace LIBC_NAMESPACE {
 
+static long filesizebits(const struct statfs &s) {
+  switch (s.f_type) {
+  case JFFS2_SUPER_MAGIC:
+  case MSDOS_SUPER_MAGIC:
+  case NCP_SUPER_MAGIC:
+    return 32;
+  }
+  return 64;
+}
+
+static long link_max(const struct statfs &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;
+}
+
+static long _2_symlinks(const struct statfs &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;
+}
+
 static long pathconfig(const struct statfs &s, int name) {
   switch (name) {
   case _PC_LINK_MAX:
-    return s.f_type;
+    return link_max(s);
+
+  case _PC_FILESIZEBITS:
+    return filesizebits(s);
+
+  case _PC_2_SYMLINKS:
+    return _2_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;
@@ -46,14 +98,19 @@ static long pathconfig(const struct statfs &s, int name) {
 
   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:
+  default:
+    return -1;
   }
 }
 
 LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
-  // struct statfs sb;
-  // if (fstatfs(PathFD(path), &sb) == -1) {
-  //   return -1;
-  // }
   cpp::optional<LinuxStatFs> result = linux_statfs(const char *path);
   if (!result.has_value()) {
     return -1;
diff --git a/libc/test/src/unistd/fpathconf_test.cpp b/libc/test/src/unistd/fpathconf_test.cpp
new file mode 100644
index 0000000000000..f51115981022f
--- /dev/null
+++ b/libc/test/src/unistd/fpathconf_test.cpp
@@ -0,0 +1,25 @@
+//===-- 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 "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);
+  ASSERT_EQ(LIBC_NAMESPACE::pathconf(fd, _PC_SYNC_IO), -1);
+  ASSERT_EQ(LIBC_NAMESPACE::pathconf(fd, _PC_PATH_MAX), _POSIX_PATH_MAX);
+}
+
+// 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 0000000000000..e8503facb9fea
--- /dev/null
+++ b/libc/test/src/unistd/pathconf_test.cpp
@@ -0,0 +1,25 @@
+//===-- 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 "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 = "fpathconf.test";
+  auto TEST_FILE = libc_make_test_file_path(FILENAME);
+  int fd = LIBC_NAMESPACE::open(TEST_FILE, O_WRONLY | O_CREAT, S_IRWXU);
+  ASSERT_EQ(LIBC_NAMESPACE::pathconf(FILENAME, _PC_SYNC_IO), -1);
+  ASSERT_EQ(LIBC_NAMESPACE::pathconf(FILENAME, _PC_PATH_MAX), _POSIX_PATH_MAX);
+}
+
+// TODO: Functionality tests

>From 1b83f13ce16e6a62d9b88e1d5c7c879f1c4dc6a9 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Sun, 12 May 2024 23:24:38 -0400
Subject: [PATCH 07/13] some fix

---
 libc/src/unistd/linux/fpathconf.cpp | 11 ++++++-----
 libc/src/unistd/linux/pathconf.cpp  | 10 ++++++----
 2 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
index d2a44ca0b8b30..ca9c9b0911328 100644
--- a/libc/src/unistd/linux/fpathconf.cpp
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -101,18 +101,19 @@ static long fpathconfig(const struct fstatfs &s, int name) {
   case _PC_REC_MAX_XFER_SIZE:
   case _PC_SYMLINK_MAX:
   case _PC_SYNC_IO:
+    return -1;
+
   default:
+    errno = EINVAL;
     return -1;
   }
 }
 
 LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
   struct fstatfs sb;
-  cpp::optional<LinuxStatFs> result = linux_fstatfs(fd);
-  if (!result.has_value()) {
-    return -1;
-  }
-  return fpathconfig(sb, name);
+  if (cpp::optional<LinuxStatFs> result = linux_fstatfs(fd))
+    return fpathconfig(sb, name);
+  return -1;
 }
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index 89c018855f8ce..80156feca18ad 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -105,17 +105,19 @@ static long pathconfig(const struct statfs &s, int name) {
   case _PC_REC_MAX_XFER_SIZE:
   case _PC_SYMLINK_MAX:
   case _PC_SYNC_IO:
+    return -1;
+
   default:
+    errno = EINVAL;
     return -1;
   }
 }
 
 LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
-  cpp::optional<LinuxStatFs> result = linux_statfs(const char *path);
-  if (!result.has_value()) {
-    return -1;
+  if (cpp::optional<LinuxStatFs> result = linux_statfs(const char *path);) {
+    return pathconfig(result.value(), name);
   }
-  return pathconfig(result.value(), name);
+  return -1;
 }
 
 } // namespace LIBC_NAMESPACE

>From 4a6c986a68e33d477552a717a643f5e34d0a8445 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Mon, 13 May 2024 21:10:49 -0400
Subject: [PATCH 08/13] refactor

---
 libc/src/unistd/CMakeLists.txt           |   7 ++
 libc/src/unistd/linux/CMakeLists.txt     |  15 +++
 libc/src/unistd/linux/fpathconf.cpp      |  99 +-------------------
 libc/src/unistd/linux/pathconf.cpp       |  99 +-------------------
 libc/src/unistd/linux/pathconf_utils.cpp | 113 +++++++++++++++++++++++
 libc/src/unistd/pathconf_utils.h         |  24 +++++
 6 files changed, 163 insertions(+), 194 deletions(-)
 create mode 100644 libc/src/unistd/linux/pathconf_utils.cpp
 create mode 100644 libc/src/unistd/pathconf_utils.h

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index c5dcb6411e6dd..67a1f37f0da2a 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -174,6 +174,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.pathconf
 )
 
+add_entrypoint_object(
+  pathconf_utils
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.pathconf_utils
+)
+
 add_entrypoint_object(
   pipe
   ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index f7435c3ffda12..3fce8d3d2f1b3 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -116,6 +116,7 @@ add_entrypoint_object(
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
+    libc.src.unistd.pathconf_utils
 )
 
 add_entrypoint_object(
@@ -297,6 +298,20 @@ add_entrypoint_object(
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
+    libc.src.unistd.pathconf_utils
+)
+
+add_entrypoint_object(
+  pathconf_utils
+  SRCS
+  pathconf_utils.cpp
+  HDRS
+    ../pathconf_utils.h
+  DEPENDS
+    libc.include.unistd
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
 )
 
 add_entrypoint_object(
diff --git a/libc/src/unistd/linux/fpathconf.cpp b/libc/src/unistd/linux/fpathconf.cpp
index ca9c9b0911328..14a37da9e1637 100644
--- a/libc/src/unistd/linux/fpathconf.cpp
+++ b/libc/src/unistd/linux/fpathconf.cpp
@@ -8,111 +8,16 @@
 
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
+#include "src/unistd/pathconf_utils.h"
 
 #include "src/errno/libc_errno.h"
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE {
 
-static long filesizebits(const struct statfs &s) {
-  switch (s.f_type) {
-  case JFFS2_SUPER_MAGIC:
-  case MSDOS_SUPER_MAGIC:
-  case NCP_SUPER_MAGIC:
-    return 32;
-  }
-  return 64;
-}
-
-static long link_max(const struct statfs &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;
-}
-
-static long _2_symlinks(const struct statfs &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;
-}
-
-static long fpathconfig(const struct fstatfs &s, int name) {
-  switch (name) {
-  case _PC_LINK_MAX:
-    return link_max(s);
-
-  case _PC_FILESIZEBITS:
-    return filesizebits(s);
-
-  case _PC_2_SYMLINKS:
-    return _2_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_namemax;
-
-  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:
-    errno = EINVAL;
-    return -1;
-  }
-}
-
 LLVM_LIBC_FUNCTION(long, fpathconf, (int fd, int name)) {
-  struct fstatfs sb;
   if (cpp::optional<LinuxStatFs> result = linux_fstatfs(fd))
-    return fpathconfig(sb, name);
+    return pathconfig(result.value(), name);
   return -1;
 }
 
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index 80156feca18ad..e43f3b2125828 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -6,113 +6,18 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/unistd/pread.h"
-
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 #include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
 #include "src/errno/libc_errno.h"
+#include "src/unistd/pathconf_utils.h"
+
 #include <stdint.h> // For uint64_t.
 #include <sys/fstatvfs.h>
 #include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE {
 
-static long filesizebits(const struct statfs &s) {
-  switch (s.f_type) {
-  case JFFS2_SUPER_MAGIC:
-  case MSDOS_SUPER_MAGIC:
-  case NCP_SUPER_MAGIC:
-    return 32;
-  }
-  return 64;
-}
-
-static long link_max(const struct statfs &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;
-}
-
-static long _2_symlinks(const struct statfs &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;
-}
-
-static long pathconfig(const struct statfs &s, int name) {
-  switch (name) {
-  case _PC_LINK_MAX:
-    return link_max(s);
-
-  case _PC_FILESIZEBITS:
-    return filesizebits(s);
-
-  case _PC_2_SYMLINKS:
-    return _2_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_namemax;
-
-  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:
-    errno = EINVAL;
-    return -1;
-  }
-}
-
 LLVM_LIBC_FUNCTION(long, pathconf, (char *path, int name)) {
   if (cpp::optional<LinuxStatFs> result = linux_statfs(const char *path);) {
     return pathconfig(result.value(), name);
diff --git a/libc/src/unistd/linux/pathconf_utils.cpp b/libc/src/unistd/linux/pathconf_utils.cpp
new file mode 100644
index 0000000000000..3a4cc0ab4446f
--- /dev/null
+++ b/libc/src/unistd/linux/pathconf_utils.cpp
@@ -0,0 +1,113 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+static long filesizebits(const struct statfs &s) {
+  switch (s.f_type) {
+  case JFFS2_SUPER_MAGIC:
+  case MSDOS_SUPER_MAGIC:
+  case NCP_SUPER_MAGIC:
+    return 32;
+  }
+  return 64;
+}
+
+static long link_max(const struct statfs &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;
+}
+
+static long _2_symlinks(const struct statfs &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;
+}
+
+static long fpathconfig(const struct fstatfs &s, int name) {
+  switch (name) {
+  case _PC_LINK_MAX:
+    return link_max(s);
+
+  case _PC_FILESIZEBITS:
+    return filesizebits(s);
+
+  case _PC_2_SYMLINKS:
+    return _2_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_namemax;
+
+  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:
+    errno = EINVAL;
+    return -1;
+  }
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/unistd/pathconf_utils.h b/libc/src/unistd/pathconf_utils.h
new file mode 100644
index 0000000000000..81bf7a14660e0
--- /dev/null
+++ b/libc/src/unistd/pathconf_utils.h
@@ -0,0 +1,24 @@
+//===-- 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_PREAD_H
+#define LLVM_LIBC_SRC_UNISTD_PREAD_H
+
+#include <unistd.h>
+
+namespace LIBC_NAMESPACE {
+
+static long filesizebits(const struct statfs &s);
+static long link_max(const struct statfs &s);
+static long _2_symlinks(const struct statfs &s);
+static long pathconfig(const struct fstatfs &s, int name);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_UNISTD_PREAD_H

>From 445bef670709153e2385fca8a77da88428468417 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Tue, 14 May 2024 21:06:05 -0400
Subject: [PATCH 09/13] some fix

---
 libc/src/unistd/CMakeLists.txt           |  2 +-
 libc/src/unistd/linux/CMakeLists.txt     |  2 +-
 libc/src/unistd/linux/pathconf_utils.cpp |  8 ++++----
 libc/src/unistd/pathconf_utils.h         | 12 ++++++------
 4 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 67a1f37f0da2a..4c351d32e3ec9 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -174,7 +174,7 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.pathconf
 )
 
-add_entrypoint_object(
+add_header_library(
   pathconf_utils
   ALIAS
   DEPENDS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 3fce8d3d2f1b3..d3af612e09dc1 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -301,7 +301,7 @@ add_entrypoint_object(
     libc.src.unistd.pathconf_utils
 )
 
-add_entrypoint_object(
+add_header_library(
   pathconf_utils
   SRCS
   pathconf_utils.cpp
diff --git a/libc/src/unistd/linux/pathconf_utils.cpp b/libc/src/unistd/linux/pathconf_utils.cpp
index 3a4cc0ab4446f..ba3a7af2aff31 100644
--- a/libc/src/unistd/linux/pathconf_utils.cpp
+++ b/libc/src/unistd/linux/pathconf_utils.cpp
@@ -15,7 +15,7 @@
 
 namespace LIBC_NAMESPACE {
 
-static long filesizebits(const struct statfs &s) {
+long filesizebits(const struct statfs &s) {
   switch (s.f_type) {
   case JFFS2_SUPER_MAGIC:
   case MSDOS_SUPER_MAGIC:
@@ -25,7 +25,7 @@ static long filesizebits(const struct statfs &s) {
   return 64;
 }
 
-static long link_max(const struct statfs &s) {
+long link_max(const struct statfs &s) {
   switch (s.f_type) {
   case EXT2_SUPER_MAGIC:
     return 32000;
@@ -41,7 +41,7 @@ static long link_max(const struct statfs &s) {
   return LINK_MAX;
 }
 
-static long _2_symlinks(const struct statfs &s) {
+long _2_symlinks(const struct statfs &s) {
   switch (s.f_type) {
   case ADFS_SUPER_MAGIC:
   case BFS_MAGIC:
@@ -54,7 +54,7 @@ static long _2_symlinks(const struct statfs &s) {
   return 1;
 }
 
-static long fpathconfig(const struct fstatfs &s, int name) {
+long pathconfig(const struct fstatfs &s, int name) {
   switch (name) {
   case _PC_LINK_MAX:
     return link_max(s);
diff --git a/libc/src/unistd/pathconf_utils.h b/libc/src/unistd/pathconf_utils.h
index 81bf7a14660e0..c5db11ae33468 100644
--- a/libc/src/unistd/pathconf_utils.h
+++ b/libc/src/unistd/pathconf_utils.h
@@ -7,17 +7,17 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_LIBC_SRC_UNISTD_PREAD_H
-#define LLVM_LIBC_SRC_UNISTD_PREAD_H
+#ifndef LLVM_LIBC_SRC_UNISTD_PATHCONF_UTILS_H
+#define LLVM_LIBC_SRC_UNISTD_PATHCONF_UTILS_H
 
 #include <unistd.h>
 
 namespace LIBC_NAMESPACE {
 
-static long filesizebits(const struct statfs &s);
-static long link_max(const struct statfs &s);
-static long _2_symlinks(const struct statfs &s);
-static long pathconfig(const struct fstatfs &s, int name);
+LIBC_INLINE long filesizebits(const struct statfs &s);
+LIBC_INLINE long link_max(const struct statfs &s);
+LIBC_INLINE long _2_symlinks(const struct statfs &s);
+LIBC_INLINE long pathconfig(const struct fstatfs &s, int name);
 
 } // namespace LIBC_NAMESPACE
 

>From fa71f67f4e10aeda08f09075cce693480b7a3f5b Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Tue, 14 May 2024 21:09:25 -0400
Subject: [PATCH 10/13] some fix

---
 libc/src/unistd/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 4c351d32e3ec9..67a1f37f0da2a 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -174,7 +174,7 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.pathconf
 )
 
-add_header_library(
+add_entrypoint_object(
   pathconf_utils
   ALIAS
   DEPENDS

>From 751e22cc0a277cd3f3c9c331f69ac441f8763fde Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Thu, 16 May 2024 23:26:58 -0400
Subject: [PATCH 11/13] some changes

---
 libc/src/unistd/CMakeLists.txt   | 2 +-
 libc/src/unistd/pathconf_utils.h | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 67a1f37f0da2a..c27b4bc8d68ac 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -174,7 +174,7 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.pathconf
 )
 
-add_entrypoint_object(
+add_object_library(
   pathconf_utils
   ALIAS
   DEPENDS
diff --git a/libc/src/unistd/pathconf_utils.h b/libc/src/unistd/pathconf_utils.h
index c5db11ae33468..f0ddbaaba5897 100644
--- a/libc/src/unistd/pathconf_utils.h
+++ b/libc/src/unistd/pathconf_utils.h
@@ -1,5 +1,4 @@
-//===-- Implementation header for pathconf_utils ----------------------*- C++
-//-*-===//
+//===-- 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.

>From 2d66e8750143ec8d63afd618514e33cf85410c44 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Tue, 21 May 2024 23:37:55 -0400
Subject: [PATCH 12/13] some fix

---
 libc/src/unistd/CMakeLists.txt           | 6 ------
 libc/src/unistd/linux/CMakeLists.txt     | 6 +++---
 libc/src/unistd/linux/pathconf.cpp       | 3 ---
 libc/src/unistd/linux/pathconf_utils.cpp | 3 +--
 libc/src/unistd/pathconf_utils.h         | 8 ++++----
 libc/test/src/unistd/pathconf_test.cpp   | 3 +--
 6 files changed, 9 insertions(+), 20 deletions(-)

diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index c27b4bc8d68ac..ddafcd7c92f21 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -174,12 +174,6 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.pathconf
 )
 
-add_object_library(
-  pathconf_utils
-  ALIAS
-  DEPENDS
-    .${LIBC_TARGET_OS}.pathconf_utils
-)
 
 add_entrypoint_object(
   pipe
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index d3af612e09dc1..8b06498b0dd7d 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -290,7 +290,7 @@ add_entrypoint_object(
 add_entrypoint_object(
   pathconf
   SRCS
-  pathconf.cpp
+    pathconf.cpp
   HDRS
     ../pathconf.h
   DEPENDS
@@ -301,10 +301,10 @@ add_entrypoint_object(
     libc.src.unistd.pathconf_utils
 )
 
-add_header_library(
+add_object_library(
   pathconf_utils
   SRCS
-  pathconf_utils.cpp
+    pathconf_utils.cpp
   HDRS
     ../pathconf_utils.h
   DEPENDS
diff --git a/libc/src/unistd/linux/pathconf.cpp b/libc/src/unistd/linux/pathconf.cpp
index e43f3b2125828..cb4457b7d29d7 100644
--- a/libc/src/unistd/linux/pathconf.cpp
+++ b/libc/src/unistd/linux/pathconf.cpp
@@ -6,9 +6,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-#include "src/__support/common.h"
-#include "src/__support/macros/sanitizer.h" // for MSAN_UNPOISON
 #include "src/errno/libc_errno.h"
 #include "src/unistd/pathconf_utils.h"
 
diff --git a/libc/src/unistd/linux/pathconf_utils.cpp b/libc/src/unistd/linux/pathconf_utils.cpp
index ba3a7af2aff31..7849230555aeb 100644
--- a/libc/src/unistd/linux/pathconf_utils.cpp
+++ b/libc/src/unistd/linux/pathconf_utils.cpp
@@ -1,5 +1,4 @@
-//===-- Linux implementation of pathconf_utils
-//---------------------------------===//
+//===-- 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.
diff --git a/libc/src/unistd/pathconf_utils.h b/libc/src/unistd/pathconf_utils.h
index f0ddbaaba5897..468f3f94e1844 100644
--- a/libc/src/unistd/pathconf_utils.h
+++ b/libc/src/unistd/pathconf_utils.h
@@ -13,10 +13,10 @@
 
 namespace LIBC_NAMESPACE {
 
-LIBC_INLINE long filesizebits(const struct statfs &s);
-LIBC_INLINE long link_max(const struct statfs &s);
-LIBC_INLINE long _2_symlinks(const struct statfs &s);
-LIBC_INLINE long pathconfig(const struct fstatfs &s, int name);
+long filesizebits(const struct statfs &s);
+long link_max(const struct statfs &s);
+long _2_symlinks(const struct statfs &s);
+long pathconfig(const struct fstatfs &s, int name);
 
 } // namespace LIBC_NAMESPACE
 
diff --git a/libc/test/src/unistd/pathconf_test.cpp b/libc/test/src/unistd/pathconf_test.cpp
index e8503facb9fea..178c683a36454 100644
--- a/libc/test/src/unistd/pathconf_test.cpp
+++ b/libc/test/src/unistd/pathconf_test.cpp
@@ -1,5 +1,4 @@
-//===-- Unittests for pathconf
-//------------------------------------------------===//
+//===-- 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.

>From 94349c2b80f92e29f40b65f27eec7178c4666985 Mon Sep 17 00:00:00 2001
From: changkhothuychung <nhat7203 at gmail.com>
Date: Tue, 11 Jun 2024 01:00:07 -0400
Subject: [PATCH 13/13] move utils to linux

---
 libc/src/unistd/linux/CMakeLists.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 8b06498b0dd7d..51aa6b0d35b6d 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -116,7 +116,7 @@ add_entrypoint_object(
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
-    libc.src.unistd.pathconf_utils
+    libc.src.unistd.linux.pathconf_utils
 )
 
 add_entrypoint_object(
@@ -298,7 +298,7 @@ add_entrypoint_object(
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
     libc.src.errno.errno
-    libc.src.unistd.pathconf_utils
+    libc.src.unistd.linux.pathconf_utils
 )
 
 add_object_library(



More information about the libc-commits mailing list