kern

annotate src/klibc/string.c @ 5:633e35c64772

- added printf family in stdio.c - added atoi/atol in stdlib.c - added nonstandard itoa/utoa in stdlib.c - added strlen/strchr/strrchr in string.c
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Dec 2010 15:07:07 +0200
parents 0489a34ab348
children af6f4adc43d6
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@5 69 return 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@5 78 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@5 84 while(*--ptr >= s) {
nuclear@5 85 if(*ptr == c) {
nuclear@5 86 return ptr;
nuclear@5 87 }
nuclear@5 88 }
nuclear@5 89 return 0;
nuclear@5 90 }