3 * Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 * (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
5 * (c) 2008, The V3VEE Project <http://www.v3vee.org>
8 * This is free software. You are permitted to use,
9 * redistribute, and modify it as specified in the file "COPYING".
17 * These are slow and simple implementations of a subset of
18 * the standard C library string functions.
19 * We also have an implementation of snprintf().
23 #include <palacios/vmm_string.h>
24 #include <palacios/vmm.h>
28 static float e = 0.00000001;
30 double ceil(double x) {
31 if ((double)(x - (int)x) == 0) {
34 return (int)(x + e) + 1;
38 void* memset(void* s, int c, size_t n)
40 unsigned char* p = (unsigned char*) s;
43 *p++ = (unsigned char) c;
51 void* memcpy(void *dst, const void* src, size_t n)
53 unsigned char* d = (unsigned char*) dst;
54 const unsigned char* s = (const unsigned char*) src;
65 int memcmp(const void *s1_, const void *s2_, size_t n)
67 const signed char *s1 = s1_, *s2 = s2_;
80 size_t strlen(const char* s)
89 * This it a GNU extension.
90 * It is like strlen(), but it will check at most maxlen
91 * characters for the terminating nul character,
92 * returning maxlen if it doesn't find a nul.
93 * This is very useful for checking the length of untrusted
94 * strings (e.g., from user space).
96 size_t strnlen(const char *s, size_t maxlen)
99 while (len < maxlen && *s++ != '\0')
104 int strcmp(const char* s1, const char* s2)
108 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
115 int strncmp(const char* s1, const char* s2, size_t limit)
120 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
127 /* limit reached and equal */
131 char *strcat(char *s1, const char *s2)
137 while(*s2) *s1++ = *s2++;
143 char *strcpy(char *dest, const char *src)
155 char *strncpy(char *dest, const char *src, size_t limit)
159 while (*src != '\0' && limit > 0) {
169 char *strdup(const char *s1)
173 ret = V3_Malloc(strlen(s1) + 1);
179 int atoi(const char *buf)
183 while (*buf >= '0' && *buf <= '9') {
192 char *strchr(const char *s, int c)
202 char *strrchr(const char *s, int c)
204 size_t len = strlen(s);
205 const char *p = s + len;
215 char *strpbrk(const char *s, const char *accept)
217 size_t setLen = strlen(accept);
221 for (i = 0; i < setLen; ++i) {