Skip to content

Add mkstemp(3) #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably move the newline to before the final expression rather than after it.

}

#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux {
use sys::syscall::{syscall, SYSPIVOTROOT};
Expand Down
10 changes: 10 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down