-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.c
42 lines (37 loc) · 793 Bytes
/
parse.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
42
#include "shell.h"
/**
* parse - parse the line entered by user
* transform it in a string without some delimiter
* @line: the line entered by the user
*
* Return: A set of string terminated by NULL
*/
char **parse(char *line)
{
int size = BUFSIZE;
int position = 0;
char **tokens = malloc(sizeof(char *) * size);
char *token;
if (tokens == NULL)
{
print_err(NULL, "Error: malloc");
exit(EXIT_FAILURE);
}
token = strtok(line, DELIM);
do {
tokens[position] = token;
position++;
if (position >= size)
{
size += BUFSIZE;
tokens = realloc(tokens, size * sizeof(char *));
if (tokens == NULL)
{
print_err(NULL, "Error: realloc");
exit(EXIT_FAILURE);
}
}
} while ((token = strtok(NULL, DELIM)));
tokens[position] = NULL;
return (tokens);
}