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/>.
22 void *memmove(void *dest, const void *src, size_t n)
37 dptr = (char*)dest + n - 1;
38 sptr = (const char*)src + n - 1;
47 int memcmp(void *aptr, void *bptr, size_t n)
49 int i, startoffs, diff;
51 unsigned char *a = aptr;
52 unsigned char *b = bptr;
54 a32 = (uint32_t*)((intptr_t)(a + 3) & 0xfffffffc);
55 b32 = (uint32_t*)((intptr_t)(b + 3) & 0xfffffffc);
57 /* if both are aligned the same way... */
58 if((startoffs = (unsigned char*)a32 - a) == (unsigned char*)b32 - b) {
59 /* catch-up to the 32bit alignment */
60 for(i=0; i<startoffs; i++) {
61 if((diff = *a++ - *b++) != 0 || --n <= 0) {
66 /* compare 32bit at once */
68 if(*a32 != *b32) break;
74 /* update byte pointers to contine with the tail */
75 a = (unsigned char*)a32;
76 b = (unsigned char*)b32;
79 /* we're here both for the tail-end of same-alignment buffers, or for the
80 * whole length of mis-aligned buffers.
83 if((diff = *a++ - *b++) != 0) {
90 size_t strlen(const char *s)
97 char *strchr(const char *s, int c)
108 char *strrchr(const char *s, int c)
115 /* go back checking for c */
124 char *strstr(const char *str, const char *substr)
127 const char *s1 = str;
128 const char *s2 = substr;
130 while(*s1 && *s1 == *s2) {
142 char *strcasestr(const char *str, const char *substr)
145 const char *s1 = str;
146 const char *s2 = substr;
148 while(*s1 && tolower(*s1) == tolower(*s2)) {
160 int strcmp(const char *s1, const char *s2)
162 while(*s1 && *s1 == *s2) {
169 int strcasecmp(const char *s1, const char *s2)
171 while(*s1 && tolower(*s1) == tolower(*s2)) {
175 return tolower(*s1) - tolower(*s2);
178 int strncmp(const char *s1, const char *s2, int n)
182 while(n-- > 0 && *s1 && *s2 && *s1 == *s2) {
191 int strncasecmp(const char *s1, const char *s2, int n)
195 while(n-- > 0 && *s1 && *s2 && tolower(*s1) == tolower(*s2)) {
201 return tolower(*s1) - tolower(*s2);
204 char *strcpy(char *dest, const char *src)
207 while((*dptr++ = *src++));
211 char *strcat(char *dest, const char *src)
213 strcpy(dest + strlen(dest), src);
217 char *strncpy(char *dest, const char *src, int n)
220 while(n-- > 0 && (*dptr++ = *src++));
225 static const char *errstr[] = {
236 "No space left on device",
242 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
243 0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,
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,
251 char *strerror(int err)
253 if(err < 0 || err > sizeof errstr / sizeof *errstr || !errstr[err]) {
256 return (char*)errstr[err];