#[cfg(not(target_os = "windows"))]
use std::fs;
#[cfg(not(target_os = "windows"))]
use std::path::{Path, PathBuf};
#[cfg(not(target_os = "windows"))]
use std::ffi::OsStr;
#[cfg(not(target_os = "windows"))]
use std::os::unix::ffi::OsStrExt;
#[cfg(not(target_os = "windows"))]
use libc::{c_char, lstat, stat, S_IFLNK, S_IFMT};
use Pid;
#[cfg(not(target_os = "windows"))]
pub fn realpath(original: &Path) -> PathBuf {
fn and(x: u32, y: u32) -> u32 {
x & y
}
if let Some(original_str) = original.to_str() {
let ori = Path::new(original_str);
if cfg!(windows) {
return PathBuf::from(ori);
}
let result = PathBuf::from(original);
let mut result_s = result.to_str().unwrap_or("").as_bytes().to_vec();
result_s.push(0);
let mut buf: stat = unsafe { ::std::mem::uninitialized() };
let res = unsafe { lstat(result_s.as_ptr() as *const c_char,
&mut buf as *mut stat) };
if res < 0 || and(buf.st_mode.into(), S_IFMT.into()) != S_IFLNK.into() {
PathBuf::new()
} else {
match fs::read_link(&result) {
Ok(f) => f,
Err(_) => PathBuf::new(),
}
}
} else {
PathBuf::new()
}
}
#[cfg(not(target_os = "windows"))]
pub fn to_cpath(path: &Path) -> Vec<u8> {
let path_os: &OsStr = path.as_ref();
let mut cpath = path_os.as_bytes().to_vec();
cpath.push(0);
cpath
}
#[cfg(not(target_os = "windows"))]
pub fn get_current_pid() -> Pid {
unsafe { ::libc::getpid() }
}
#[cfg(target_os = "windows")]
pub fn get_current_pid() -> Pid {
use winapi::um::processthreadsapi::GetCurrentProcessId;
unsafe { GetCurrentProcessId() as Pid }
}