-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add dirname and basename #2974
add dirname and basename #2974
Conversation
Some changes occurred in OpenBSD module cc @semarie |
r? @JohnTitor (rust-highfive has picked a reviewer for you, use r? to override) |
LGTM for OpenBSD part. Regarding Linux, at library level, you can't have both functions with the same symbol. When you include #[link_name = "__xpg_basename"]
pub fn basename(path: *mut ::c_char) -> *mut ::c_char; Please note that a proper documentation might be required to point that the function is the posix one (and not the gnu one). I dunno if libc should expose posix or gnu one or both (but with different names). |
8f8d7e8
to
f6e954c
Compare
Just tested, we can have both of them in Rust since we don't have a preprocessor: // libc: linux/gnu/mod.rs
#[link_name = "__xpg_basename"]
pub fn posix_basename(path: *mut ::c_char) -> *mut ::c_char;
#[link_name = "basename"]
pub fn gnu_basename(path: *const ::c_char) -> *mut ::c_char; Expected Behavior
Test its functionality// main.rs: test code
use libc::{c_char, gnu_basename, posix_basename, printf};
fn main() {
let path1 = String::from("/\0");
let path2 = String::from("/\0");
let res_format =
String::from("posix_basename: %s \ngnu_basename: %s\n\0");
unsafe {
let res1 = posix_basename(path1.as_ptr() as *mut c_char);
let res2 = gnu_basename(path2.as_ptr() as *const c_char);
printf(res_format.as_ptr() as *mut c_char, res1, res2);
}
} $ cargo r -q
posix_basename: /
gnu_basename: libc-test
$ cd libc-test
$ cargo t
cargo:warning=/home/steve/Documents/workspace/libc/target/debug/build/libc-test-592f01d15ee93e7a/out/main.c:45272:24: error: ‘posix_basename’ undeclared (first use in this
function)
cargo:warning=45272 | return posix_basename;
cargo:warning= | ^~~~~~~~~~~~~~
cargo:warning=/home/steve/Documents/workspace/libc/target/debug/build/libc-test-592f01d15ee93e7a/out/main.c:45272:24: note: each undeclared identifier is reported only once
for each function it appears in
cargo:warning=/home/steve/Documents/workspace/libc/target/debug/build/libc-test-592f01d15ee93e7a/out/main.c: In function ‘__test_fn_gnu_basename’:
cargo:warning=/home/steve/Documents/workspace/libc/target/debug/build/libc-test-592f01d15ee93e7a/out/main.c:45277:24: error: ‘gnu_basename’ undeclared (first use in this fu
nction); did you mean ‘ns_samename’?
cargo:warning=45277 | return gnu_basename;
cargo:warning= | ^~~~~~~~~~~~
cargo:warning= | ns_samename |
If these are completely usable, I'm fine to accept your suggestion with skipping tests :) |
f6e954c
to
d1e20b1
Compare
d1e20b1
to
5ffdbc6
Compare
Thanks! @bors r+ |
☀️ Test successful - checks-actions, checks-cirrus-freebsd-12, checks-cirrus-freebsd-13, checks-cirrus-freebsd-14 |
This PR adds
dirname(3)
andbasename(3)
on the following platforms:I tested this PR on my host machine (Linux with glibc), and got the following error:
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
?