From c5d4b7bc111fdbcfeb7253591a9725380269b073 Mon Sep 17 00:00:00 2001
From: WATANABE Yuki <magicant@wonderwand.net>
Date: Wed, 6 Mar 2024 00:53:55 +0900
Subject: [PATCH] Support confstr on Linux

---
 libc-test/semver/apple.txt            |  1 -
 libc-test/semver/linux-musl.txt       |  2 ++
 libc-test/semver/linux.txt            | 27 ++++++++++++++++++++++++++
 libc-test/semver/unix.txt             |  1 +
 src/unix/bsd/apple/mod.rs             |  5 -----
 src/unix/linux_like/linux/mod.rs      | 28 +++++++++++++++++++++++++++
 src/unix/linux_like/linux/musl/mod.rs |  3 +++
 src/unix/mod.rs                       |  5 +++++
 8 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/libc-test/semver/apple.txt b/libc-test/semver/apple.txt
index e01934bf0ceb3..84b902a36e12a 100644
--- a/libc-test/semver/apple.txt
+++ b/libc-test/semver/apple.txt
@@ -1863,7 +1863,6 @@ clock_getres
 clonefile
 clonefileat
 cmsghdr
-confstr
 connectx
 copyfile
 copyfile_callback_t
diff --git a/libc-test/semver/linux-musl.txt b/libc-test/semver/linux-musl.txt
index 7e5a81194bdf8..23ab0138e242a 100644
--- a/libc-test/semver/linux-musl.txt
+++ b/libc-test/semver/linux-musl.txt
@@ -47,6 +47,8 @@ XDP_UMEM_PGOFF_COMPLETION_RING
 XSK_UNALIGNED_BUF_OFFSET_SHIFT
 XSK_UNALIGNED_BUF_ADDR_MASK
 XDP_PKT_CONTD
+_CS_POSIX_V6_WIDTH_RESTRICTED_ENVS
+_CS_POSIX_V7_WIDTH_RESTRICTED_ENVS
 adjtimex
 aio_cancel
 aio_error
diff --git a/libc-test/semver/linux.txt b/libc-test/semver/linux.txt
index 1e0ac781ef7d9..ba2c0e9462b36 100644
--- a/libc-test/semver/linux.txt
+++ b/libc-test/semver/linux.txt
@@ -3233,6 +3233,33 @@ XATTR_REPLACE
 XTABS
 YESEXPR
 YESSTR
+_CS_PATH
+_CS_POSIX_V6_ILP32_OFF32_CFLAGS
+_CS_POSIX_V6_ILP32_OFF32_LDFLAGS
+_CS_POSIX_V6_ILP32_OFF32_LIBS
+_CS_POSIX_V6_ILP32_OFFBIG_CFLAGS
+_CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS
+_CS_POSIX_V6_ILP32_OFFBIG_LIBS
+_CS_POSIX_V6_LP64_OFF64_CFLAGS
+_CS_POSIX_V6_LP64_OFF64_LDFLAGS
+_CS_POSIX_V6_LP64_OFF64_LIBS
+_CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS
+_CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS
+_CS_POSIX_V6_LPBIG_OFFBIG_LIBS
+_CS_POSIX_V7_ILP32_OFF32_CFLAGS
+_CS_POSIX_V7_ILP32_OFF32_LDFLAGS
+_CS_POSIX_V7_ILP32_OFF32_LIBS
+_CS_POSIX_V7_ILP32_OFFBIG_CFLAGS
+_CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS
+_CS_POSIX_V7_ILP32_OFFBIG_LIBS
+_CS_POSIX_V7_LP64_OFF64_CFLAGS
+_CS_POSIX_V7_LP64_OFF64_LDFLAGS
+_CS_POSIX_V7_LP64_OFF64_LIBS
+_CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS
+_CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS
+_CS_POSIX_V7_LPBIG_OFFBIG_LIBS
+_CS_V6_ENV
+_CS_V7_ENV
 _IOFBF
 _IOLBF
 _IONBF
diff --git a/libc-test/semver/unix.txt b/libc-test/semver/unix.txt
index 5e84434b46bf4..3dc08ba090f9d 100644
--- a/libc-test/semver/unix.txt
+++ b/libc-test/semver/unix.txt
@@ -489,6 +489,7 @@ clockid_t
 close
 closedir
 closelog
+confstr
 connect
 creat
 dev_t
diff --git a/src/unix/bsd/apple/mod.rs b/src/unix/bsd/apple/mod.rs
index d9e3831ea2e1a..d0f29812a49be 100644
--- a/src/unix/bsd/apple/mod.rs
+++ b/src/unix/bsd/apple/mod.rs
@@ -5640,11 +5640,6 @@ extern "C" {
     pub fn fchflags(fd: ::c_int, flags: ::c_uint) -> ::c_int;
     pub fn clock_getres(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int;
     pub fn clock_gettime(clk_id: ::clockid_t, tp: *mut ::timespec) -> ::c_int;
-    #[cfg_attr(
-        all(target_os = "macos", target_arch = "x86"),
-        link_name = "confstr$UNIX2003"
-    )]
-    pub fn confstr(name: ::c_int, buf: *mut ::c_char, len: ::size_t) -> ::size_t;
     pub fn lio_listio(
         mode: ::c_int,
         aiocb_list: *const *mut aiocb,
diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs
index ee2999861f5c5..90083a4abefbb 100644
--- a/src/unix/linux_like/linux/mod.rs
+++ b/src/unix/linux_like/linux/mod.rs
@@ -1614,6 +1614,34 @@ pub const _SC_XOPEN_STREAMS: ::c_int = 246;
 pub const _SC_THREAD_ROBUST_PRIO_INHERIT: ::c_int = 247;
 pub const _SC_THREAD_ROBUST_PRIO_PROTECT: ::c_int = 248;
 
+pub const _CS_PATH: ::c_int = 0;
+pub const _CS_POSIX_V7_ILP32_OFF32_CFLAGS: ::c_int = 1132;
+pub const _CS_POSIX_V7_ILP32_OFF32_LDFLAGS: ::c_int = 1133;
+pub const _CS_POSIX_V7_ILP32_OFF32_LIBS: ::c_int = 1134;
+pub const _CS_POSIX_V7_ILP32_OFFBIG_CFLAGS: ::c_int = 1136;
+pub const _CS_POSIX_V7_ILP32_OFFBIG_LDFLAGS: ::c_int = 1137;
+pub const _CS_POSIX_V7_ILP32_OFFBIG_LIBS: ::c_int = 1138;
+pub const _CS_POSIX_V7_LP64_OFF64_CFLAGS: ::c_int = 1140;
+pub const _CS_POSIX_V7_LP64_OFF64_LDFLAGS: ::c_int = 1141;
+pub const _CS_POSIX_V7_LP64_OFF64_LIBS: ::c_int = 1142;
+pub const _CS_POSIX_V7_LPBIG_OFFBIG_CFLAGS: ::c_int = 1144;
+pub const _CS_POSIX_V7_LPBIG_OFFBIG_LDFLAGS: ::c_int = 1145;
+pub const _CS_POSIX_V7_LPBIG_OFFBIG_LIBS: ::c_int = 1146;
+pub const _CS_V7_ENV: ::c_int = 1149;
+pub const _CS_POSIX_V6_ILP32_OFF32_CFLAGS: ::c_int = 1116;
+pub const _CS_POSIX_V6_ILP32_OFF32_LDFLAGS: ::c_int = 1117;
+pub const _CS_POSIX_V6_ILP32_OFF32_LIBS: ::c_int = 1118;
+pub const _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS: ::c_int = 1120;
+pub const _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS: ::c_int = 1121;
+pub const _CS_POSIX_V6_ILP32_OFFBIG_LIBS: ::c_int = 1122;
+pub const _CS_POSIX_V6_LP64_OFF64_CFLAGS: ::c_int = 1124;
+pub const _CS_POSIX_V6_LP64_OFF64_LDFLAGS: ::c_int = 1125;
+pub const _CS_POSIX_V6_LP64_OFF64_LIBS: ::c_int = 1126;
+pub const _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS: ::c_int = 1128;
+pub const _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS: ::c_int = 1129;
+pub const _CS_POSIX_V6_LPBIG_OFFBIG_LIBS: ::c_int = 1130;
+pub const _CS_V6_ENV: ::c_int = 1148;
+
 pub const RLIM_SAVED_MAX: ::rlim_t = RLIM_INFINITY;
 pub const RLIM_SAVED_CUR: ::rlim_t = RLIM_INFINITY;
 
diff --git a/src/unix/linux_like/linux/musl/mod.rs b/src/unix/linux_like/linux/musl/mod.rs
index 36d8c20381432..6726b4dab0427 100644
--- a/src/unix/linux_like/linux/musl/mod.rs
+++ b/src/unix/linux_like/linux/musl/mod.rs
@@ -814,6 +814,9 @@ pub const XSK_UNALIGNED_BUF_ADDR_MASK: ::c_ulonglong = (1 << XSK_UNALIGNED_BUF_O
 
 pub const XDP_PKT_CONTD: ::__u32 = 1 << 0;
 
+pub const _CS_POSIX_V7_WIDTH_RESTRICTED_ENVS: ::c_int = 5;
+pub const _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS: ::c_int = 1;
+
 cfg_if! {
     if #[cfg(target_arch = "s390x")] {
         pub const POSIX_FADV_DONTNEED: ::c_int = 6;
diff --git a/src/unix/mod.rs b/src/unix/mod.rs
index 49984d3f00c65..895f415db2f4b 100644
--- a/src/unix/mod.rs
+++ b/src/unix/mod.rs
@@ -860,6 +860,11 @@ extern "C" {
         link_name = "close$NOCANCEL"
     )]
     pub fn close(fd: ::c_int) -> ::c_int;
+    #[cfg_attr(
+        all(target_os = "macos", target_arch = "x86"),
+        link_name = "confstr$UNIX2003"
+    )]
+    pub fn confstr(name: ::c_int, buf: *mut ::c_char, len: ::size_t) -> ::size_t;
     pub fn dup(fd: ::c_int) -> ::c_int;
     pub fn dup2(src: ::c_int, dst: ::c_int) -> ::c_int;
     pub fn execl(path: *const c_char, arg0: *const c_char, ...) -> ::c_int;