kern

annotate src/klibc/string.c @ 91:f83f50c17c3b

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