refactor all unix types
This commit is contained in:
@@ -1,68 +1,77 @@
|
||||
import ctypes::*;
|
||||
|
||||
export libc;
|
||||
export libc_constants;
|
||||
export pipe;
|
||||
export fd_FILE;
|
||||
export close;
|
||||
export fclose;
|
||||
export waitpid;
|
||||
export getcwd;
|
||||
export exec_suffix;
|
||||
export target_os;
|
||||
export dylib_filename;
|
||||
export get_exe_path;
|
||||
|
||||
// FIXME Refactor into unix_os module or some such. Doesn't
|
||||
// seem to work right now.
|
||||
|
||||
#[link_name = ""]
|
||||
#[abi = "cdecl"]
|
||||
native mod libc {
|
||||
fn read(fd: int, buf: *u8, count: uint) -> int;
|
||||
fn write(fd: int, buf: *u8, count: uint) -> int;
|
||||
fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
|
||||
fn fwrite(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
|
||||
fn open(s: str::sbuf, flags: int, mode: uint) -> int;
|
||||
fn close(fd: int) -> int;
|
||||
fn read(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
|
||||
fn write(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
|
||||
fn fread(buf: *u8, size: size_t, n: size_t, f: libc::FILE) -> size_t;
|
||||
fn fwrite(buf: *u8, size: size_t, n: size_t, f: libc::FILE) -> size_t;
|
||||
fn open(s: str::sbuf, flags: c_int, mode: unsigned) -> fd_t;
|
||||
fn close(fd: fd_t) -> int;
|
||||
type FILE;
|
||||
fn fopen(path: str::sbuf, mode: str::sbuf) -> FILE;
|
||||
fn fdopen(fd: int, mode: str::sbuf) -> FILE;
|
||||
fn fdopen(fd: fd_t, mode: str::sbuf) -> FILE;
|
||||
fn fclose(f: FILE);
|
||||
fn fgetc(f: FILE) -> int;
|
||||
fn ungetc(c: int, f: FILE);
|
||||
fn feof(f: FILE) -> int;
|
||||
fn fseek(f: FILE, offset: int, whence: int) -> int;
|
||||
fn ftell(f: FILE) -> int;
|
||||
fn fgetc(f: FILE) -> c_int;
|
||||
fn ungetc(c: c_int, f: FILE);
|
||||
fn feof(f: FILE) -> c_int;
|
||||
fn fseek(f: FILE, offset: long, whence: c_int) -> c_int;
|
||||
fn ftell(f: FILE) -> long;
|
||||
type dir;
|
||||
fn opendir(d: str::sbuf) -> dir;
|
||||
fn closedir(d: dir) -> int;
|
||||
fn closedir(d: dir) -> c_int;
|
||||
type dirent;
|
||||
fn readdir(d: dir) -> dirent;
|
||||
fn getenv(n: str::sbuf) -> str::sbuf;
|
||||
fn setenv(n: str::sbuf, v: str::sbuf, overwrite: int) -> int;
|
||||
fn unsetenv(n: str::sbuf) -> int;
|
||||
fn pipe(buf: *mutable int) -> int;
|
||||
fn waitpid(pid: int, &status: int, options: int) -> int;
|
||||
fn _NSGetExecutablePath(buf: str::sbuf,
|
||||
bufsize: *mutable ctypes::uint32_t) -> int;
|
||||
fn setenv(n: str::sbuf, v: str::sbuf, overwrite: c_int) -> c_int;
|
||||
fn unsetenv(n: str::sbuf) -> c_int;
|
||||
fn pipe(buf: *mutable fd_t) -> c_int;
|
||||
fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> pid_t;
|
||||
}
|
||||
|
||||
mod libc_constants {
|
||||
const O_RDONLY: int = 0;
|
||||
const O_WRONLY: int = 1;
|
||||
const O_RDWR: int = 2;
|
||||
const O_APPEND: int = 8;
|
||||
const O_CREAT: int = 512;
|
||||
const O_EXCL: int = 2048;
|
||||
const O_TRUNC: int = 1024;
|
||||
const O_TEXT: int = 0; // nonexistent in darwin libc
|
||||
const O_BINARY: int = 0; // nonexistent in darwin libc
|
||||
const O_RDONLY: int = 0i32;
|
||||
const O_WRONLY: int = 1i32;
|
||||
const O_RDWR: int = 2i32;
|
||||
const O_APPEND: int = 8i32;
|
||||
const O_CREAT: int = 512i32;
|
||||
const O_EXCL: int = 2048i32;
|
||||
const O_TRUNC: int = 1024i32;
|
||||
const O_TEXT: int = 0i32; // nonexistent in darwin libc
|
||||
const O_BINARY: int = 0i32; // nonexistent in darwin libc
|
||||
|
||||
const S_IRUSR: uint = 256u;
|
||||
const S_IWUSR: uint = 128u;
|
||||
const S_IRUSR: uint = 256u32;
|
||||
const S_IWUSR: uint = 128u32;
|
||||
}
|
||||
|
||||
// FIXME turn into constants
|
||||
fn exec_suffix() -> str { ret ""; }
|
||||
fn target_os() -> str { ret "macos"; }
|
||||
|
||||
fn dylib_filename(base: str) -> str { ret "lib" + base + ".dylib"; }
|
||||
|
||||
fn pipe() -> {in: int, out: int} {
|
||||
let fds = {mutable in: 0, mutable out: 0};
|
||||
assert (os::libc::pipe(ptr::mut_addr_of(fds.in)) == 0);
|
||||
fn pipe() -> {in: fd_t, out: fd_t} {
|
||||
let fds = {mutable in: 0i32, mutable out: 0i32};
|
||||
assert (os::libc::pipe(ptr::mut_addr_of(fds.in)) == 0i32);
|
||||
ret {in: fds.in, out: fds.out};
|
||||
}
|
||||
|
||||
fn fd_FILE(fd: int) -> libc::FILE {
|
||||
fn fd_FILE(fd: fd_t) -> libc::FILE {
|
||||
ret str::as_buf("r", {|modebuf| libc::fdopen(fd, modebuf) });
|
||||
}
|
||||
|
||||
fn close(fd: int) -> int {
|
||||
fn close(fd: fd_t) -> int {
|
||||
libc::close(fd)
|
||||
}
|
||||
|
||||
@@ -70,9 +79,9 @@ fn fclose(file: libc::FILE) {
|
||||
libc::fclose(file)
|
||||
}
|
||||
|
||||
fn waitpid(pid: int) -> int {
|
||||
let status = 0;
|
||||
assert (os::libc::waitpid(pid, status, 0) != -1);
|
||||
fn waitpid(pid: pid_t) -> i32 {
|
||||
let status = 0i32;
|
||||
assert (os::libc::waitpid(pid, status, 0i32) != -1i32);
|
||||
ret status;
|
||||
}
|
||||
|
||||
@@ -83,13 +92,24 @@ native mod rustrt {
|
||||
|
||||
fn getcwd() -> str { ret rustrt::rust_getcwd(); }
|
||||
|
||||
native "cdecl" mod mac_libc = "" {
|
||||
fn _NSGetExecutablePath(buf: str::sbuf,
|
||||
bufsize: *mutable uint32_t) -> c_int;
|
||||
}
|
||||
|
||||
fn exec_suffix() -> str { ret ""; }
|
||||
|
||||
fn target_os() -> str { ret "macos"; }
|
||||
|
||||
fn dylib_filename(base: str) -> str { ret "lib" + base + ".dylib"; }
|
||||
|
||||
fn get_exe_path() -> option::t<fs::path> {
|
||||
// FIXME: This doesn't handle the case where the buffer is too small
|
||||
let bufsize = 1023u32;
|
||||
let path = str::unsafe_from_bytes(vec::init_elt(0u8, bufsize as uint));
|
||||
ret str::as_buf(path, { |path_buf|
|
||||
if libc::_NSGetExecutablePath(path_buf,
|
||||
ptr::mut_addr_of(bufsize)) == 0 {
|
||||
if mac_libc::_NSGetExecutablePath(path_buf,
|
||||
ptr::mut_addr_of(bufsize)) == 0i32 {
|
||||
option::some(fs::dirname(path) + fs::path_sep())
|
||||
} else {
|
||||
option::none
|
||||
|
||||
Reference in New Issue
Block a user