-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_2.c
78 lines (72 loc) · 1.85 KB
/
utils_2.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
70
71
72
73
74
75
76
77
78
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/30 17:06:28 by x230 #+# #+# */
/* Updated: 2023/08/01 15:57:22 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// Helper function to check if character is a delimiter
bool is_delimiter(char c, const char *delims)
{
while (*delims != '\0')
{
if (c == *delims)
{
return (true);
}
delims++;
}
return (false);
}
// Updated strtok function
char *ft_strtok(char *str, const char *delims)
{
static char *stock = NULL;
char *ptr;
bool found_token;
ptr = NULL;
found_token = false;
if (str != NULL)
stock = str;
while (*stock != '\0')
{
if (!found_token && !is_delimiter(*stock, delims))
{
found_token = true;
ptr = stock;
}
else if (found_token && is_delimiter(*stock, delims))
{
*stock = '\0';
stock += 1;
break ;
}
stock++;
}
return (ptr);
}
char *ft_strstr(char *str, char *to_find)
{
int i;
int j;
i = 0;
if (to_find[0] == '\0')
return (str);
while (str[i] != '\0')
{
j = 0;
while (str[i + j] != '\0' && str[i + j] == to_find[j])
{
if (to_find[j + 1] == '\0')
return (&str[i]);
++j;
}
++i;
}
return (0);
}