forked from erikaosgue/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_strtok.c
More file actions
45 lines (44 loc) · 1.02 KB
/
_strtok.c
File metadata and controls
45 lines (44 loc) · 1.02 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
#include "shell.h"
/**
* _strtok - tokenize a string by the delimeters
* @str: Pointer to the string
* @delim: string of delimeters
* Return: a pointer to the string tokeinze, NULL otherwise
*/
char *_strtok(char *str, const char *delim)
{
static char *ptrAux;
char *token = NULL;
size_t len = 0;
if (delim == NULL)
return (str);
if (str == NULL)
{
str = ptrAux;
if (str == NULL)
return (NULL);
}
/* finds if the first characters are delimeters and avoid them to be tokens */
len = _strspn(str, delim);
str += len;
if (*str == '\0')
{
return (NULL);
}
token = str;
/* str will be pointing to the first delimeter found */
str = _strpbrk(token, delim);
/* if str is not null means there are more strings to be tokenize */
if (str != NULL)
{
/* will make the delimeter null character */
*str = '\0';
/*will be poiting to the next position of the found delimeter */
/* which will be null*/
ptrAux = str + 1;
}
/* if there are no more tokens will be null */
else
ptrAux = NULL;
return (token);
}