about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorStefan Plantikow <stefan.plantikow@googlemail.com>2012-01-05 00:11:25 +0100
committerStefan Plantikow <stefan.plantikow@googlemail.com>2012-01-05 02:07:12 +0100
commit6284190ef9918e05cb9147a2a81100ddcb06fea8 (patch)
treee5e8075a4de03db9c4efee2b6d994343a674b3b5 /src/libstd
parent16405386f0a843167e234d8d54855a537b0f261d (diff)
parent3971b520bcdd556ff78120c77ffd13785e1c3695 (diff)
downloadrust-6284190ef9918e05cb9147a2a81100ddcb06fea8.tar.gz
rust-6284190ef9918e05cb9147a2a81100ddcb06fea8.zip
Merge branch 'master' into kmath
Conflicts:
	src/libcore/cmath.rs
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/deque.rs2
-rw-r--r--src/libstd/freebsd_os.rs155
-rw-r--r--src/libstd/fs.rs4
-rw-r--r--src/libstd/generic_os.rs4
-rw-r--r--src/libstd/list.rs35
-rw-r--r--src/libstd/rope.rs2
-rw-r--r--src/libstd/run_program.rs3
-rw-r--r--src/libstd/std.rc7
-rw-r--r--src/libstd/util.rs2
-rw-r--r--src/libstd/uv.rs5
10 files changed, 210 insertions, 9 deletions
diff --git a/src/libstd/deque.rs b/src/libstd/deque.rs
index 7c0a13bb5f8..8da26c55b10 100644
--- a/src/libstd/deque.rs
+++ b/src/libstd/deque.rs
@@ -57,7 +57,7 @@ fn create<copy T>() -> t<T> {
 
         ret rv;
     }
-    fn get<T>(elts: [mutable cell<T>], i: uint) -> T {
+    fn get<copy T>(elts: [mutable cell<T>], i: uint) -> T {
         ret alt elts[i] { option::some(t) { t } _ { fail } };
     }
     obj deque<copy T>(mutable nelts: uint,
diff --git a/src/libstd/freebsd_os.rs b/src/libstd/freebsd_os.rs
new file mode 100644
index 00000000000..4fecf418213
--- /dev/null
+++ b/src/libstd/freebsd_os.rs
@@ -0,0 +1,155 @@
+/*
+Module: os
+
+TODO: Restructure and document
+*/
+
+import core::option;
+import core::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;
+export fsync_fd;
+
+// FIXME Somehow merge stuff duplicated here and macosx_os.rs. Made difficult
+// by https://github.com/graydon/rust/issues#issue/268
+
+#[link_name = ""]               // FIXME remove after #[nolink] is snapshotted
+#[nolink]
+#[abi = "cdecl"]
+native mod libc {
+    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) -> c_int;
+    type FILE;
+    fn fopen(path: str::sbuf, mode: str::sbuf) -> FILE;
+    fn fdopen(fd: fd_t, mode: str::sbuf) -> FILE;
+    fn fclose(f: FILE);
+    fn fflush(f: FILE) -> c_int;
+    fn fsync(fd: fd_t) -> c_int;
+    fn fileno(f: FILE) -> fd_t;
+    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) -> 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: 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;
+    fn readlink(path: str::sbuf, buf: str::sbuf, bufsize: size_t) -> ssize_t;
+    fn mkdir(path: str::sbuf, mode: c_int) -> c_int;
+    fn rmdir(path: str::sbuf) -> c_int;
+    fn chdir(path: str::sbuf) -> c_int;
+
+    fn sysctl(name: *c_int, namelen: c_uint,
+              oldp: *u8, &oldlenp: size_t,
+              newp: *u8, newlen: size_t) -> c_int;
+}
+
+mod libc_constants {
+    const O_RDONLY: c_int = 0i32;
+    const O_WRONLY: c_int = 1i32;
+    const O_RDWR: c_int   = 2i32;
+    const O_APPEND: c_int = 8i32;
+    const O_CREAT: c_int  = 512i32;
+    const O_EXCL: c_int   = 2048i32;
+    const O_TRUNC: c_int  = 1024i32;
+    const O_TEXT: c_int   = 0i32;     // nonexistent in FreeBSD libc
+    const O_BINARY: c_int = 0i32;     // nonexistent in FreeBSD libc
+
+    const S_IRUSR: unsigned = 256u32;
+    const S_IWUSR: unsigned = 128u32;
+
+    const CTL_KERN: c_int = 1i32;
+    const KERN_PROC: c_int = 14i32;
+    const KERN_PROC_PATHNAME: c_int = 12i32;
+}
+
+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: fd_t) -> libc::FILE {
+    ret str::as_buf("r", {|modebuf| libc::fdopen(fd, modebuf) });
+}
+
+fn close(fd: fd_t) -> c_int {
+    libc::close(fd)
+}
+
+fn fclose(file: libc::FILE) {
+    libc::fclose(file)
+}
+
+fn fsync_fd(fd: fd_t, _l: io::fsync::level) -> c_int {
+    ret libc::fsync(fd);
+}
+
+fn waitpid(pid: pid_t) -> i32 {
+    let status = 0i32;
+    assert (os::libc::waitpid(pid, status, 0i32) != -1i32);
+    ret status;
+}
+
+#[abi = "cdecl"]
+native mod rustrt {
+    fn rust_getcwd() -> str;
+}
+
+fn getcwd() -> str { ret rustrt::rust_getcwd(); }
+
+fn exec_suffix() -> str { ret ""; }
+
+fn target_os() -> str { ret "freebsd"; }
+
+fn dylib_filename(base: str) -> str { ret "lib" + base + ".so"; }
+
+/// Returns the directory containing the running program
+/// followed by a path separator
+fn get_exe_path() -> option::t<fs::path> unsafe {
+    let bufsize = 1023u;
+    let path = str::unsafe_from_bytes(vec::init_elt(0u8, bufsize));
+    let mib = [libc_constants::CTL_KERN,
+               libc_constants::KERN_PROC,
+               libc_constants::KERN_PROC_PATHNAME, -1i32];
+    ret str::as_buf(path, { |path_buf|
+        if libc::sysctl(vec::unsafe::to_ptr(mib),
+                        vec::len(mib) as c_uint,
+                        path_buf, bufsize,
+                        ptr::null(), 0u) == 0i32 {
+            option::some(fs::dirname(path) + fs::path_sep())
+        } else {
+            option::none
+        }
+    });
+}
+
+// Local Variables:
+// mode: rust;
+// fill-column: 78;
+// indent-tabs-mode: nil
+// c-basic-offset: 4
+// buffer-file-coding-system: utf-8-unix
+// End:
diff --git a/src/libstd/fs.rs b/src/libstd/fs.rs
index 6c8c614ab55..33030565143 100644
--- a/src/libstd/fs.rs
+++ b/src/libstd/fs.rs
@@ -148,6 +148,7 @@ fn make_dir(p: path, mode: ctypes::c_int) -> bool {
 
     #[cfg(target_os = "linux")]
     #[cfg(target_os = "macos")]
+    #[cfg(target_os = "freebsd")]
     fn mkdir(_p: path, _mode: ctypes::c_int) -> bool {
         ret str::as_buf(_p, {|buf| os::libc::mkdir(buf, _mode) == 0i32 });
     }
@@ -186,6 +187,7 @@ fn remove_dir(p: path) -> bool {
 
     #[cfg(target_os = "linux")]
     #[cfg(target_os = "macos")]
+    #[cfg(target_os = "freebsd")]
     fn rmdir(_p: path) -> bool {
         ret str::as_buf(_p, {|buf| os::libc::rmdir(buf) == 0i32 });
     }
@@ -201,6 +203,7 @@ fn change_dir(p: path) -> bool {
 
     #[cfg(target_os = "linux")]
     #[cfg(target_os = "macos")]
+    #[cfg(target_os = "freebsd")]
     fn chdir(_p: path) -> bool {
         ret str::as_buf(_p, {|buf| os::libc::chdir(buf) == 0i32 });
     }
@@ -367,6 +370,7 @@ fn normalize(p: path) -> path {
 
     #[cfg(target_os = "linux")]
     #[cfg(target_os = "macos")]
+    #[cfg(target_os = "freebsd")]
     fn reabsolute(orig: path, new: path) -> path {
         if path_is_absolute(orig) {
             path_sep() + new
diff --git a/src/libstd/generic_os.rs b/src/libstd/generic_os.rs
index babfd0281ba..c1312ad4608 100644
--- a/src/libstd/generic_os.rs
+++ b/src/libstd/generic_os.rs
@@ -28,18 +28,20 @@ fn setenv(n: str, v: str) { }
 
 #[cfg(target_os = "linux")]
 #[cfg(target_os = "macos")]
+#[cfg(target_os = "freebsd")]
 fn getenv(n: str) -> option::t<str> unsafe {
     let s = str::as_buf(n, {|buf| os::libc::getenv(buf) });
     ret if unsafe::reinterpret_cast(s) == 0 {
             option::none::<str>
         } else {
             let s = unsafe::reinterpret_cast(s);
-            option::some::<str>(str::str_from_cstr(s))
+            option::some::<str>(str::from_cstr(s))
         };
 }
 
 #[cfg(target_os = "linux")]
 #[cfg(target_os = "macos")]
+#[cfg(target_os = "freebsd")]
 fn setenv(n: str, v: str) {
     // FIXME (868)
     str::as_buf(
diff --git a/src/libstd/list.rs b/src/libstd/list.rs
index c6a24d275eb..f6b46ed7d2e 100644
--- a/src/libstd/list.rs
+++ b/src/libstd/list.rs
@@ -98,6 +98,27 @@ fn has<copy T>(ls: list<T>, elt: T) -> bool {
 }
 
 /*
+Function: is_empty
+
+Returns true if the list is empty.
+*/
+pure fn is_empty<copy T>(ls: list<T>) -> bool {
+    alt ls {
+        nil. { true }
+        _ { false }
+    }
+}
+
+/*
+Function: is_not_empty
+
+Returns true if the list is not empty.
+*/
+pure fn is_not_empty<copy T>(ls: list<T>) -> bool {
+    ret !is_empty(ls);
+}
+
+/*
 Function: len
 
 Returns the length of a list
@@ -112,8 +133,11 @@ Function: tail
 
 Returns all but the first element of a list
 */
-pure fn tail<copy T>(ls: list<T>) -> list<T> {
-    alt ls { cons(_, tl) { ret *tl; } nil. { fail "list empty" } }
+pure fn tail<copy T>(ls: list<T>) : is_not_empty(ls) -> list<T> {
+    alt ls {
+        cons(_, tl) { ret *tl; }
+        nil. { fail "list empty" }
+    }
 }
 
 /*
@@ -121,8 +145,11 @@ Function: head
 
 Returns the first element of a list
 */
-pure fn head<copy T>(ls: list<T>) -> T {
-    alt ls { cons(hd, _) { ret hd; } nil. { fail "list empty" } }
+pure fn head<copy T>(ls: list<T>) : is_not_empty(ls) -> T {
+    alt ls {
+        cons(hd, _) { ret hd; }
+        nil. { fail "list empty" }
+    }
 }
 
 /*
diff --git a/src/libstd/rope.rs b/src/libstd/rope.rs
index dea9d187110..6b821dc6223 100644
--- a/src/libstd/rope.rs
+++ b/src/libstd/rope.rs
@@ -443,7 +443,7 @@ fn iter_chars(rope: rope, it: block(char)) {
     loop_chars(rope) {|x|
         it(x);
         ret true
-    }
+    };
 }
 
 /*
diff --git a/src/libstd/run_program.rs b/src/libstd/run_program.rs
index 55163696579..11bc15d578f 100644
--- a/src/libstd/run_program.rs
+++ b/src/libstd/run_program.rs
@@ -266,6 +266,7 @@ fn waitpid(pid: pid_t) -> int {
 
     #[cfg(target_os = "linux")]
     #[cfg(target_os = "macos")]
+    #[cfg(target_os = "freebsd")]
     fn waitpid_os(pid: pid_t) -> int {
         #[cfg(target_os = "linux")]
         fn WIFEXITED(status: i32) -> bool {
@@ -273,6 +274,7 @@ fn waitpid(pid: pid_t) -> int {
         }
 
         #[cfg(target_os = "macos")]
+        #[cfg(target_os = "freebsd")]
         fn WIFEXITED(status: i32) -> bool {
             (status & 0x7fi32) == 0i32
         }
@@ -283,6 +285,7 @@ fn waitpid(pid: pid_t) -> int {
         }
 
         #[cfg(target_os = "macos")]
+        #[cfg(target_os = "freebsd")]
         fn WEXITSTATUS(status: i32) -> i32 {
             status >> 8i32
         }
diff --git a/src/libstd/std.rc b/src/libstd/std.rc
index bcd2b82cf1a..8acb1ac9ef9 100644
--- a/src/libstd/std.rc
+++ b/src/libstd/std.rc
@@ -98,6 +98,13 @@ mod os;
 #[path = "posix_fs.rs"]
 mod os_fs;
 
+#[cfg(target_os = "freebsd")]
+#[path = "freebsd_os.rs"]
+mod os;
+#[cfg(target_os = "freebsd")]
+#[path = "posix_fs.rs"]
+mod os_fs;
+
 // Local Variables:
 // mode: rust;
 // fill-column: 78;
diff --git a/src/libstd/util.rs b/src/libstd/util.rs
index a15b5291546..f4d984a8937 100644
--- a/src/libstd/util.rs
+++ b/src/libstd/util.rs
@@ -7,7 +7,7 @@ Function: id
 
 The identity function
 */
-pure fn id<T>(x: T) -> T { x }
+pure fn id<copy T>(x: T) -> T { x }
 
 /*
 Function: unreachable
diff --git a/src/libstd/uv.rs b/src/libstd/uv.rs
index a12fdc8cecc..b24008448bb 100644
--- a/src/libstd/uv.rs
+++ b/src/libstd/uv.rs
@@ -5,6 +5,7 @@ the C libuv API. Does very little right now pending scheduler improvements.
 
 #[cfg(target_os = "linux")];
 #[cfg(target_os = "macos")];
+#[cfg(target_os = "freebsd")];
 
 export sanity_check;
 export loop_t, idle_t;
@@ -39,6 +40,7 @@ type idle_cb = opaque_cb;
 
 #[cfg(target_os = "linux")]
 #[cfg(target_os = "macos")]
+#[cfg(target_os = "freebsd")]
 type handle_private_fields = {
     a00: ctypes::c_int,
     a01: ctypes::c_int,
@@ -121,6 +123,7 @@ fn sanity_check() {
 
 #[cfg(target_os = "linux")]
 #[cfg(target_os = "macos")]
+#[cfg(target_os = "freebsd")]
 fn handle_fields_new() -> handle_fields {
     {
         loop: ptr::null(),
@@ -149,4 +152,4 @@ fn idle_new() -> idle_t {
     {
         fields: handle_fields_new()
     }
-}
\ No newline at end of file
+}