kern

view src/klibc/string.c @ 53:23abbeea4d5f

llalallalala
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 08 Aug 2011 09:53:10 +0300
parents 633e35c64772
children ebca81749ef5
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 }