Replace my own implementation with simpler strtok()

This commit is contained in:
Artur Tamborski 2018-12-17 12:08:20 +01:00
parent ace246133e
commit acd001edbb

View File

@ -56,34 +56,23 @@ sigint(int signal)
int int
tokenize(char *input) tokenize(char *input)
{ {
char curr, prev = '\n'; char whitespaces[] = " \t\n";
char **tokens = g_tokens; char **tokens = g_tokens;
char *start = g_args;
char *args = g_args; char *args = g_args;
int real_input = 0; char *token;
size_t len;
int argc = 0; int argc = 0;
while ((curr = *input++) != '\0') token = strtok(input, whitespaces);
while (token != NULL)
{ {
if (curr == ' ' || curr == '\n') len = strlen(token) + 1;
{ memcpy(args, token, len);
if (curr == prev || !real_input) *tokens++ = args;
continue; args += len + 1;
argc++;
*tokens++ = start; token = strtok(NULL, whitespaces);
*args++ = '\0';
start = args;
argc++;
if (curr == '\n')
break;
} else
{
*args++ = curr;
real_input = 1;
}
prev = curr;
} }
tokens = NULL; tokens = NULL;