megadrive_test1

view src/libc/string.c @ 7:8253942b0a1a

in the middle of something
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Feb 2017 14:00:26 +0200
parents
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 return s;
10 }
12 void *memcpy(void *dest, const void *src, size_t n)
13 {
14 char *dptr = dest;
15 const char *sptr = src;
17 while(n--) {
18 *dptr++ = *sptr++;
19 }
20 return dest;
21 }
23 void *memmove(void *dest, const void *src, size_t n)
24 {
25 int i;
26 char *dptr;
27 const char *sptr;
29 if(dest <= src) {
30 /* forward copy */
31 dptr = dest;
32 sptr = src;
33 for(i=0; i<n; i++) {
34 *dptr++ = *sptr++;
35 }
36 } else {
37 /* backwards copy */
38 dptr = (char*)dest + n - 1;
39 sptr = (char*)src + n - 1;
40 for(i=0; i<n; i++) {
41 *dptr-- = *sptr--;
42 }
43 }
45 return dest;
46 }
48 size_t strlen(const char *s)
49 {
50 size_t len = 0;
51 while(*s++) len++;
52 return len;
53 }
55 char *strchr(const char *s, int c)
56 {
57 while(*s) {
58 if(*s == c) {
59 return (char*)s;
60 }
61 s++;
62 }
63 return 0;
64 }
66 char *strrchr(const char *s, int c)
67 {
68 const char *ptr = s;
70 /* find the end */
71 while(*ptr) ptr++;
73 /* go back checking for c */
74 while(--ptr >= s) {
75 if(*ptr == c) {
76 return (char*)ptr;
77 }
78 }
79 return 0;
80 }
82 char *strstr(const char *str, const char *substr)
83 {
84 while(*str) {
85 const char *s1 = str;
86 const char *s2 = substr;
88 while(*s1 && *s1 == *s2) {
89 s1++;
90 s2++;
91 }
92 if(!*s2) {
93 return (char*)str;
94 }
95 str++;
96 }
97 return 0;
98 }
100 int strcmp(const char *s1, const char *s2)
101 {
102 while(*s1 && *s1 == *s2) {
103 s1++;
104 s2++;
105 }
106 return *s1 - *s2;
107 }