diff --git a/libc-test/src/makedev.c b/libc-test/src/makedev.c index 7f99d60728bb4..62752c72ab97f 100644 --- a/libc-test/src/makedev.c +++ b/libc-test/src/makedev.c @@ -3,11 +3,19 @@ #include #endif -// Since makedev is a macro instead of a function, it isn't available to FFI. -// libc must reimplement it, which is error-prone. This file provides FFI -// access to the actual macro so it can be tested against the Rust -// reimplementation. +// Since makedev, major, minor are macros instead of functions, they aren't +// available to FFI. libc must reimplement them, which is error-prone. This +// file provides FFI access to the actual macros so they can be tested against +// the Rust reimplementation. dev_t makedev_ffi(unsigned major, unsigned minor) { return makedev(major, minor); } + +unsigned int major_ffi(dev_t dev) { + return major(dev); +} + +unsigned int minor_ffi(dev_t dev) { + return minor(dev); +} diff --git a/libc-test/test/makedev.rs b/libc-test/test/makedev.rs index cb00975b9a41f..b7260c2689356 100644 --- a/libc-test/test/makedev.rs +++ b/libc-test/test/makedev.rs @@ -1,4 +1,4 @@ -//! Compare libc's makdev function against the actual C macros, for various +//! Compare libc's makedev, major, minor functions against the actual C macros, for various //! inputs. #[cfg(any( @@ -16,11 +16,17 @@ mod t { extern "C" { pub fn makedev_ffi(major: c_uint, minor: c_uint) -> dev_t; + pub fn major_ffi(dev: dev_t) -> c_uint; + pub fn minor_ffi(dev: dev_t) -> c_uint; } fn compare(major: c_uint, minor: c_uint) { - let expected = unsafe { makedev_ffi(major, minor) }; - assert_eq!(libc::makedev(major, minor), expected); + let dev = unsafe { makedev_ffi(major, minor) }; + assert_eq!(libc::makedev(major, minor), dev); + let major = unsafe { major_ffi(dev) }; + assert_eq!(libc::major(dev), major as _); + let minor = unsafe { minor_ffi(dev) }; + assert_eq!(libc::minor(dev), minor as _); } // Every OS should be able to handle 8 bit major and minor numbers diff --git a/src/fuchsia/mod.rs b/src/fuchsia/mod.rs index f9a7ed929eaaf..cbef2ef12bf1c 100644 --- a/src/fuchsia/mod.rs +++ b/src/fuchsia/mod.rs @@ -3436,20 +3436,6 @@ f! { set1.bits == set2.bits } - pub fn major(dev: crate::dev_t) -> c_uint { - let mut major = 0; - major |= (dev & 0x00000000000fff00) >> 8; - major |= (dev & 0xfffff00000000000) >> 32; - major as c_uint - } - - pub fn minor(dev: crate::dev_t) -> c_uint { - let mut minor = 0; - minor |= (dev & 0x00000000000000ff) >> 0; - minor |= (dev & 0x00000ffffff00000) >> 12; - minor as c_uint - } - pub fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar { cmsg.offset(1) as *mut c_uchar } @@ -3532,6 +3518,20 @@ safe_f! { dev |= (minor & 0xffffff00) << 12; dev } + + pub {const} fn major(dev: crate::dev_t) -> c_uint { + let mut major = 0; + major |= (dev & 0x00000000000fff00) >> 8; + major |= (dev & 0xfffff00000000000) >> 32; + major as c_uint + } + + pub {const} fn minor(dev: crate::dev_t) -> c_uint { + let mut minor = 0; + minor |= (dev & 0x00000000000000ff) >> 0; + minor |= (dev & 0x00000ffffff00000) >> 12; + minor as c_uint + } } fn __CMSG_LEN(cmsg: *const cmsghdr) -> ssize_t { diff --git a/src/unix/aix/mod.rs b/src/unix/aix/mod.rs index ca94debe88652..8bf978db2947f 100644 --- a/src/unix/aix/mod.rs +++ b/src/unix/aix/mod.rs @@ -2546,25 +2546,6 @@ f! { let fd = fd as usize; return ((*set).fds_bits[fd / bits] & (1 << (fd % bits))) != 0; } - - pub fn major(dev: crate::dev_t) -> c_uint { - let x = dev >> 16; - x as c_uint - } - - pub fn minor(dev: crate::dev_t) -> c_uint { - let y = dev & 0xFFFF; - y as c_uint - } - - pub fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t { - let major = major as crate::dev_t; - let minor = minor as crate::dev_t; - let mut dev = 0; - dev |= major << 16; - dev |= minor; - dev - } } safe_f! { @@ -2612,6 +2593,25 @@ safe_f! { pub {const} fn WCOREDUMP(_status: c_int) -> bool { false } + + pub {const} fn major(dev: crate::dev_t) -> c_uint { + let x = dev >> 16; + x as c_uint + } + + pub {const} fn minor(dev: crate::dev_t) -> c_uint { + let y = dev & 0xFFFF; + y as c_uint + } + + pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t { + let major = major as crate::dev_t; + let minor = minor as crate::dev_t; + let mut dev = 0; + dev |= major << 16; + dev |= minor; + dev + } } #[link(name = "thread")] diff --git a/src/unix/bsd/apple/mod.rs b/src/unix/bsd/apple/mod.rs index 7cce58e5d6db3..9344ac0b7b05c 100644 --- a/src/unix/bsd/apple/mod.rs +++ b/src/unix/bsd/apple/mod.rs @@ -5544,18 +5544,6 @@ f! { pub {const} fn VM_MAKE_TAG(id: u8) -> u32 { (id as u32) << 24u32 } - - pub fn major(dev: dev_t) -> i32 { - (dev >> 24) & 0xff - } - - pub fn minor(dev: dev_t) -> i32 { - dev & 0xffffff - } - - pub fn makedev(major: i32, minor: i32) -> dev_t { - (major << 24) | minor - } } safe_f! { @@ -5578,6 +5566,18 @@ safe_f! { pub {const} fn WIFSTOPPED(status: c_int) -> bool { _WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) != 0x13 } + + pub {const} fn makedev(major: i32, minor: i32) -> dev_t { + (major << 24) | minor + } + + pub {const} fn major(dev: dev_t) -> i32 { + (dev >> 24) & 0xff + } + + pub {const} fn minor(dev: dev_t) -> i32 { + dev & 0xffffff + } } extern "C" { diff --git a/src/unix/bsd/freebsdlike/dragonfly/mod.rs b/src/unix/bsd/freebsdlike/dragonfly/mod.rs index d12cd12885b11..27dd19b36c555 100644 --- a/src/unix/bsd/freebsdlike/dragonfly/mod.rs +++ b/src/unix/bsd/freebsdlike/dragonfly/mod.rs @@ -1591,14 +1591,6 @@ f! { let (idx, offset) = ((cpu >> 6) & 3, cpu & 63); 0 != cpuset.ary[idx] & (1 << offset) } - - pub fn major(dev: crate::dev_t) -> c_int { - ((dev >> 8) & 0xff) as c_int - } - - pub fn minor(dev: crate::dev_t) -> c_int { - (dev & 0xffff00ff) as c_int - } } safe_f! { @@ -1614,6 +1606,14 @@ safe_f! { dev |= minor; dev } + + pub {const} fn major(dev: crate::dev_t) -> c_int { + ((dev >> 8) & 0xff) as c_int + } + + pub {const} fn minor(dev: crate::dev_t) -> c_int { + (dev & 0xffff00ff) as c_int + } } extern "C" { diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs index bda5a71ec5692..b06fceb58f3f2 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs @@ -441,14 +441,12 @@ safe_f! { let minor = minor as crate::dev_t; (major << 8) | minor } -} -f! { - pub fn major(dev: crate::dev_t) -> c_int { + pub {const} fn major(dev: crate::dev_t) -> c_int { ((dev >> 8) & 0xff) as c_int } - pub fn minor(dev: crate::dev_t) -> c_int { + pub {const} fn minor(dev: crate::dev_t) -> c_int { (dev & 0xffff00ff) as c_int } } diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs index 761d7f3fe44de..2d5a73521c2a2 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd12/mod.rs @@ -496,14 +496,12 @@ safe_f! { dev |= ((minor & 0xffff00ff) as dev_t) << 0; dev } -} -f! { - pub fn major(dev: crate::dev_t) -> c_int { + pub {const} fn major(dev: crate::dev_t) -> c_int { (((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int } - pub fn minor(dev: crate::dev_t) -> c_int { + pub {const} fn minor(dev: crate::dev_t) -> c_int { (((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int } } diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs index 5a5fc6d0ae7e7..297aec4d5f610 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd13/mod.rs @@ -518,14 +518,12 @@ safe_f! { dev |= ((minor & 0xffff00ff) as dev_t) << 0; dev } -} -f! { - pub fn major(dev: crate::dev_t) -> c_int { + pub {const} fn major(dev: crate::dev_t) -> c_int { (((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int } - pub fn minor(dev: crate::dev_t) -> c_int { + pub {const} fn minor(dev: crate::dev_t) -> c_int { (((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int } } diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs index 1f76eec38aa7a..b2e598f9e4af4 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd14/mod.rs @@ -518,14 +518,12 @@ safe_f! { dev |= ((minor & 0xffff00ff) as dev_t) << 0; dev } -} -f! { - pub fn major(dev: crate::dev_t) -> c_int { + pub {const} fn major(dev: crate::dev_t) -> c_int { (((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int } - pub fn minor(dev: crate::dev_t) -> c_int { + pub {const} fn minor(dev: crate::dev_t) -> c_int { (((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int } } diff --git a/src/unix/bsd/freebsdlike/freebsd/freebsd15/mod.rs b/src/unix/bsd/freebsdlike/freebsd/freebsd15/mod.rs index 638b7bc3352ad..4abeb745a9027 100644 --- a/src/unix/bsd/freebsdlike/freebsd/freebsd15/mod.rs +++ b/src/unix/bsd/freebsdlike/freebsd/freebsd15/mod.rs @@ -518,14 +518,12 @@ safe_f! { dev |= ((minor & 0xffff00ff) as dev_t) << 0; dev } -} -f! { - pub fn major(dev: crate::dev_t) -> c_int { + pub {const} fn major(dev: crate::dev_t) -> c_int { (((dev >> 32) & 0xffffff00) | ((dev >> 8) & 0xff)) as c_int } - pub fn minor(dev: crate::dev_t) -> c_int { + pub {const} fn minor(dev: crate::dev_t) -> c_int { (((dev >> 24) & 0xff00) | (dev & 0xffff00ff)) as c_int } } diff --git a/src/unix/bsd/netbsdlike/netbsd/mod.rs b/src/unix/bsd/netbsdlike/netbsd/mod.rs index 1840015e1d14b..78822532941c5 100644 --- a/src/unix/bsd/netbsdlike/netbsd/mod.rs +++ b/src/unix/bsd/netbsdlike/netbsd/mod.rs @@ -2460,17 +2460,6 @@ f! { pub fn PROT_MPROTECT_EXTRACT(x: c_int) -> c_int { (x >> 3) & 0x7 } - - pub fn major(dev: crate::dev_t) -> c_int { - (((dev as u32) & 0x000fff00) >> 8) as c_int - } - - pub fn minor(dev: crate::dev_t) -> c_int { - let mut res = 0; - res |= ((dev as u32) & 0xfff00000) >> 12; - res |= (dev as u32) & 0x000000ff; - res as c_int - } } safe_f! { @@ -2499,6 +2488,17 @@ safe_f! { dev |= minor & 0xff; dev } + + pub {const} fn major(dev: crate::dev_t) -> c_int { + (((dev as u32) & 0x000fff00) >> 8) as c_int + } + + pub {const} fn minor(dev: crate::dev_t) -> c_int { + let mut res = 0; + res |= ((dev as u32) & 0xfff00000) >> 12; + res |= (dev as u32) & 0x000000ff; + res as c_int + } } extern "C" { diff --git a/src/unix/bsd/netbsdlike/openbsd/mod.rs b/src/unix/bsd/netbsdlike/openbsd/mod.rs index c15c2e77a1493..20448d0cfc187 100644 --- a/src/unix/bsd/netbsdlike/openbsd/mod.rs +++ b/src/unix/bsd/netbsdlike/openbsd/mod.rs @@ -1949,19 +1949,6 @@ f! { pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint { (_ALIGN(mem::size_of::()) + _ALIGN(length as usize)) as c_uint } - - pub fn major(dev: crate::dev_t) -> c_uint { - ((dev as c_uint) >> 8) & 0xff - } - - pub fn minor(dev: crate::dev_t) -> c_uint { - let dev = dev as c_uint; - let mut res = 0; - res |= (dev) & 0xff; - res |= ((dev) & 0xffff0000) >> 8; - - res - } } safe_f! { @@ -1990,6 +1977,18 @@ safe_f! { dev |= (minor & 0xffff00) << 8; dev } + + pub {const} fn major(dev: crate::dev_t) -> c_uint { + ((dev as c_uint) >> 8) & 0xff + } + + pub {const} fn minor(dev: crate::dev_t) -> c_uint { + let dev = dev as c_uint; + let mut res = 0; + res |= (dev) & 0xff; + res |= ((dev) & 0xffff0000) >> 8; + res + } } extern "C" { diff --git a/src/unix/hurd/mod.rs b/src/unix/hurd/mod.rs index 78955110dde45..c47e7ec5e5f07 100644 --- a/src/unix/hurd/mod.rs +++ b/src/unix/hurd/mod.rs @@ -3526,14 +3526,6 @@ f! { set1.bits == set2.bits } - pub fn major(dev: crate::dev_t) -> c_uint { - ((dev >> 8) & 0xff) as c_uint - } - - pub fn minor(dev: crate::dev_t) -> c_uint { - (dev & 0xffff00ff) as c_uint - } - pub fn IPTOS_TOS(tos: u8) -> u8 { tos & IPTOS_TOS_MASK } @@ -4566,6 +4558,14 @@ safe_f! { dev } + pub {const} fn major(dev: crate::dev_t) -> c_uint { + ((dev >> 8) & 0xff) as c_uint + } + + pub {const} fn minor(dev: crate::dev_t) -> c_uint { + (dev & 0xffff00ff) as c_uint + } + pub fn SIGRTMAX() -> c_int { unsafe { __libc_current_sigrtmax() } } diff --git a/src/unix/linux_like/android/mod.rs b/src/unix/linux_like/android/mod.rs index 053f0bbbfdf42..4e67758fe6f72 100644 --- a/src/unix/linux_like/android/mod.rs +++ b/src/unix/linux_like/android/mod.rs @@ -3597,12 +3597,6 @@ f! { set1.__bits == set2.__bits } - pub fn major(dev: crate::dev_t) -> c_int { - ((dev >> 8) & 0xfff) as c_int - } - pub fn minor(dev: crate::dev_t) -> c_int { - ((dev & 0xff) | ((dev >> 12) & 0xfff00)) as c_int - } pub fn NLA_ALIGN(len: c_int) -> c_int { return ((len) + NLA_ALIGNTO - 1) & !(NLA_ALIGNTO - 1); } @@ -3618,6 +3612,14 @@ safe_f! { let mi = mi as crate::dev_t; ((ma & 0xfff) << 8) | (mi & 0xff) | ((mi & 0xfff00) << 12) } + + pub {const} fn major(dev: crate::dev_t) -> c_int { + ((dev >> 8) & 0xfff) as c_int + } + + pub {const} fn minor(dev: crate::dev_t) -> c_int { + ((dev & 0xff) | ((dev >> 12) & 0xfff00)) as c_int + } } extern "C" { diff --git a/src/unix/linux_like/emscripten/mod.rs b/src/unix/linux_like/emscripten/mod.rs index ef788152a031b..36dc2c997c0b6 100644 --- a/src/unix/linux_like/emscripten/mod.rs +++ b/src/unix/linux_like/emscripten/mod.rs @@ -1455,41 +1455,41 @@ f! { pub fn CPU_EQUAL(set1: &cpu_set_t, set2: &cpu_set_t) -> bool { set1.bits == set2.bits } +} + +safe_f! { + pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t { + let major = major as crate::dev_t; + let minor = minor as crate::dev_t; + let mut dev = 0; + dev |= (major & 0xfffff000) << 31 << 1; + dev |= (major & 0x00000fff) << 8; + dev |= (minor & 0xffffff00) << 12; + dev |= minor & 0x000000ff; + dev + } - pub fn major(dev: crate::dev_t) -> c_uint { + pub {const} fn major(dev: crate::dev_t) -> c_uint { // see // https://github.com/emscripten-core/emscripten/blob/ // main/system/lib/libc/musl/include/sys/sysmacros.h let mut major = 0; - major |= (dev & 0x00000fff) >> 8; - major |= (dev & 0xfffff000) >> 31 >> 1; + major |= (dev >> 31 >> 1) & 0xfffff000; + major |= (dev >> 8) & 0x00000fff; major as c_uint } - pub fn minor(dev: crate::dev_t) -> c_uint { + pub {const} fn minor(dev: crate::dev_t) -> c_uint { // see // https://github.com/emscripten-core/emscripten/blob/ // main/system/lib/libc/musl/include/sys/sysmacros.h let mut minor = 0; - minor |= (dev & 0x000000ff) >> 0; - minor |= (dev & 0xffffff00) >> 12; + minor |= (dev >> 12) & 0xffffff00; + minor |= dev & 0x000000ff; minor as c_uint } } -safe_f! { - pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t { - let major = major as crate::dev_t; - let minor = minor as crate::dev_t; - let mut dev = 0; - dev |= (major & 0x00000fff) << 8; - dev |= (major & 0xfffff000) << 31 << 1; - dev |= (minor & 0x000000ff) << 0; - dev |= (minor & 0xffffff00) << 12; - dev - } -} - extern "C" { pub fn getrlimit(resource: c_int, rlim: *mut crate::rlimit) -> c_int; pub fn setrlimit(resource: c_int, rlim: *const crate::rlimit) -> c_int; diff --git a/src/unix/linux_like/linux/mod.rs b/src/unix/linux_like/linux/mod.rs index 996d09f01b7f0..f2630d0a4fdf7 100644 --- a/src/unix/linux_like/linux/mod.rs +++ b/src/unix/linux_like/linux/mod.rs @@ -5969,20 +5969,6 @@ f! { () } - pub fn major(dev: crate::dev_t) -> c_uint { - let mut major = 0; - major |= (dev & 0x00000000000fff00) >> 8; - major |= (dev & 0xfffff00000000000) >> 32; - major as c_uint - } - - pub fn minor(dev: crate::dev_t) -> c_uint { - let mut minor = 0; - minor |= (dev & 0x00000000000000ff) >> 0; - minor |= (dev & 0x00000ffffff00000) >> 12; - minor as c_uint - } - pub fn IPTOS_TOS(tos: u8) -> u8 { tos & IPTOS_TOS_MASK } @@ -6074,6 +6060,20 @@ safe_f! { dev } + pub {const} fn major(dev: crate::dev_t) -> c_uint { + let mut major = 0; + major |= (dev & 0x00000000000fff00) >> 8; + major |= (dev & 0xfffff00000000000) >> 32; + major as c_uint + } + + pub {const} fn minor(dev: crate::dev_t) -> c_uint { + let mut minor = 0; + minor |= (dev & 0x00000000000000ff) >> 0; + minor |= (dev & 0x00000ffffff00000) >> 12; + minor as c_uint + } + pub {const} fn SCTP_PR_TTL_ENABLED(policy: c_int) -> bool { policy == SCTP_PR_SCTP_TTL } diff --git a/src/unix/nto/mod.rs b/src/unix/nto/mod.rs index 1a49904278476..3be0b6c5d209e 100644 --- a/src/unix/nto/mod.rs +++ b/src/unix/nto/mod.rs @@ -2803,14 +2803,6 @@ f! { let ngrps = if ngrps > 0 { ngrps - 1 } else { 0 }; mem::size_of::() + mem::size_of::() * ngrps } - - pub fn major(dev: crate::dev_t) -> c_uint { - ((dev as c_uint) >> 10) & 0x3f - } - - pub fn minor(dev: crate::dev_t) -> c_uint { - (dev as c_uint) & 0x3ff - } } safe_f! { @@ -2853,6 +2845,14 @@ safe_f! { pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t { ((major << 10) | (minor)) as crate::dev_t } + + pub {const} fn major(dev: crate::dev_t) -> c_uint { + ((dev as c_uint) >> 10) & 0x3f + } + + pub {const} fn minor(dev: crate::dev_t) -> c_uint { + (dev as c_uint) & 0x3ff + } } // Network related functions are provided by libsocket and regex