kern

view src/klibc/string.c @ 98:921a264297a4

merged the filesystem stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 17:03:30 +0300
parents ebca81749ef5
children
line source
1 #include <string.h>
3 void memset(void *s, int c, size_t n)
4 {
5 char *ptr = s;
6 while(n--) {
7 *ptr++ = c;
8 }
9 }
11 /* Does the same thing as memset only with 16bit values.
12 * n in this case is the number of values, not the number of bytes.
13 */
14 void memset16(void *s, int c, size_t n)
15 {
16 short *ptr = s;
17 while(n--) {
18 *ptr++ = c;
19 }
20 }
22 void *memcpy(void *dest, const void *src, size_t n)
23 {
24 char *dptr = dest;
25 const char *sptr = src;
27 while(n--) {
28 *dptr++ = *sptr++;
29 }
30 return dest;
31 }
33 void *memmove(void *dest, const void *src, size_t n)
34 {
35 int i;
36 char *dptr;
37 const char *sptr;
39 if(dest <= src) {
40 /* forward copy */
41 dptr = dest;
42 sptr = src;
43 for(i=0; i<n; i++) {
44 *dptr++ = *sptr++;
45 }
46 } else {
47 /* backwards copy */
48 dptr = dest + n - 1;
49 sptr = src + n - 1;
50 for(i=0; i<n; i++) {
51 *dptr-- = *sptr--;
52 }
53 }
55 return dest;
56 }
58 size_t strlen(const char *s)
59 {
60 size_t len = 0;
61 while(*s++) len++;
62 return len;
63 }
65 char *strchr(const char *s, int c)
66 {
67 while(*s) {
68 if(*s == c) {
69 return (char*)s;
70 }
71 s++;
72 }
73 return 0;
74 }
76 char *strrchr(const char *s, int c)
77 {
78 const char *ptr = s;
80 /* find the end */
81 while(*ptr) ptr++;
83 /* go back checking for c */
84 while(--ptr >= s) {
85 if(*ptr == c) {
86 return (char*)ptr;
87 }
88 }
89 return 0;
90 }
92 char *strstr(const char *str, const char *substr)
93 {
94 while(*str) {
95 const char *s1 = str;
96 const char *s2 = substr;
98 while(*s1 && *s1 == *s2) {
99 s1++;
100 s2++;
101 }
102 if(!*s2) {
103 return (char*)str;
104 }
105 str++;
106 }
107 return 0;
108 }
110 int strcmp(const char *s1, const char *s2)
111 {
112 while(*s1 && *s1 == *s2) {
113 s1++;
114 s2++;
115 }
116 return *s1 - *s2;
117 }