ce3c2e7b7436010fd8025b7a1f20555a1a5cffd4
[bootcensus] / src / libc / string.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018  John Tsiombikas <nuclear@member.fsf.org>
4
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.
9
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.
14
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/>.
17 */
18 #include <string.h>
19
20 /*
21 void memset(void *s, int c, size_t n)
22 {
23         char *ptr = s;
24         while(n--) {
25                 *ptr++ = c;
26         }
27 }
28 */
29
30 /* Does the same thing as memset only with 16bit values.
31  * n in this case is the number of values, not the number of bytes.
32  */
33 /*
34 void memset16(void *s, int c, size_t n)
35 {
36         int16_t *ptr = s;
37         while(n--) {
38                 *ptr++ = c;
39         }
40 }
41 */
42 /*
43 void *memcpy(void *dest, const void *src, size_t n)
44 {
45         char *dptr = dest;
46         const char *sptr = src;
47
48         while(n--) {
49                 *dptr++ = *sptr++;
50         }
51         return dest;
52 }
53 */
54
55 void *memmove(void *dest, const void *src, size_t n)
56 {
57         int i;
58         char *dptr;
59         const char *sptr;
60
61         if(dest <= src) {
62                 /* forward copy */
63                 dptr = dest;
64                 sptr = src;
65                 for(i=0; i<n; i++) {
66                         *dptr++ = *sptr++;
67                 }
68         } else {
69                 /* backwards copy */
70                 dptr = (char*)dest + n - 1;
71                 sptr = (const char*)src + n - 1;
72                 for(i=0; i<n; i++) {
73                         *dptr-- = *sptr--;
74                 }
75         }
76
77         return dest;
78 }
79
80 size_t strlen(const char *s)
81 {
82         size_t len = 0;
83         while(*s++) len++;
84         return len;
85 }
86
87 char *strchr(const char *s, int c)
88 {
89         while(*s) {
90                 if(*s == c) {
91                         return (char*)s;
92                 }
93                 s++;
94         }
95         return 0;
96 }
97
98 char *strrchr(const char *s, int c)
99 {
100         const char *ptr = s;
101
102         /* find the end */
103         while(*ptr) ptr++;
104
105         /* go back checking for c */
106         while(--ptr >= s) {
107                 if(*ptr == c) {
108                         return (char*)ptr;
109                 }
110         }
111         return 0;
112 }
113
114 char *strstr(const char *str, const char *substr)
115 {
116         while(*str) {
117                 const char *s1 = str;
118                 const char *s2 = substr;
119
120                 while(*s1 && *s1 == *s2) {
121                         s1++;
122                         s2++;
123                 }
124                 if(!*s2) {
125                         return (char*)str;
126                 }
127                 str++;
128         }
129         return 0;
130 }
131
132 int strcmp(const char *s1, const char *s2)
133 {
134         while(*s1 && *s1 == *s2) {
135                 s1++;
136                 s2++;
137         }
138         return *s1 - *s2;
139 }