proxmark main_loop() replace char refactoring

This commit is contained in:
merlokk 2017-10-18 14:41:55 +03:00
commit 63e950c5ea
3 changed files with 18 additions and 13 deletions

View file

@ -23,6 +23,7 @@
#include "cmdmain.h"
#include "uart.h"
#include "ui.h"
#include "util.h"
#include "cmdparser.h"
#include "cmdhw.h"
#include "whereami.h"
@ -111,7 +112,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
// file with script
FILE *script_file = NULL;
char script_cmd_buf[256]; // iceman, needs lua script the same file_path_buffer as the rest
char script_cmd_buf[256] = {0}; // iceman, needs lua script the same file_path_buffer as the rest
if (script_cmds_file) {
script_file = fopen(script_cmds_file, "r");
@ -130,12 +131,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
fclose(script_file);
script_file = NULL;
} else {
char *nl;
nl = strrchr(script_cmd_buf, '\r');
if (nl) *nl = '\0';
nl = strrchr(script_cmd_buf, '\n');
if (nl) *nl = '\0';
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);
@ -164,12 +160,7 @@ void main_loop(char *script_cmds_file, char *script_cmd, bool usb_present) {
printf("\nStdin end. Exit...\n");
break;
}
char *nl;
nl = strrchr(script_cmd_buf, '\r');
if (nl) *nl = '\0';
nl = strrchr(script_cmd_buf, '\n');
if (nl) *nl = '\0';
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);

View file

@ -623,7 +623,19 @@ void clean_ascii(unsigned char *buf, size_t len) {
}
}
// replace \r \n to \0
void strcleanrn(char *buf, size_t len) {
strcreplace(buf, len, '\n', '\0');
strcreplace(buf, len, '\r', '\0');
}
// replace char in buffer
void strcreplace(char *buf, size_t len, char from, char to) {
for (size_t i = 0; i < len; i++) {
if (buf[i] == from)
buf[i] = to;
}
}
// determine number of logical CPU cores (use for multithreaded functions)

View file

@ -77,6 +77,8 @@ extern uint32_t le32toh (uint8_t *data);
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);
extern int num_CPUs(void); // number of logical CPUs