1/** 2 * \file 3 * \brief Implementations of standard libc string functions. 4 */ 5 6/* 7 * Copyright (c) 2007, 2008, 2014, ETH Zurich. 8 * All rights reserved. 9 * 10 * This file is distributed under the terms in the attached LICENSE file. 11 * If you do not find this file, copies can be found by writing to: 12 * ETH Zurich D-INFK, CAB F.78, Universitaetstr. 6, CH-8092 Zurich, 13 * Attn: Systems Group. 14 */ 15 16#include <stdint.h> 17#include <assert.h> 18#include <stddef.h> 19#include <string.h> 20 21#if 0 22void * 23memset (void *s, int c, size_t n) 24{ 25 uint8_t *p = (uint8_t *)s; 26 for (size_t m = 0; m < n; m++) { 27 *p++ = c; 28 } 29 return s; 30} 31#endif 32 33char * 34strstr(const char *a, const char *b) 35{ 36 const char *res = a; 37 38 for (; *res != '\0'; res++) { 39 if (strcmp(res, b) == 0) { 40 return (char *)res; 41 } 42 } 43 44 return NULL; 45} 46 47void * 48memchr(const void *s, int c, size_t n) 49{ 50 size_t i; 51 const uint8_t *p; 52 53 for (i = 0, p = s; i < n; i++, p++) 54 if (*p == c) 55 return (void *)p; 56 57 return NULL; 58} 59 60size_t 61strlen(const char *s) 62{ 63 size_t i = 0; 64 65 while (*s != '\0') { 66 i++; 67 s++; 68 } 69 70 return i; 71} 72 73char * 74strncpy(char *dest, const char *src, size_t count) 75{ 76 char *tmp = dest; 77 int c = count; 78 while (c >= 0) { 79 if ((*tmp = *src) != 0) src++; 80 tmp++; 81 c--; 82 } 83 return dest; 84} 85 86char * 87strcpy(char *dest, const char *src) 88{ 89 char *pos = dest; 90 while ((*pos++ = *src++) != 0); 91 return dest; 92} 93 94#if 0 95void * 96memmove(void *dst, const void *src, size_t sz) 97{ 98 size_t i; 99 const char *source = src; 100 char *dest = dst; 101 102 if(sz == 0) { 103 return dst; 104 } 105 106 /* XXX: This is slooooooooooowwwwwwwwwww ...... */ 107 if(src >= dst) { // copy front to back 108 for(i = 0; i < sz; i++) 109 dest[i] = source[i]; 110 } else { // copy back to front 111 for(i = sz - 1; i > 0; i--) 112 dest[i] = source[i]; 113 114 dest[0] = source[0]; 115 } 116 117 return dst; 118} 119#endif 120 121void * 122memcpy(void *dst, const void *src, size_t len) 123{ 124 char *d = dst; 125 const char *s = src; 126 127 /* check that we don't overlap (should use memmove()) */ 128 assert((src < dst && (char *)src + len <= (char *)dst) 129 || (dst < src && (char *)dst + len <= (char *)src)); 130 131 while (len--) 132 *d++ = *s++; 133 134 return dst; 135} 136 137char * 138strchr(const char *s, int c) 139{ 140 unsigned int i; 141 unsigned int n = strlen(s); 142 143 for(i = 0; i < n ; i++) { 144 if(s[i] == c) 145 return (char *)&s[i]; 146 } 147 148 return NULL; 149} 150 151char * 152strrchr(const char *s, int c) 153{ 154 unsigned int i; 155 unsigned int n = strlen(s); 156 157 if(n == 0) 158 return NULL; 159 160 for(i = n - 1; ; i--) { 161 if (s[i] == c) { 162 return (char *)&s[i]; 163 } else if (i == 0) { 164 return NULL; 165 } 166 } 167} 168 169int 170strncmp(const char *s1, const char *s2, size_t n) 171{ 172 int result; 173 174 for(unsigned int i = 0; i < n; i++) { 175 if((result = s2[i] - s1[i]) != 0) { 176 return result; 177 } 178 179 if(s1[i] == '\0' || s2[i] == '\0') { 180 break; 181 } 182 } 183 184 return 0; 185} 186 187int 188strcmp(const char *s1, const char *s2) 189{ 190 size_t len1 = strlen(s1), len2 = strlen(s2), 191 maxlen = len1 > len2 ? len1 : len2; 192 193 return strncmp(s1, s2, maxlen); 194} 195 196size_t 197strspn(const char* s, const char* set) 198{ 199 const char* l = s; 200 201 while (('\0' != *l) && (NULL != strchr(set, *l))) 202 { 203 l++; 204 } 205 return (l - s); 206} 207 208size_t 209strcspn(const char* s, const char* set) 210{ 211 const char* l = s; 212 213 while (('\0' != *l) && (NULL == strchr(set, *l))) 214 { 215 l++; 216 } 217 return (l - s); 218} 219 220char * 221strtok(char *s, const char *delim) 222{ 223 static char *save; 224 225 if (s == NULL) 226 { 227 s = save; 228 } 229 230 s = s + strspn(s, delim); 231 if ('\0' == *s) 232 { 233 return NULL; 234 } 235 236 save = s + strcspn(s, delim); 237 if ('\0' != *save) 238 { 239 *save++ = '\0'; 240 } 241 return s; 242} 243