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().
44 #define NEED_STRNLEN 0
46 #define NEED_STRNCMP 0
48 #define NEED_STRNCAT 0
50 #define NEED_STRNCPY 0
54 #define NEED_STRRCHR 0
55 #define NEED_STRPBRK 0
59 #include <palacios/vmm_string.h>
60 #include <palacios/vmm.h>
64 static float e = 0.00000001;
66 double v3_ceil(double x) {
67 if ((double)(x - (int)x) == 0) {
70 return (int)(x + e) + 1;
75 void* memset(void* s, int c, size_t n)
77 unsigned char* p = (unsigned char*) s;
80 *p++ = (unsigned char) c;
89 void* memcpy(void *dst, const void* src, size_t n)
91 unsigned char* d = (unsigned char*) dst;
92 const unsigned char* s = (const unsigned char*) src;
105 int memcmp(const void *s1_, const void *s2_, size_t n)
107 const signed char *s1 = s1_, *s2 = s2_;
123 size_t strlen(const char* s)
136 * This it a GNU extension.
137 * It is like strlen(), but it will check at most maxlen
138 * characters for the terminating nul character,
139 * returning maxlen if it doesn't find a nul.
140 * This is very useful for checking the length of untrusted
141 * strings (e.g., from user space).
143 size_t strnlen(const char *s, size_t maxlen)
146 while (len < maxlen && *s++ != '\0')
154 int strcmp(const char* s1, const char* s2)
158 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
168 int strncmp(const char* s1, const char* s2, size_t limit)
173 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
180 /* limit reached and equal */
187 char *strcat(char *s1, const char *s2)
193 while(*s2) *s1++ = *s2++;
202 char *strncat(char *s1, const char *s2, size_t limit)
209 if(*s2 == '\0') break;
220 char *strcpy(char *dest, const char *src)
235 char *strncpy(char *dest, const char *src, size_t limit)
239 while (*src != '\0' && limit > 0) {
253 char *strdup(const char *s1)
257 ret = V3_Malloc(strlen(s1) + 1);
268 int atoi(const char *buf)
272 while (*buf >= '0' && *buf <= '9') {
284 char *strchr(const char *s, int c)
297 char *strrchr(const char *s, int c)
299 size_t len = strlen(s);
300 const char *p = s + len;
312 char *strpbrk(const char *s, const char *accept)
314 size_t setLen = strlen(accept);
318 for (i = 0; i < setLen; ++i) {