ref: d3672bb4f2ef344e41ff7fd3cb8c801a8edc6513
parent: b8739b70c8fbb2f8930c7cb2b1d1a6544aa7024f
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Apr 12 20:58:08 EDT 2015
Implement getcwd() on Linux. This is needed for the newest mbld.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -48,6 +48,7 @@
fltbits.myr \
fmt.myr \
fltfmt.myr \
+ getcwd.myr \
hashfuncs.myr \
hasprefix.myr \
hassuffix.myr \
--- a/libstd/bldfile
+++ b/libstd/bldfile
@@ -36,21 +36,22 @@
endian.myr
env+posixy.myr
env+plan9.myr
- errno.myr
+ errno.myr
execvp.myr
extremum.myr
fltbits.myr
fltfmt.myr
fmt.myr
+ getcwd.myr
hashfuncs.myr
hasprefix.myr
hassuffix.myr
htab.myr
- getint.myr
+ getint.myr
intparse.myr
ipparse.myr
mk.myr
- mkpath.myr
+ mkpath.myr
now.myr
option.myr
optparse.myr
@@ -75,9 +76,9 @@
strjoin.myr
strsplit.myr
strstrip.myr
- syswrap+plan9.myr
- syswrap-ss+posixy-linux.myr
- syswrap-ss+posixy-osx.myr
+ syswrap+plan9.myr
+ syswrap-ss+posixy-linux.myr
+ syswrap-ss+posixy-osx.myr
syswrap+posixy.myr
swap.myr
try.myr
--- /dev/null
+++ b/libstd/getcwd.myr
@@ -1,0 +1,30 @@
+use "syswrap.use"
+use "errno.use"
+use "alloc.use"
+use "extremum.use"
+use "fmt.use"
+
+pkg std =
+ const getcwd : (-> byte[:])
+;;
+
+const getcwd = {
+ var len, n, buf
+
+ len = 128
+ while true
+ buf = std.slalloc(len)
+ n = bgetcwd(buf)
+ std.put("got %i, buf = %s\n", n, buf[:n])
+ if n >= 0
+ /* n is the length of the nul terminated c string */
+ -> buf[:n-1]
+ elif n != Erange
+ std.slfree(buf)
+ -> ""
+ else
+ len *= 2
+ ;;
+ ;;
+}
+
--- a/libstd/sys+linux-x64.myr
+++ b/libstd/sys+linux-x64.myr
@@ -608,6 +608,7 @@
generic ioctl : (fd:fd, req : int64, arg:@a# -> int64)
const getdents64 : (fd:fd, buf : byte[:] -> int64)
const chdir : (p : byte[:] -> int64)
+ const getcwd : (buf : byte[:] -> int64)
/* fd stuff */
const pipe : (fds : fd[2]# -> int64)
@@ -726,6 +727,7 @@
generic ioctl = {fd, req, arg; -> syscall(Sysioctl, a(fd), a(req), a(arg)) castto(int64)}
const getdents64 = {fd, buf; -> syscall(Sysgetdents64, a(fd), buf castto(byte#), a(buf.len))}
const chdir = {dir; -> syscall(Syschdir, cstring(dir))}
+const getcwd = {buf; -> syscall(Sysgetcwd, a(buf), a(buf.len))}
/* file stuff */
const pipe = {fds; -> syscall(Syspipe, a(fds))}
--- a/libstd/syswrap+posixy.myr
+++ b/libstd/syswrap+posixy.myr
@@ -2,6 +2,7 @@
use "cstrconv.use"
use "option.use"
use "types.use"
+use "errno.use"
pkg std =
type fd = sys.fd
@@ -52,8 +53,8 @@
/* path manipulation */
const mkdir : (path : byte[:], mode : int64 -> int64)
- const chdir : (path : byte[:] -> bool)
const remove : (path : byte[:] -> bool)
+ const chdir : (path : byte[:] -> bool)
/* process stuff */
const getpid : ( -> pid)
@@ -67,6 +68,7 @@
pkglocal const getmem : (sz : size -> byte#)
pkglocal const freemem : (p : byte#, sz : size -> void)
pkglocal const curtime : (-> time)
+ pkglocal const bgetcwd : (buf : byte[:] -> errno)
;;
/* fd stuff */
@@ -84,6 +86,7 @@
const mkdir = {path, mode; -> sys.mkdir(path, mode)}
const chdir = {path; -> sys.chdir(path) == 0}
const remove = {path; -> sys.unlink(path) == 0}
+const bgetcwd = {buf; -> sys.getcwd(buf) castto(errno)}
/* useful/portable bits of uname */
const getsysinfo = {si