summaryrefslogtreecommitdiff
path: root/libstd
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-04-14 17:33:24 -0700
committerOri Bernstein <ori@eigenstate.org>2015-04-14 17:33:24 -0700
commit6db511e5321351c94c524a6e0bfc7f3df4dfb482 (patch)
tree90a12b527ab31979847b7e5e50d5ee92b98ee6d1 /libstd
parent4c08e7b46eb4bcf159da14f9bd481d413f479224 (diff)
downloadmc-6db511e5321351c94c524a6e0bfc7f3df4dfb482.tar.gz
Get a more or less working getcwd under OSX.
TODO: It still truncates long paths.
Diffstat (limited to 'libstd')
-rw-r--r--libstd/cstrconv.myr12
-rw-r--r--libstd/getcwd.myr2
-rw-r--r--libstd/syswrap-ss+posixy-linux.myr14
-rw-r--r--libstd/syswrap-ss+posixy-osx.myr16
4 files changed, 40 insertions, 4 deletions
diff --git a/libstd/cstrconv.myr b/libstd/cstrconv.myr
index 969a65f..fa9e0bf 100644
--- a/libstd/cstrconv.myr
+++ b/libstd/cstrconv.myr
@@ -1,6 +1,7 @@
use "types.use"
pkg std =
+ const cstrlen : (buf : byte[:] -> size)
const cstrconv : (buf : byte[:] -> byte[:])
const cstrconvp : (p : byte# -> byte[:])
;;
@@ -26,3 +27,14 @@ const cstrconvp = {p
;;
-> p[:i]
}
+
+const cstrlen = {buf
+ var i
+
+ for i = 0; i < buf.len; i++
+ if buf[i] == 0
+ break
+ ;;
+ ;;
+ -> i
+}
diff --git a/libstd/getcwd.myr b/libstd/getcwd.myr
index 3e9bb03..4c53bb2 100644
--- a/libstd/getcwd.myr
+++ b/libstd/getcwd.myr
@@ -17,7 +17,7 @@ const getcwd = {
n = bgetcwd(buf)
if n >= 0
/* n is the length of the nul terminated c string */
- -> buf[:n-1]
+ -> buf[:n]
elif n != Erange
std.slfree(buf)
-> ""
diff --git a/libstd/syswrap-ss+posixy-linux.myr b/libstd/syswrap-ss+posixy-linux.myr
index 485c0af..382a7e6 100644
--- a/libstd/syswrap-ss+posixy-linux.myr
+++ b/libstd/syswrap-ss+posixy-linux.myr
@@ -6,4 +6,16 @@ pkg std =
;;
const exit = {status; sys.exit_group(status)}
-const bgetcwd = {buf; -> sys.getcwd(buf) castto(errno)}
+const bgetcwd = {buf;
+ var err
+ err = sys.getcwd(buf) castto(errno)
+ /*
+ if we got a length back, it includes
+ the nul byte. chop that off.
+ */
+ if err > 0
+ -> err - 1
+ else
+ -> err
+ ;;
+}
diff --git a/libstd/syswrap-ss+posixy-osx.myr b/libstd/syswrap-ss+posixy-osx.myr
index 91a5dee..5e7f001 100644
--- a/libstd/syswrap-ss+posixy-osx.myr
+++ b/libstd/syswrap-ss+posixy-osx.myr
@@ -1,5 +1,8 @@
use sys
use "errno.use"
+use "cstrconv.use"
+use "slcp.use"
+use "die.use"
pkg std =
const exit : (status:int -> void)
@@ -9,7 +12,7 @@ pkg std =
const exit = {status; sys.exit(status)}
const bgetcwd = {buf
var path : byte[sys.Maxpathlen]
- var fd
+ var fd, len, err
fd = sys.open(".", sys.Ordonly)
if fd < 0
@@ -19,5 +22,14 @@ const bgetcwd = {buf
FIXME: if the path is longer than sys.Pathmax, we should fall back to
the ugly method of finding the path.
*/
- -> sys.fcntl(fd, sys.Fgetpath, path[:] castto(byte#)) castto(errno)
+ err = sys.fcntl(fd, sys.Fgetpath, path[:] castto(byte#)) castto(errno)
+ if err < 0
+ -> err
+ ;;
+ len = cstrlen(path[:])
+ if len >= buf.len
+ -> Erange
+ ;;
+ slcp(buf[:len], path[:len])
+ -> len castto(errno)
}