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_;
74 size_t strlen(const char* s)
83 * This it a GNU extension.
84 * It is like strlen(), but it will check at most maxlen
85 * characters for the terminating nul character,
86 * returning maxlen if it doesn't find a nul.
87 * This is very useful for checking the length of untrusted
88 * strings (e.g., from user space).
90 size_t strnlen(const char *s, size_t maxlen)
93 while (len < maxlen && *s++ != '\0')
98 int strcmp(const char* s1, const char* s2)
102 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
109 int strncmp(const char* s1, const char* s2, size_t limit)
114 if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
121 /* limit reached and equal */
125 char *strcat(char *s1, const char *s2)
131 while(*s2) *s1++ = *s2++;
137 char *strncat(char *s1, const char *s2, size_t limit)
144 if(*s2 == '\0') break;
152 char *strcpy(char *dest, const char *src)
164 char *strncpy(char *dest, const char *src, size_t limit)
168 while (*src != '\0' && limit > 0) {
178 char *strdup(const char *s1)
182 ret = Malloc(strlen(s1) + 1);
188 int atoi(const char *buf)
192 while (*buf >= '0' && *buf <= '9') {
201 char *strchr(const char *s, int c)
211 char *strrchr(const char *s, int c)
213 size_t len = strlen(s);
214 const char *p = s + len;
224 char *strpbrk(const char *s, const char *accept)
226 size_t setLen = strlen(accept);
230 for (i = 0; i < setLen; ++i) {
240 struct String_Output_Sink {
241 struct Output_Sink o;
246 static void String_Emit(struct Output_Sink *o_, int ch)
248 struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
255 static void String_Finish(struct Output_Sink *o_)
257 struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
263 * Output was truncated; write terminator at end of buffer
264 * (we will have advanced one character too far)
269 int snprintf(char *s, size_t size, const char *fmt, ...)
271 struct String_Output_Sink sink;
275 /* Prepare string output sink */
276 sink.o.Emit = &String_Emit;
277 sink.o.Finish = &String_Finish;
282 /* Format the string */
284 rc = Format_Output(&sink.o, fmt, args);
290 int fprintf(FILE *file, char *fmt, ...)
292 // PrintDebug("In fprintf!!\n");
298 int printf(char *fmt, ...)
300 // PrintDebug("In fprintf!!\n");
306 int fflush(FILE *stream)
308 //PrintDebug("In fflush!!\n");
315 //PrintDebug("Abort!!\n");
317 //__asm__ __volatile__("trap");
318 //__builtin_unreached();