forked from mwanyambu/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplt_input.c
More file actions
43 lines (39 loc) · 673 Bytes
/
splt_input.c
File metadata and controls
43 lines (39 loc) · 673 Bytes
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
43
#include "main.h"
/**
* split_input - tokenizes user input
* @input: user input
* Return: input split
*/
char **split_input(char *input)
{
int c = 0, size = BUFFER_SIZE;
char **token = malloc(size * sizeof(char *)), *split;
if (!token)
{
perror("split_input");
exit(EXIT_FAILURE);
}
split = strtok(input, DELIMITER);
while (split != NULL)
{
if (split[0] == '#')
{
break;
}
token[c] = split;
c++;
if (c >= size)
{
size += BUFFER_SIZE;
token = realloc(token, size * sizeof(char *));
if (!token)
{
perror("token");
exit(EXIT_FAILURE);
}
}
split = strtok(NULL, DELIMITER);
}
token[c] = NULL;
return (token);
}