diff --git a/project/Core/Src/tcp_cmd.c b/project/Core/Src/tcp_cmd.c index aea35e7..474afb2 100644 --- a/project/Core/Src/tcp_cmd.c +++ b/project/Core/Src/tcp_cmd.c @@ -58,17 +58,20 @@ static void tcp_cmd_print_help(struct tcp_pcb* pcb) { } /** - * @brief This function removes the newline from a string + * @brief This function removes the newline from a string the string can contain multiple lines * @param str The string to remove the newline from */ -void remove_newline(char* str) { - int i = 0; - while (str[i] != '\0') { - if (str[i] == '\n' || str[i] == '\r') { - str[i] = '\0'; +void remove_newline(char* str, size_t len) { + size_t i = 0; + size_t j = 0; + while (str[i] != '\0' && j < len) { + if (str[j] != '\n' && str[j] != '\r') { + str[i] = str[j]; + i++; } - i++; + j++; } + str[i] = '\0'; } /** @@ -352,7 +355,7 @@ err_t tcp_cmd_recv(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) { // Tell the tcp stack that we have taken the data tcp_recved(pcb, p->tot_len); - remove_newline(cmd); + remove_newline(cmd, strlen(cmd)); LOG_INFO(TAG, "cmd: %s", cmd); // Split string into tokens by delimiter (space) argv[0] = get_next_token(cmd, " ", &next);