diff --git a/client/proxmark3.c b/client/proxmark3.c index 691311e4..8f401826 100644 --- a/client/proxmark3.c +++ b/client/proxmark3.c @@ -117,7 +117,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) { if (script_cmds_file) { script_file = fopen(script_cmds_file, "r"); if (script_file) { - printf("using 'scripting' commands file %s\n", script_cmds_file); + printf("executing commands from file: %s\n", script_cmds_file); } } @@ -133,27 +133,24 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) { } else { strcleanrn(script_cmd_buf, sizeof(script_cmd_buf)); - if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) { - memset(cmd, 0, strlen(script_cmd_buf) + 1); - strcpy(cmd, script_cmd_buf); + if ((cmd = strmcopy(script_cmd_buf)) != NULL) { printf(PROXPROMPT"%s\n", cmd); } } } else { // If there is a script command if (execCommand){ - if ((cmd = (char*) malloc(strlen(script_cmd) + 1)) != NULL) { - memset(cmd, 0, strlen(script_cmd) + 1); - strcpy(cmd, script_cmd); + if ((cmd = strmcopy(script_cmd)) != NULL) { printf(PROXPROMPT"%s\n", cmd); } - + execCommand = false; } else { // exit after exec command if (script_cmd) break; + // if there is a pipe from stdin if (stdinOnPipe) { memset(script_cmd_buf, 0, sizeof(script_cmd_buf)); if (!fgets(script_cmd_buf, sizeof(script_cmd_buf), stdin)) { @@ -162,9 +159,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) { } strcleanrn(script_cmd_buf, sizeof(script_cmd_buf)); - if ((cmd = (char*) malloc(strlen(script_cmd_buf) + 1)) != NULL) { - memset(cmd, 0, strlen(script_cmd_buf) + 1); - strcpy(cmd, script_cmd_buf); + if ((cmd = strmcopy(script_cmd_buf)) != NULL) { printf(PROXPROMPT"%s\n", cmd); } diff --git a/client/util.c b/client/util.c index bc422bba..8357f601 100644 --- a/client/util.c +++ b/client/util.c @@ -637,6 +637,15 @@ void strcreplace(char *buf, size_t len, char from, char to) { } } +char *strmcopy(char *buf) { + char * str = NULL; + if ((str = (char*) malloc(strlen(buf) + 1)) != NULL) { + memset(str, 0, strlen(buf) + 1); + strcpy(str, buf); + } + return str; +} + // determine number of logical CPU cores (use for multithreaded functions) extern int num_CPUs(void) diff --git a/client/util.h b/client/util.h index 5730faa7..d6ed7d17 100644 --- a/client/util.h +++ b/client/util.h @@ -79,6 +79,7 @@ extern void rol(uint8_t *data, const size_t len); extern void clean_ascii(unsigned char *buf, size_t len); void strcleanrn(char *buf, size_t len); void strcreplace(char *buf, size_t len, char from, char to); +char *strmcopy(char *buf); extern int num_CPUs(void); // number of logical CPUs