3 * Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
6 * This is free software. You are permitted to use,
7 * redistribute, and modify it as specified in the file "COPYING".
12 * These are slow and simple implementations of a subset of
13 * the standard C library string functions.
14 * We also have an implementation of snprintf().
18 #include <palacios/vmm_string.h>
19 #include <palacios/vmm.h>
23 static float e = 0.00000001;
25 double ceil(double x) {
26 if ((double)(x - (int)x) == 0) {
29 return (int)(x + e) + 1;
33 void* memset(void* s, int c, size_t n)
35 unsigned char* p = (unsigned char*) s;
38 *p++ = (unsigned char) c;
46 void* memcpy(void *dst, const void* src, size_t n)
48 unsigned char* d = (unsigned char*) dst;
49 const unsigned char* s = (const unsigned char*) src;
60 int memcmp(const void *s1_, const void *s2_, size_t n)
62 const signed char *s1 = s1_, *s2 = s2_;
75 size_t strlen(const char* s)
84 * This it a GNU extension.
85 * It is like strlen(), but it will check at most maxlen
86 * characters for the terminating nul character,
87 * returning maxlen if it doesn't find a nul.
88 * This is very useful for checking the length of untrusted
89 * strings (e.g., from user space).
91 size_t strnlen(const char *s, size_t maxlen)
94 while (len < maxlen && *s++ != '\0')
99 int strcmp(const char* s1, const char* s2)
103 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
110 int strncmp(const char* s1, const char* s2, size_t limit)
115 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
122 /* limit reached and equal */
126 char *strcat(char *s1, const char *s2)
132 while(*s2) *s1++ = *s2++;
138 char *strcpy(char *dest, const char *src)
150 char *strncpy(char *dest, const char *src, size_t limit)
154 while (*src != '\0' && limit > 0) {
164 char *strdup(const char *s1)
168 ret = V3_Malloc(strlen(s1) + 1);
174 int atoi(const char *buf)
178 while (*buf >= '0' && *buf <= '9') {
187 char *strchr(const char *s, int c)
197 char *strrchr(const char *s, int c)
199 size_t len = strlen(s);
200 const char *p = s + len;
210 char *strpbrk(const char *s, const char *accept)
212 size_t setLen = strlen(accept);
216 for (i = 0; i < setLen; ++i) {