From acd001edbb56873e95a265a25e70e00cb2d948cd Mon Sep 17 00:00:00 2001 From: Artur Tamborski Date: Mon, 17 Dec 2018 12:08:20 +0100 Subject: [PATCH] Replace my own implementation with simpler strtok() --- src/main.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/main.c b/src/main.c index 4fc14b0..853167a 100644 --- a/src/main.c +++ b/src/main.c @@ -56,34 +56,23 @@ sigint(int signal) int tokenize(char *input) { - char curr, prev = '\n'; + char whitespaces[] = " \t\n"; char **tokens = g_tokens; - char *start = g_args; char *args = g_args; - int real_input = 0; + char *token; + size_t len; int argc = 0; - while ((curr = *input++) != '\0') + token = strtok(input, whitespaces); + while (token != NULL) { - if (curr == ' ' || curr == '\n') - { - if (curr == prev || !real_input) - continue; + len = strlen(token) + 1; + memcpy(args, token, len); + *tokens++ = args; + args += len + 1; + argc++; - *tokens++ = start; - *args++ = '\0'; - start = args; - argc++; - - if (curr == '\n') - break; - } else - { - *args++ = curr; - real_input = 1; - } - - prev = curr; + token = strtok(NULL, whitespaces); } tokens = NULL;