Replace my own implementation with simpler strtok()
This commit is contained in:
parent
ace246133e
commit
acd001edbb
31
src/main.c
31
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;
|
||||
|
||||
*tokens++ = start;
|
||||
*args++ = '\0';
|
||||
start = args;
|
||||
len = strlen(token) + 1;
|
||||
memcpy(args, token, len);
|
||||
*tokens++ = args;
|
||||
args += len + 1;
|
||||
argc++;
|
||||
|
||||
if (curr == '\n')
|
||||
break;
|
||||
} else
|
||||
{
|
||||
*args++ = curr;
|
||||
real_input = 1;
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
token = strtok(NULL, whitespaces);
|
||||
}
|
||||
|
||||
tokens = NULL;
|
||||
|
Loading…
Reference in New Issue
Block a user