Repository navigation
Expand file tree
/
Copy pathstring.c
More file actions
242 lines (199 loc) · 3.92 KB
/
Copy pathstring.c
File metadata and controls
242 lines (199 loc) · 3.92 KB
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
236
237
238
239
240
241
242
/**
* \file
* \brief Implementations of standard libc string functions.
*/
/*
* Copyright (c) 2007, 2008, 2014, ETH Zurich.
* All rights reserved.
*
* This file is distributed under the terms in the attached LICENSE file.
* If you do not find this file, copies can be found by writing to:
* ETH Zurich D-INFK, CAB F.78, Universitaetstr. 6, CH-8092 Zurich,
* Attn: Systems Group.
*/
#include <stdint.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
#if 0
void *
memset (void *s, int c, size_t n)
{
uint8_t *p = (uint8_t *)s;
for (size_t m = 0; m < n; m++) {
*p++ = c;
}
return s;
}
#endif
char *
strstr(const char *a, const char *b)
{
const char *res = a;
for (; *res != '\0'; res++) {
if (strcmp(res, b) == 0) {
return (char *)res;
}
}
return NULL;
}
void *
memchr(const void *s, int c, size_t n)
{
size_t i;
const uint8_t *p;
for (i = 0, p = s; i < n; i++, p++)
if (*p == c)
return (void *)p;
return NULL;
}
size_t
strlen(const char *s)
{
size_t i = 0;
while (*s != '\0') {
i++;
s++;
}
return i;
}
char *
strncpy(char *dest, const char *src, size_t count)
{
char *tmp = dest;
int c = count;
while (c >= 0) {
if ((*tmp = *src) != 0) src++;
tmp++;
c--;
}
return dest;
}
char *
strcpy(char *dest, const char *src)
{
char *pos = dest;
while ((*pos++ = *src++) != 0);
return dest;
}
#if 0
void *
memmove(void *dst, const void *src, size_t sz)
{
size_t i;
const char *source = src;
char *dest = dst;
if(sz == 0) {
return dst;
}
/* XXX: This is slooooooooooowwwwwwwwwww ...... */
if(src >= dst) { // copy front to back
for(i = 0; i < sz; i++)
dest[i] = source[i];
} else { // copy back to front
for(i = sz - 1; i > 0; i--)
dest[i] = source[i];
dest[0] = source[0];
}
return dst;
}
#endif
void *
memcpy(void *dst, const void *src, size_t len)
{
char *d = dst;
const char *s = src;
/* check that we don't overlap (should use memmove()) */
assert((src < dst && (char *)src + len <= (char *)dst)
|| (dst < src && (char *)dst + len <= (char *)src));
while (len--)
*d++ = *s++;
return dst;
}
char *
strchr(const char *s, int c)
{
unsigned int i;
unsigned int n = strlen(s);
for(i = 0; i < n ; i++) {
if(s[i] == c)
return (char *)&s[i];
}
return NULL;
}
char *
strrchr(const char *s, int c)
{
unsigned int i;
unsigned int n = strlen(s);
if(n == 0)
return NULL;
for(i = n - 1; ; i--) {
if (s[i] == c) {
return (char *)&s[i];
} else if (i == 0) {
return NULL;
}
}
}
int
strncmp(const char *s1, const char *s2, size_t n)
{
int result;
for(unsigned int i = 0; i < n; i++) {
if((result = s2[i] - s1[i]) != 0) {
return result;
}
if(s1[i] == '\0' || s2[i] == '\0') {
break;
}
}
return 0;
}
int
strcmp(const char *s1, const char *s2)
{
size_t len1 = strlen(s1), len2 = strlen(s2),
maxlen = len1 > len2 ? len1 : len2;
return strncmp(s1, s2, maxlen);
}
size_t
strspn(const char* s, const char* set)
{
const char* l = s;
while (('\0' != *l) && (NULL != strchr(set, *l)))
{
l++;
}
return (l - s);
}
size_t
strcspn(const char* s, const char* set)
{
const char* l = s;
while (('\0' != *l) && (NULL == strchr(set, *l)))
{
l++;
}
return (l - s);
}
char *
strtok(char *s, const char *delim)
{
static char *save;
if (s == NULL)
{
s = save;
}
s = s + strspn(s, delim);
if ('\0' == *s)
{
return NULL;
}
save = s + strcspn(s, delim);
if ('\0' != *save)
{
*save++ = '\0';
}
return s;
}