1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-04-25 16:54:32 +02:00

Fix safe FFI exercise on macOS (#572)

* Fix safe FFI exercise on macOS

Use the macOS definition of dirent.

Fixes #570

---------

Co-authored-by: Martin Geisler <martin@geisler.net>
This commit is contained in:
Matt Schulte 2023-04-20 21:10:12 -04:00 committed by GitHub
parent e371fd7e54
commit 6a61829d85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,9 @@
// ANCHOR: ffi
mod ffi {
use std::os::raw::{c_char, c_int, c_long, c_ulong, c_ushort};
use std::os::raw::{c_char, c_int};
#[cfg(not(target_os = "macos"))]
use std::os::raw::{c_long, c_ulong, c_ushort};
// Opaque type. See https://doc.rust-lang.org/nomicon/ffi.html.
#[repr(C)]
@ -24,6 +26,7 @@ mod ffi {
}
// Layout as per readdir(3) and definitions in /usr/include/x86_64-linux-gnu.
#[cfg(not(target_os = "macos"))]
#[repr(C)]
pub struct dirent {
pub d_ino: c_long,
@ -33,6 +36,18 @@ mod ffi {
pub d_name: [c_char; 256],
}
// Layout as per man entry for dirent
#[cfg(target_os = "macos")]
#[repr(C)]
pub struct dirent {
pub d_ino: u64,
pub d_seekoff: u64,
pub d_reclen: u16,
pub d_namlen: u16,
pub d_type: u8,
pub d_name: [c_char; 1024],
}
extern "C" {
pub fn opendir(s: *const c_char) -> *mut DIR;
pub fn readdir(s: *mut DIR) -> *const dirent;