webmcp

diff libraries/extos/extos.c @ 344:985ffb3ef69f

Added new function extos.stat(...)
author jbe
date Thu Mar 26 02:11:59 2015 +0100 (2015-03-26)
parents 34bf5f7abe0d
children b1748c6c3c89
line diff
     1.1 --- a/libraries/extos/extos.c	Thu Mar 26 01:56:10 2015 +0100
     1.2 +++ b/libraries/extos/extos.c	Thu Mar 26 02:11:59 2015 +0100
     1.3 @@ -4,6 +4,7 @@
     1.4  #include <time.h>
     1.5  #include <unistd.h>
     1.6  #include <sys/types.h>
     1.7 +#include <sys/stat.h>
     1.8  #include <sys/wait.h>
     1.9  #include <signal.h>
    1.10  #include <errno.h>
    1.11 @@ -295,6 +296,59 @@
    1.12    return 1;
    1.13  }
    1.14  
    1.15 +static int extos_stat(lua_State *L) {
    1.16 +  const char *filename;
    1.17 +  struct stat sb;
    1.18 +  filename = luaL_checkstring(L, 1);
    1.19 +  if (stat(filename, &sb)) {
    1.20 +    char errmsg[EXTOS_MAX_ERRLEN+1];
    1.21 +    strerror_r(errno, errmsg, EXTOS_MAX_ERRLEN+1);
    1.22 +    lua_pushnil(L);
    1.23 +    lua_pushfstring(L, "Could not get file stats for \"%s\": %s", filename, errmsg);
    1.24 +    return 2;
    1.25 +  }
    1.26 +  lua_createtable(L, 0, 19);
    1.27 +  lua_pushinteger(L, sb.st_dev);
    1.28 +  lua_setfield(L, -2, "dev");
    1.29 +  lua_pushinteger(L, sb.st_ino);
    1.30 +  lua_setfield(L, -2, "ino");
    1.31 +  lua_pushinteger(L, sb.st_nlink);
    1.32 +  lua_setfield(L, -2, "nlink");
    1.33 +  lua_pushinteger(L, sb.st_atime);
    1.34 +  lua_setfield(L, -2, "atime");
    1.35 +  lua_pushinteger(L, sb.st_mtime);
    1.36 +  lua_setfield(L, -2, "mtime");
    1.37 +  lua_pushinteger(L, sb.st_ctime);
    1.38 +  lua_setfield(L, -2, "ctime");
    1.39 +  lua_pushinteger(L, sb.st_size);
    1.40 +  lua_setfield(L, -2, "size");
    1.41 +  lua_pushinteger(L, sb.st_blksize);
    1.42 +  lua_setfield(L, -2, "blksize");
    1.43 +  lua_pushinteger(L, sb.st_blocks);
    1.44 +  lua_setfield(L, -2, "blocks");
    1.45 +  lua_pushinteger(L, sb.st_uid);
    1.46 +  lua_setfield(L, -2, "uid");
    1.47 +  lua_pushinteger(L, sb.st_gid);
    1.48 +  lua_setfield(L, -2, "gid");
    1.49 +  lua_pushinteger(L, sb.st_mode);
    1.50 +  lua_setfield(L, -2, "mode");
    1.51 +  lua_pushboolean(L, S_ISBLK(sb.st_mode));
    1.52 +  lua_setfield(L, -2, "isblk");
    1.53 +  lua_pushboolean(L, S_ISCHR(sb.st_mode));
    1.54 +  lua_setfield(L, -2, "ischr");
    1.55 +  lua_pushboolean(L, S_ISDIR(sb.st_mode));
    1.56 +  lua_setfield(L, -2, "isdir");
    1.57 +  lua_pushboolean(L, S_ISFIFO(sb.st_mode));
    1.58 +  lua_setfield(L, -2, "isfifo");
    1.59 +  lua_pushboolean(L, S_ISLNK(sb.st_mode));
    1.60 +  lua_setfield(L, -2, "islnk");
    1.61 +  lua_pushboolean(L, S_ISREG(sb.st_mode));
    1.62 +  lua_setfield(L, -2, "isreg");
    1.63 +  lua_pushboolean(L, S_ISSOCK(sb.st_mode));
    1.64 +  lua_setfield(L, -2, "issock");
    1.65 +  return 1;
    1.66 +}
    1.67 +
    1.68  static int extos_crypt(lua_State *L) {
    1.69    const char *key;
    1.70    const char *salt;
    1.71 @@ -331,6 +385,7 @@
    1.72  static const struct luaL_Reg extos_module_functions[] = {
    1.73    {"pfilter",              extos_pfilter},
    1.74    {"listdir",              extos_listdir},
    1.75 +  {"stat",                 extos_stat},
    1.76    {"crypt",                extos_crypt},
    1.77    {"hires_time",           extos_hires_time},
    1.78    {"monotonic_hires_time", extos_monotonic_hires_time},

Impressum / About Us