Skip to content

Commit

Permalink
Merge pull request #1 from qbit/master
Browse files Browse the repository at this point in the history
add changes for execpromises
  • Loading branch information
n0la authored Apr 5, 2018
2 parents e885f3b + 3b2f478 commit 7d03ff6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 61 deletions.
6 changes: 3 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
8 changes: 3 additions & 5 deletions example/openbsd-example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
58 changes: 5 additions & 53 deletions src/lua-openbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 7d03ff6

Please sign in to comment.