-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
191 lines (167 loc) · 4.59 KB
/
Copy pathstring.c
File metadata and controls
191 lines (167 loc) · 4.59 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
/*
* string.c - Standard C string implementation
*
* Author: u/ApparentlyPlus
*/
#include <klibc/string.h>
#include <stdint.h>
void* kmemset(void *dest, int c, size_t n) {
uint8_t* p = (uint8_t*)dest;
while (n && ((uintptr_t)p & 7)) { *p++ = (uint8_t)c; n--; }
uint64_t fill = (uint8_t)c;
fill |= fill << 8; fill |= fill << 16; fill |= fill << 32;
uint64_t* q = (uint64_t*)p;
size_t words = n / 8; n &= 7;
while (words--) *q++ = fill;
p = (uint8_t*)q;
while (n--) *p++ = (uint8_t)c;
return dest;
}
void *kmemcpy(void *dest, const void *src, size_t n) {
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = (const uint8_t*)src;
while (n && ((uintptr_t)d & 7)) { *d++ = *s++; n--; }
if (!((uintptr_t)s & 7)) {
uint64_t* dq = (uint64_t*)d;
const uint64_t* sq = (const uint64_t*)s;
size_t words = n / 8; n &= 7;
while (words--) *dq++ = *sq++;
d = (uint8_t*)dq; s = (const uint8_t*)sq;
}
while (n--) *d++ = *s++;
return dest;
}
void *kmemmove(void *dest, const void *src, size_t n) {
uint8_t* d = (uint8_t*)dest;
const uint8_t* s = (const uint8_t*)src;
if (d == s || n == 0) return dest;
if (d < s) {
while (n && ((uintptr_t)d & 7)) { *d++ = *s++; n--; }
if (!((uintptr_t)s & 7)) {
uint64_t* dq = (uint64_t*)d; const uint64_t* sq = (const uint64_t*)s;
size_t words = n / 8; n &= 7;
while (words--) *dq++ = *sq++;
d = (uint8_t*)dq; s = (const uint8_t*)sq;
}
while (n--) *d++ = *s++;
} else {
d += n; s += n;
while (n--) *(--d) = *(--s);
}
return dest;
}
int kmemcmp(const void *s1, const void *s2, size_t n) {
const unsigned char *a = s1, *b = s2;
while (n--) {
if (*a != *b) return *a - *b;
a++; b++;
}
return 0;
}
size_t kstrlen(const char *str) {
size_t len = 0;
while (*str++) len++;
return len;
}
char *kstrcpy(char *dest, const char *src) {
char *d = dest;
while ((*d++ = *src++));
return dest;
}
char *kstrncpy(char *dest, const char *src, size_t n) {
char *d = dest;
while (n && (*src != '\0')) {
*d++ = *src++;
n--;
}
while (n--) *d++ = '\0';
return dest;
}
int kstrcmp(const char *s1, const char *s2) {
while (*s1 && (*s1 == *s2)) {
s1++; s2++;
}
return (unsigned char)*s1 - (unsigned char)*s2;
}
int kstrncmp(const char *s1, const char *s2, size_t n) {
while (n && *s1 && (*s1 == *s2)) {
s1++; s2++; n--;
}
if (n == 0) return 0;
return (unsigned char)*s1 - (unsigned char)*s2;
}
char *kstrchr(const char *s, int c) {
while (*s) {
if (*s == (char)c) return (char *)s;
s++;
}
return (c == 0) ? (char *)s : NULL;
}
char *kstrrchr(const char *s, int c) {
const char *last = NULL;
while (*s) {
if (*s == (char)c) last = s;
s++;
}
return (char *)((c == 0) ? s : last);
}
char *kstrcat(char *dest, const char *src) {
char *d = dest;
while (*d) d++;
while ((*d++ = *src++));
return dest;
}
char *kstrncat(char *dest, const char *src, size_t n) {
char *d = dest;
while (*d) d++;
while (n-- && (*src != '\0')) *d++ = *src++;
*d = '\0';
return dest;
}
bool kisspace(int c) {
return (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v');
}
bool kisdigit(int c) {
return (c >= '0' && c <= '9');
}
unsigned long kstrtoul(const char *nptr, char **endptr, int base) {
const char *s = nptr;
unsigned long acc = 0;
int any = 0;
while (kisspace((unsigned char)*s)) s++;
if (base == 0) {
if (*s == '0') {
s++;
if (*s == 'x' || *s == 'X') {
s++;
base = 16;
} else base = 8;
} else base = 10;
} else if (base == 16) {
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) s += 2;
}
for (;; s++) {
int c = (unsigned char)*s;
if (kisdigit(c)) c -= '0';
else if (c >= 'A' && c <= 'Z') c -= 'A' - 10;
else if (c >= 'a' && c <= 'z') c -= 'a' - 10;
else break;
if (c >= base) break;
acc = acc * base + c;
any = 1;
}
if (endptr != 0) *endptr = (char *)(any ? s : nptr);
return acc;
}
long kstrtol(const char *nptr, char **endptr, int base) {
const char *s = nptr;
unsigned long acc;
int neg = 0;
while (kisspace((unsigned char)*s)) s++;
if (*s == '-') {
neg = 1;
s++;
} else if (*s == '+') s++;
acc = kstrtoul(s, endptr, base);
return neg ? -(long)acc : (long)acc;
}