diff options
author | sanine <sanine.not@pm.me> | 2023-04-10 12:12:52 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-10 12:12:52 -0500 |
commit | dc1c0f61f062554d5cb10803e9cc09a1d900b683 (patch) | |
tree | 42165b546559a51d4f842ed6ef6650ceb74cda4f /src/bindings.c | |
parent | d377b707b67b477ee5b22b08c714be34f3e82aa3 (diff) |
rewrite bindings.c to be MINGW compatiblewindows
Diffstat (limited to 'src/bindings.c')
-rw-r--r-- | src/bindings.c | 78 |
1 files changed, 47 insertions, 31 deletions
diff --git a/src/bindings.c b/src/bindings.c index 5813d68..00a3fc9 100644 --- a/src/bindings.c +++ b/src/bindings.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <string.h> +#include <stdbool.h> #include <lua.h> #include <lauxlib.h> #include <sys/types.h> @@ -13,6 +14,13 @@ #include "bindings.h" #include "logging.h" +#ifdef __MINGW32__ + // mingw has weird mkdir for some reason? (or maybe linux is just non-POSIX idk) + #define MKDIR(pathname, mode) mkdir(pathname) +#else + #define MKDIR(pathname, mode) mkdir(pathname, mode) +#endif + struct concat_buffer { char *buf; size_t size; @@ -31,10 +39,10 @@ static void md_callback(const MD_CHAR *html, MD_SIZE size, void *data) if (d->index + size >= d->size) { char *new_buf = realloc(d->buf, d->size * 2); if (new_buf == NULL) { - // failed to allocate memory, abort! - argent_log(WARN, "failed to properly allocate memory for html buffer!"); - d->ok = false; - return; + // failed to allocate memory, abort! + argent_log(WARN, "failed to properly allocate memory for html buffer!"); + d->ok = false; + return; } d->buf = new_buf; @@ -50,7 +58,7 @@ static void md_callback(const MD_CHAR *html, MD_SIZE size, void *data) int markdown(lua_State *L) { argent_log(TRACE, "begin markdown parsing"); - char *markdown_buffer = luaL_checkstring(L, 1); + const char *markdown_buffer = luaL_checkstring(L, 1); size_t len = strlen(markdown_buffer); argent_log(TRACE, "markdown input (%ld bytes): %s", len, markdown_buffer); @@ -74,7 +82,7 @@ int markdown(lua_State *L) data.buf[data.index] = 0; argent_log(TRACE, "finished HTML buffer (%d bytes, %d chars): %s", - data.size, data.index, data.buf); + data.size, data.index, data.buf); lua_pushstring(L, data.buf); free(data.buf); @@ -96,13 +104,14 @@ int current_working_directory(lua_State *L) /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ -static void throw_directory_error(lua_State *L, char *dir_name); +static void throw_directory_error(lua_State *L, const char *dir_name); +static bool is_directory(const char *path); static struct dirent *read_dir(lua_State *L, DIR *directory); int scan_directory(lua_State *L) { argent_log(TRACE, "begin scan_directory()"); - char *dir_name = luaL_checkstring(L, 1); + const char *dir_name = luaL_checkstring(L, 1); DIR *directory = opendir(dir_name); if (directory == NULL) @@ -118,13 +127,13 @@ int scan_directory(lua_State *L) struct dirent *entry; while ((entry = read_dir(L, directory)) != NULL) { lua_pushstring(L, entry->d_name); - if (entry->d_type == DT_DIR) { - lua_rawseti(L, directory_table, directory_index); - directory_index += 1; + if (is_directory(entry->d_name)) { + lua_rawseti(L, directory_table, directory_index); + directory_index += 1; } else { - lua_rawseti(L, file_table, file_index); - file_index += 1; + lua_rawseti(L, file_table, file_index); + file_index += 1; } } @@ -135,7 +144,17 @@ int scan_directory(lua_State *L) } -static void throw_directory_error(lua_State *L, char *dir_name) +static bool is_directory(const char *path) +{ + struct stat sb; + if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { + return true; + } + return false; +} + + +static void throw_directory_error(lua_State *L, const char *dir_name) { argent_log(ERROR, "failed to open directory: %d\n", errno); switch(errno) { @@ -205,16 +224,16 @@ static const char* mkdir_error_string() int create_directory(lua_State *L) { - char *dir_name = luaL_checkstring(L, 1); + const char *dir_name = luaL_checkstring(L, 1); mode_t mode = S_IRWXU | S_IRWXG | (S_IROTH | S_IXOTH); // u+rwx, g+rwx, a+rx - int error = mkdir(dir_name, mode); + int error = MKDIR(dir_name, mode); if (error) { argent_log(ERROR, "failed to create directory '%s': %s", - dir_name, mkdir_error_string()); + dir_name, mkdir_error_string()); luaL_error(L, "failed to create directory '%s': %s", - dir_name, mkdir_error_string()); + dir_name, mkdir_error_string()); } return 0; @@ -232,9 +251,6 @@ static const char* open_error_string() case EBUSY: return "Device busy"; - case EDQUOT: - return "User cannot create new inodes"; - case EEXIST: return "File exists"; @@ -275,17 +291,17 @@ static const char* open_error_string() int copy_file(lua_State *L) { - char *source_name = luaL_checkstring(L, 1); - char *dest_name = luaL_checkstring(L, 2); + const char *source_name = luaL_checkstring(L, 1); + const char *dest_name = luaL_checkstring(L, 2); int source_fd, dest_fd; source_fd = open(source_name, 0); if (source_fd == -1) { argent_log(ERROR, "failed to open file '%s': %s", - source_name, open_error_string()); + source_name, open_error_string()); luaL_error(L, "failed to open file '%s': %s", - source_name, open_error_string()); + source_name, open_error_string()); } struct stat source_stat; @@ -293,18 +309,18 @@ int copy_file(lua_State *L) if (error) { close(source_fd); argent_log(ERROR, "failed to stat '%s': %s", - source_name, open_error_string()); + source_name, open_error_string()); luaL_error(L, "failed to stat '%s': %s", - source_name, open_error_string()); + source_name, open_error_string()); } mode_t dest_mode = source_stat.st_mode; dest_fd = creat(dest_name, dest_mode); if (dest_fd == -1) { close(source_fd); argent_log(ERROR, "failed to open file '%s': %s", - dest_name, open_error_string()); + dest_name, open_error_string()); luaL_error(L, "failed to open file '%s': %s", - dest_name, open_error_string()); + dest_name, open_error_string()); } unsigned char buffer[256]; @@ -335,8 +351,8 @@ int copy_file(lua_State *L) int argent_log_lua(lua_State *L) { - char *level_string = luaL_checkstring(L, 1); - char *message = luaL_checkstring(L, 2); + const char *level_string = luaL_checkstring(L, 1); + const char *message = luaL_checkstring(L, 2); int level; |