From 5ffdbc684775b453e5b01cc69b72d48827650157 Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Tue, 25 Oct 2022 08:04:54 +0800 Subject: [PATCH] expose dirname and basename --- libc-test/build.rs | 23 +++++++++++++++++++ libc-test/semver/android.txt | 2 ++ libc-test/semver/apple.txt | 2 ++ libc-test/semver/dragonfly.txt | 2 ++ libc-test/semver/freebsd.txt | 4 +++- libc-test/semver/linux-gnu.txt | 5 +++- libc-test/semver/linux-musl.txt | 4 +++- libc-test/semver/netbsd.txt | 2 ++ libc-test/semver/openbsd.txt | 2 ++ src/unix/bsd/apple/mod.rs | 3 +++ src/unix/bsd/freebsdlike/dragonfly/mod.rs | 3 +++ .../bsd/freebsdlike/freebsd/freebsd11/mod.rs | 3 +++ .../bsd/freebsdlike/freebsd/freebsd12/mod.rs | 3 +++ .../bsd/freebsdlike/freebsd/freebsd13/mod.rs | 3 +++ .../bsd/freebsdlike/freebsd/freebsd14/mod.rs | 3 +++ src/unix/bsd/netbsdlike/mod.rs | 3 +++ src/unix/linux_like/android/mod.rs | 3 +++ src/unix/linux_like/linux/gnu/mod.rs | 8 +++++++ src/unix/linux_like/linux/musl/mod.rs | 3 +++ 19 files changed, 78 insertions(+), 3 deletions(-) diff --git a/libc-test/build.rs b/libc-test/build.rs index d5bf3ea61ba39..85d6d17f84641 100644 --- a/libc-test/build.rs +++ b/libc-test/build.rs @@ -204,6 +204,7 @@ fn test_apple(target: &str) { "iconv.h", "ifaddrs.h", "langinfo.h", + "libgen.h", "libproc.h", "limits.h", "locale.h", @@ -417,6 +418,7 @@ fn test_openbsd(target: &str) { "errno.h", "execinfo.h", "fcntl.h", + "libgen.h", "limits.h", "link.h", "locale.h", @@ -992,6 +994,7 @@ fn test_netbsd(target: &str) { "elf.h", "errno.h", "fcntl.h", + "libgen.h", "limits.h", "link.h", "locale.h", @@ -1206,6 +1209,7 @@ fn test_dragonflybsd(target: &str) { "ifaddrs.h", "kvm.h", "langinfo.h", + "libgen.h", "limits.h", "link.h", "locale.h", @@ -1505,6 +1509,7 @@ fn test_android(target: &str) { "fcntl.h", "grp.h", "ifaddrs.h", + "libgen.h", "limits.h", "link.h", "locale.h", @@ -1866,6 +1871,7 @@ fn test_freebsd(target: &str) { "iconv.h", "ifaddrs.h", "langinfo.h", + "libgen.h", "libutil.h", "limits.h", "link.h", @@ -2771,6 +2777,7 @@ fn test_linux(target: &str) { "iconv.h", "ifaddrs.h", "langinfo.h", + "libgen.h", "limits.h", "link.h", "locale.h", @@ -3410,6 +3417,22 @@ fn test_linux(target: &str) { // it can't be changed from struct. "pthread_sigqueue" => true, + // There are two versions of basename(3) on Linux with glibc, see + // + // https://man7.org/linux/man-pages/man3/basename.3.html + // + // If libgen.h is included, then the POSIX version will be available; + // If _GNU_SOURCE is defined and string.h is included, then the GNU one + // will be used. + // + // libc exposes both of them, providing a prefix to differentiate between + // them. + // + // Because the name with prefix is not a valid symbol in C, we have to + // skip the tests. + "posix_basename" if gnu => true, + "gnu_basename" if gnu => true, + _ => false, } }); diff --git a/libc-test/semver/android.txt b/libc-test/semver/android.txt index d5d9fc5600ee6..6445af0264b56 100644 --- a/libc-test/semver/android.txt +++ b/libc-test/semver/android.txt @@ -3548,3 +3548,5 @@ winsize wmemchr write writev +dirname +basename \ No newline at end of file diff --git a/libc-test/semver/apple.txt b/libc-test/semver/apple.txt index 723541817468c..95af36bbb41f7 100644 --- a/libc-test/semver/apple.txt +++ b/libc-test/semver/apple.txt @@ -2227,3 +2227,5 @@ wait4 waitid xsw_usage xucred +dirname +basename \ No newline at end of file diff --git a/libc-test/semver/dragonfly.txt b/libc-test/semver/dragonfly.txt index fa472ce662747..f8f0fa08915a8 100644 --- a/libc-test/semver/dragonfly.txt +++ b/libc-test/semver/dragonfly.txt @@ -1546,3 +1546,5 @@ wait4 waitid xucred eaccess +dirname +basename diff --git a/libc-test/semver/freebsd.txt b/libc-test/semver/freebsd.txt index 649ef77161524..09bda1f2dd64f 100644 --- a/libc-test/semver/freebsd.txt +++ b/libc-test/semver/freebsd.txt @@ -1925,4 +1925,6 @@ wait4 waitid xallocx xucred -eaccess \ No newline at end of file +eaccess +dirname +basename \ No newline at end of file diff --git a/libc-test/semver/linux-gnu.txt b/libc-test/semver/linux-gnu.txt index a4289ad8496cf..bf663a193014b 100644 --- a/libc-test/semver/linux-gnu.txt +++ b/libc-test/semver/linux-gnu.txt @@ -657,4 +657,7 @@ eaccess asctime_r ctime_r strftime -strptime \ No newline at end of file +strptime +dirname +posix_basename +gnu_basename \ No newline at end of file diff --git a/libc-test/semver/linux-musl.txt b/libc-test/semver/linux-musl.txt index c00934af108e2..7af14189312d1 100644 --- a/libc-test/semver/linux-musl.txt +++ b/libc-test/semver/linux-musl.txt @@ -52,4 +52,6 @@ euidaccess eaccess asctime_r strftime -strptime \ No newline at end of file +strptime +dirname +basename \ No newline at end of file diff --git a/libc-test/semver/netbsd.txt b/libc-test/semver/netbsd.txt index f14f2eae45775..97d76cb3049d2 100644 --- a/libc-test/semver/netbsd.txt +++ b/libc-test/semver/netbsd.txt @@ -1533,3 +1533,5 @@ uucred vm_size_t wait4 waitid +dirname +basename \ No newline at end of file diff --git a/libc-test/semver/openbsd.txt b/libc-test/semver/openbsd.txt index 0ae23f549bd26..d540671e1b037 100644 --- a/libc-test/semver/openbsd.txt +++ b/libc-test/semver/openbsd.txt @@ -1231,3 +1231,5 @@ utmp utrace wait4 xucred +dirname +basename \ No newline at end of file diff --git a/src/unix/bsd/apple/mod.rs b/src/unix/bsd/apple/mod.rs index 8a7e2f1f09e01..6b391893a3bcd 100644 --- a/src/unix/bsd/apple/mod.rs +++ b/src/unix/bsd/apple/mod.rs @@ -5870,6 +5870,9 @@ extern "C" { pub fn malloc_size(ptr: *const ::c_void) -> ::size_t; pub fn malloc_good_size(size: ::size_t) -> ::size_t; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + pub fn basename(path: *mut ::c_char) -> *mut ::c_char; } pub unsafe fn mach_task_self() -> ::mach_port_t { diff --git a/src/unix/bsd/freebsdlike/dragonfly/mod.rs b/src/unix/bsd/freebsdlike/dragonfly/mod.rs index 96818616b2c6f..70fe6e2edd03b 100644 --- a/src/unix/bsd/freebsdlike/dragonfly/mod.rs +++ b/src/unix/bsd/freebsdlike/dragonfly/mod.rs @@ -1663,6 +1663,9 @@ extern "C" { pub fn umtx_sleep(ptr: *const ::c_int, value: ::c_int, timeout: ::c_int) -> ::c_int; pub fn umtx_wakeup(ptr: *const ::c_int, count: ::c_int) -> ::c_int; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + pub fn basename(path: *mut ::c_char) -> *mut ::c_char; } #[link(name = "rt")] diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs index aaa04358472be..563c0f936ffef 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs @@ -463,6 +463,9 @@ extern "C" { ) -> ::c_int; pub fn fdatasync(fd: ::c_int) -> ::c_int; + + pub fn dirname(path: *const ::c_char) -> *mut ::c_char; + pub fn basename(path: *const ::c_char) -> *mut ::c_char; } cfg_if! { diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs index e7f8ad7802875..df00b6c1d63fe 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs @@ -487,6 +487,9 @@ extern "C" { pub fn setproctitle_fast(fmt: *const ::c_char, ...); pub fn timingsafe_bcmp(a: *const ::c_void, b: *const ::c_void, len: ::size_t) -> ::c_int; pub fn timingsafe_memcmp(a: *const ::c_void, b: *const ::c_void, len: ::size_t) -> ::c_int; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + pub fn basename(path: *mut ::c_char) -> *mut ::c_char; } cfg_if! { diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs index bcd09006b5930..798431c35e191 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs @@ -535,6 +535,9 @@ extern "C" { len: ::size_t, flags: ::c_uint, ) -> ::ssize_t; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + pub fn basename(path: *mut ::c_char) -> *mut ::c_char; } #[link(name = "kvm")] diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs index 9da66960483e3..3e0ec40519461 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs @@ -535,6 +535,9 @@ extern "C" { len: ::size_t, flags: ::c_uint, ) -> ::ssize_t; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + pub fn basename(path: *mut ::c_char) -> *mut ::c_char; } #[link(name = "kvm")] diff --git a/src/unix/bsd/netbsdlike/mod.rs b/src/unix/bsd/netbsdlike/mod.rs index 6c2096b1d9acf..b71531c253db1 100644 --- a/src/unix/bsd/netbsdlike/mod.rs +++ b/src/unix/bsd/netbsdlike/mod.rs @@ -743,6 +743,9 @@ extern "C" { pub fn gethostid() -> ::c_long; pub fn sethostid(hostid: ::c_long) -> ::c_int; pub fn ftok(path: *const ::c_char, id: ::c_int) -> ::key_t; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + pub fn basename(path: *mut ::c_char) -> *mut ::c_char; } cfg_if! { diff --git a/src/unix/linux_like/android/mod.rs b/src/unix/linux_like/android/mod.rs index 67e5eb52b4e60..683a4c3ef9ae1 100644 --- a/src/unix/linux_like/android/mod.rs +++ b/src/unix/linux_like/android/mod.rs @@ -3227,6 +3227,9 @@ extern "C" { pub fn reallocarray(ptr: *mut ::c_void, nmemb: ::size_t, size: ::size_t) -> *mut ::c_void; pub fn pthread_getcpuclockid(thread: ::pthread_t, clk_id: *mut ::clockid_t) -> ::c_int; + + pub fn dirname(path: *const ::c_char) -> *mut ::c_char; + pub fn basename(path: *const ::c_char) -> *mut ::c_char; } cfg_if! { diff --git a/src/unix/linux_like/linux/gnu/mod.rs b/src/unix/linux_like/linux/gnu/mod.rs index 3506d922ddf74..98a58a2acb866 100644 --- a/src/unix/linux_like/linux/gnu/mod.rs +++ b/src/unix/linux_like/linux/gnu/mod.rs @@ -1346,6 +1346,14 @@ extern "C" { tm: *const ::tm, ) -> ::size_t; pub fn strptime(s: *const ::c_char, format: *const ::c_char, tm: *mut ::tm) -> *mut ::c_char; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + /// POSIX version of `basename(3)`, defined in `libgen.h`. + #[link_name = "__xpg_basename"] + pub fn posix_basename(path: *mut ::c_char) -> *mut ::c_char; + /// GNU version of `basename(3)`, defined in `string.h`. + #[link_name = "basename"] + pub fn gnu_basename(path: *const ::c_char) -> *mut ::c_char; } extern "C" { diff --git a/src/unix/linux_like/linux/musl/mod.rs b/src/unix/linux_like/linux/musl/mod.rs index 32d35782187f8..25291c2306115 100644 --- a/src/unix/linux_like/linux/musl/mod.rs +++ b/src/unix/linux_like/linux/musl/mod.rs @@ -767,6 +767,9 @@ extern "C" { tm: *const ::tm, ) -> ::size_t; pub fn strptime(s: *const ::c_char, format: *const ::c_char, tm: *mut ::tm) -> *mut ::c_char; + + pub fn dirname(path: *mut ::c_char) -> *mut ::c_char; + pub fn basename(path: *mut ::c_char) -> *mut ::c_char; } cfg_if! {