fix remove_newline function
This commit is contained in:
2023-12-02 10:38:10 +01:00
parent 5268ad0b7a
commit 6a0be6e469

View File

@@ -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 * @param str The string to remove the newline from
*/ */
void remove_newline(char* str) { void remove_newline(char* str, size_t len) {
int i = 0; size_t i = 0;
while (str[i] != '\0') { size_t j = 0;
if (str[i] == '\n' || str[i] == '\r') { while (str[i] != '\0' && j < len) {
str[i] = '\0'; 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 // Tell the tcp stack that we have taken the data
tcp_recved(pcb, p->tot_len); tcp_recved(pcb, p->tot_len);
remove_newline(cmd); remove_newline(cmd, strlen(cmd));
LOG_INFO(TAG, "cmd: %s", cmd); LOG_INFO(TAG, "cmd: %s", cmd);
// Split string into tokens by delimiter (space) // Split string into tokens by delimiter (space)
argv[0] = get_next_token(cmd, " ", &next); argv[0] = get_next_token(cmd, " ", &next);