From 3f79ddf06375c3b34569c8b9ce91a607bbd0f052 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sun, 28 Jun 2020 15:14:04 +0200 Subject: pkgs/profpatsch/execline: add el_exec and el_substitute el_exec: wraps the various execve wrappers in skalib that are useful for writing execline-like utils. currently only `xpathexec0` is supported, which execs into the argv you give it or errors with the right error if file not found. el_substitute: execline argv substitution! Wraps the execline function, so it will behave exactly the same as the existing execline utils, like `importas`. --- pkgs/profpatsch/execline/el_exec.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 pkgs/profpatsch/execline/el_exec.rs (limited to 'pkgs/profpatsch/execline/el_exec.rs') diff --git a/pkgs/profpatsch/execline/el_exec.rs b/pkgs/profpatsch/execline/el_exec.rs new file mode 100644 index 00000000..25fad36f --- /dev/null +++ b/pkgs/profpatsch/execline/el_exec.rs @@ -0,0 +1,32 @@ +use std::ffi::CStr; +extern crate libc; + +fn to_c_argv>(s: &[S]) -> (&[S], Vec<*const libc::c_char>) { + let mut c_ptr_args = s.iter() + .map(|s| s.as_ref().as_ptr()) + .collect::>(); + c_ptr_args.push(std::ptr::null()); + (s, c_ptr_args) +} + +/// Exec into argv, or exit 0 if it’s empty. +/// Will throw 127 if the executable is not found (ENOENT) +/// and 126 for any other exec error. +pub fn xpathexec0<'a, S: AsRef>(argv: &'a [S]) { + let (c_strings, c_argv) = to_c_argv(argv); + + unsafe { + C::xpathexec0( + c_argv.as_ptr() as *const *const libc::c_char + ) + } +} + +mod C { + #[link(name = "skarnet")] + extern "C" { + pub fn xpathexec0( + argv: *const *const libc::c_char + ); + } +} -- cgit 1.4.1