Skip to content
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 fnmatch.h #3937

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ fn test_apple(target: &str) {
"errno.h",
"execinfo.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"glob.h",
"grp.h",
Expand Down Expand Up @@ -454,6 +455,7 @@ fn test_openbsd(target: &str) {
"errno.h",
"execinfo.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"libgen.h",
"limits.h",
Expand Down Expand Up @@ -731,6 +733,7 @@ fn test_redox(target: &str) {
"dlfcn.h",
"errno.h",
"fcntl.h",
"fnmatch.h",
"grp.h",
"limits.h",
"locale.h",
Expand Down Expand Up @@ -790,6 +793,7 @@ fn test_solarish(target: &str) {
"errno.h",
"execinfo.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"glob.h",
"grp.h",
Expand Down Expand Up @@ -1030,6 +1034,7 @@ fn test_netbsd(target: &str) {
"elf.h",
"errno.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"libgen.h",
"limits.h",
Expand Down Expand Up @@ -1244,6 +1249,7 @@ fn test_dragonflybsd(target: &str) {
"errno.h",
"execinfo.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"glob.h",
"grp.h",
Expand Down Expand Up @@ -1464,6 +1470,7 @@ fn test_wasi(target: &str) {
"dirent.h",
"errno.h",
"fcntl.h",
"fnmatch.h",
"langinfo.h",
"limits.h",
"locale.h",
Expand Down Expand Up @@ -1579,6 +1586,7 @@ fn test_android(target: &str) {
"elf.h",
"errno.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"grp.h",
"ifaddrs.h",
Expand Down Expand Up @@ -2087,6 +2095,7 @@ fn test_freebsd(target: &str) {
"errno.h",
"execinfo.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"glob.h",
"grp.h",
Expand Down Expand Up @@ -2706,6 +2715,7 @@ fn test_emscripten(target: &str) {
"dlfcn.h",
"errno.h",
"fcntl.h",
"fnmatch.h",
"glob.h",
"grp.h",
"ifaddrs.h",
Expand Down Expand Up @@ -2974,6 +2984,7 @@ fn test_neutrino(target: &str) {
"dlfcn.h",
"sys/elf.h",
"fcntl.h",
"fnmatch.h",
"glob.h",
"grp.h",
"iconv.h",
Expand Down Expand Up @@ -3368,6 +3379,7 @@ fn test_linux(target: &str) {
"dlfcn.h",
"elf.h",
"fcntl.h",
"fnmatch.h",
"getopt.h",
"glob.h",
[gnu]: "gnu/libc-version.h",
Expand Down
6 changes: 6 additions & 0 deletions libc-test/semver/unix.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ FD_ZERO
FILE
FIOCLEX
FIONBIO
FNM_CASEFOLD
FNM_NOESCAPE
FNM_NOMATCH
FNM_PATHNAME
FNM_PERIOD
F_DUPFD
F_DUPFD_CLOEXEC
F_GETFD
Expand Down Expand Up @@ -526,6 +531,7 @@ fgetpos
fgets
fileno
flock
fnmatch
fopen
fork
fpathconf
Expand Down
22 changes: 22 additions & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,24 @@ pub const ATF_PERM: ::c_int = 0x04;
pub const ATF_PUBL: ::c_int = 0x08;
pub const ATF_USETRAILERS: ::c_int = 0x10;

pub const FNM_PERIOD: c_int = 1 << 2;
pub const FNM_CASEFOLD: c_int = 1 << 4;
pub const FNM_NOMATCH: c_int = 1;

cfg_if! {
if #[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "android",
))] {
pub const FNM_PATHNAME: c_int = 1 << 1;
pub const FNM_NOESCAPE: c_int = 1 << 0;
} else {
pub const FNM_PATHNAME: c_int = 1 << 0;
pub const FNM_NOESCAPE: c_int = 1 << 1;
}
}
Comment on lines +327 to +339
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks pretty straightforward, but do you have links to headers defining these? I am a bit surprised they are opposite on a few OSs, we should make sure that's documented in the PR.

Copy link
Contributor Author

@epilys epilys Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly,

macos

https://opensource.apple.com/source/Libc/Libc-825.40.1/include/fnmatch.h.auto.html

#define	FNM_NOESCAPE	0x01	/* Disable backslash escaping. */
#define	FNM_PATHNAME	0x02	/* Slash must be matched by slash. */

freebsd

https://github.com/freebsd/freebsd-src/blob/4a983f05d98ba1c5eebe65d29dd061dc3eab7594/include/fnmatch.h#L4

#define	FNM_NOESCAPE	0x01	/* Disable backslash escaping. */
#define	FNM_PATHNAME	0x02	/* Slash must be matched by slash. */

android

https://cs.android.com/android/platform/superproject/main/+/main:bionic/libc/include/fnmatch.h;drc=bbd39aa8e66dfe325732934d1aeb0da52b4750d5

/** fnmatch() flag to disable backslash escaping. */
#define FNM_NOESCAPE     0x01 
/** fnmatch() flag to ensure that slashes must be matched by slashes. */
#define FNM_PATHNAME     0x02

musl

https://git.musl-libc.org/cgit/musl/tree/include/fnmatch.h?id=03860303c298f2ec7a9c0de683a68f4ab61697f0

#define	FNM_PATHNAME 0x1
#define	FNM_NOESCAPE 0x2

glibc

https://elixir.bootlin.com/glibc/glibc-2.40.9000/source/posix/fnmatch.h#L33

#define	FNM_PATHNAME	(1 << 0) /* No wildcard can ever match `/'.  */
#define	FNM_NOESCAPE	(1 << 1) /* Backslashes don't quote special chars.  */

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the sources!


extern "C" {
pub static in6addr_loopback: in6_addr;
pub static in6addr_any: in6_addr;
Expand Down Expand Up @@ -1573,6 +1591,10 @@ cfg_if! {
}
}

extern "C" {
pub fn fnmatch(pattern: *const c_char, name: *const c_char, flags: c_int) -> c_int;
}

cfg_if! {
if #[cfg(target_env = "newlib")] {
mod newlib;
Expand Down