-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_strtok.c
More file actions
71 lines (59 loc) · 1.3 KB
/
_strtok.c
File metadata and controls
71 lines (59 loc) · 1.3 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "shell.h"
/* GLOBAL VAR TKN_PTR */
static char *TKN_PTR = "";
static int NO_INIT_TKN_PTR = 1;
/* helper function prototype */
int isdelimiter(char c, char *delimiter);
/**
* _strtok - Divides a string into tokens
* @str: String to be divided
* @delimiter: Delimiter by which to str will be divided
*
* Return: Token in string
*/
char *_strtok(char *str, char *delimiter)
{
char *curr_pos;
char *tkn_start = NULL;
if (NO_INIT_TKN_PTR == 1)
{
TKN_PTR = NULL;
NO_INIT_TKN_PTR = 0;
}
if ((str == NULL && TKN_PTR == NULL) || (str != NULL && str[0] == '\0'))
return (NULL);
if (str != NULL)
TKN_PTR = str;
for (curr_pos = TKN_PTR; *curr_pos != '\0'; curr_pos++)
{
if (!isdelimiter(*curr_pos, delimiter))
{
tkn_start = curr_pos;
while (*curr_pos != '\0' && !isdelimiter(*curr_pos, delimiter))
curr_pos++;
TKN_PTR = curr_pos + 1;
if (*curr_pos == '\0')
TKN_PTR = curr_pos;
*curr_pos = '\0';
return (tkn_start);
}
}
return (NULL);
}
/**
* isdelimiter - Evaluates if a char is a delimiter or not
* @c: Char to evaluate
* @delimiter: Set of chars as delimiters
*
* Return: 1 if c is a delimiter, 0 otherwise
*/
int isdelimiter(char c, char *delimiter)
{
while (*delimiter != '\0')
{
if (c == *delimiter)
return (1);
delimiter++;
}
return (0);
}