summaryrefslogtreecommitdiff
path: root/luxio.c
diff options
context:
space:
mode:
authorRob Kendrick (humdrum) <rjek@rjek.com>2012-05-04 10:41:52 +0100
committerRob Kendrick (humdrum) <rjek@rjek.com>2012-05-04 10:41:52 +0100
commitd12098f873b1765356d35b0706394277a6787dbc (patch)
tree973e619618ac76cb7216c6874fd406f165ce9b60 /luxio.c
parent777305aff31c321a85717aa198e196ffc1a5eecd (diff)
downloadluxio-d12098f873b1765356d35b0706394277a6787dbc.tar.gz
stat, lstat and fstat bindings, complete with sio interfaces
Diffstat (limited to 'luxio.c')
-rw-r--r--luxio.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/luxio.c b/luxio.c
index ff98b8f..2cdd532 100644
--- a/luxio.c
+++ b/luxio.c
@@ -331,6 +331,91 @@ luxio_mkdir(lua_State *L)
}
static int
+luxio_push_stat_table(lua_State *L, struct stat *s)
+{
+ lua_newtable(L);
+
+#define PUSH_ENTRY(e) do { lua_pushstring(L, #e); \
+ lua_pushinteger(L, s->st_##e); \
+ lua_settable(L, 3); } while (0)
+
+ PUSH_ENTRY(dev);
+ PUSH_ENTRY(ino);
+ PUSH_ENTRY(mode);
+ PUSH_ENTRY(nlink);
+ PUSH_ENTRY(uid);
+ PUSH_ENTRY(gid);
+ PUSH_ENTRY(rdev);
+ PUSH_ENTRY(size);
+ PUSH_ENTRY(blksize);
+ PUSH_ENTRY(blocks);
+ PUSH_ENTRY(atime);
+ PUSH_ENTRY(mtime);
+ PUSH_ENTRY(ctime);
+
+#undef PUSH_ENTRY
+
+ return 1;
+}
+
+static int
+luxio_stat(lua_State *L)
+{
+ const char *pathname = luaL_checkstring(L, 1);
+ struct stat s;
+ int r = stat(pathname, &s);
+
+ if (r < 0) {
+ lua_pushinteger(L, r);
+ lua_pushinteger(L, errno);
+ return 2;
+ }
+
+ lua_pushnumber(L, r);
+ luxio_push_stat_table(L, &s);
+
+ return 2;
+}
+
+static int
+luxio_lstat(lua_State *L)
+{
+ const char *pathname = luaL_checkstring(L, 1);
+ struct stat s;
+ int r = lstat(pathname, &s);
+
+ if (r < 0) {
+ lua_pushinteger(L, r);
+ lua_pushinteger(L, errno);
+ return 2;
+ }
+
+ lua_pushnumber(L, r);
+ luxio_push_stat_table(L, &s);
+
+ return 2;
+}
+
+static int
+luxio_fstat(lua_State *L)
+{
+ int fd = luaL_checkinteger(L, 1);
+ struct stat s;
+ int r = fstat(fd, &s);
+
+ if (r < 0) {
+ lua_pushinteger(L, r);
+ lua_pushinteger(L, errno);
+ return 2;
+ }
+
+ lua_pushnumber(L, r);
+ luxio_push_stat_table(L, &s);
+
+ return 2;
+}
+
+static int
luxio_strerror(lua_State *L)
{
lua_pushstring(L, strerror(luaL_checkint(L, 1)));
@@ -1660,6 +1745,10 @@ luxio_functions[] = {
{ "mkfifo", luxio_mkfifo },
{ "mkdir", luxio_mkdir },
+ { "stat", luxio_stat },
+ { "lstat", luxio_lstat },
+ { "fstat", luxio_fstat },
+
{ "socket", luxio_socket },
{ "listen", luxio_listen },
{ "shutdown", luxio_shutdown },