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().
19 #include <palacios/vmm.h>
21 //extern int PrintDebug(char *fmt, ...);
23 extern void *Malloc(size_t size);
25 /* Standard I/O predefined streams
27 FILE _streams = {0, 0, 0, 0, 0, NULL, NULL, 0, 0};
29 FILE *stdin = (&_streams);
30 FILE *stdout = (&_streams);
31 FILE *stderr = (&_streams);
33 void* memset(void* s, int c, size_t n)
35 unsigned char* p = (unsigned char*) s;
38 *p++ = (unsigned char) c;
45 void* memcpy(void *dst, const void* src, size_t n)
47 unsigned char* d = (unsigned char*) dst;
48 const unsigned char* s = (const unsigned char*) src;
58 int memcmp(const void *s1_, const void *s2_, size_t n)
60 const signed char *s1 = s1_, *s2 = s2_;
73 size_t strlen(const char* s)
82 * This it a GNU extension.
83 * It is like strlen(), but it will check at most maxlen
84 * characters for the terminating nul character,
85 * returning maxlen if it doesn't find a nul.
86 * This is very useful for checking the length of untrusted
87 * strings (e.g., from user space).
89 size_t strnlen(const char *s, size_t maxlen)
92 while (len < maxlen && *s++ != '\0')
97 int strcmp(const char* s1, const char* s2)
101 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
108 int strncmp(const char* s1, const char* s2, size_t limit)
113 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
120 /* limit reached and equal */
124 char *strcat(char *s1, const char *s2)
130 while(*s2) *s1++ = *s2++;
136 char *strncat(char *s1, const char *s2, size_t limit)
143 if(*s2 == '\0') break;
151 char *strcpy(char *dest, const char *src)
163 char *strncpy(char *dest, const char *src, size_t limit)
167 while (*src != '\0' && limit > 0) {
177 char *strdup(const char *s1)
181 ret = Malloc(strlen(s1) + 1);
187 int atoi(const char *buf)
191 while (*buf >= '0' && *buf <= '9') {
200 char *strchr(const char *s, int c)
210 char *strrchr(const char *s, int c)
212 size_t len = strlen(s);
213 const char *p = s + len;
223 char *strpbrk(const char *s, const char *accept)
225 size_t setLen = strlen(accept);
229 for (i = 0; i < setLen; ++i) {
239 struct String_Output_Sink {
240 struct Output_Sink o;
245 static void String_Emit(struct Output_Sink *o_, int ch)
247 struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
254 static void String_Finish(struct Output_Sink *o_)
256 struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
262 * Output was truncated; write terminator at end of buffer
263 * (we will have advanced one character too far)
268 int snprintf(char *s, size_t size, const char *fmt, ...)
270 struct String_Output_Sink sink;
274 /* Prepare string output sink */
275 sink.o.Emit = &String_Emit;
276 sink.o.Finish = &String_Finish;
281 /* Format the string */
283 rc = Format_Output(&sink.o, fmt, args);
289 int fprintf(FILE *file, char *fmt, ...)
291 // PrintDebug("In fprintf!!\n");
297 int printf(char *fmt, ...)
299 // PrintDebug("In fprintf!!\n");
305 int fflush(FILE *stream)
307 //PrintDebug("In fflush!!\n");
314 //PrintDebug("Abort!!\n");
316 //__asm__ __volatile__("trap");
317 //__builtin_unreached();