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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// MIT License, Copyright (c) 2020 Marvin Borner
#include <def.h>
#include <errno.h>
#include <mem.h>
#include <str.h>
u32 strlen(const char *str)
{
const char *s = str;
while (*s)
s++;
return s - str;
}
u32 strnlen(const char *str, u32 max)
{
const char *s = str;
while (max && *s) {
s++;
max--;
}
return s - str;
}
u32 strlcpy(char *dst, const char *src, u32 size)
{
const char *orig = src;
u32 left = size;
if (left)
while (--left)
if (!(*dst++ = *src++))
break;
if (!left) {
if (!size)
*dst = 0;
while (*src++)
;
}
return src - orig - 1;
}
s32 strcmp(const char *s1, const char *s2)
{
const u8 *c1 = (const u8 *)s1;
const u8 *c2 = (const u8 *)s2;
u8 ch;
int d = 0;
while (1) {
d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
break;
}
return d;
}
s32 strncmp(const char *s1, const char *s2, u32 n)
{
const u8 *c1 = (const u8 *)s1;
const u8 *c2 = (const u8 *)s2;
u8 ch;
int d = 0;
while (n--) {
d = (int)(ch = *c1++) - (int)*c2++;
if (d || !ch)
break;
}
return d;
}
char *strchr(char *s, char c)
{
while (*s != c) {
if (!*s)
return NULL;
s++;
}
return s;
}
char *strrchr(char *s, char c)
{
char *ret = 0;
do {
if (*s == c)
ret = s;
} while (*s++);
return ret;
}
u32 strlcat(char *dst, const char *src, u32 size)
{
const char *orig_dst = dst;
const char *orig_src = src;
u32 n = size;
while (n-- && *dst)
dst++;
u32 len = dst - orig_dst;
n = size - len;
if (!n--)
return len + strlen(src);
while (*src) {
if (n) {
*dst++ = *src;
n--;
}
src++;
}
src = 0;
return len + (src - orig_src);
}
char *strinv(char *s)
{
u32 s_str = strlen(s);
int iterations = (int)s_str / 2;
for (int i = 0; i < iterations; i++) {
char aux = s[i];
s[i] = s[(s_str - i) - 1];
s[(s_str - i) - 1] = aux;
}
return s;
}
char *strdup(const char *s)
{
int l = strlen(s) + 1;
char *d = malloc(l);
memcpy(d, s, l);
return d;
}
const char *strerror(u32 error)
{
switch (error) {
case 0:
return "Success";
case EPERM:
return "Operation not permitted";
case ENOENT:
return "No such file or directory";
case ESRCH:
return "No such process";
case EINTR:
return "Interrupted system call";
case EIO:
return "I/O error";
case ENXIO:
return "No such device or address";
case E2BIG:
return "Argument list too long";
case ENOEXEC:
return "Exec format error";
case EBADF:
return "Bad file number";
case ECHILD:
return "No child processes";
case EAGAIN:
return "Try again";
case ENOMEM:
return "Out of memory";
case EACCES:
return "Permission denied";
case EFAULT:
return "Bad address";
case ENOTBLK:
return "Block device required";
case EBUSY:
return "Device or resource busy";
case EEXIST:
return "File exists";
case EXDEV:
return "Cross-device link";
case ENODEV:
return "No such device";
case ENOTDIR:
return "Not a directory";
case EISDIR:
return "Is a directory";
case EINVAL:
return "Invalid argument";
case ENFILE:
return "File table overflow";
case EMFILE:
return "Too many open files";
case ENOTTY:
return "Not a typewriter";
case ETXTBSY:
return "Text file busy";
case EFBIG:
return "File too large";
case ENOSPC:
return "No space left on device";
case ESPIPE:
return "Illegal seek";
case EROFS:
return "Read-only file system";
case EMLINK:
return "Too many links";
case EPIPE:
return "Broken pipe";
case EDOM:
return "Math argument out of domain of func";
case ERANGE:
return "Math result not representable";
case EMAX:
return "Max errno";
default:
return "Unknown error";
}
}
#ifdef KERNEL
#include <drivers/cpu.h>
u32 strlen_user(const char *str)
{
stac();
u32 ret = strlen(str);
clac();
return ret;
}
u32 strnlen_user(const char *str, u32 max)
{
stac();
u32 ret = strnlen(str, max);
clac();
return ret;
}
u32 strlcpy_user(char *dst, const char *src, u32 size)
{
stac();
u32 ret = strlcpy(dst, src, size);
clac();
return ret;
}
s32 strcmp_user(const char *s1, const char *s2)
{
stac();
s32 ret = strcmp(s1, s2);
clac();
return ret;
}
s32 strncmp_user(const char *s1, const char *s2, u32 n)
{
stac();
s32 ret = strncmp(s1, s2, n);
clac();
return ret;
}
char *strchr_user(char *s, char c)
{
stac();
char *ret = strchr(s, c);
clac();
return ret;
}
char *strrchr_user(char *s, char c)
{
stac();
char *ret = strrchr(s, c);
clac();
return ret;
}
u32 strlcat_user(char *dst, const char *src, u32 size)
{
stac();
u32 ret = strlcat(dst, src, size);
clac();
return ret;
}
char *strinv_user(char *s)
{
stac();
char *ret = strinv(s);
clac();
return ret;
}
char *strdup_user(const char *s)
{
stac();
char *ret = strdup(s);
clac();
return ret;
}
#endif
|