From a73880ed5b3d83cab93ac815140043731863e80b Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 27 Sep 2013 10:50:00 +0200 Subject: irecovery: fix /execute command and add error checking when reading files --- tools/irecovery.c | 82 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 35 deletions(-) (limited to 'tools') diff --git a/tools/irecovery.c b/tools/irecovery.c index 12f05b7..bb6afea 100644 --- a/tools/irecovery.c +++ b/tools/irecovery.c @@ -64,6 +64,33 @@ static void shell_usage() { printf("\t/exit\t\tExit interactive shell.\n"); } +static void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) { + FILE *f; + uint64_t size; + + *length = 0; + + f = fopen(filename, "rb"); + if (!f) { + return; + } + + fseek(f, 0, SEEK_END); + size = ftell(f); + rewind(f); + + if (size == 0) { + fclose(f); + return; + } + + *buffer = (char*)malloc(sizeof(char)*(size+1)); + fread(*buffer, sizeof(char), size, f); + fclose(f); + + *length = size; +} + static void parse_command(irecv_client_t client, unsigned char* command, unsigned int size) { char* cmd = strdup((char*)command); char* action = strtok(cmd, " "); @@ -119,7 +146,17 @@ static void parse_command(irecv_client_t client, unsigned char* command, unsigne char* filename = strtok(NULL, " "); debug("Executing script %s\n", filename); if (filename != NULL) { - irecv_execute_script(client, filename); + char* buffer = NULL; + uint64_t buffer_length = 0; + buffer_read_from_filename(filename, &buffer, &buffer_length); + if (buffer) { + buffer[buffer_length] = '\0'; + + irecv_execute_script(client, buffer); + free(buffer); + } else { + printf("Could not read file '%s'\n", filename); + } } } @@ -257,33 +294,6 @@ void print_progress_bar(double progress) { } } -static void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) { - FILE *f; - uint64_t size; - - *length = 0; - - f = fopen(filename, "rb"); - if (!f) { - return; - } - - fseek(f, 0, SEEK_END); - size = ftell(f); - rewind(f); - - if (size == 0) { - fclose(f); - return; - } - - *buffer = (char*)malloc(sizeof(char)*(size+1)); - fread(*buffer, sizeof(char), size, f); - fclose(f); - - *length = size; -} - static void print_usage(int argc, char **argv) { char *name = NULL; name = strrchr(argv[0], '/'); @@ -445,16 +455,18 @@ int main(int argc, char* argv[]) { case kSendScript: buffer_read_from_filename(argument, &buffer, &buffer_length); - buffer[buffer_length] = '\0'; + if (buffer) { + buffer[buffer_length] = '\0'; - error = irecv_execute_script(client, buffer); - if(error != IRECV_E_SUCCESS) { - debug("%s\n", irecv_strerror(error)); - } + error = irecv_execute_script(client, buffer); + if(error != IRECV_E_SUCCESS) { + debug("%s\n", irecv_strerror(error)); + } - if (buffer) free(buffer); - + } else { + fprintf(stderr, "Could not read file '%s'\n", argument); + } break; case kShowMode: -- cgit v1.1-32-gdbae