-
Notifications
You must be signed in to change notification settings - Fork 0
/
_strtok.c
54 lines (51 loc) · 1.18 KB
/
_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
50
51
52
53
54
#include "shell.h"
/**
* _strtok - tokenize string
* @str: string to be tokenized
* @delimeter: token delimiter
* @whichf: which strcmp to use, 0 for strcmpd, and 1 for strcmps
*
* Return: a character pointer to the current delimited token
*/
char *_strtok(char *str, const char *delimeter, int whichf)
{
static char *save;
char *_new = NULL;
int i = 0, (*func)(char *, const char *), loc, in_quotes = 0;
func = (whichf == 0) ? _strcmpd : _strcmps;
loc = (whichf) ? _strlen(delimeter) - 1 : 0;
if (!str || !*str)
{
if (!save || !*save)
return (NULL);
while (in_quotes || (func(save + i, delimeter) != 1 && *(save + i) != '\0'))
{
if (*(save + i) == '\'' || *(save + i) == '\"')
in_quotes = !in_quotes;
i++;
}
if (*(save + i) == '\0')
{
_new = save, save = NULL;
return (_new);
}
_new = save;
*(save + i) = '\0';
save = save + i + loc + 1;
return (_new);
}
while (in_quotes || (func(str + i, delimeter) != 1 && *(str + i) != '\0'))
{
if (*(str + i) == '\'' || *(str + i) == '\"')
in_quotes = !in_quotes;
i++;
}
if (*(str + i) == '\0')
{
save = NULL;
return (str);
}
save = str + i + loc + 1;
*(str + i) = '\0';
return (str);
}