forked from sidneyriffic/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_strtok.c
184 lines (178 loc) · 2.76 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "shell.h"
/**
* strtok - tokenizes a string
* @str: string to tokenize
* @delim: delimiters used to create tokens
* Return: token
*/
char *strtok(char *str, char *delim)
{
static char *saved_string;
int i;
int j;
char *tmp_str;
char *tmp_delim;
/*if NULL passed in str becomes where saved string left off*/
if (str == 0)
str = saved_string;
if (str == 0)
return (0);
tmp_str = str;
tmp_delim = delim;
/*skip initial delimiters*/
i = 0;
while (tmp_str[i] != 0)
{
j = 0;
while (delim[j] != 0)
{
if (tmp_str[i] == tmp_delim[j])
break;
j++;
}
if (tmp_delim[j] == 0)
break;
i++;
}
str = str + i;
if (*str == 0)
{
saved_string = str;
return (0);
}
/*start new token*/
tmp_str = tmp_str + i;
i = 0;
while (tmp_str[i] != 0)
{
j = 0;
while (tmp_delim[j] != 0)
{
if (tmp_str[i] == tmp_delim[j])
break;
j++;
}
if (tmp_delim[j] != 0)
break;
i++;
}
saved_string = tmp_str;
if (tmp_str[i] != 0)
{
/*saves string for next call*/
saved_string = (saved_string + i + 1);
tmp_str[i] = '\0';
}
else
{
saved_string = '\0'; /*if end of input string.*/
}
return (tmp_str);
}
/**
* strtokqe - string token with quotes and escapes
* @str: string
* @delim: delimiters
* @escflags: escape flags
* flags are bitwise.
* 1 = use \ to escape delims
* 2 = single quote skips
* 4 = double quote skips
* Return: string
*/
char *strtokqe(char *str, char *delim, int escflags)
{
static char *saved_string;
int i;
int j;
/*if NULL passed in str becomes where saved string left off*/
if (str == 0)
str = saved_string;
if (str == 0)
return (0);
/*skip initial delimiters*/
i = 0;
while (str[i] != 0)
{
j = 0;
while (delim[j] != 0)
{
if (str[i] == delim[j])
break;
j++;
}
if (delim[j] == 0)
break;
i++;
}
str = str + i;
if (*str == 0)
{
saved_string = str;
return (0);
}
/*start new token*/
i = 0;
while (str[i] != 0)
{
if (str[i] == '\\' && escflags & 1)
{
if (str[i + 1] != 0)
i++;
i++;
continue;
}
if (str[i] == '\'' && escflags & 2)
{
i++;
while (str[i] != '\'')
{
if (str[i] == '\\' && escflags & 1)
{
if (str[i + 1] != 0)
i++;
i++;
continue;
}
i++;
}
}
if (str[i] == '"' && escflags & 4)
{
i++;
while (str[i] != '"')
{
if (str[i] == '\\' && escflags & 1)
{
if (str[i + 1] != 0)
i++;
i++;
continue;
}
i++;
}
}
j = 0;
while (delim[j] != 0)
{
if (str[i] == delim[j])
break;
j++;
}
if (delim[j] != 0)
break;
i++;
}
saved_string = str;
if (str[i] != 0)
{
/*saves string for next call*/
saved_string = (saved_string + i + 1);
str[i] = '\0';
}
else
{
saved_string = '\0'; /*if end of input string.*/
}
return (str);
}