From 69f7d09103035d66a812e4d64d9ec8bc7a6fa6c4 Mon Sep 17 00:00:00 2001 From: Alex Tereschenko Date: Sat, 20 May 2017 13:17:05 +0200 Subject: [PATCH] mraa.c: remove surplus variables, correct memory allocations Signed-off-by: Alex Tereschenko Signed-off-by: Brendan Le Foll --- src/mraa.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/mraa.c b/src/mraa.c index d4aa217..db9055d 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -108,7 +108,6 @@ imraa_init() return MRAA_SUCCESS; } char* env_var; - mraa_result_t ret; mraa_platform_t platform_type = MRAA_NULL_PLATFORM; uid_t proc_euid = geteuid(); struct passwd* proc_user = getpwuid(proc_euid); @@ -127,7 +126,7 @@ imraa_init() env_var = getenv(MRAA_JSONPLAT_ENV_VAR); if (env_var != NULL) { // We only care about success, the init will write to syslog if things went wrong - switch ((ret = mraa_init_json_platform(env_var))) { + switch (mraa_init_json_platform(env_var)) { case MRAA_SUCCESS: platform_type = plat->platform_type; break; @@ -1012,16 +1011,11 @@ mraa_file_contains(const char* filename, const char* content) char* file = mraa_file_unglob(filename); if (file != NULL) { - size_t len = 1024; - char* line = calloc(len, sizeof(char)); - if (line == NULL) { - free(file); - return 0; - } + size_t len = 0; + char* line = NULL; FILE* fh = fopen(file, "r"); if (fh == NULL) { free(file); - free(line); return 0; } while ((getline(&line, &len, fh) != -1) && (found == 0)) { @@ -1047,16 +1041,11 @@ mraa_file_contains_both(const char* filename, const char* content, const char* c char* file = mraa_file_unglob(filename); if (file != NULL) { - size_t len = 1024; - char* line = calloc(len, sizeof(char)); - if (line == NULL) { - free(file); - return 0; - } + size_t len = 0; + char* line = NULL; FILE* fh = fopen(file, "r"); if (fh == NULL) { free(file); - free(line); return 0; } while ((getline(&line, &len, fh) != -1) && (found == 0)) { @@ -1188,14 +1177,10 @@ mraa_find_i2c_bus(const char* devname, int startfrom) { char path[64]; int fd; - int i = startfrom; - int ret = -1; - // because feeding mraa_find_i2c_bus result back into the function is // useful treat -1 as 0 - if (startfrom < 0) { - startfrom = 0; - } + int i = (startfrom < 0) ? 0 : startfrom; + int ret = -1; // find how many i2c buses we have if we haven't already if (num_i2c_devices == 0) {