From 245fb948f18c5dd4e8a88c34fa6aa3ef3e859d53 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 19 May 2018 01:09:56 -0400 Subject: [PATCH] Add os.cwd --- src/core/os.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/core/os.c b/src/core/os.c index 245ee4dc..326ae5e6 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -28,6 +28,7 @@ #ifdef DST_WINDOWS #include +#include #else #include #endif @@ -116,6 +117,21 @@ static int os_sleep(DstArgs args) { return 0; } +static int os_cwd(DstArgs args) { + DST_FIXARITY(args, 0); + char buf[FILENAME_MAX]; + char *ptr; +#ifdef DST_WINDOWS + ptr = _getcwd(buf, FILENAME_MAX); +#else + ptr = getcwd(buf, FILENAME_MAX); +#endif + if (NULL == ptr) { + DST_THROW(args, "could not get current directory"); + } + DST_RETURN_CSTRING(args, ptr); +} + static const DstReg cfuns[] = { {"os.execute", os_execute}, {"os.exit", os_exit}, @@ -123,6 +139,7 @@ static const DstReg cfuns[] = { {"os.setenv", os_setenv}, {"os.clock", os_clock}, {"os.sleep", os_sleep}, + {"os.cwd", os_cwd}, {NULL, NULL} };