CLeanups/fixes

This commit is contained in:
twinaphex 2015-09-17 20:24:49 +02:00
parent 483a4264e5
commit 82e15d1377
5 changed files with 46 additions and 40 deletions

View File

@ -39,6 +39,7 @@
#include <boolean.h> #include <boolean.h>
#include <retro_dirent.h> #include <retro_dirent.h>
#include <retro_inline.h> #include <retro_inline.h>
#include <retro_file.h>
#include <retro_log.h> #include <retro_log.h>
#include <compat/strl.h> #include <compat/strl.h>
#include <rhash.h> #include <rhash.h>
@ -49,25 +50,6 @@
#include "../../general.h" #include "../../general.h"
#include "platform_linux.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 bool cpu_inited_once;
static cpu_family g_cpuFamily; static cpu_family g_cpuFamily;
static uint64_t g_cpuFeatures; static uint64_t g_cpuFeatures;
@ -299,7 +281,7 @@ static void cpulist_read_from(CpuList* list, const char* filename)
list->mask = 0; 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)); RARCH_ERR("Could not read %s: %s\n", filename, strerror(errno));
return; return;
@ -339,9 +321,7 @@ static void linux_cpu_init(void)
g_cpuFeatures = 0; g_cpuFeatures = 0;
g_cpuCount = 1; g_cpuCount = 1;
load_generic_file(&cpuinfo_len, "/proc/cpuinfo", cpuinfo, sizeof cpuinfo); if (!retro_fmemcpy("/proc/cpuinfo", cpuinfo, sizeof(cpuinfo), &cpuinfo_len))
if (cpuinfo_len < 0)
return; return;
/* Count the CPU cores, the value may be 0 for single-core CPUs */ /* 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"); 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; return;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "info"); 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; return;
ptr = &state[0]; ptr = &state[0];
@ -1120,7 +1100,7 @@ static void check_proc_acpi_sysfs_battery(const char * node, bool * have_battery
return; return;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "status"); 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; return;
if (strstr(state, "Discharging")) if (strstr(state, "Discharging"))
@ -1129,7 +1109,7 @@ static void check_proc_acpi_sysfs_battery(const char * node, bool * have_battery
*have_battery = true; *have_battery = true;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "capacity"); 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; return;
capacity = atoi(state); capacity = atoi(state);
@ -1148,7 +1128,7 @@ static void check_proc_acpi_ac_adapter(const char * node, bool *have_ac)
ssize_t buflen = 0; ssize_t buflen = 0;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state"); 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; return;
ptr = &state[0]; 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; const char *base = proc_acpi_sysfs_ac_adapter_path;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "online"); 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; return;
if (strstr(state, "1")) if (strstr(state, "1"))
@ -1220,7 +1200,7 @@ static bool frontend_linux_powerstate_check_apm(
ssize_t buflen = 0; ssize_t buflen = 0;
char *str = NULL; 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; return false;
ptr = &buf[0]; ptr = &buf[0];

View File

@ -3,6 +3,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h>
#if defined(_WIN32) #if defined(_WIN32)
# include <compat/posix_string.h> # include <compat/posix_string.h>
@ -37,10 +38,10 @@
#include <fcntl.h> #include <fcntl.h>
#endif #endif
typedef struct RFILE struct RFILE
{ {
int fd; int fd;
} RFILE; };
RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len) RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
{ {
@ -99,3 +100,24 @@ void retro_fclose(RFILE *stream)
close(stream->fd); close(stream->fd);
free(stream); 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;
}

View File

@ -28,6 +28,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <boolean.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -51,6 +53,8 @@ ssize_t retro_fwrite(RFILE *stream, const void *s, size_t len);
void retro_fclose(RFILE *stream); void retro_fclose(RFILE *stream);
bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -14,12 +14,12 @@ struct bintree_node
struct bintree_node *right; struct bintree_node *right;
}; };
typedef struct bintree struct bintree
{ {
struct bintree_node *root; struct bintree_node *root;
bintree_cmp_func cmp; bintree_cmp_func cmp;
void *ctx; void *ctx;
} bintree_t; };
static void *NIL_NODE = &NIL_NODE; static void *NIL_NODE = &NIL_NODE;

View File

@ -31,21 +31,21 @@ struct node_iter_ctx
libretrodb_index_t *idx; libretrodb_index_t *idx;
}; };
typedef struct libretrodb struct libretrodb
{ {
RFILE *fd; RFILE *fd;
uint64_t root; uint64_t root;
uint64_t count; uint64_t count;
uint64_t first_index_offset; uint64_t first_index_offset;
char path[1024]; char path[1024];
} libretrodb_t; };
typedef struct libretrodb_index struct libretrodb_index
{ {
char name[50]; char name[50];
uint64_t key_size; uint64_t key_size;
uint64_t next; uint64_t next;
} libretrodb_index_t; };
typedef struct libretrodb_metadata typedef struct libretrodb_metadata
{ {
@ -58,14 +58,14 @@ typedef struct libretrodb_header
uint64_t metadata_offset; uint64_t metadata_offset;
} libretrodb_header_t; } libretrodb_header_t;
typedef struct libretrodb_cursor struct libretrodb_cursor
{ {
int is_valid; int is_valid;
RFILE *fd; RFILE *fd;
int eof; int eof;
libretrodb_query_t *query; libretrodb_query_t *query;
libretrodb_t *db; libretrodb_t *db;
} libretrodb_cursor_t; };
static struct rmsgpack_dom_value sentinal; static struct rmsgpack_dom_value sentinal;