forked from sebascastel/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrtok.c
63 lines (61 loc) · 1.29 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
55
56
57
58
59
60
61
62
63
#include "holberton.h"
/**
* check_match - checks if c is equal to a given char of a string
* @c: character to be compared
* @str: input string
* Return: Always 0
*/
unsigned int check_match(char c, const char *str)
{
unsigned int i;
for (i = 0; str[i] != '\0'; i++)
{
if (c == str[i])
return (1);
}
return (0);
}
/**
* _strtok - breaks a string into a seq. of zero or nonempty tokens
* @str: string to be parsed
* @delim: delimit the token in the parsed string
* Return: returns a pointer to a null-terminated string
*/
char *_strtok(char *str, const char *delim)
{
static char *token_start;
static char *next_token;
unsigned int i;
if (str != NULL)
next_token = str;
token_start = next_token;
if (token_start == NULL)
return (NULL);
for (i = 0; next_token[i] != '\0'; i++)
{
if (check_match(next_token[i], delim) == 0)
break;
}
if (next_token[i] == '\0' || next_token[i] == '#')
{
next_token = NULL;
return (NULL);
}
token_start = next_token + i;
next_token = token_start;
for (i = 0; next_token[i] != '\0'; i++)
{
if (check_match(next_token[i], delim) == 1)
break;
}
if (next_token[i] == '\0')
next_token = NULL;
else
{
next_token[i] = '\0';
next_token = next_token + i + 1;
if (*next_token == '\0')
next_token = NULL;
}
return (token_start);
}