forked from aldostools/webMAN-MOD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibc.c
235 lines (175 loc) · 3.66 KB
/
libc.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void *memset(void *m, int c, size_t n)
{
char *s = (char *) m;
while (n--)
*s++ = (char) c;
return m;
}
void *memcpy(void *dst0, const void *src0, size_t n)
{
char *dst = (char *)dst0;
char *src = (char *)src0;
void *save = dst0;
while (n--)
*dst++ = *src++;
return save;
}
int memcmp(const void* s1, const void* s2, size_t n)
{
const unsigned char *p1 = s1, *p2 = s2;
while(n--)
if( *p1 != *p2 )
return *p1 - *p2;
else
p1++, p2++;
return 0;
}
size_t strlen(const char *s)
{
const char *p = s;
while (*s) s++;
return s - p;
}
char *strchr(const char *s, int c)
{
if(!s) return 0;
while (*s != (char)c)
if (!*s++)
return 0;
return (char *)s;
}
char *strrchr(const char *s, int c)
{
if(!s) return 0;
char *ret = 0;
char cc = (char)c;
do
{
if( *s == cc ) ret = (char*)s;
}
while(*s++);
return ret;
}
char *strstr(const char *s1, const char *s2)
{
if(!s1) return 0;
size_t n = strlen(s2); if(n == 0) return 0;
while(*s1)
if(!memcmp(s1++, s2, n))
return (char*)(s1 - 1);
return 0;
}
int strncasecmp (__const char *s1, __const char *s2, size_t n)
{
int c1, c2;
while(n--)
{
c1 = *((unsigned char *)(s1++)); if (c1 >= 'A' && c1 <= 'Z') c1 += 0x20; // ('a' - 'A')
c2 = *((unsigned char *)(s2++)); if (c2 >= 'A' && c2 <= 'Z') c2 += 0x20; // ('a' - 'A')
if (c1 != c2) return (c1 - c2);
if (c1 == '\0') return 0;
}
return 0;
}
int strcasecmp (__const char *s1, __const char *s2)
{
int c1, c2;
while(*s1)
{
c1 = *((unsigned char *)(s1++)); if (c1 >= 'A' && c1 <= 'Z') c1 += 0x20; // ('a' - 'A')
c2 = *((unsigned char *)(s2++)); if (c2 >= 'A' && c2 <= 'Z') c2 += 0x20; // ('a' - 'A')
if (c1 != c2) return (c1 - c2);
}
return 0;
}
char *strcasestr(const char *s1, const char *s2);
char *strcasestr(const char *s1, const char *s2)
{
if(!s1) return 0;
size_t n = strlen(s2); if(n == 0) return 0;
while(*s1)
if(!strncasecmp(s1++, s2, n))
return (char*)(s1 - 1);
return 0;
}
int strncmp(const char *s1, const char *s2, size_t n)
{
if(n == 0) return 0;
while((n > 0) && *s1 && (*s1==*s2)) s1++, s2++, n--;
return *(const unsigned char*)s1-*(const unsigned char*)s2;
}
int strcmp(const char *s1, const char *s2)
{
while(*s1 && (*s1==*s2)) s1++, s2++;
return *(const unsigned char*)s1 - *(const unsigned char*)s2;
}
char *strcpy(char *dest, const char* src)
{
char *ret = dest;
while ((*dest++ = *src++)) ;
return ret;
}
char *strncpy(char *dest, const char *src, size_t n)
{
char *ret = dest;
do
{
if (!n--) return ret;
}
while ((*dest++ = *src++));
while (n--) *dest++ = 0;
return ret;
}
char *strcat(char *dest, const char *src)
{
char *ret = dest;
while (*dest) dest++;
while ((*dest++ = *src++)) ;
return ret;
}
char *strtok(char *str, const char *delim)
{
static char* p = 0;
if(str) p = str;
else
if(!p) return 0;
str = p + strspn(p, delim);
p = str + strcspn(str, delim);
if(p == str) return p = 0;
p = *p ? *p = 0, p + 1 : 0;
return str;
}
size_t strcspn(const char *s1, const char *s2)
{
size_t ret = 0;
while(*s1)
if(strchr(s2,*s1))
return ret;
else
s1++, ret++;
return ret;
}
size_t strspn(const char *s1, const char *s2)
{
size_t ret = 0;
while(*s1 && strchr(s2, *s1++))
ret++;
return ret;
}
int extcmp(const char *s1, const char *s2, size_t n);
int extcmp(const char *s1, const char *s2, size_t n)
{
size_t s = strlen(s1);
if(n > s) return -1;
return memcmp(s1 + (s - n), s2, n);
}
int extcasecmp(const char *s1, const char *s2, size_t n);
int extcasecmp(const char *s1, const char *s2, size_t n)
{
size_t s = strlen(s1);
if(n > s) return -1;
return strncasecmp(s1 + (s - n), s2, n);
}