-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrtok.c
49 lines (43 loc) · 791 Bytes
/
strtok.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
47
48
49
#include "crikey.h"
/**
* _strtok - Tokenizes a string by a character delimiter
* @src: The string to be tokenized on the first call, should be NULL for
* subsequent calls when tokenizing the same string
* @delim: The delimiter to tokenize by
*
* Return: Pointer to the beginning of the new token
*/
char *_strtok(char *src, char delim)
{
static char *start;
static char *last_null;
char *ret = NULL;
if (src)
start = src;
if (!start)
return (NULL);
if (*start == '\0')
{
if (start == last_null)
{
start = NULL;
return (NULL);
}
last_null = start;
return (start);
}
ret = start;
while (*start)
{
if (*start == delim)
{
*start = '\0';
last_null = start;
start++;
return (ret);
}
start++;
}
last_null = start;
return (ret);
}