-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_split.c
46 lines (41 loc) · 1.21 KB
/
str_split.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
43
44
45
46
#include "simple_shell.h"
/**
* tokening - splits and creates a full string command.
* @s: The delimiter for strtok.
* @buffer: The pointer to input string.
*
* Return: A string with full command.
*/
char **tokening(char *buffer, const char *s)
{
char *token = NULL, **commands = NULL;
size_t bufsize = 0;
int i = 0;
if (buffer == NULL)
return (NULL);
bufsize = _strlen(buffer);
commands = malloc((bufsize + 1) * sizeof(char *));
if (commands == NULL)
{
perror("Unable to allocate buffer");
free(buffer);
free_dp(commands);
exit(EXIT_FAILURE);
}
token = strtok(buffer, s);
while (token != NULL)
{
commands[i] = malloc(_strlen(token) + 1);
if (commands[i] == NULL)
{
perror("Unable to allocate buffer");
free_dp(commands);
return (NULL);
}
_strcpy(commands[i], token);
token = strtok(NULL, s);
i++;
}
commands[i] = NULL;
return (commands);
}