diff options
author | Richard Ipsum <richardipsum@fastmail.co.uk> | 2018-10-12 18:10:54 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2018-10-21 13:39:56 +0100 |
commit | 80bbeba5ca28d9901fa1b66ae4cb08ba58018bb8 (patch) | |
tree | 054b30adb69ad166089ef8bff63c4b67d8c272f3 | |
parent | 301de4c825ae109cc3a7c780d662863ada62753b (diff) | |
download | luxio-80bbeba5ca28d9901fa1b66ae4cb08ba58018bb8.tar.gz |
Bind creat(2)
-rw-r--r-- | luxio.c | 24 | ||||
-rw-r--r-- | tests/test-creat.lua | 35 |
2 files changed, 58 insertions, 1 deletions
@@ -1365,7 +1365,28 @@ luxio_open(lua_State *L) /* 5.3.1 */ return 2; } -/* TODO: creat() 5.3.2 */ +/*** Create a file or device. + +Returns file descriptor on success. On error returns -1 with errno set +appropriately. + +@tparam string path +@tparam[opt] number mode, must be specified if creating. +@treturn result File descriptor +@treturn errno +@function creat +*/ +static int +luxio_creat(lua_State *L) +{ + const char *pathname = luaL_checkstring(L, 1); + mode_t mode = luaL_optinteger(L, 2, 0); + + lua_pushinteger(L, creat(pathname, mode)); + lua_pushinteger(L, errno); + + return 2; +} /*** Set file mode creation mask. @tparam number mask @@ -4406,6 +4427,7 @@ luxio_iconv(lua_State *L) static const struct luaL_Reg luxio_functions[] = { { "open", luxio_open }, + { "creat", luxio_creat }, { "close", luxio_close }, { "read", luxio_read }, { "write", luxio_write }, diff --git a/tests/test-creat.lua b/tests/test-creat.lua new file mode 100644 index 0000000..432788b --- /dev/null +++ b/tests/test-creat.lua @@ -0,0 +1,35 @@ +local l = require "luxio" +local sio = require "luxio.simple" + +path = "luxio-test-creat-test-file" + +local fd, errno = l.creat(path, nil) + +if fd == -1 then + io.stderr:write(("create: %s\n"):format(l.strerror(errno))) + os.exit(l.EXIT_FAILURE) +end + +if l.stat(path) == 0 then + print(("Successfully created test file %s"):format(path)) +end + +l.unlink(path) + +local mode = sio.tomode("-rw-r--r--") +local fd, errno = l.creat(path, mode) + +if fd == -1 then + io.stderr:write(("creat: %s\n"):format(l.strerror(errno))) + os.exit(l.EXIT_FAILURE) +end + +local r, st = l.stat(path) + +if r == 0 and l.bit.band(st['mode'], tonumber(777, 8)) == mode then + print(("Successfully created test file %s"):format(path)) +else + print("Test failed!") +end + +l.unlink(path) |