2011-10-26 16:24:31 -07:00
|
|
|
/*
|
|
|
|
|
Module: os
|
|
|
|
|
|
|
|
|
|
TODO: Restructure and document
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-14 22:29:40 -08:00
|
|
|
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;
|
2011-11-16 23:14:18 +01:00
|
|
|
export fsync_fd;
|
2011-11-14 22:29:40 -08:00
|
|
|
|
2011-03-15 17:13:19 -07:00
|
|
|
// FIXME Somehow merge stuff duplicated here and macosx_os.rs. Made difficult
|
2011-03-11 13:30:18 +01:00
|
|
|
// by https://github.com/graydon/rust/issues#issue/268
|
2011-11-14 22:29:40 -08:00
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[link_name = ""]
|
|
|
|
|
#[abi = "cdecl"]
|
|
|
|
|
native mod libc {
|
2011-11-14 22:29:40 -08:00
|
|
|
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;
|
2011-11-15 16:49:25 -08:00
|
|
|
fn close(fd: fd_t) -> c_int;
|
2010-09-22 15:44:13 -07:00
|
|
|
type FILE;
|
2011-09-01 17:27:58 -07:00
|
|
|
fn fopen(path: str::sbuf, mode: str::sbuf) -> FILE;
|
2011-11-14 22:29:40 -08:00
|
|
|
fn fdopen(fd: fd_t, mode: str::sbuf) -> FILE;
|
2011-07-27 14:19:39 +02:00
|
|
|
fn fclose(f: FILE);
|
2011-11-16 23:14:18 +01:00
|
|
|
fn fflush(f: FILE) -> c_int;
|
|
|
|
|
fn fsync(fd: fd_t) -> c_int;
|
|
|
|
|
fn fdatasync(fd: fd_t) -> c_int;
|
|
|
|
|
fn fileno(f: FILE) -> fd_t;
|
2011-11-14 22:29:40 -08:00
|
|
|
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;
|
2010-09-22 15:44:13 -07:00
|
|
|
type dir;
|
2011-09-01 17:27:58 -07:00
|
|
|
fn opendir(d: str::sbuf) -> dir;
|
2011-11-14 22:29:40 -08:00
|
|
|
fn closedir(d: dir) -> c_int;
|
2011-03-10 15:56:51 +01:00
|
|
|
type dirent;
|
2011-07-27 14:19:39 +02:00
|
|
|
fn readdir(d: dir) -> dirent;
|
2011-09-01 17:27:58 -07:00
|
|
|
fn getenv(n: str::sbuf) -> str::sbuf;
|
2011-11-14 22:29:40 -08:00
|
|
|
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;
|
2011-11-15 14:27:55 -08:00
|
|
|
fn readlink(path: str::sbuf, buf: str::sbuf, bufsize: size_t) -> ssize_t;
|
2011-11-22 16:11:03 -08:00
|
|
|
fn mkdir(path: str::sbuf, mode: c_int) -> c_int;
|
|
|
|
|
fn rmdir(path: str::sbuf) -> c_int;
|
2011-12-01 22:19:10 -05:00
|
|
|
fn chdir(path: str::sbuf) -> c_int;
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
2010-08-04 17:14:11 -07:00
|
|
|
|
|
|
|
|
mod libc_constants {
|
2011-11-15 16:49:25 -08:00
|
|
|
const O_RDONLY: c_int = 0i32;
|
|
|
|
|
const O_WRONLY: c_int = 1i32;
|
|
|
|
|
const O_RDWR: c_int = 2i32;
|
|
|
|
|
const O_APPEND: c_int = 1024i32;
|
|
|
|
|
const O_CREAT: c_int = 64i32;
|
|
|
|
|
const O_EXCL: c_int = 128i32;
|
|
|
|
|
const O_TRUNC: c_int = 512i32;
|
|
|
|
|
const O_TEXT: c_int = 0i32; // nonexistent in linux libc
|
|
|
|
|
const O_BINARY: c_int = 0i32; // nonexistent in linux libc
|
2010-09-22 15:44:13 -07:00
|
|
|
|
2011-11-15 16:49:25 -08:00
|
|
|
const S_IRUSR: unsigned = 256u32;
|
|
|
|
|
const S_IWUSR: unsigned = 128u32;
|
2011-11-14 22:29:40 -08:00
|
|
|
}
|
2011-03-15 16:56:59 -07:00
|
|
|
|
2011-11-14 22:29:40 -08:00
|
|
|
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);
|
2011-07-27 14:19:39 +02:00
|
|
|
ret {in: fds.in, out: fds.out};
|
2011-03-11 13:30:18 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-14 22:29:40 -08:00
|
|
|
fn fd_FILE(fd: fd_t) -> libc::FILE {
|
2011-09-02 15:34:58 -07:00
|
|
|
ret str::as_buf("r", {|modebuf| libc::fdopen(fd, modebuf) });
|
2011-08-31 17:34:50 -07:00
|
|
|
}
|
2011-03-11 13:30:18 +01:00
|
|
|
|
2011-11-15 16:49:25 -08:00
|
|
|
fn close(fd: fd_t) -> c_int {
|
2011-11-08 11:09:40 -08:00
|
|
|
libc::close(fd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn fclose(file: libc::FILE) {
|
|
|
|
|
libc::fclose(file)
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-16 23:14:18 +01:00
|
|
|
fn fsync_fd(fd: fd_t, level: io::fsync::level) -> c_int {
|
|
|
|
|
alt level {
|
|
|
|
|
io::fsync::fsync. | io::fsync::fullfsync. { ret libc::fsync(fd); }
|
|
|
|
|
io::fsync::fdatasync. { ret libc::fdatasync(fd); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-14 22:29:40 -08:00
|
|
|
fn waitpid(pid: pid_t) -> i32 {
|
|
|
|
|
let status = 0i32;
|
|
|
|
|
assert (os::libc::waitpid(pid, status, 0i32) != -1i32);
|
2011-07-12 18:28:40 -07:00
|
|
|
ret status;
|
2011-03-11 13:30:18 +01:00
|
|
|
}
|
2011-06-17 11:12:51 -07:00
|
|
|
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
|
|
|
|
native mod rustrt {
|
2011-09-02 15:34:58 -07:00
|
|
|
fn rust_getcwd() -> str;
|
2011-06-17 11:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
2011-09-02 15:34:58 -07:00
|
|
|
fn getcwd() -> str { ret rustrt::rust_getcwd(); }
|
2011-06-17 11:12:51 -07:00
|
|
|
|
2011-11-14 22:29:40 -08:00
|
|
|
fn exec_suffix() -> str { ret ""; }
|
|
|
|
|
|
|
|
|
|
fn target_os() -> str { ret "linux"; }
|
|
|
|
|
|
|
|
|
|
fn dylib_filename(base: str) -> str { ret "lib" + base + ".so"; }
|
|
|
|
|
|
2011-10-04 13:35:39 -07:00
|
|
|
/// Returns the directory containing the running program
|
|
|
|
|
/// followed by a path separator
|
|
|
|
|
fn get_exe_path() -> option::t<fs::path> {
|
|
|
|
|
let bufsize = 1023u;
|
|
|
|
|
let path = str::unsafe_from_bytes(vec::init_elt(0u8, bufsize));
|
|
|
|
|
ret str::as_buf("/proc/self/exe", { |proc_self_buf|
|
|
|
|
|
str::as_buf(path, { |path_buf|
|
|
|
|
|
if libc::readlink(proc_self_buf, path_buf, bufsize) != -1 {
|
|
|
|
|
option::some(fs::dirname(path) + fs::path_sep())
|
|
|
|
|
} else {
|
|
|
|
|
option::none
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
2011-06-17 11:12:51 -07:00
|
|
|
|
2010-09-22 15:44:13 -07:00
|
|
|
// Local Variables:
|
|
|
|
|
// mode: rust;
|
|
|
|
|
// fill-column: 78;
|
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
|
// c-basic-offset: 4
|
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
|
// End:
|