-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unix_functions2.c
69 lines (66 loc) · 1.25 KB
/
Unix_functions2.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
64
65
66
67
68
69
#include "shell.h"
/**
* _strcmp - function compares two strings.
* @s1: string to be compared.
* @s2: second string to be compared.
*
* Return: The integer difference of s1 and s2.
*/
int _strcmp(char *s1, char *s2)
{
while ((*s1 && *s2) && (*s1 == *s2))
{
s1++;
s2++;
}
return (*s1 - *s2);
}
/**
* *_strtok - function tokenizes a string given a delimiter.
* @str: pointer to the string to be tokenized
* @delim: pointer to string containing the delimiter
* Return: pointer to next token.
* Else: NULL
*/
char *_strtok(char *str, const char *delim)
{
static char *new_tok;
char *tok;
if (str != NULL)
{
new_tok = str;
}
if (new_tok == NULL)
{
return (NULL);
}
/*Find the start of the next token*/
tok = new_tok;
while (*tok != '\0' && _strchr(delim, *tok) != NULL)
{
tok++;
}
/*If we've reached the end of the string, there are no more tokens to return*/
if (*tok == '\0')
{
new_tok = NULL;
return (NULL);
}
/*Find the end of the current token*/
new_tok = tok + 1;
while (*new_tok != '\0' && _strchr(delim, *new_tok) == NULL)
{
new_tok++;
}
/*If we've reached the end of the string, new_token should be NULL*/
if (*new_tok == '\0')
{
new_tok = NULL;
}
else
{
*new_tok = '\0';
new_tok++;
}
return (tok);
}