diff options
-rw-r--r-- | src/unistd.rs | 13 | ||||
-rw-r--r-- | test/test_unistd.rs | 10 |
2 files changed, 23 insertions, 0 deletions
diff --git a/src/unistd.rs b/src/unistd.rs index 71448248..31367791 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -374,6 +374,19 @@ pub fn sleep(seconds: libc::c_uint) -> c_uint { unsafe { libc::sleep(seconds) } } +#[inline] +pub fn mkstemp<P: ?Sized + NixPath>(template: &P) -> Result<RawFd> { + let res = try!(template.with_nix_path(|path| { + let mut path_copy = path.to_bytes_with_nul().to_owned(); + let c_template: *mut c_char = path_copy.as_mut_ptr() as *mut c_char; + unsafe { + libc::mkstemp(c_template) + } + })); + Errno::result(res) + +} + #[cfg(any(target_os = "linux", target_os = "android"))] mod linux { use sys::syscall::{syscall, SYSPIVOTROOT}; diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 410a32d5..50ff8e87 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -45,6 +45,16 @@ fn test_wait() { } } +#[test] +fn test_mkstemp() { + let result = mkstemp("/tmp/tempfile.XXXXXXXX"); + match result { + Ok(fd) => { + close(fd).expect("Couldn't close the file descriptor"); + } + Err(e) => panic!("mkstemp failed: {}", e) + } +} #[test] fn test_getpid() { |