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] / palacios / src / palacios / vmm_string.c
1 /*
2  * String library 
3  * Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4  * Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without restriction,
9  * including without limitation the rights to use, copy, modify, merge,
10  * publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
18  * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19  * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
20  * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
21  * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
22  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
25  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27 /* Modifications by Jack Lange <jarusl@cs.northwestern.edu> */
28
29
30
31 /*
32  * NOTE:
33  * These are slow and simple implementations of a subset of
34  * the standard C library string functions.
35  * We also have an implementation of snprintf().
36  */
37
38
39 #include <palacios/vmm_string.h>
40 #include <palacios/vmm.h>
41
42
43
44 static float e = 0.00000001;
45
46 double ceil(double x) {
47   if ((double)(x - (int)x) == 0) {
48     return (int)x;
49   }
50   return (int)(x + e) + 1;
51 }
52
53 #if 0
54 void* memset(void* s, int c, size_t n)
55 {
56     unsigned char* p = (unsigned char*) s;
57
58     while (n > 0) {
59         *p++ = (unsigned char) c;
60         --n;
61     }
62
63     return s;
64 }
65
66
67 void* memcpy(void *dst, const void* src, size_t n)
68 {
69     unsigned char* d = (unsigned char*) dst;
70     const unsigned char* s = (const unsigned char*) src;
71
72     while (n > 0) {
73         *d++ = *s++;
74         --n;
75     }
76
77     return dst;
78 }
79
80
81 int memcmp(const void *s1_, const void *s2_, size_t n)
82 {
83     const signed char *s1 = s1_, *s2 = s2_;
84
85     while (n > 0) {
86         int cmp = *s1 - *s2;
87         if (cmp != 0)
88             return cmp;
89         ++s1;
90         ++s2;
91     }
92
93     return 0;
94 }
95
96 size_t strlen(const char* s)
97 {
98     size_t len = 0;
99     while (*s++ != '\0')
100         ++len;
101     return len;
102 }
103
104 /*
105  * This it a GNU extension.
106  * It is like strlen(), but it will check at most maxlen
107  * characters for the terminating nul character,
108  * returning maxlen if it doesn't find a nul.
109  * This is very useful for checking the length of untrusted
110  * strings (e.g., from user space).
111  */
112 size_t strnlen(const char *s, size_t maxlen)
113 {
114     size_t len = 0;
115     while (len < maxlen && *s++ != '\0')
116         ++len;
117     return len;
118 }
119
120 int strcmp(const char* s1, const char* s2)
121 {
122     while (1) {
123         int cmp = *s1 - *s2;
124         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
125             return cmp;
126         ++s1;
127         ++s2;
128     }
129 }
130
131 int strncmp(const char* s1, const char* s2, size_t limit)
132 {
133     size_t i = 0;
134     while (i < limit) {
135         int cmp = *s1 - *s2;
136         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
137             return cmp;
138         ++s1;
139         ++s2;
140         ++i;
141     }
142
143     /* limit reached and equal */
144     return 0;
145 }
146
147 char *strcat(char *s1, const char *s2)
148 {
149     char *t1;
150
151     t1 = s1;
152     while (*s1) s1++;
153     while(*s2) *s1++ = *s2++;
154     *s1 = '\0';
155
156     return t1;
157 }
158
159 char *strcpy(char *dest, const char *src)
160 {
161     char *ret = dest;
162
163     while (*src) {
164         *dest++ = *src++;
165     }
166     *dest = '\0';
167
168     return ret;
169 }
170
171 char *strncpy(char *dest, const char *src, size_t limit)
172 {
173     char *ret = dest;
174
175     while (*src != '\0' && limit > 0) {
176         *dest++ = *src++;
177         --limit;
178     }
179     if (limit > 0)
180         *dest = '\0';
181
182     return ret;
183 }
184
185 char *strdup(const char *s1)
186 {
187     char *ret;
188
189     ret = V3_Malloc(strlen(s1) + 1);
190     strcpy(ret, s1);
191
192     return ret;
193 }
194
195 int atoi(const char *buf) 
196 {
197     int ret = 0;
198
199     while (*buf >= '0' && *buf <= '9') {
200        ret *= 10;
201        ret += *buf - '0';
202        buf++;
203     }
204
205     return ret;
206 }
207
208 char *strchr(const char *s, int c)
209 {
210     while (*s != '\0') {
211         if (*s == c)
212             return (char *) s;
213         ++s;
214     }
215     return 0;
216 }
217
218 char *strrchr(const char *s, int c)
219 {
220     size_t len = strlen(s);
221     const char *p = s + len;
222
223     while (p > s) {
224         --p;
225         if (*p == c)
226             return (char*) p;
227     }
228     return 0;
229 }
230
231 char *strpbrk(const char *s, const char *accept)
232 {
233     size_t setLen = strlen(accept);
234
235     while (*s != '\0') {
236         size_t i;
237         for (i = 0; i < setLen; ++i) {
238             if (*s == accept[i])
239                 return (char *) s;
240         }
241         ++s;
242     }
243
244     return 0;
245 }
246
247 #endif