-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_strtok.c
More file actions
51 lines (48 loc) · 1.01 KB
/
_strtok.c
File metadata and controls
51 lines (48 loc) · 1.01 KB
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
47
48
49
50
51
#include "shell.h"
/**
* _strtok_ - function for to make token the of line input for user.
* @line: char pointer input.
* @token: char input.
* Return: the array token or NULL if fails.
*/
char **_strtok_(char *line, char token)
{
int i = 0, j = 0, k;
size_t len = 1024;
char **array_token = NULL;
if (!line)
return (NULL);
_ts_handler_(line);
array_token = calloc(len, sizeof(char *));
if (!array_token)
{
free(array_token);
return (NULL);
}
array_token[j] = calloc(len, sizeof(char));
if (!array_token[j])
{
free(array_token[j]), free(array_token);
return (NULL);
}
for (i = 0, k = 0; line[i]; i++, k++)
{
if (line[i] == token && line[i + 1] != token)
{
array_token[j][k] = '\0', i++, j++, k = 0;
array_token[j] = calloc(len, sizeof(char));
if (!array_token[j])
{
for (j--; j >= 0; j--)
free(array_token[j]);
free(array_token);
return (NULL);
}
}
if (line[i] == '\n')
array_token[j][k] = '\0';
else
array_token[j][k] = line[i];
}
return (array_token);
}