From bb85cb33d93b232d2a0ac5b2a37ed7ee8a421a11 Mon Sep 17 00:00:00 2001 From: Christian Mehlis Date: Tue, 29 Oct 2013 21:48:26 +0100 Subject: [PATCH] shell: dont't use malloc on each line --- sys/include/shell.h | 2 ++ sys/shell/shell.c | 13 +++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sys/include/shell.h b/sys/include/shell.h index ed2e617767..f2587ddc0c 100644 --- a/sys/include/shell.h +++ b/sys/include/shell.h @@ -34,6 +34,8 @@ typedef struct shell_t { void (*put_char)(int); } shell_t; +#define SHELL_BUFFER_SIZE (127) + /** * @brief Initialize a shell object * @param shell Pointer to preallocated shell object diff --git a/sys/shell/shell.c b/sys/shell/shell.c index 20689c0251..2339222238 100644 --- a/sys/shell/shell.c +++ b/sys/shell/shell.c @@ -92,9 +92,10 @@ static void print_help(const shell_command_t *command_list) static void handle_input_line(shell_t *shell, char *line) { + char line_copy[SHELL_BUFFER_SIZE]; char *saveptr; - char *linedup = strdup(line); - char *command = strtok_r(linedup, " ", &saveptr); + strncpy(line_copy, line, sizeof(line_copy)); + char *command = strtok_r(line_copy, " ", &saveptr); void (*handler)(char *) = NULL; @@ -113,8 +114,6 @@ static void handle_input_line(shell_t *shell, char *line) } } } - - free(linedup); } static int readline(shell_t *shell, char *buf, size_t size) @@ -155,7 +154,7 @@ static inline void print_prompt(shell_t *shell) void shell_run(shell_t *shell) { - char line_buf[255]; + char line_buf[SHELL_BUFFER_SIZE]; print_prompt(shell); @@ -163,9 +162,7 @@ void shell_run(shell_t *shell) int res = readline(shell, line_buf, sizeof(line_buf)); if (!res) { - char *line_copy = strdup(line_buf); - handle_input_line(shell, line_copy); - free(line_copy); + handle_input_line(shell, line_buf); } print_prompt(shell);