diff options
Diffstat (limited to 'ctr-std/src/sys/unix/os.rs')
| -rw-r--r-- | ctr-std/src/sys/unix/os.rs | 94 |
1 files changed, 81 insertions, 13 deletions
diff --git a/ctr-std/src/sys/unix/os.rs b/ctr-std/src/sys/unix/os.rs index de087d9..5de756d 100644 --- a/ctr-std/src/sys/unix/os.rs +++ b/ctr-std/src/sys/unix/os.rs @@ -1,4 +1,4 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -8,26 +8,17 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! Implementation of `std::os` functionality for unix systems - -#![allow(unused_imports)] // lots of cfg code here - -use os::unix::prelude::*; - use error::Error as StdError; use ffi::{CString, CStr, OsString, OsStr}; use fmt; use io; use iter; -use libc::{self, c_int, c_char, c_void}; -use marker::PhantomData; -use mem; -use memchr; +use libc::{self, c_int, c_char}; use path::{self, PathBuf}; -use ptr; use slice; use str; -use vec; +use sys::{unsupported, Void}; +use sys::unix::ext::ffi::{OsStrExt, OsStringExt}; const TMPBUF_SZ: usize = 128; @@ -64,6 +55,42 @@ pub fn error_string(errno: i32) -> String { } } +pub fn getcwd() -> io::Result<PathBuf> { + let mut buf = Vec::with_capacity(512); + loop { + unsafe { + let ptr = buf.as_mut_ptr() as *mut libc::c_char; + if !libc::getcwd(ptr, buf.capacity()).is_null() { + let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len(); + buf.set_len(len); + buf.shrink_to_fit(); + return Ok(PathBuf::from(OsString::from_vec(buf))); + } else { + let error = io::Error::last_os_error(); + if error.raw_os_error() != Some(libc::ERANGE) { + return Err(error); + } + } + + // Trigger the internal buffer resizing logic of `Vec` by requiring + // more space than the current capacity. + let cap = buf.capacity(); + buf.set_len(cap); + buf.reserve(1); + } + } +} + +pub fn chdir(p: &path::Path) -> io::Result<()> { + let p: &OsStr = p.as_ref(); + let p = CString::new(p.as_bytes())?; + unsafe { + match libc::chdir(p.as_ptr()) == (0 as c_int) { + true => Ok(()), + false => Err(io::Error::last_os_error()), + } + } +} pub struct SplitPaths<'a> { iter: iter::Map<slice::Split<'a, u8, fn(&u8) -> bool>, @@ -118,6 +145,47 @@ impl StdError for JoinPathsError { fn description(&self) -> &str { "failed to join paths" } } +pub fn current_exe() -> io::Result<PathBuf> { + unsupported() +} + +pub struct Env(Void); + +impl Iterator for Env { + type Item = (OsString, OsString); + fn next(&mut self) -> Option<(OsString, OsString)> { + match self.0 {} + } +} + +pub fn env() -> Env { + panic!("not supported on 3DS yet") +} + +pub fn getenv(_k: &OsStr) -> io::Result<Option<OsString>> { + return Ok(None) +} + +pub fn setenv(_k: &OsStr, _v: &OsStr) -> io::Result<()> { + unsupported() +} + +pub fn unsetenv(_n: &OsStr) -> io::Result<()> { + unsupported() +} + +pub fn temp_dir() -> PathBuf { + PathBuf::from("/tmp") +} + +pub fn home_dir() -> Option<PathBuf> { + None +} + pub fn exit(code: i32) -> ! { unsafe { libc::exit(code as c_int) } } + +pub fn getpid() -> u32 { + panic!("no pids on 3DS") +} |