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