From 3b2f4785d72c6254cf03c5f4d07dfb0e62d4d701 Mon Sep 17 00:00:00 2001
From: Aaron Bieber <aaron@bolddaemon.com>
Date: Tue, 3 Apr 2018 07:14:40 -0600
Subject: [PATCH] add changes for execpromises

---
 README                      |  6 ++--
 example/openbsd-example.lua |  8 ++---
 src/lua-openbsd.c           | 58 ++++---------------------------------
 3 files changed, 11 insertions(+), 61 deletions(-)

diff --git a/README b/README
index ccd48a7..6723eb6 100644
--- a/README
+++ b/README
@@ -8,13 +8,13 @@ Implements:
 
 Works and has been tested on Lua 5.1, 5.2 and 5.3.
 
-Note that pledge() (on the 19th January of 2016) will fail if you
-give it a list of paths as the second parameter:
+Note that pledge() pre 6.3 takes an optional set of paths
+as the second argument.
 
  -- OK
  o.pledge('rpath stdio')
  -- Error
- o.pledge('rpath stdio', {})
+ o.pledge('rpath stdio', 'stdio')
 
 Build:
 
diff --git a/example/openbsd-example.lua b/example/openbsd-example.lua
index 18a1b03..0f0c82f 100644
--- a/example/openbsd-example.lua
+++ b/example/openbsd-example.lua
@@ -8,11 +8,9 @@ print(ret, s)
 ret, s = o.pledge("rpath stdio", {})
 print(ret, s)
 
--- Same as pledge("rpath", { "/var", "/home", ..., NULL })
-ret, s = o.pledge("rpath stdio", {"/var", "/home", "/test", "/more",
-                                  "/asdf", "/test", "/stuff", "/meh",
-                                  "/alestorm", "/tbdm", "/opeth",
-                                  "/atthegates"})
+-- Same as pledge("rpath stdio wpath", "rpath stdio")
+ret, s = o.pledge("rpath stdio wpath", "rpath stdio")
+
 print(ret, s)
 
 print(o.arc4random())
diff --git a/src/lua-openbsd.c b/src/lua-openbsd.c
index fe1c9c4..69aac64 100644
--- a/src/lua-openbsd.c
+++ b/src/lua-openbsd.c
@@ -15,24 +15,11 @@ static void lo_die(lua_State *L, char const *e)
     lua_error(L);
 }
 
-static void free_strings(char **str, size_t n)
-{
-    size_t i = 0;
-
-    if (str) {
-        for (; i < n; i++) {
-            free(str[i]);
-        }
-    }
-    free(str);
-}
-
 int lua_pledge(lua_State *L)
 {
     char const *farg = NULL;
-    char **dirs = NULL;
+    char const *earg = NULL;
     int ret = 0;
-    size_t idx = 0;
     int top = 0;
 
 #ifndef HAVE_PLEDGE
@@ -44,51 +31,16 @@ int lua_pledge(lua_State *L)
     luaL_argcheck(L, lua_isstring(L, 1), 1,
                   "pledge: first argument must be string");
     if (top == 2) {
-        luaL_argcheck(L, lua_istable(L, 2), 1,
-                      "pledge: second argument must be table");
+        luaL_argcheck(L, lua_isstring(L, 2), 1,
+                      "pledge: second argument must be string");
     } else if (top > 2) {
         lo_die(L, "pledge: too many arguments");
     }
 
     farg = lua_tostring(L, 1);
+    earg = lua_tostring(L, 2);
 
-    if (!lua_isnoneornil(L, 2)) {
-        size_t sz = 1;
-        dirs = calloc(2, sizeof(char*));
-        if (dirs == NULL) {
-            lo_die(L, "pledge: calloc");
-        }
-
-        lua_pushnil(L);
-        while (lua_next(L, 2)) {
-            if (lua_isstring(L, -1)) {
-                char const *str = lua_tostring(L, -1);
-
-                if (idx == (sz-1)) {
-                    char **tmp = NULL;
-
-                    tmp = reallocarray(dirs, sz+10, sizeof(char*));
-                    if (tmp == NULL) {
-                        free_strings(dirs, idx+1);
-                        lo_die(L, "pledge: calloc()");
-                    }
-                    dirs = tmp;
-                    sz = sz + 10;
-                }
-
-                dirs[idx] = strdup(str);
-                if (dirs[idx] == NULL) {
-                    free_strings(dirs, idx+1);
-                    lo_die(L, "pledge: strdup");
-                }
-                ++idx;
-            }
-            lua_pop(L, 1);
-        }
-    }
-
-    ret = pledge(farg, (char const **)dirs);
-    free_strings(dirs, idx+1);
+    ret = pledge(farg, earg);
 
     lua_pushnumber(L, ret);
     if (ret == -1) {