From 82e15d1377d87b4b27aa7db01ec4551b8ea212b6 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Thu, 17 Sep 2015 20:24:49 +0200 Subject: [PATCH] CLeanups/fixes --- frontend/drivers/platform_linux.c | 40 +++++++--------------------- libretro-common/file/retro_file.c | 26 ++++++++++++++++-- libretro-common/include/retro_file.h | 4 +++ libretro-db/bintree.c | 4 +-- libretro-db/libretrodb.c | 12 ++++----- 5 files changed, 46 insertions(+), 40 deletions(-) diff --git a/frontend/drivers/platform_linux.c b/frontend/drivers/platform_linux.c index 7a951e4c7e..88e13abb06 100644 --- a/frontend/drivers/platform_linux.c +++ b/frontend/drivers/platform_linux.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -49,25 +50,6 @@ #include "../../general.h" #include "platform_linux.h" -static bool load_generic_file(ssize_t *len, const char *path, char *buf, size_t buflen) -{ - const int fd = open(path, O_RDONLY); - if (fd < 0) - return false; - - do - { - *len = read(fd, buf, buflen); - }while(*len < 0 && errno == EINTR); - - close(fd); - if (*len < 0) - return false; - buf[*len] = '\0'; /* null-terminate the string. */ - - return true; -} - static bool cpu_inited_once; static cpu_family g_cpuFamily; static uint64_t g_cpuFeatures; @@ -299,7 +281,7 @@ static void cpulist_read_from(CpuList* list, const char* filename) list->mask = 0; - if (!load_generic_file(&filelen, filename, file, sizeof(file))) + if (!retro_fmemcpy(filename, file, sizeof(file), &filelen)) { RARCH_ERR("Could not read %s: %s\n", filename, strerror(errno)); return; @@ -339,9 +321,7 @@ static void linux_cpu_init(void) g_cpuFeatures = 0; g_cpuCount = 1; - load_generic_file(&cpuinfo_len, "/proc/cpuinfo", cpuinfo, sizeof cpuinfo); - - if (cpuinfo_len < 0) + if (!retro_fmemcpy("/proc/cpuinfo", cpuinfo, sizeof(cpuinfo), &cpuinfo_len)) return; /* Count the CPU cores, the value may be 0 for single-core CPUs */ @@ -1005,11 +985,11 @@ static void check_proc_acpi_battery(const char * node, bool * have_battery, snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state"); - if (!load_generic_file(&buflen, path, state, sizeof (state))) + if (!retro_fmemcpy(path, state, sizeof(state), &buflen)) return; snprintf(path, sizeof(path), "%s/%s/%s", base, node, "info"); - if (!load_generic_file(&buflen, path, info, sizeof (info))) + if (!retro_fmemcpy(path, info, sizeof(info), &buflen)) return; ptr = &state[0]; @@ -1120,7 +1100,7 @@ static void check_proc_acpi_sysfs_battery(const char * node, bool * have_battery return; snprintf(path, sizeof(path), "%s/%s/%s", base, node, "status"); - if (!load_generic_file(&buflen, path, state, sizeof (state))) + if (!retro_fmemcpy(path, state, sizeof (state), &buflen)) return; if (strstr(state, "Discharging")) @@ -1129,7 +1109,7 @@ static void check_proc_acpi_sysfs_battery(const char * node, bool * have_battery *have_battery = true; snprintf(path, sizeof(path), "%s/%s/%s", base, node, "capacity"); - if (!load_generic_file(&buflen, path, state, sizeof (state))) + if (!retro_fmemcpy(path, state, sizeof (state), &buflen)) return; capacity = atoi(state); @@ -1148,7 +1128,7 @@ static void check_proc_acpi_ac_adapter(const char * node, bool *have_ac) ssize_t buflen = 0; snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state"); - if (!load_generic_file(&buflen, path, state, sizeof (state))) + if (!retro_fmemcpy(path, state, sizeof(state), &buflen)) return; ptr = &state[0]; @@ -1170,7 +1150,7 @@ static void check_proc_acpi_sysfs_ac_adapter(const char * node, bool *have_ac) const char *base = proc_acpi_sysfs_ac_adapter_path; snprintf(path, sizeof(path), "%s/%s/%s", base, node, "online"); - if (!load_generic_file(&buflen, path, state, sizeof (state))) + if (!retro_fmemcpy(path, state, sizeof(state), &buflen)) return; if (strstr(state, "1")) @@ -1220,7 +1200,7 @@ static bool frontend_linux_powerstate_check_apm( ssize_t buflen = 0; char *str = NULL; - if (!load_generic_file(&buflen, proc_apm_path, buf, sizeof(buf))) + if (!retro_fmemcpy(proc_apm_path, buf, sizeof(buf), &buflen)) return false; ptr = &buf[0]; diff --git a/libretro-common/file/retro_file.c b/libretro-common/file/retro_file.c index d3a6b0ae3a..aea4b1dee1 100644 --- a/libretro-common/file/retro_file.c +++ b/libretro-common/file/retro_file.c @@ -3,6 +3,7 @@ #include #include #include +#include #if defined(_WIN32) # include @@ -37,10 +38,10 @@ #include #endif -typedef struct RFILE +struct RFILE { int fd; -} RFILE; +}; RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len) { @@ -99,3 +100,24 @@ void retro_fclose(RFILE *stream) close(stream->fd); free(stream); } + +bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written) +{ + RFILE *stream = retro_fopen(path, RFILE_MODE_READ, -1); + if (!stream) + return false; + + do + { + *bytes_written = retro_fread(stream, s, len); + }while(*bytes_written < 0 && errno == EINTR); + + retro_fclose(stream); + if (*bytes_written < 0) + return false; + + /* NULL-terminate the string. */ + s[*bytes_written] = '\0'; + + return true; +} diff --git a/libretro-common/include/retro_file.h b/libretro-common/include/retro_file.h index 920ebf426c..cf535da63f 100644 --- a/libretro-common/include/retro_file.h +++ b/libretro-common/include/retro_file.h @@ -28,6 +28,8 @@ #include +#include + #ifdef __cplusplus extern "C" { #endif @@ -51,6 +53,8 @@ ssize_t retro_fwrite(RFILE *stream, const void *s, size_t len); void retro_fclose(RFILE *stream); +bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written); + #ifdef __cplusplus } #endif diff --git a/libretro-db/bintree.c b/libretro-db/bintree.c index 37ad0b73e7..4bd7ce5027 100644 --- a/libretro-db/bintree.c +++ b/libretro-db/bintree.c @@ -14,12 +14,12 @@ struct bintree_node struct bintree_node *right; }; -typedef struct bintree +struct bintree { struct bintree_node *root; bintree_cmp_func cmp; void *ctx; -} bintree_t; +}; static void *NIL_NODE = &NIL_NODE; diff --git a/libretro-db/libretrodb.c b/libretro-db/libretrodb.c index 6808155111..dc750e6297 100644 --- a/libretro-db/libretrodb.c +++ b/libretro-db/libretrodb.c @@ -31,21 +31,21 @@ struct node_iter_ctx libretrodb_index_t *idx; }; -typedef struct libretrodb +struct libretrodb { RFILE *fd; uint64_t root; uint64_t count; uint64_t first_index_offset; char path[1024]; -} libretrodb_t; +}; -typedef struct libretrodb_index +struct libretrodb_index { char name[50]; uint64_t key_size; uint64_t next; -} libretrodb_index_t; +}; typedef struct libretrodb_metadata { @@ -58,14 +58,14 @@ typedef struct libretrodb_header uint64_t metadata_offset; } libretrodb_header_t; -typedef struct libretrodb_cursor +struct libretrodb_cursor { int is_valid; RFILE *fd; int eof; libretrodb_query_t *query; libretrodb_t *db; -} libretrodb_cursor_t; +}; static struct rmsgpack_dom_value sentinal;