string.c (3200B)
1 /* $Id: //depot/blt/lib/libc/string.c#4 $ 2 ** 3 ** Copyright 1998 Brian J. Swetland 4 ** All rights reserved. 5 ** 6 ** Redistribution and use in source and binary forms, with or without 7 ** modification, are permitted provided that the following conditions 8 ** are met: 9 ** 1. Redistributions of source code must retain the above copyright 10 ** notice, this list of conditions, and the following disclaimer. 11 ** 2. Redistributions in binary form must reproduce the above copyright 12 ** notice, this list of conditions, and the following disclaimer in the 13 ** documentation and/or other materials provided with the distribution. 14 ** 3. The name of the author may not be used to endorse or promote products 15 ** derived from this software without specific prior written permission. 16 ** 17 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <string.h> 30 31 char *strncpy(char *dst, const char *src, size_t size) 32 { 33 char *d = dst; 34 35 while(size && *src) size--, *dst++ = *src++; 36 if(size) *dst = 0; 37 38 return d; 39 } 40 41 int strncmp(const char *s1, const char *s2, size_t n) 42 { 43 while(n && *s1 && *s2){ 44 if(*s1 != *s2) return -1; 45 s1++; 46 s2++; 47 n--; 48 49 } 50 if(n) return -1; 51 52 return 0; 53 } 54 55 char *strchr (const char *cs, int c) 56 { 57 const char *s; 58 59 s = cs; 60 while (*s) 61 { 62 if (*s == c) 63 return (char *) s; 64 s++; 65 } 66 return NULL; 67 } 68 69 void *memset(void *s, int c, size_t n) 70 { 71 unsigned char *u = (unsigned char *) s; 72 while(n) { 73 *u = c; 74 u++; 75 n--; 76 } 77 return s; 78 } 79 80 void *memmove(void *dest, const void *src, size_t n) 81 { 82 unsigned char *d; 83 unsigned char *s; 84 85 if(dest < src){ 86 d = (unsigned char *)dest; 87 s = (unsigned char *)src; 88 while(n){ 89 *d = *s; 90 d++; 91 s++; 92 n--; 93 } 94 } else { 95 d = ((unsigned char *) dest)+n; 96 s = ((unsigned char *) src)+n; 97 while(n){ 98 d--; 99 s--; 100 *d = *s; 101 n--; 102 } 103 } 104 return dest; 105 } 106 107 int memcmp(const void *dst, const void *src, size_t size) 108 { 109 while(size) { 110 if(*((char *)dst) != *((char *)src)) return 1; 111 ((char *) dst)++; 112 ((char *) src)++; 113 size--; 114 } 115 return 0; 116 } 117 118 void *memcpy(void *dst, const void *src, size_t size) 119 { 120 void *r = dst; 121 122 while(size) { 123 *(((unsigned char *) dst)++) = *(((unsigned char *) src)++); 124 size--; 125 } 126 return r; 127 } 128