How do I write to a specific raw file descriptor from Rust?

后端 未结 2 1397
不思量自难忘°
不思量自难忘° 2021-01-18 23:21

I need to write to file descriptor 3. I have been searching for it but the documentation is quite poor. The only thing that I have found is the use of libc libr

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-19 00:24

    The libc crate is "just" a wrapping library to interface between C and Rust so to know how to use a function one should read the manual of the C function, there are many source for that, here one for fdopen():

    The fdopen() function associates a stream with the existing file descriptor, fd. The mode of the stream (one of the values "r", "r+", "w", "w+", "a", "a+") must be compatible with the mode of the file descriptor. The file position indicator of the new stream is set to that belonging to fd, and the error and end-of-file indicators are cleared. Modes "w" or "w+" do not cause truncation of the file. The file descriptor is not dup'ed, and will be closed when the stream created by fdopen() is closed. The result of applying fdopen() to a shared memory object is undefined.

    Basic use is so:

    use libc::fdopen;
    use std::ffi::CString;
    
    fn main() {
        let mode = CString::new("w").unwrap();
        unsafe {
            let _ = fdopen(3, mode.as_ptr());
        }
    }
    

    To use it, you could use fwrite():

    The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

    So, complete example:

    use libc::{c_void, fdopen, fwrite};
    use std::ffi::CString;
    
    fn main() {
        let mode = CString::new("w").unwrap();
        let file = unsafe {
            let file = fdopen(3, mode.as_ptr());
            if file.is_null() {
                panic!("can't open file");
            }
            file
        };
    
        let welcome = "Hello world!";
    
        let result = unsafe { fwrite(welcome.as_ptr() as *const c_void, 1, welcome.len(), file) };
        if result != welcome.len() {
            panic!("write not successful");
        }
    }
    

提交回复
热议问题