oscall 0.1.1

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
//! The arguments and the environment on Windows: the command line, split
//! as the C runtime splits it, and the environment block.

use crate::Result;
use crate::process::env::{WideWriter, split_command_line};
use crate::sys::{dll, last_error};

dll!("kernel32.dll" {
    fn GetCommandLineW() -> *const u16;
    fn GetModuleFileNameW(module: *mut core::ffi::c_void, name: *mut u16, size: u32) -> u32;
    fn GetEnvironmentStringsW() -> *mut u16;
    fn FreeEnvironmentStringsW(block: *mut u16) -> i32;
});

const ERROR_INSUFFICIENT_BUFFER: i32 = 122;

/// The units of a NUL-terminated string, without the NUL.
///
/// # Safety
///
/// `string` points to a string that a NUL ends and that outlives the
/// iterator.
unsafe fn units(string: *const u16) -> impl Iterator<Item = u16> {
    (0..)
        // SAFETY: the caller's contract: nothing is read past the NUL,
        // where `take_while` stops.
        .map(move |i| unsafe { string.add(i).read() })
        .take_while(|&unit| unit != 0)
}

pub(crate) fn args(buf: &mut [u8]) -> Result<usize> {
    let mut out = WideWriter::new(buf);
    // SAFETY: the function returns a pointer to the command line of the
    // process, a NUL-terminated string that lives as long as the process.
    let line = unsafe { GetCommandLineW() };
    if !line.is_null() {
        // SAFETY: as above.
        split_command_line(unsafe { units(line) }, &mut out)?;
    }
    if out.len() == 0 {
        // An empty command line: the one argument is the program's path,
        // as the C runtime and `std` have it.
        let mut path = [0u16; 1024];
        // SAFETY: the function writes at most the size given, and a null
        // module is the program of the process.
        let len = unsafe {
            GetModuleFileNameW(core::ptr::null_mut(), path.as_mut_ptr(), path.len() as u32)
        };
        if len == 0 {
            return Err(last_error());
        }
        if len as usize >= path.len() {
            return Err(crate::Error::from_raw_os_error(ERROR_INSUFFICIENT_BUFFER));
        }
        for &unit in &path[..len as usize] {
            out.push(unit)?;
        }
        out.end()?;
    }
    Ok(out.len())
}

pub(crate) fn vars(buf: &mut [u8]) -> Result<usize> {
    // SAFETY: the function returns a copy of the environment block, made
    // under the lock of the process's environment, or null.
    let block = unsafe { GetEnvironmentStringsW() };
    if block.is_null() {
        return Err(last_error());
    }
    let mut out = WideWriter::new(buf);
    let mut at = block.cast_const();
    let result = loop {
        // SAFETY: the block is strings that each end with a NUL, and an
        // empty one after the last: `at` is the start of one of them.
        if unsafe { at.read() } == 0 {
            break Ok(out.len());
        }
        let mut len = 0;
        // SAFETY: as above.
        let pushed = unsafe { units(at) }.try_for_each(|unit| {
            len += 1;
            out.push(unit)
        });
        if let Err(error) = pushed.and_then(|()| out.end()) {
            break Err(error);
        }
        // SAFETY: past the string and its NUL is the next string, or the
        // NUL that ends the block.
        at = unsafe { at.add(len + 1) };
    };
    // SAFETY: the block came from GetEnvironmentStringsW and is not used
    // afterwards.
    unsafe { FreeEnvironmentStringsW(block) };
    result
}