-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokens.c
45 lines (44 loc) · 896 Bytes
/
tokens.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
#include "shell.h"
/**
* get_tokens - Tokenizes a string based on delimiters.
* @str: The input string to tokenize.
* @delim: The delimiter characters used for tokenization.
* Return: An array of tokenized strings or NULL if an error occurs.
*/
char **get_tokens(char *str, char *delim)
{
int i, j, capacity;
char *token, *str_copy;
char **tokens;
i = 0;
capacity = _strlen(str);
str_copy = _strdup(str);
if (str_copy == NULL)
return (NULL);
tokens = malloc(capacity * sizeof(char *));
if (tokens == NULL)
{
free(str_copy);
return (NULL);
}
token = strtok(str_copy, delim);
while (token != NULL)
{
tokens[i] = _strdup(token);
if (tokens[i] == NULL)
{
for (j = 0; j < i; j++)
{
free(tokens[j]);
}
free(tokens);
free(str_copy);
return (NULL);
}
token = strtok(NULL, delim);
i++;
}
tokens[i] = NULL;
free(str_copy);
return (tokens);
}