-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenize.c
41 lines (38 loc) · 953 Bytes
/
tokenize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "simpleshell.h"
/**
* tokenize - this function separate the string using a designed delimiter
* @data: a pointer to the program's data
* Return: an array of the different parts of the string
*/
void tokenize(data_of_program *data)
{
char *delimiter = " \t";
int i, j, counter = 2, length;
length = str_length(data->input_line);
if (length)
{
if (data->input_line[length - 1] == '\n')
data->input_line[length - 1] = '\0';
}
for (i = 0; data->input_line[i]; i++)
{
for (j = 0; delimiter[j]; j++)
{
if (data->input_line[i] == delimiter[j])
counter++;
}
}
data->tokens = malloc(counter * sizeof(char *));
if (data->tokens == NULL)
{
perror(data->program_name);
exit(errno);
}
i = 0;
data->tokens[i] = str_duplicate(_strtok(data->input_line, delimiter));
data->command_name = str_duplicate(data->tokens[0]);
while (data->tokens[i++])
{
data->tokens[i] = str_duplicate(_strtok(NULL, delimiter));
}
}