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.


added new copyright and license
[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     }
69
70     return 0;
71 }
72
73 size_t strlen(const char* s)
74 {
75     size_t len = 0;
76     while (*s++ != '\0')
77         ++len;
78     return len;
79 }
80
81 /*
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).
88  */
89 size_t strnlen(const char *s, size_t maxlen)
90 {
91     size_t len = 0;
92     while (len < maxlen && *s++ != '\0')
93         ++len;
94     return len;
95 }
96
97 int strcmp(const char* s1, const char* s2)
98 {
99     while (1) {
100         int cmp = *s1 - *s2;
101         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
102             return cmp;
103         ++s1;
104         ++s2;
105     }
106 }
107
108 int strncmp(const char* s1, const char* s2, size_t limit)
109 {
110     size_t i = 0;
111     while (i < limit) {
112         int cmp = *s1 - *s2;
113         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
114             return cmp;
115         ++s1;
116         ++s2;
117         ++i;
118     }
119
120     /* limit reached and equal */
121     return 0;
122 }
123
124 char *strcat(char *s1, const char *s2)
125 {
126     char *t1;
127
128     t1 = s1;
129     while (*s1) s1++;
130     while(*s2) *s1++ = *s2++;
131     *s1 = '\0';
132
133     return t1;
134 }
135
136 char *strncat(char *s1, const char *s2, size_t limit)
137 {
138     size_t i = 0;
139     char *t1;
140     t1 = s1;
141     while (*s1) s1++;
142     while (i < limit) {
143                 if(*s2 == '\0') break;
144                 *s1++ = *s2++;          
145     }
146     *s1 = '\0';
147     return t1;
148 }
149         
150
151 char *strcpy(char *dest, const char *src)
152 {
153     char *ret = dest;
154
155     while (*src) {
156         *dest++ = *src++;
157     }
158     *dest = '\0';
159
160     return ret;
161 }
162
163 char *strncpy(char *dest, const char *src, size_t limit)
164 {
165     char *ret = dest;
166
167     while (*src != '\0' && limit > 0) {
168         *dest++ = *src++;
169         --limit;
170     }
171     if (limit > 0)
172         *dest = '\0';
173
174     return ret;
175 }
176
177 char *strdup(const char *s1)
178 {
179     char *ret;
180
181     ret = Malloc(strlen(s1) + 1);
182     strcpy(ret, s1);
183
184     return ret;
185 }
186
187 int atoi(const char *buf) 
188 {
189     int ret = 0;
190
191     while (*buf >= '0' && *buf <= '9') {
192        ret *= 10;
193        ret += *buf - '0';
194        buf++;
195     }
196
197     return ret;
198 }
199
200 char *strchr(const char *s, int c)
201 {
202     while (*s != '\0') {
203         if (*s == c)
204             return (char *) s;
205         ++s;
206     }
207     return 0;
208 }
209
210 char *strrchr(const char *s, int c)
211 {
212     size_t len = strlen(s);
213     const char *p = s + len;
214
215     while (p > s) {
216         --p;
217         if (*p == c)
218             return (char*) p;
219     }
220     return 0;
221 }
222
223 char *strpbrk(const char *s, const char *accept)
224 {
225     size_t setLen = strlen(accept);
226
227     while (*s != '\0') {
228         size_t i;
229         for (i = 0; i < setLen; ++i) {
230             if (*s == accept[i])
231                 return (char *) s;
232         }
233         ++s;
234     }
235
236     return 0;
237 }
238
239 struct String_Output_Sink {
240     struct Output_Sink o;
241     char *s;
242     size_t n, size;
243 };
244
245 static void String_Emit(struct Output_Sink *o_, int ch)
246 {
247     struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
248
249     if (o->n < o->size)
250         *(o->s)++ = ch;
251     ++(o->n);
252 }
253
254 static void String_Finish(struct Output_Sink *o_)
255 {
256     struct String_Output_Sink *o = (struct String_Output_Sink*) o_;
257
258     if (o->n < o->size)
259         *(o->s) = '\0';
260     else
261         /*
262          * Output was truncated; write terminator at end of buffer
263          * (we will have advanced one character too far)
264          */
265         *(o->s - 1) = '\0';
266 }
267
268 int snprintf(char *s, size_t size, const char *fmt, ...)
269 {
270     struct String_Output_Sink sink;
271     int rc;
272     va_list args;
273
274     /* Prepare string output sink */
275     sink.o.Emit = &String_Emit;
276     sink.o.Finish = &String_Finish;
277     sink.s = s;
278     sink.n = 0;
279     sink.size = size;
280
281     /* Format the string */
282     va_start(args, fmt);
283     rc = Format_Output(&sink.o, fmt, args);
284     va_end(args);
285
286     return rc;
287 }
288
289 int fprintf(FILE *file, char *fmt, ...)
290 {
291    // PrintDebug("In fprintf!!\n");
292
293    return 0;
294
295 }
296
297 int printf(char *fmt, ...)
298 {
299    // PrintDebug("In fprintf!!\n");
300
301    return 0;
302
303 }
304
305 int fflush(FILE *stream)
306 {
307     //PrintDebug("In fflush!!\n");
308
309     return 0;
310 }
311
312 void abort(void)
313 {
314    //PrintDebug("Abort!!\n");
315
316    //__asm__ __volatile__("trap"); 
317    //__builtin_unreached();
318
319
320    while(1);
321    
322 }
323
324 int tolower(int ch)
325 {
326      return _tolower(ch);
327 }
328