diff options
author | Rob Kendrick (monotony) <rjek@rjek.com> | 2012-05-07 10:57:45 +0100 |
---|---|---|
committer | Rob Kendrick (monotony) <rjek@rjek.com> | 2012-05-07 10:57:45 +0100 |
commit | 43ab538e5e7dd2f76334130474a54176d7ca243b (patch) | |
tree | 1ba27f17765ce541838eb74cfe169ab1525d92c8 /luxio.c | |
parent | a22310bca8e590f27153cffd7dc4535ee422b1ae (diff) | |
download | luxio-43ab538e5e7dd2f76334130474a54176d7ca243b.tar.gz |
Misc code cleanups
Diffstat (limited to 'luxio.c')
-rw-r--r-- | luxio.c | 127 |
1 files changed, 96 insertions, 31 deletions
@@ -5,6 +5,7 @@ */ #define LUXIO_RELEASE 0 +#define LUXIO_ABI 0 #define LUXIO_COPYRIGHT "Copyright 2012 Rob Kendrick <rjek+luxio@rjek.com>" #include "config.h" @@ -34,6 +35,7 @@ #include <unistd.h> #include <dirent.h> #include <time.h> +#include <limits.h> #ifdef HAVE_SENDFILE # include <sys/sendfile.h> @@ -86,15 +88,17 @@ luxio__exec(lua_State *L, bool usep) args[c] = NULL; - if (usep) + if (usep) { ret = execvp(path, args); - else + } else { ret = execv(path, args); + } /* if we got here, there's an error. */ free(args); lua_pushinteger(L, ret); lua_pushinteger(L, errno); + return 2; } @@ -194,6 +198,7 @@ luxio_alarm(lua_State *L) /* 3.4.1 */ { unsigned int seconds = luaL_checkinteger(L, 1); lua_pushinteger(L, alarm(seconds)); + return 1; } @@ -202,6 +207,7 @@ luxio_pause(lua_State *L) /* 3.4.2 */ { lua_pushinteger(L, pause()); lua_pushinteger(L, errno); + return 2; } @@ -212,6 +218,7 @@ luxio_sleep(lua_State *L) /* 3.4.3 */ lua_pushinteger(L, sleep(seconds)); lua_pushinteger(L, errno); + return 2; } @@ -221,6 +228,7 @@ static int luxio_getpid(lua_State *L) /* 4.1.1 */ { lua_pushinteger(L, getpid()); + return 1; } @@ -228,6 +236,7 @@ static int luxio_getppid(lua_State *L) /* 4.1.1 */ { lua_pushinteger(L, getppid()); + return 1; } @@ -237,6 +246,7 @@ static int luxio_getuid(lua_State *L) /* 4.2.1 */ { lua_pushinteger(L, getuid()); + return 1; } @@ -244,6 +254,7 @@ static int luxio_geteuid(lua_State *L) /* 4.2.1 */ { lua_pushinteger(L, geteuid()); + return 1; } @@ -251,6 +262,7 @@ static int luxio_getgid(lua_State *L) /* 4.2.1 */ { lua_pushinteger(L, getgid()); + return 1; } @@ -258,6 +270,7 @@ static int luxio_getegid(lua_State *L) /* 4.2.1 */ { lua_pushinteger(L, getegid()); + return 1; } @@ -268,6 +281,7 @@ luxio_setuid(lua_State *L) /* 4.2.2 */ lua_pushinteger(L, setuid(uid)); lua_pushinteger(L, errno); + return 2; } @@ -278,6 +292,7 @@ luxio_setgid(lua_State *L) /* 4.2.2 */ lua_pushinteger(L, setgid(gid)); lua_pushinteger(L, errno); + return 2; } @@ -286,17 +301,19 @@ luxio_setgid(lua_State *L) /* 4.2.2 */ static int luxio_getlogin(lua_State *L) /* 4.2.4 */ { - char buf[128]; /* if this system has a username longer, stuff'em */ + char buf[LOGIN_NAME_MAX]; int r = getlogin_r(buf, sizeof(buf)); if (r != 0) { lua_pushinteger(L, r); lua_pushinteger(L, errno); + return 2; } lua_pushinteger(L, r); lua_pushstring(L, buf); + return 2; } @@ -318,6 +335,7 @@ luxio_uname(lua_State *L) /* 4.4.1 */ if (r < 0) { lua_pushinteger(L, errno); + return 2; } @@ -348,6 +366,7 @@ luxio_time(lua_State *L) /* 4.5.1 */ { lua_pushinteger(L, time(NULL)); lua_pushinteger(L, errno); + return 2; } @@ -377,7 +396,6 @@ luxio_times(lua_State *L) /* 4.5.2 */ #undef TIMES_FIELD return 2; - } /* 4.6 Environment variables *************************************************/ @@ -460,8 +478,12 @@ luxio_readdir_tostring(lua_State *L) luxio_readdir_state *s = luaL_checkudata(L, 1, LUXIO_READDIR_METATABLE); char buf[sizeof("dirent: 0xffffffffffffffff")]; + /* we can't use lua_pushfstring here, because our pointer might + * be 64 bits. + */ snprintf(buf, sizeof(buf), "dirent: %p", s); lua_pushstring(L, buf); + return 1; } @@ -499,7 +521,8 @@ luxio_opendir(lua_State *L) /* 5.1.2 */ s->dirp = d; /* + 256 because it'd always be +1 if it weren't for the horrors * of Solaris. If we were using autoconf, we could use Ben - * Hutchings' function mentioned in the article above. + * Hutchings' function mentioned in his article "readdir_r considered + * harmful". */ bufz = sizeof(struct dirent) + pathconf(path, _PC_NAME_MAX) + 256; s->buf = malloc(bufz); @@ -528,7 +551,8 @@ luxio_fdopendir(lua_State *L) /* POSIX.1-2008 */ s->dirp = d; /* + 256 because it'd always be +1 if it weren't for the horrors * of Solaris. If we were using autoconf, we could use Ben - * Hutchings' function mentioned in the article above. + * Hutchings' function mentioned in his article "readdir_r considered + * harmful". */ bufz = sizeof(struct dirent) + fpathconf(fd, _PC_NAME_MAX) + 256; s->buf = malloc(bufz); @@ -579,10 +603,12 @@ luxio_readdir(lua_State *L) /* 5.1.2 */ if (s->ent == NULL) { /* end of directory */ lua_pushnil(L); + return 1; } lua_pushinteger(L, err); + return 1; } @@ -590,6 +616,7 @@ static int luxio_rewinddir(lua_State *L) /* 5.1.2 */ { luxio_readdir_state *s = luaL_checkudata(L, 1, LUXIO_READDIR_METATABLE); + rewinddir(s->dirp); return 0; @@ -601,8 +628,10 @@ static int luxio_chdir(lua_State *L) /* 5.2.1 */ { const char *path = luaL_checkstring(L, 1); + lua_pushinteger(L, chdir(path)); lua_pushinteger(L, errno); + return 2; } @@ -644,6 +673,7 @@ luxio_open(lua_State *L) /* 5.3.1 */ lua_pushinteger(L, result); lua_pushinteger(L, errno); + return 2; } @@ -653,7 +683,9 @@ static int luxio_umask(lua_State *L) /* 5.3.3 */ { mode_t mask = luaL_checkinteger(L, 1); + lua_pushinteger(L, umask(mask)); + return 1; } @@ -665,6 +697,7 @@ luxio_link(lua_State *L) /* 5.3.4 */ lua_pushinteger(L, link(existing, new)); lua_pushinteger(L, errno); + return 2; } @@ -703,6 +736,7 @@ luxio_unlink(lua_State *L) /* 5.5.1 */ lua_pushinteger(L, unlink(s)); lua_pushinteger(L, errno); + return 2; } @@ -713,6 +747,7 @@ luxio_rmdir(lua_State *L) /* 5.5.2 */ lua_pushinteger(L, rmdir(pathname)); lua_pushinteger(L, errno); + return 2; } @@ -724,6 +759,7 @@ luxio_rename(lua_State *L) /* 5.5.3 */ lua_pushinteger(L, rename(old, new)); lua_pushinteger(L, errno); + return 2; } @@ -764,15 +800,14 @@ luxio_stat(lua_State *L) /* 5.6.2 */ struct stat s; int r = stat(pathname, &s); + lua_pushinteger(L, r); + if (r < 0) { - lua_pushinteger(L, r); lua_pushinteger(L, errno); - return 2; + } else { + (void) luxio_push_stat_table(L, &s); } - lua_pushnumber(L, r); - luxio_push_stat_table(L, &s); - return 2; } @@ -783,15 +818,14 @@ luxio_fstat(lua_State *L) /* 5.6.2 */ struct stat s; int r = fstat(fd, &s); + lua_pushinteger(L, r); + if (r < 0) { - lua_pushinteger(L, r); lua_pushinteger(L, errno); - return 2; + } else { + (void) luxio_push_stat_table(L, &s); } - lua_pushnumber(L, r); - luxio_push_stat_table(L, &s); - return 2; } @@ -801,15 +835,14 @@ luxio_lstat(lua_State *L) /* POSIX.1-2001 */ const char *pathname = luaL_checkstring(L, 1); struct stat s; int r = lstat(pathname, &s); + + lua_pushinteger(L, r); if (r < 0) { - lua_pushinteger(L, r); lua_pushinteger(L, errno); - return 2; + } else { + (void) luxio_push_stat_table(L, &s); } - - lua_pushnumber(L, r); - luxio_push_stat_table(L, &s); return 2; } @@ -850,6 +883,7 @@ luxio_chown(lua_State *L) /* 5.6.5 */ lua_pushinteger(L, chown(path, owner, group)); lua_pushinteger(L, errno); + return 2; } @@ -863,6 +897,7 @@ luxio_ftruncate(lua_State *L) /* 5.6.7 */ lua_pushinteger(L, ftruncate(fildes, length)); lua_pushinteger(L, errno); + return 2; } @@ -950,6 +985,7 @@ luxio_dup(lua_State *L) /* 6.2.1 */ lua_pushinteger(L, dup(oldfd)); lua_pushinteger(L, errno); + return 2; } @@ -961,6 +997,7 @@ luxio_dup2(lua_State *L) /* 6.2.1 */ lua_pushinteger(L, dup2(oldfd, newfd)); lua_pushinteger(L, errno); + return 2; } @@ -974,6 +1011,7 @@ luxio_dup3(lua_State *L) /* GNU extension */ lua_pushinteger(L, dup3(oldfd, newfd, flags)); lua_pushinteger(L, errno); + return 2; } #endif @@ -985,6 +1023,7 @@ luxio_close(lua_State *L) /* 6.3.1 */ { lua_pushinteger(L, close(luaL_checkint(L, 1))); lua_pushinteger(L, errno); + return 2; } @@ -1022,6 +1061,7 @@ luxio_read(lua_State *L) /* 6.4.1 */ } free(buf); + return 2; } @@ -1037,6 +1077,7 @@ luxio_write(lua_State *L) /* 6.4.2 */ lua_pushinteger(L, write(fd, buf + offset, count - offset)); lua_pushinteger(L, errno); + return 2; } @@ -1126,6 +1167,7 @@ luxio_fcntl(lua_State *L) /* 6.5.2 */ arg_long = luaL_checkinteger(L, 3); lua_pushinteger(L, fcntl(fd, cmd, arg_long)); lua_pushinteger(L, errno); + return 2; default: @@ -1146,6 +1188,7 @@ luxio_lseek(lua_State *L) /* 6.5.3 */ lua_pushinteger(L, (lua_Number)lseek64(fd, offset, whence)); lua_pushinteger(L, errno); + return 2; } #else @@ -1158,6 +1201,7 @@ luxio_lseek(lua_State *L) /* 6.5.3 */ lua_pushinteger(L, lseek(fd, offset, whence)); lua_pushinteger(L, errno); + return 2; } #endif @@ -1171,6 +1215,7 @@ luxio_fsync(lua_State *L) /* 6.6.1 */ lua_pushinteger(L, fsync(fildes)); lua_pushinteger(L, errno); + return 2; } @@ -1181,6 +1226,7 @@ luxio_fdatasync(lua_State *L) /* 6.6.2 */ lua_pushinteger(L, fdatasync(fildes)); lua_pushinteger(L, errno); + return 2; } @@ -1639,6 +1685,7 @@ static int luxio_gai_strerror(lua_State *L) { lua_pushstring(L, gai_strerror(luaL_checkint(L, 1))); + return 1; } @@ -1843,10 +1890,12 @@ luxio_bitop_or(lua_State *L) int value = luaL_checkint(L, 1); int n = lua_gettop(L); - while (n > 1) + while (n > 1) { value |= luaL_checkint(L, n--); + } lua_pushnumber(L, value); + return 1; } @@ -1856,10 +1905,12 @@ luxio_bitop_and(lua_State *L) int value = luaL_checkint(L, 1); int n = lua_gettop(L); - while (n > 1) + while (n > 1) { value &= luaL_checkint(L, n--); + } lua_pushnumber(L, value); + return 1; } @@ -1869,10 +1920,12 @@ luxio_bitop_clear(lua_State *L) int value = luaL_checkint(L, 1); int n = lua_gettop(L); - while (n > 1) + while (n > 1) { value &= ~luaL_checkint(L, n--); + } lua_pushnumber(L, value); + return 1; } @@ -1885,13 +1938,16 @@ luxio_bitop_invert(lua_State *L) /* Special case, passed 1 value, we invert that rather than * inverting the other bits supplied */ - if (n == 1) + if (n == 1) { value = ~value; - else - while (n > 1) + } else { + while (n > 1) { value ^= luaL_checkint(L, n--); + } + } lua_pushnumber(L, value); + return 1; } @@ -1902,8 +1958,9 @@ luxio_bitop_test(lua_State *L) int goal = 0; int n = lua_gettop(L); - while (n > 1) + while (n > 1) { goal |= luaL_checkint(L, n--); + } lua_pushboolean(L, (value & goal) == goal); @@ -1955,6 +2012,7 @@ luxio_timeval_tostring(lua_State *L) { struct timeval *a = luaL_checkudata(L, 1, LUXIO_TIMEVAL_METATABLE); char buffer[LUXIO_TIME_BUFLEN]; + snprintf(buffer, LUXIO_TIME_BUFLEN, "timeval: %ld.%06ld", a->tv_sec, a->tv_usec); @@ -2008,7 +2066,6 @@ luxio_timeval_newindex(lua_State *L) return 0; } -/* Hideous forward declaration */ static void luxio__bless_timeval(lua_State *L); static int @@ -2102,6 +2159,7 @@ static int luxio_strerror(lua_State *L) { lua_pushstring(L, strerror(luaL_checkint(L, 1))); + return 1; } @@ -2245,7 +2303,9 @@ luxio_bitop_functions[] = { #include "luxio_constants.h" -#define NUMERIC_CONSTANT(x) lua_pushstring(L, #x); lua_pushinteger(L, x); lua_settable(L, -3); +#define NUMERIC_CONSTANT(x) do { lua_pushstring(L, #x); \ + lua_pushinteger(L, x); \ + lua_settable(L, -3); } while (0) int luaopen_luxio(lua_State *L) @@ -2259,7 +2319,8 @@ luaopen_luxio(lua_State *L) luaL_newlib(L, luxio_bitop_functions); #else luaL_register(L, "luxio", luxio_functions); - lua_createtable(L, 0, (sizeof(luxio_bitop_functions) / sizeof(struct luaL_Reg)) - 1); + lua_createtable(L, 0, (sizeof(luxio_bitop_functions) / + sizeof(struct luaL_Reg)) - 1); luaL_register(L, NULL, luxio_bitop_functions); #endif lua_setfield(L, -2, "bit"); @@ -2286,6 +2347,10 @@ luaopen_luxio(lua_State *L) lua_pushnumber(L, LUXIO_RELEASE); lua_settable(L, -3); + lua_pushstring(L, "_ABI"); + lua_pushnumber(L, LUXIO_ABI); + lua_settable(L, -3); + /* push values that are not compile-time known */ #ifdef SIGRTMIN NUMERIC_CONSTANT(SIGRTMIN); |