-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_line.c
42 lines (35 loc) · 829 Bytes
/
parse_line.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_line - function that parses the line
* @line: line given
* Return: Tokens
*/
char **parse_line(char *line)
{
int buffer_size = BUFFER_SIZE, position = 0;
char **tokens = malloc(buffer_size * sizeof(char *)), *token;
if (!tokens)
{
write(STDERR_FILENO, "Allocation error\n", _strlen("Allocation error\n"));
exit(EXIT_FAILURE);
}
token = _strtok(line, TOKEN_DELIM);
while (token != NULL)
{
tokens[position] = token;
position++;
if (position >= buffer_size)
{
buffer_size += BUFFER_SIZE;
tokens = realloc(tokens, buffer_size * sizeof(char *));
if (!tokens)
{
write(STDERR_FILENO, "Allocation error\n", _strlen("Allocation error\n"));
exit(EXIT_FAILURE);
}
}
token = _strtok(NULL, TOKEN_DELIM);
}
tokens[position] = NULL;
return (tokens);
}