diff options
-rw-r--r-- | util-src/encodings.c | 77 | ||||
-rw-r--r-- | util-src/hashes.c | 38 | ||||
-rw-r--r-- | util-src/net.c | 14 | ||||
-rw-r--r-- | util-src/pposix.c | 78 | ||||
-rw-r--r-- | util-src/ringbuffer.c | 51 | ||||
-rw-r--r-- | util-src/signal.c | 22 | ||||
-rw-r--r-- | util-src/table.c | 8 | ||||
-rw-r--r-- | util-src/windows.c | 12 |
8 files changed, 162 insertions, 138 deletions
diff --git a/util-src/encodings.c b/util-src/encodings.c index 4d6ac437..a633d7fa 100644 --- a/util-src/encodings.c +++ b/util-src/encodings.c @@ -30,7 +30,7 @@ static const char code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -static void base64_encode(luaL_Buffer* b, unsigned int c1, unsigned int c2, unsigned int c3, int n) { +static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n) { unsigned long tuple = c3 + 256UL * (c2 + 256UL * c1); int i; char s[4]; @@ -47,9 +47,9 @@ static void base64_encode(luaL_Buffer* b, unsigned int c1, unsigned int c2, unsi luaL_addlstring(b, s, 4); } -static int Lbase64_encode(lua_State* L) { /** encode(s) */ +static int Lbase64_encode(lua_State *L) { /** encode(s) */ size_t l; - const unsigned char* s = (const unsigned char*)luaL_checklstring(L, 1, &l); + const unsigned char *s = (const unsigned char *)luaL_checklstring(L, 1, &l); luaL_Buffer b; int n; luaL_buffinit(L, &b); @@ -62,6 +62,7 @@ static int Lbase64_encode(lua_State* L) { /** encode(s) */ case 1: base64_encode(&b, s[0], 0, 0, 1); break; + case 2: base64_encode(&b, s[0], s[1], 0, 2); break; @@ -71,15 +72,17 @@ static int Lbase64_encode(lua_State* L) { /** encode(s) */ return 1; } -static void base64_decode(luaL_Buffer* b, int c1, int c2, int c3, int c4, int n) { +static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n) { unsigned long tuple = c4 + 64L * (c3 + 64L * (c2 + 64L * c1)); char s[3]; switch(--n) { case 3: s[2] = (char) tuple; + case 2: s[1] = (char)(tuple >> 8); + case 1: s[0] = (char)(tuple >> 16); } @@ -87,9 +90,9 @@ static void base64_decode(luaL_Buffer* b, int c1, int c2, int c3, int c4, int n) luaL_addlstring(b, s, n); } -static int Lbase64_decode(lua_State* L) { /** decode(s) */ +static int Lbase64_decode(lua_State *L) { /** decode(s) */ size_t l; - const char* s = luaL_checklstring(L, 1, &l); + const char *s = luaL_checklstring(L, 1, &l); luaL_Buffer b; int n = 0; char t[4]; @@ -99,7 +102,8 @@ static int Lbase64_decode(lua_State* L) { /** decode(s) */ int c = *s++; switch(c) { - const char* p; + const char *p; + default: p = strchr(code, c); @@ -115,15 +119,18 @@ static int Lbase64_decode(lua_State* L) { /** decode(s) */ } break; + case '=': switch(n) { case 1: base64_decode(&b, t[0], 0, 0, 0, 1); break; + case 2: base64_decode(&b, t[0], t[1], 0, 0, 2); break; + case 3: base64_decode(&b, t[0], t[1], t[2], 0, 3); break; @@ -131,9 +138,11 @@ static int Lbase64_decode(lua_State* L) { /** decode(s) */ n = 0; break; + case 0: luaL_pushresult(&b); return 1; + case '\n': case '\r': case '\t': @@ -163,9 +172,9 @@ static const luaL_Reg Reg_base64[] = { /* * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid. */ -static const char* utf8_decode(const char* o, int* val) { +static const char *utf8_decode(const char *o, int *val) { static unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF}; - const unsigned char* s = (const unsigned char*)o; + const unsigned char *s = (const unsigned char *)o; unsigned int c = s[0]; unsigned int res = 0; /* final result */ @@ -198,20 +207,20 @@ static const char* utf8_decode(const char* o, int* val) { *val = res; } - return (const char*)s + 1; /* +1 to include first byte */ + return (const char *)s + 1; /* +1 to include first byte */ } /* * Check that a string is valid UTF-8 * Returns NULL if not */ -const char* check_utf8(lua_State* L, int idx, size_t* l) { +const char *check_utf8(lua_State *L, int idx, size_t *l) { size_t pos, len; - const char* s = luaL_checklstring(L, 1, &len); + const char *s = luaL_checklstring(L, 1, &len); pos = 0; while(pos <= len) { - const char* s1 = utf8_decode(s + pos, NULL); + const char *s1 = utf8_decode(s + pos, NULL); if(s1 == NULL) { /* conversion error? */ return NULL; @@ -227,12 +236,12 @@ const char* check_utf8(lua_State* L, int idx, size_t* l) { return s; } -static int Lutf8_valid(lua_State* L) { +static int Lutf8_valid(lua_State *L) { lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL); return 1; } -static int Lutf8_length(lua_State* L) { +static int Lutf8_length(lua_State *L) { size_t len; if(!check_utf8(L, 1, &len)) { @@ -258,10 +267,10 @@ static const luaL_Reg Reg_utf8[] = { #include <unicode/ustring.h> #include <unicode/utrace.h> -static int icu_stringprep_prep(lua_State* L, const UStringPrepProfile* profile) { +static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) { size_t input_len; int32_t unprepped_len, prepped_len, output_len; - const char* input; + const char *input; char output[1024]; UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */ @@ -306,10 +315,10 @@ static int icu_stringprep_prep(lua_State* L, const UStringPrepProfile* profile) } } -UStringPrepProfile* icu_nameprep; -UStringPrepProfile* icu_nodeprep; -UStringPrepProfile* icu_resourceprep; -UStringPrepProfile* icu_saslprep; +UStringPrepProfile *icu_nameprep; +UStringPrepProfile *icu_nodeprep; +UStringPrepProfile *icu_resourceprep; +UStringPrepProfile *icu_saslprep; /* initialize global ICU stringprep profiles */ void init_icu() { @@ -346,9 +355,9 @@ static const luaL_Reg Reg_stringprep[] = { #include <stringprep.h> -static int stringprep_prep(lua_State* L, const Stringprep_profile* profile) { +static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) { size_t len; - const char* s; + const char *s; char string[1024]; int ret; @@ -398,10 +407,10 @@ static const luaL_Reg Reg_stringprep[] = { #include <unicode/ustdio.h> #include <unicode/uidna.h> /* IDNA2003 or IDNA2008 ? ? ? */ -static int Lidna_to_ascii(lua_State* L) { /** idna.to_ascii(s) */ +static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */ size_t len; int32_t ulen, dest_len, output_len; - const char* s = luaL_checklstring(L, 1, &len); + const char *s = luaL_checklstring(L, 1, &len); UChar ustr[1024]; UErrorCode err = U_ZERO_ERROR; UChar dest[1024]; @@ -432,10 +441,10 @@ static int Lidna_to_ascii(lua_State* L) { /** idna.to_ascii(s) */ } } -static int Lidna_to_unicode(lua_State* L) { /** idna.to_unicode(s) */ +static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */ size_t len; int32_t ulen, dest_len, output_len; - const char* s = luaL_checklstring(L, 1, &len); + const char *s = luaL_checklstring(L, 1, &len); UChar ustr[1024]; UErrorCode err = U_ZERO_ERROR; UChar dest[1024]; @@ -472,10 +481,10 @@ static int Lidna_to_unicode(lua_State* L) { /** idna.to_unicode(s) */ #include <idna.h> #include <idn-free.h> -static int Lidna_to_ascii(lua_State* L) { /** idna.to_ascii(s) */ +static int Lidna_to_ascii(lua_State *L) { /** idna.to_ascii(s) */ size_t len; - const char* s = check_utf8(L, 1, &len); - char* output = NULL; + const char *s = check_utf8(L, 1, &len); + char *output = NULL; int ret; if(s == NULL || len != strlen(s)) { @@ -496,10 +505,10 @@ static int Lidna_to_ascii(lua_State* L) { /** idna.to_ascii(s) */ } } -static int Lidna_to_unicode(lua_State* L) { /** idna.to_unicode(s) */ +static int Lidna_to_unicode(lua_State *L) { /** idna.to_unicode(s) */ size_t len; - const char* s = luaL_checklstring(L, 1, &len); - char* output = NULL; + const char *s = luaL_checklstring(L, 1, &len); + char *output = NULL; int ret = idna_to_unicode_8z8z(s, &output, 0); if(ret == IDNA_SUCCESS) { @@ -522,7 +531,7 @@ static const luaL_Reg Reg_idna[] = { /***************** end *****************/ -LUALIB_API int luaopen_util_encodings(lua_State* L) { +LUALIB_API int luaopen_util_encodings(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif diff --git a/util-src/hashes.c b/util-src/hashes.c index 697c632f..82f5876e 100644 --- a/util-src/hashes.c +++ b/util-src/hashes.c @@ -33,8 +33,8 @@ typedef unsigned __int32 uint32_t; #define HMAC_IPAD 0x36363636 #define HMAC_OPAD 0x5c5c5c5c -const char* hex_tab = "0123456789abcdef"; -void toHex(const unsigned char* in, int length, unsigned char* out) { +const char *hex_tab = "0123456789abcdef"; +void toHex(const unsigned char *in, int length, unsigned char *out) { int i; for(i = 0; i < length; i++) { @@ -67,15 +67,15 @@ MAKE_HASH_FUNCTION(Lsha512, SHA512, SHA512_DIGEST_LENGTH) MAKE_HASH_FUNCTION(Lmd5, MD5, MD5_DIGEST_LENGTH) struct hash_desc { - int (*Init)(void*); - int (*Update)(void*, const void*, size_t); - int (*Final)(unsigned char*, void*); + int (*Init)(void *); + int (*Update)(void *, const void *, size_t); + int (*Final)(unsigned char *, void *); size_t digestLength; - void* ctx, *ctxo; + void *ctx, *ctxo; }; -static void hmac(struct hash_desc* desc, const char* key, size_t key_len, - const char* msg, size_t msg_len, unsigned char* result) { +static void hmac(struct hash_desc *desc, const char *key, size_t key_len, + const char *msg, size_t msg_len, unsigned char *result) { union xory { unsigned char bytes[64]; uint32_t quadbytes[16]; @@ -89,7 +89,7 @@ static void hmac(struct hash_desc* desc, const char* key, size_t key_len, desc->Init(desc->ctx); desc->Update(desc->ctx, key, key_len); desc->Final(hashedKey, desc->ctx); - key = (const char*)hashedKey; + key = (const char *)hashedKey; key_len = desc->digestLength; } @@ -142,7 +142,7 @@ MAKE_HMAC_FUNCTION(Lhmac_sha256, SHA256, SHA256_DIGEST_LENGTH, SHA256_CTX) MAKE_HMAC_FUNCTION(Lhmac_sha512, SHA512, SHA512_DIGEST_LENGTH, SHA512_CTX) MAKE_HMAC_FUNCTION(Lhmac_md5, MD5, MD5_DIGEST_LENGTH, MD5_CTX) -static int LscramHi(lua_State* L) { +static int LscramHi(lua_State *L) { union xory { unsigned char bytes[SHA_DIGEST_LENGTH]; uint32_t quadbytes[SHA_DIGEST_LENGTH / 4]; @@ -154,14 +154,14 @@ static int LscramHi(lua_State* L) { union xory res; size_t str_len, salt_len; struct hash_desc desc; - const char* str = luaL_checklstring(L, 1, &str_len); - const char* salt = luaL_checklstring(L, 2, &salt_len); - char* salt2; + const char *str = luaL_checklstring(L, 1, &str_len); + const char *salt = luaL_checklstring(L, 2, &salt_len); + char *salt2; const int iter = luaL_checkinteger(L, 3); - desc.Init = (int (*)(void*))SHA1_Init; - desc.Update = (int (*)(void*, const void*, size_t))SHA1_Update; - desc.Final = (int (*)(unsigned char*, void*))SHA1_Final; + desc.Init = (int (*)(void *))SHA1_Init; + desc.Update = (int (*)(void *, const void *, size_t))SHA1_Update; + desc.Final = (int (*)(unsigned char *, void *))SHA1_Final; desc.digestLength = SHA_DIGEST_LENGTH; desc.ctx = &ctx; desc.ctxo = &ctxo; @@ -181,7 +181,7 @@ static int LscramHi(lua_State* L) { for(i = 1; i < iter; i++) { int j; - hmac(&desc, str, str_len, (char*)Ust, sizeof(Ust), Und.bytes); + hmac(&desc, str, str_len, (char *)Ust, sizeof(Ust), Und.bytes); for(j = 0; j < SHA_DIGEST_LENGTH / 4; j++) { res.quadbytes[j] ^= Und.quadbytes[j]; @@ -190,7 +190,7 @@ static int LscramHi(lua_State* L) { memcpy(Ust, Und.bytes, sizeof(Ust)); } - lua_pushlstring(L, (char*)res.bytes, SHA_DIGEST_LENGTH); + lua_pushlstring(L, (char *)res.bytes, SHA_DIGEST_LENGTH); return 1; } @@ -210,7 +210,7 @@ static const luaL_Reg Reg[] = { { NULL, NULL } }; -LUALIB_API int luaopen_util_hashes(lua_State* L) { +LUALIB_API int luaopen_util_hashes(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif diff --git a/util-src/net.c b/util-src/net.c index 18f3d27a..32f69025 100644 --- a/util-src/net.c +++ b/util-src/net.c @@ -32,19 +32,19 @@ /* Enumerate all locally configured IP addresses */ -const char* const type_strings[] = { +const char *const type_strings[] = { "both", "ipv4", "ipv6", NULL }; -static int lc_local_addresses(lua_State* L) { +static int lc_local_addresses(lua_State *L) { #ifndef _WIN32 /* Link-local IPv4 addresses; see RFC 3927 and RFC 5735 */ const long ip4_linklocal = htonl(0xa9fe0000); /* 169.254.0.0 */ const long ip4_mask = htonl(0xffff0000); - struct ifaddrs* addr = NULL, *a; + struct ifaddrs *addr = NULL, *a; #endif int n = 1; int type = luaL_checkoption(L, 1, "both", type_strings); @@ -69,7 +69,7 @@ static int lc_local_addresses(lua_State* L) { for(a = addr; a; a = a->ifa_next) { int family; char ipaddr[INET6_ADDRSTRLEN]; - const char* tmp = NULL; + const char *tmp = NULL; if(a->ifa_addr == NULL || a->ifa_flags & IFF_LOOPBACK) { continue; @@ -78,7 +78,7 @@ static int lc_local_addresses(lua_State* L) { family = a->ifa_addr->sa_family; if(ipv4 && family == AF_INET) { - struct sockaddr_in* sa = (struct sockaddr_in*)a->ifa_addr; + struct sockaddr_in *sa = (struct sockaddr_in *)a->ifa_addr; if(!link_local && ((sa->sin_addr.s_addr & ip4_mask) == ip4_linklocal)) { continue; @@ -86,7 +86,7 @@ static int lc_local_addresses(lua_State* L) { tmp = inet_ntop(family, &sa->sin_addr, ipaddr, sizeof(ipaddr)); } else if(ipv6 && family == AF_INET6) { - struct sockaddr_in6* sa = (struct sockaddr_in6*)a->ifa_addr; + struct sockaddr_in6 *sa = (struct sockaddr_in6 *)a->ifa_addr; if(!link_local && IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr)) { continue; @@ -124,7 +124,7 @@ static int lc_local_addresses(lua_State* L) { return 1; } -int luaopen_util_net(lua_State* L) { +int luaopen_util_net(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif diff --git a/util-src/pposix.c b/util-src/pposix.c index b6874318..5e21be56 100644 --- a/util-src/pposix.c +++ b/util-src/pposix.c @@ -64,7 +64,7 @@ /* Daemonization support */ -static int lc_daemonize(lua_State* L) { +static int lc_daemonize(lua_State *L) { pid_t pid; @@ -118,7 +118,7 @@ static int lc_daemonize(lua_State* L) { /* Syslog support */ -const char* const facility_strings[] = { +const char *const facility_strings[] = { "auth", #if !(defined(sun) || defined(__sun)) "authpriv", @@ -180,9 +180,9 @@ int facility_constants[] = { constant. " -- syslog manpage */ -char* syslog_ident = NULL; +char *syslog_ident = NULL; -int lc_syslog_open(lua_State* L) { +int lc_syslog_open(lua_State *L) { int facility = luaL_checkoption(L, 2, "daemon", facility_strings); facility = facility_constants[facility]; @@ -198,7 +198,7 @@ int lc_syslog_open(lua_State* L) { return 0; } -const char* const level_strings[] = { +const char *const level_strings[] = { "debug", "info", "notice", @@ -214,7 +214,7 @@ int level_constants[] = { LOG_CRIT, -1 }; -int lc_syslog_log(lua_State* L) { +int lc_syslog_log(lua_State *L) { int level = level_constants[luaL_checkoption(L, 1, "notice", level_strings)]; if(lua_gettop(L) == 3) { @@ -226,7 +226,7 @@ int lc_syslog_log(lua_State* L) { return 0; } -int lc_syslog_close(lua_State* L) { +int lc_syslog_close(lua_State *L) { closelog(); if(syslog_ident) { @@ -237,7 +237,7 @@ int lc_syslog_close(lua_State* L) { return 0; } -int lc_syslog_setmask(lua_State* L) { +int lc_syslog_setmask(lua_State *L) { int level_idx = luaL_checkoption(L, 1, "notice", level_strings); int mask = 0; @@ -251,24 +251,24 @@ int lc_syslog_setmask(lua_State* L) { /* getpid */ -int lc_getpid(lua_State* L) { +int lc_getpid(lua_State *L) { lua_pushinteger(L, getpid()); return 1; } /* UID/GID functions */ -int lc_getuid(lua_State* L) { +int lc_getuid(lua_State *L) { lua_pushinteger(L, getuid()); return 1; } -int lc_getgid(lua_State* L) { +int lc_getgid(lua_State *L) { lua_pushinteger(L, getgid()); return 1; } -int lc_setuid(lua_State* L) { +int lc_setuid(lua_State *L) { int uid = -1; if(lua_gettop(L) < 1) { @@ -277,7 +277,7 @@ int lc_setuid(lua_State* L) { if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) { /* Passed UID is actually a string, so look up the UID */ - struct passwd* p; + struct passwd *p; p = getpwnam(lua_tostring(L, 1)); if(!p) { @@ -303,9 +303,11 @@ int lc_setuid(lua_State* L) { case EINVAL: lua_pushstring(L, "invalid-uid"); break; + case EPERM: lua_pushstring(L, "permission-denied"); break; + default: lua_pushstring(L, "unknown-error"); } @@ -324,7 +326,7 @@ int lc_setuid(lua_State* L) { return 2; } -int lc_setgid(lua_State* L) { +int lc_setgid(lua_State *L) { int gid = -1; if(lua_gettop(L) < 1) { @@ -333,7 +335,7 @@ int lc_setgid(lua_State* L) { if(!lua_isnumber(L, 1) && lua_tostring(L, 1)) { /* Passed GID is actually a string, so look up the GID */ - struct group* g; + struct group *g; g = getgrnam(lua_tostring(L, 1)); if(!g) { @@ -359,9 +361,11 @@ int lc_setgid(lua_State* L) { case EINVAL: lua_pushstring(L, "invalid-gid"); break; + case EPERM: lua_pushstring(L, "permission-denied"); break; + default: lua_pushstring(L, "unknown-error"); } @@ -380,10 +384,10 @@ int lc_setgid(lua_State* L) { return 2; } -int lc_initgroups(lua_State* L) { +int lc_initgroups(lua_State *L) { int ret; gid_t gid; - struct passwd* p; + struct passwd *p; if(!lua_isstring(L, 1)) { lua_pushnil(L); @@ -407,9 +411,11 @@ int lc_initgroups(lua_State* L) { case LUA_TNIL: gid = p->pw_gid; break; + case LUA_TNUMBER: gid = lua_tointeger(L, 2); break; + default: lua_pushnil(L); lua_pushstring(L, "invalid-gid"); @@ -424,10 +430,12 @@ int lc_initgroups(lua_State* L) { lua_pushnil(L); lua_pushstring(L, "no-memory"); break; + case EPERM: lua_pushnil(L); lua_pushstring(L, "permission-denied"); break; + default: lua_pushnil(L); lua_pushstring(L, "unknown-error"); @@ -440,7 +448,7 @@ int lc_initgroups(lua_State* L) { return 2; } -int lc_umask(lua_State* L) { +int lc_umask(lua_State *L) { char old_mode_string[7]; mode_t old_mode = umask(strtoul(luaL_checkstring(L, 1), NULL, 8)); @@ -451,7 +459,7 @@ int lc_umask(lua_State* L) { return 1; } -int lc_mkdir(lua_State* L) { +int lc_mkdir(lua_State *L) { int ret = mkdir(luaL_checkstring(L, 1), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH); /* mode 775 */ @@ -476,7 +484,7 @@ int lc_mkdir(lua_State* L) { * Example usage: * pposix.setrlimit("NOFILE", 1000, 2000) */ -int string2resource(const char* s) { +int string2resource(const char *s) { if(!strcmp(s, "CORE")) { return RLIMIT_CORE; } @@ -526,7 +534,7 @@ int string2resource(const char* s) { return -1; } -unsigned long int arg_to_rlimit(lua_State* L, int idx, rlim_t current) { +unsigned long int arg_to_rlimit(lua_State *L, int idx, rlim_t current) { switch(lua_type(L, idx)) { case LUA_TSTRING: @@ -536,15 +544,17 @@ unsigned long int arg_to_rlimit(lua_State* L, int idx, rlim_t current) { case LUA_TNUMBER: return lua_tointeger(L, idx); + case LUA_TNONE: case LUA_TNIL: return current; + default: return luaL_argerror(L, idx, "unexpected type"); } } -int lc_setrlimit(lua_State* L) { +int lc_setrlimit(lua_State *L) { struct rlimit lim; int arguments = lua_gettop(L); int rid = -1; @@ -583,9 +593,9 @@ int lc_setrlimit(lua_State* L) { return 1; } -int lc_getrlimit(lua_State* L) { +int lc_getrlimit(lua_State *L) { int arguments = lua_gettop(L); - const char* resource = NULL; + const char *resource = NULL; int rid = -1; struct rlimit lim; @@ -628,12 +638,12 @@ int lc_getrlimit(lua_State* L) { return 3; } -int lc_abort(lua_State* L) { +int lc_abort(lua_State *L) { abort(); return 0; } -int lc_uname(lua_State* L) { +int lc_uname(lua_State *L) { struct utsname uname_info; if(uname(&uname_info) != 0) { @@ -660,9 +670,9 @@ int lc_uname(lua_State* L) { return 1; } -int lc_setenv(lua_State* L) { - const char* var = luaL_checkstring(L, 1); - const char* value; +int lc_setenv(lua_State *L) { + const char *var = luaL_checkstring(L, 1); + const char *value; /* If the second argument is nil or nothing, unset the var */ if(lua_isnoneornil(L, 2)) { @@ -689,7 +699,7 @@ int lc_setenv(lua_State* L) { } #ifdef WITH_MALLINFO -int lc_meminfo(lua_State* L) { +int lc_meminfo(lua_State *L) { struct mallinfo info = mallinfo(); lua_newtable(L); /* This is the total size of memory allocated with sbrk by malloc, in bytes. */ @@ -717,10 +727,10 @@ int lc_meminfo(lua_State* L) { * */ #if _XOPEN_SOURCE >= 600 || _POSIX_C_SOURCE >= 200112L || defined(_GNU_SOURCE) -int lc_fallocate(lua_State* L) { +int lc_fallocate(lua_State *L) { int ret; off_t offset, len; - FILE* f = *(FILE**) luaL_checkudata(L, 1, LUA_FILEHANDLE); + FILE *f = *(FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE); if(f == NULL) { return luaL_error(L, "attempt to use a closed file"); @@ -763,12 +773,14 @@ int lc_fallocate(lua_State* L) { } else { lua_pushnil(L); lua_pushstring(L, strerror(ret)); + /* posix_fallocate() can leave a bunch of NULs at the end, so we cut that * this assumes that offset == length of the file */ if(ftruncate(fileno(f), offset) != 0) { lua_pushstring(L, strerror(errno)); return 3; } + return 2; } } @@ -776,7 +788,7 @@ int lc_fallocate(lua_State* L) { /* Register functions */ -int luaopen_util_pposix(lua_State* L) { +int luaopen_util_pposix(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif diff --git a/util-src/ringbuffer.c b/util-src/ringbuffer.c index 640dc648..831b1fe2 100644 --- a/util-src/ringbuffer.c +++ b/util-src/ringbuffer.c @@ -15,23 +15,23 @@ typedef struct { char buffer[]; } ringbuffer; -char readchar(ringbuffer* b) { +char readchar(ringbuffer *b) { b->blen--; return b->buffer[(b->rpos++) % b->alen]; } -void writechar(ringbuffer* b, char c) { +void writechar(ringbuffer *b, char c) { b->blen++; b->buffer[(b->wpos++) % b->alen] = c; } /* make sure position counters stay within the allocation */ -void modpos(ringbuffer* b) { +void modpos(ringbuffer *b) { b->rpos = b->rpos % b->alen; b->wpos = b->wpos % b->alen; } -int find(ringbuffer* b, const char* s, int l) { +int find(ringbuffer *b, const char *s, int l) { size_t i, j; int m; @@ -58,10 +58,10 @@ int find(ringbuffer* b, const char* s, int l) { return 0; } -int rb_find(lua_State* L) { +int rb_find(lua_State *L) { size_t l, m; - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); - const char* s = luaL_checklstring(L, 2, &l); + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); + const char *s = luaL_checklstring(L, 2, &l); m = find(b, s, l); if(m > 0) { @@ -72,8 +72,8 @@ int rb_find(lua_State* L) { return 0; } -int rb_read(lua_State* L) { - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); +int rb_read(lua_State *L) { + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); int r = luaL_checkinteger(L, 2); int peek = lua_toboolean(L, 3); @@ -99,10 +99,10 @@ int rb_read(lua_State* L) { return 1; } -int rb_readuntil(lua_State* L) { +int rb_readuntil(lua_State *L) { size_t l, m; - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); - const char* s = luaL_checklstring(L, 2, &l); + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); + const char *s = luaL_checklstring(L, 2, &l); m = find(b, s, l); if(m > 0) { @@ -114,10 +114,10 @@ int rb_readuntil(lua_State* L) { return 0; } -int rb_write(lua_State* L) { +int rb_write(lua_State *L) { size_t l, w = 0; - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); - const char* s = luaL_checklstring(L, 2, &l); + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); + const char *s = luaL_checklstring(L, 2, &l); /* Does `l` bytes fit? */ if((l + b->blen) > b->alen) { @@ -137,31 +137,31 @@ int rb_write(lua_State* L) { return 1; } -int rb_tostring(lua_State* L) { - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); +int rb_tostring(lua_State *L) { + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); lua_pushfstring(L, "ringbuffer: %p %d/%d", b, b->blen, b->alen); return 1; } -int rb_length(lua_State* L) { - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); +int rb_length(lua_State *L) { + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); lua_pushinteger(L, b->blen); return 1; } -int rb_size(lua_State* L) { - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); +int rb_size(lua_State *L) { + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); lua_pushinteger(L, b->alen); return 1; } -int rb_free(lua_State* L) { - ringbuffer* b = luaL_checkudata(L, 1, "ringbuffer_mt"); +int rb_free(lua_State *L) { + ringbuffer *b = luaL_checkudata(L, 1, "ringbuffer_mt"); lua_pushinteger(L, b->alen - b->blen); return 1; } -int rb_new(lua_State* L) { +int rb_new(lua_State *L) { size_t size = luaL_optinteger(L, 1, sysconf(_SC_PAGESIZE)); ringbuffer *b = lua_newuserdata(L, sizeof(ringbuffer) + size); @@ -176,10 +176,11 @@ int rb_new(lua_State* L) { return 1; } -int luaopen_util_ringbuffer(lua_State* L) { +int luaopen_util_ringbuffer(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif + if(luaL_newmetatable(L, "ringbuffer_mt")) { lua_pushcfunction(L, rb_tostring); lua_setfield(L, -2, "__tostring"); diff --git a/util-src/signal.c b/util-src/signal.c index 3f29febb..a083df54 100644 --- a/util-src/signal.c +++ b/util-src/signal.c @@ -41,7 +41,7 @@ #define lsig struct lua_signal { - char* name; /* name of the signal */ + char *name; /* name of the signal */ int sig; /* the signal */ }; @@ -153,20 +153,20 @@ static const struct lua_signal lua_signals[] = { {NULL, 0} }; -static lua_State* Lsig = NULL; +static lua_State *Lsig = NULL; static lua_Hook Hsig = NULL; static int Hmask = 0; static int Hcount = 0; static struct signal_event { int Nsig; - struct signal_event* next_event; -}* signal_queue = NULL; + struct signal_event *next_event; +} *signal_queue = NULL; -static struct signal_event* last_event = NULL; +static struct signal_event *last_event = NULL; -static void sighook(lua_State* L, lua_Debug* ar) { - struct signal_event* event; +static void sighook(lua_State *L, lua_Debug *ar) { + struct signal_event *event; /* restore the old hook */ lua_sethook(L, Hsig, Hmask, Hcount); @@ -220,7 +220,7 @@ static void handle(int sig) { * in an unstable state. */ -static int l_signal(lua_State* L) { +static int l_signal(lua_State *L) { int args = lua_gettop(L); int t, sig; /* type, signal */ @@ -295,7 +295,7 @@ static int l_signal(lua_State* L) { * signal = signal number or string */ -static int l_raise(lua_State* L) { +static int l_raise(lua_State *L) { /* int args = lua_gettop(L); */ int t = 0; /* type */ lua_Number ret; @@ -338,7 +338,7 @@ static int l_raise(lua_State* L) { * signal = signal number or string */ -static int l_kill(lua_State* L) { +static int l_kill(lua_State *L) { int t; /* type */ lua_Number ret; /* return value */ @@ -383,7 +383,7 @@ static const struct luaL_Reg lsignal_lib[] = { {NULL, NULL} }; -int luaopen_util_signal(lua_State* L) { +int luaopen_util_signal(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif diff --git a/util-src/table.c b/util-src/table.c index c9c09170..0e547858 100644 --- a/util-src/table.c +++ b/util-src/table.c @@ -1,25 +1,27 @@ #include <lua.h> #include <lauxlib.h> -static int Lcreate_table(lua_State* L) { +static int Lcreate_table(lua_State *L) { lua_createtable(L, luaL_checkinteger(L, 1), luaL_checkinteger(L, 2)); return 1; } -static int Lpack(lua_State* L) { +static int Lpack(lua_State *L) { int arg; unsigned int n_args = lua_gettop(L); lua_createtable(L, n_args, 1); lua_insert(L, 1); + for(arg = n_args; arg >= 1; arg--) { lua_rawseti(L, 1, arg); } + lua_pushinteger(L, n_args); lua_setfield(L, -2, "n"); return 1; } -int luaopen_util_table(lua_State* L) { +int luaopen_util_table(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif diff --git a/util-src/windows.c b/util-src/windows.c index e4457994..89bec57b 100644 --- a/util-src/windows.c +++ b/util-src/windows.c @@ -23,9 +23,9 @@ #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) #endif -static int Lget_nameservers(lua_State* L) { +static int Lget_nameservers(lua_State *L) { char stack_buffer[1024]; // stack allocated buffer - IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer; + IP4_ARRAY *ips = (IP4_ARRAY *) stack_buffer; DWORD len = sizeof(stack_buffer); DNS_STATUS status; @@ -51,13 +51,13 @@ static int Lget_nameservers(lua_State* L) { } } -static int lerror(lua_State* L, char* string) { +static int lerror(lua_State *L, char *string) { lua_pushnil(L); lua_pushfstring(L, "%s: %d", string, GetLastError()); return 2; } -static int Lget_consolecolor(lua_State* L) { +static int Lget_consolecolor(lua_State *L) { HWND console = GetStdHandle(STD_OUTPUT_HANDLE); WORD color; DWORD read_len; @@ -79,7 +79,7 @@ static int Lget_consolecolor(lua_State* L) { lua_pushnumber(L, color); return 1; } -static int Lset_consolecolor(lua_State* L) { +static int Lset_consolecolor(lua_State *L) { int color = luaL_checkint(L, 1); HWND console = GetStdHandle(STD_OUTPUT_HANDLE); @@ -102,7 +102,7 @@ static const luaL_Reg Reg[] = { { NULL, NULL } }; -LUALIB_API int luaopen_util_windows(lua_State* L) { +LUALIB_API int luaopen_util_windows(lua_State *L) { #if (LUA_VERSION_NUM > 501) luaL_checkversion(L); #endif |