2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018-2019 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY, without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>.
24 void *memmove(void *dest, const void *src, size_t n)
39 dptr = (char*)dest + n - 1;
40 sptr = (const char*)src + n - 1;
49 int memcmp(void *aptr, void *bptr, size_t n)
51 int i, startoffs, diff;
53 unsigned char *a = aptr;
54 unsigned char *b = bptr;
56 a32 = (uint32_t*)((intptr_t)(a + 3) & 0xfffffffc);
57 b32 = (uint32_t*)((intptr_t)(b + 3) & 0xfffffffc);
59 /* if both are aligned the same way... */
60 if((startoffs = (unsigned char*)a32 - a) == (unsigned char*)b32 - b) {
61 /* catch-up to the 32bit alignment */
62 for(i=0; i<startoffs; i++) {
63 if((diff = *a++ - *b++) != 0 || --n <= 0) {
68 /* compare 32bit at once */
70 if(*a32 != *b32) break;
76 /* update byte pointers to contine with the tail */
77 a = (unsigned char*)a32;
78 b = (unsigned char*)b32;
81 /* we're here both for the tail-end of same-alignment buffers, or for the
82 * whole length of mis-aligned buffers.
85 if((diff = *a++ - *b++) != 0) {
92 size_t strlen(const char *s)
99 char *strchr(const char *s, int c)
110 char *strrchr(const char *s, int c)
117 /* go back checking for c */
126 char *strstr(const char *str, const char *substr)
129 const char *s1 = str;
130 const char *s2 = substr;
132 while(*s1 && *s1 == *s2) {
144 char *strcasestr(const char *str, const char *substr)
147 const char *s1 = str;
148 const char *s2 = substr;
150 while(*s1 && tolower(*s1) == tolower(*s2)) {
162 int strcmp(const char *s1, const char *s2)
164 while(*s1 && *s1 == *s2) {
171 int strcasecmp(const char *s1, const char *s2)
173 while(*s1 && tolower(*s1) == tolower(*s2)) {
177 return tolower(*s1) - tolower(*s2);
180 int strncmp(const char *s1, const char *s2, int n)
184 while(n-- > 0 && *s1 && *s2 && *s1 == *s2) {
193 int strncasecmp(const char *s1, const char *s2, int n)
197 while(n-- > 0 && *s1 && *s2 && tolower(*s1) == tolower(*s2)) {
203 return tolower(*s1) - tolower(*s2);
206 char *strcpy(char *dest, const char *src)
209 while((*dptr++ = *src++));
213 char *strcat(char *dest, const char *src)
215 strcpy(dest + strlen(dest), src);
219 char *strncpy(char *dest, const char *src, int n)
222 while(n-- > 0 && (*dptr++ = *src++));
227 static const char *errstr[] = {
238 "No space left on device",
244 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
245 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
246 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
247 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
248 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
253 char *strerror(int err)
255 if(err < 0 || err > sizeof errstr / sizeof *errstr || !errstr[err]) {
258 return (char*)errstr[err];