Skip to content

Commit a8b7b9c

Browse files
committed
Auto merge of #2974 - SteveLauC:dirname-basename, r=JohnTitor
add dirname and basename This PR adds `dirname(3)` and `basename(3)` on the following platforms: * Linux with glibc * Linux with musl * Android * FreeBSD * DragonFlyBSD * NetBSD * OpenBSD * Apple platforms I tested this PR on my host machine (Linux with glibc), and got the following error: ``` RUNNING ALL TESTS bad basename function pointer: rust: 140093945892128 (0x7f6a29e14d20) != c 140093945544944 (0x7f6a29dc00f0) thread 'main' panicked at 'some tests failed', /home/steve/Documents/workspace/libc/target/debug/build/libc-test-592f01d15ee93e7a/out/main.rs:12:21 stack backtrace: 0: std::panicking::begin_panic at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:616:12 1: main::main at /home/steve/Documents/workspace/libc/target/debug/build/libc-test-592f01d15ee93e7a/out/main.rs:12:21 2: core::ops::function::FnOnce::call_once at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:248:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. error: test failed, to rerun pass '--test main' ``` The reason for this error probably is that there are two `basename(3)` on Linux with glibc, the POSIX version and the GNU version, and they clash with each other. In C, if one `#include <libgen.h>`, then the POSIX version will be available; If one ` #define _GNU_SOURCE` and `#include <string.h>`, then the GNU one will be used. Can we distinguish them in `libc`?
2 parents 3cdabff + 5ffdbc6 commit a8b7b9c

File tree

19 files changed

+78
-3
lines changed

19 files changed

+78
-3
lines changed

libc-test/build.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ fn test_apple(target: &str) {
204204
"iconv.h",
205205
"ifaddrs.h",
206206
"langinfo.h",
207+
"libgen.h",
207208
"libproc.h",
208209
"limits.h",
209210
"locale.h",
@@ -420,6 +421,7 @@ fn test_openbsd(target: &str) {
420421
"errno.h",
421422
"execinfo.h",
422423
"fcntl.h",
424+
"libgen.h",
423425
"limits.h",
424426
"link.h",
425427
"locale.h",
@@ -995,6 +997,7 @@ fn test_netbsd(target: &str) {
995997
"elf.h",
996998
"errno.h",
997999
"fcntl.h",
1000+
"libgen.h",
9981001
"limits.h",
9991002
"link.h",
10001003
"locale.h",
@@ -1209,6 +1212,7 @@ fn test_dragonflybsd(target: &str) {
12091212
"ifaddrs.h",
12101213
"kvm.h",
12111214
"langinfo.h",
1215+
"libgen.h",
12121216
"limits.h",
12131217
"link.h",
12141218
"locale.h",
@@ -1508,6 +1512,7 @@ fn test_android(target: &str) {
15081512
"fcntl.h",
15091513
"grp.h",
15101514
"ifaddrs.h",
1515+
"libgen.h",
15111516
"limits.h",
15121517
"link.h",
15131518
"locale.h",
@@ -1869,6 +1874,7 @@ fn test_freebsd(target: &str) {
18691874
"iconv.h",
18701875
"ifaddrs.h",
18711876
"langinfo.h",
1877+
"libgen.h",
18721878
"libutil.h",
18731879
"limits.h",
18741880
"link.h",
@@ -2774,6 +2780,7 @@ fn test_linux(target: &str) {
27742780
"iconv.h",
27752781
"ifaddrs.h",
27762782
"langinfo.h",
2783+
"libgen.h",
27772784
"limits.h",
27782785
"link.h",
27792786
"locale.h",
@@ -3413,6 +3420,22 @@ fn test_linux(target: &str) {
34133420
// it can't be changed from struct.
34143421
"pthread_sigqueue" => true,
34153422

3423+
// There are two versions of basename(3) on Linux with glibc, see
3424+
//
3425+
// https://man7.org/linux/man-pages/man3/basename.3.html
3426+
//
3427+
// If libgen.h is included, then the POSIX version will be available;
3428+
// If _GNU_SOURCE is defined and string.h is included, then the GNU one
3429+
// will be used.
3430+
//
3431+
// libc exposes both of them, providing a prefix to differentiate between
3432+
// them.
3433+
//
3434+
// Because the name with prefix is not a valid symbol in C, we have to
3435+
// skip the tests.
3436+
"posix_basename" if gnu => true,
3437+
"gnu_basename" if gnu => true,
3438+
34163439
_ => false,
34173440
}
34183441
});

libc-test/semver/android.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,3 +3548,5 @@ winsize
35483548
wmemchr
35493549
write
35503550
writev
3551+
dirname
3552+
basename

libc-test/semver/apple.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,3 +2227,5 @@ wait4
22272227
waitid
22282228
xsw_usage
22292229
xucred
2230+
dirname
2231+
basename

libc-test/semver/dragonfly.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,3 +1546,5 @@ wait4
15461546
waitid
15471547
xucred
15481548
eaccess
1549+
dirname
1550+
basename

libc-test/semver/freebsd.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,4 +1925,6 @@ wait4
19251925
waitid
19261926
xallocx
19271927
xucred
1928-
eaccess
1928+
eaccess
1929+
dirname
1930+
basename

libc-test/semver/linux-gnu.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,4 +657,7 @@ eaccess
657657
asctime_r
658658
ctime_r
659659
strftime
660-
strptime
660+
strptime
661+
dirname
662+
posix_basename
663+
gnu_basename

libc-test/semver/linux-musl.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ euidaccess
5252
eaccess
5353
asctime_r
5454
strftime
55-
strptime
55+
strptime
56+
dirname
57+
basename

libc-test/semver/netbsd.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,3 +1533,5 @@ uucred
15331533
vm_size_t
15341534
wait4
15351535
waitid
1536+
dirname
1537+
basename

libc-test/semver/openbsd.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,3 +1231,5 @@ utmp
12311231
utrace
12321232
wait4
12331233
xucred
1234+
dirname
1235+
basename

src/unix/bsd/apple/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5870,6 +5870,9 @@ extern "C" {
58705870

58715871
pub fn malloc_size(ptr: *const ::c_void) -> ::size_t;
58725872
pub fn malloc_good_size(size: ::size_t) -> ::size_t;
5873+
5874+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
5875+
pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
58735876
}
58745877

58755878
pub unsafe fn mach_task_self() -> ::mach_port_t {

src/unix/bsd/freebsdlike/dragonfly/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,9 @@ extern "C" {
16631663

16641664
pub fn umtx_sleep(ptr: *const ::c_int, value: ::c_int, timeout: ::c_int) -> ::c_int;
16651665
pub fn umtx_wakeup(ptr: *const ::c_int, count: ::c_int) -> ::c_int;
1666+
1667+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
1668+
pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
16661669
}
16671670

16681671
#[link(name = "rt")]

src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ extern "C" {
463463
) -> ::c_int;
464464

465465
pub fn fdatasync(fd: ::c_int) -> ::c_int;
466+
467+
pub fn dirname(path: *const ::c_char) -> *mut ::c_char;
468+
pub fn basename(path: *const ::c_char) -> *mut ::c_char;
466469
}
467470

468471
cfg_if! {

src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,9 @@ extern "C" {
487487
pub fn setproctitle_fast(fmt: *const ::c_char, ...);
488488
pub fn timingsafe_bcmp(a: *const ::c_void, b: *const ::c_void, len: ::size_t) -> ::c_int;
489489
pub fn timingsafe_memcmp(a: *const ::c_void, b: *const ::c_void, len: ::size_t) -> ::c_int;
490+
491+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
492+
pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
490493
}
491494

492495
cfg_if! {

src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ extern "C" {
535535
len: ::size_t,
536536
flags: ::c_uint,
537537
) -> ::ssize_t;
538+
539+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
540+
pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
538541
}
539542

540543
#[link(name = "kvm")]

src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ extern "C" {
535535
len: ::size_t,
536536
flags: ::c_uint,
537537
) -> ::ssize_t;
538+
539+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
540+
pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
538541
}
539542

540543
#[link(name = "kvm")]

src/unix/bsd/netbsdlike/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,9 @@ extern "C" {
743743
pub fn gethostid() -> ::c_long;
744744
pub fn sethostid(hostid: ::c_long) -> ::c_int;
745745
pub fn ftok(path: *const ::c_char, id: ::c_int) -> ::key_t;
746+
747+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
748+
pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
746749
}
747750

748751
cfg_if! {

src/unix/linux_like/android/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3227,6 +3227,9 @@ extern "C" {
32273227
pub fn reallocarray(ptr: *mut ::c_void, nmemb: ::size_t, size: ::size_t) -> *mut ::c_void;
32283228

32293229
pub fn pthread_getcpuclockid(thread: ::pthread_t, clk_id: *mut ::clockid_t) -> ::c_int;
3230+
3231+
pub fn dirname(path: *const ::c_char) -> *mut ::c_char;
3232+
pub fn basename(path: *const ::c_char) -> *mut ::c_char;
32303233
}
32313234

32323235
cfg_if! {

src/unix/linux_like/linux/gnu/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,14 @@ extern "C" {
13461346
tm: *const ::tm,
13471347
) -> ::size_t;
13481348
pub fn strptime(s: *const ::c_char, format: *const ::c_char, tm: *mut ::tm) -> *mut ::c_char;
1349+
1350+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
1351+
/// POSIX version of `basename(3)`, defined in `libgen.h`.
1352+
#[link_name = "__xpg_basename"]
1353+
pub fn posix_basename(path: *mut ::c_char) -> *mut ::c_char;
1354+
/// GNU version of `basename(3)`, defined in `string.h`.
1355+
#[link_name = "basename"]
1356+
pub fn gnu_basename(path: *const ::c_char) -> *mut ::c_char;
13491357
}
13501358

13511359
extern "C" {

src/unix/linux_like/linux/musl/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,9 @@ extern "C" {
767767
tm: *const ::tm,
768768
) -> ::size_t;
769769
pub fn strptime(s: *const ::c_char, format: *const ::c_char, tm: *mut ::tm) -> *mut ::c_char;
770+
771+
pub fn dirname(path: *mut ::c_char) -> *mut ::c_char;
772+
pub fn basename(path: *mut ::c_char) -> *mut ::c_char;
770773
}
771774

772775
cfg_if! {

0 commit comments

Comments
 (0)