3 * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without restriction,
9 * including without limitation the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18 * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
22 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
25 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 /* Modifications by Jack Lange <jarusl@cs.northwestern.edu> */
33 * These are slow and simple implementations of a subset of
34 * the standard C library string functions.
35 * We also have an implementation of snprintf().
39 #include <palacios/vmm_types.h>
40 #include <palacios/vmm_string.h>
41 #include <palacios/vmm.h>
44 #ifdef CONFIG_BUILT_IN_MEMSET
45 void * memset(void * s, int c, size_t n) {
46 uchar_t * p = (uchar_t *) s;
57 #ifdef CONFIG_BUILT_IN_MEMCPY
58 void * memcpy(void * dst, const void * src, size_t n) {
59 uchar_t * d = (uchar_t *) dst;
60 const uchar_t * s = (const uchar_t *)src;
72 #ifdef CONFIG_BUILT_IN_MEMCMP
73 int memcmp(const void * s1_, const void * s2_, size_t n) {
74 const char * s1 = s1_;
75 const char * s2 = s2_;
78 int cmp = (*s1 - *s2);
93 #ifdef CONFIG_BUILT_IN_STRLEN
94 size_t strlen(const char * s) {
97 while (*s++ != '\0') {
107 #ifdef CONFIG_BUILT_IN_STRNLEN
109 * This it a GNU extension.
110 * It is like strlen(), but it will check at most maxlen
111 * characters for the terminating nul character,
112 * returning maxlen if it doesn't find a nul.
113 * This is very useful for checking the length of untrusted
114 * strings (e.g., from user space).
116 size_t strnlen(const char * s, size_t maxlen) {
119 while ((len < maxlen) && (*s++ != '\0')) {
128 #ifdef CONFIG_BUILT_IN_STRCMP
129 int strcmp(const char * s1, const char * s2) {
131 int cmp = (*s1 - *s2);
133 if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
144 #ifdef CONFIG_BUILT_IN_STRNCMP
145 int strncmp(const char * s1, const char * s2, size_t limit) {
149 int cmp = (*s1 - *s2);
151 if ((cmp != 0) || (*s1 == '\0') || (*s2 == '\0')) {
160 /* limit reached and equal */
166 #ifdef CONFIG_BUILT_IN_STRCAT
167 char * strcat(char * s1, const char * s2) {
170 while (*s1) { s1++; }
171 while (*s2) { *s1++ = *s2++; }
180 #ifdef CONFIG_BUILT_IN_STRNCAT
181 char * strncat(char * s1, const char * s2, size_t limit) {
187 while (*s1) { s1++; }
202 #ifdef CONFIG_BUILT_IN_STRCPY
203 char * strcpy(char * dest, const char * src)
217 #ifdef CONFIG_BUILT_IN_STRNCPY
218 char * strncpy(char * dest, const char * src, size_t limit) {
221 while ((*src != '\0') && (limit > 0)) {
235 #ifdef CONFIG_BUILT_IN_STRDUP
236 char * strdup(const char * s1) {
239 ret = V3_Malloc(strlen(s1) + 1);
249 #ifdef CONFIG_BUILT_IN_ATOI
250 int atoi(const char * buf) {
253 while ((*buf >= '0') && (*buf <= '9')) {
264 #ifdef CONFIG_BUILT_IN_STRCHR
265 char * strchr(const char * s, int c) {
276 #ifdef CONFIG_BUILT_IN_STRRCHR
277 char * strrchr(const char * s, int c) {
278 size_t len = strlen(s);
279 const char * p = s + len;
292 #ifdef CONFIG_BUILT_IN_STRPBRK
293 char * strpbrk(const char * s, const char * accept) {
294 size_t setLen = strlen(accept);
298 for (i = 0; i < setLen; ++i) {
299 if (*s == accept[i]) {