Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Release 1.0
[palacios.git] / geekos / src / common / string.c
1 /*
2  * String library
3  * Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4  * $Revision: 1.2 $
5  * 
6  * This is free software.  You are permitted to use,
7  * redistribute, and modify it as specified in the file "COPYING".
8  */
9
10 /*
11  * NOTE:
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().
15  */
16
17 #include <fmtout.h>
18 #include <string.h>
19 #include <palacios/vmm.h>
20
21 //extern int PrintDebug(char *fmt, ...);
22
23 extern void *Malloc(size_t size);
24
25 /* Standard I/O predefined streams
26 */
27 FILE   _streams = {0, 0, 0, 0, 0, NULL, NULL, 0, 0};
28
29 FILE  *stdin = (&_streams);
30 FILE  *stdout = (&_streams);
31 FILE  *stderr = (&_streams);
32
33 void* memset(void* s, int c, size_t n)
34 {
35     unsigned char* p = (unsigned char*) s;
36
37     while (n > 0) {
38         *p++ = (unsigned char) c;
39         --n;
40     }
41
42     return s;
43 }
44
45 void* memcpy(void *dst, const void* src, size_t n)
46 {
47     unsigned char* d = (unsigned char*) dst;
48     const unsigned char* s = (const unsigned char*) src;
49
50     while (n > 0) {
51         *d++ = *s++;
52         --n;
53     }
54
55     return dst;
56 }
57
58 int memcmp(const void *s1_, const void *s2_, size_t n)
59 {
60     const signed char *s1 = s1_, *s2 = s2_;
61
62     while (n > 0) {
63         int cmp = *s1 - *s2;
64         if (cmp != 0)
65             return cmp;
66         ++s1;
67         ++s2;
68         --n;
69     }
70
71     return 0;
72 }
73
74 size_t strlen(const char* s)
75 {
76     size_t len = 0;
77     while (*s++ != '\0')
78         ++len;
79     return len;
80 }
81
82 /*
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).
89  */
90 size_t strnlen(const char *s, size_t maxlen)
91 {
92     size_t len = 0;
93     while (len < maxlen && *s++ != '\0')
94         ++len;
95     return len;
96 }
97
98 int strcmp(const char* s1, const char* s2)
99 {
100     while (1) {
101         int cmp = *s1 - *s2;
102         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
103             return cmp;
104         ++s1;
105         ++s2;
106     }
107 }
108
109 int strncmp(const char* s1, const char* s2, size_t limit)
110 {
111     size_t i = 0;
112     while (i < limit) {
113         int cmp = *s1 - *s2;
114         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
115             return cmp;
116         ++s1;
117         ++s2;
118         ++i;
119     }
120
121     /* limit reached and equal */
122     return 0;
123 }
124
125 char *strcat(char *s1, const char *s2)
126 {
127     char *t1;
128
129     t1 = s1;
130     while (*s1) s1++;
131     while(*s2) *s1++ = *s2++;
132     *s1 = '\0';
133
134     return t1;
135 }
136
137 char *strncat(char *s1, const char *s2, size_t limit)
138 {
139     size_t i = 0;
140     char *t1;
141     t1 = s1;
142     while (*s1) s1++;
143     while (i < limit) {
144                 if(*s2 == '\0') break;
145                 *s1++ = *s2++;          
146     }
147     *s1 = '\0';
148     return t1;
149 }
150         
151
152 char *strcpy(char *dest, const char *src)
153 {
154     char *ret = dest;
155
156     while (*src) {
157         *dest++ = *src++;
158     }
159     *dest = '\0';
160
161     return ret;
162 }
163
164 char *strncpy(char *dest, const char *src, size_t limit)
165 {
166     char *ret = dest;
167
168     while (*src != '\0' && limit > 0) {
169         *dest++ = *src++;
170         --limit;
171     }
172     if (limit > 0)
173         *dest = '\0';
174
175     return ret;
176 }
177
178 char *strdup(const char *s1)
179 {
180     char *ret;
181
182     ret = Malloc(strlen(s1) + 1);
183     strcpy(ret, s1);
184
185     return ret;
186 }
187
188 int atoi(const char *buf) 
189 {
190     int ret = 0;
191
192     while (*buf >= '0' && *buf <= '9') {
193        ret *= 10;
194        ret += *buf - '0';
195        buf++;
196     }
197
198     return ret;
199 }
200
201 char *strchr(const char *s, int c)
202 {
203     while (*s != '\0') {
204         if (*s == c)
205             return (char *) s;
206         ++s;
207     }
208     return 0;
209 }
210
211 char *strrchr(const char *s, int c)
212 {
213     size_t len = strlen(s);
214     const char *p = s + len;
215
216     while (p > s) {
217         --p;
218         if (*p == c)
219             return (char*) p;
220     }
221     return 0;
222 }
223
224 char *strpbrk(const char *s, const char *accept)
225 {
226     size_t setLen = strlen(accept);
227
228     while (*s != '\0') {
229         size_t i;
230         for (i = 0; i < setLen; ++i) {
231             if (*s == accept[i])
232                 return (char *) s;
233         }
234         ++s;
235     }
236
237     return 0;
238 }
239
240 struct String_Output_Sink {
241     struct Output_Sink o;
242     char *s;
243     size_t n, size;
244 };
245
246 static void String_Emit(struct Output_Sink *o_, int ch)
247 {
248     struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
249
250     if (o->n < o->size)
251         *(o->s)++ = ch;
252     ++(o->n);
253 }
254
255 static void String_Finish(struct Output_Sink *o_)
256 {
257     struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
258
259     if (o->n < o->size)
260         *(o->s) = '\0';
261     else
262         /*
263          * Output was truncated; write terminator at end of buffer
264          * (we will have advanced one character too far)
265          */
266         *(o->s - 1) = '\0';
267 }
268
269 int snprintf(char *s, size_t size, const char *fmt, ...)
270 {
271     struct String_Output_Sink sink;
272     int rc;
273     va_list args;
274
275     /* Prepare string output sink */
276     sink.o.Emit = &String_Emit;
277     sink.o.Finish = &String_Finish;
278     sink.s = s;
279     sink.n = 0;
280     sink.size = size;
281
282     /* Format the string */
283     va_start(args, fmt);
284     rc = Format_Output(&sink.o, fmt, args);
285     va_end(args);
286
287     return rc;
288 }
289
290 int fprintf(FILE *file, char *fmt, ...)
291 {
292    // PrintDebug("In fprintf!!\n");
293
294    return 0;
295
296 }
297
298 int printf(char *fmt, ...)
299 {
300    // PrintDebug("In fprintf!!\n");
301
302    return 0;
303
304 }
305
306 int fflush(FILE *stream)
307 {
308     //PrintDebug("In fflush!!\n");
309
310     return 0;
311 }
312
313 void abort(void)
314 {
315    //PrintDebug("Abort!!\n");
316
317    //__asm__ __volatile__("trap"); 
318    //__builtin_unreached();
319
320
321    while(1);
322    
323 }
324
325 int tolower(int ch)
326 {
327      return _tolower(ch);
328 }
329