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.


modified copyright tags
[palacios.git] / palacios / src / palacios / vmm_string.c
1 /*
2  * String library
3  * Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4  * (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> 
5  * (c) 2008, The V3VEE Project <http://www.v3vee.org> 
6  * $Revision: 1.2 $
7  * 
8  * This is free software.  You are permitted to use,
9  * redistribute, and modify it as specified in the file "COPYING".
10  */
11
12
13
14
15 /*
16  * NOTE:
17  * These are slow and simple implementations of a subset of
18  * the standard C library string functions.
19  * We also have an implementation of snprintf().
20  */
21
22
23 #include <palacios/vmm_string.h>
24 #include <palacios/vmm.h>
25
26
27
28 static float e = 0.00000001;
29
30 double ceil(double x) {
31   if ((double)(x - (int)x) == 0) {
32     return (int)x;
33   }
34   return (int)(x + e) + 1;
35 }
36
37 #if 0
38 void* memset(void* s, int c, size_t n)
39 {
40     unsigned char* p = (unsigned char*) s;
41
42     while (n > 0) {
43         *p++ = (unsigned char) c;
44         --n;
45     }
46
47     return s;
48 }
49
50
51 void* memcpy(void *dst, const void* src, size_t n)
52 {
53     unsigned char* d = (unsigned char*) dst;
54     const unsigned char* s = (const unsigned char*) src;
55
56     while (n > 0) {
57         *d++ = *s++;
58         --n;
59     }
60
61     return dst;
62 }
63
64
65 int memcmp(const void *s1_, const void *s2_, size_t n)
66 {
67     const signed char *s1 = s1_, *s2 = s2_;
68
69     while (n > 0) {
70         int cmp = *s1 - *s2;
71         if (cmp != 0)
72             return cmp;
73         ++s1;
74         ++s2;
75     }
76
77     return 0;
78 }
79
80 size_t strlen(const char* s)
81 {
82     size_t len = 0;
83     while (*s++ != '\0')
84         ++len;
85     return len;
86 }
87
88 /*
89  * This it a GNU extension.
90  * It is like strlen(), but it will check at most maxlen
91  * characters for the terminating nul character,
92  * returning maxlen if it doesn't find a nul.
93  * This is very useful for checking the length of untrusted
94  * strings (e.g., from user space).
95  */
96 size_t strnlen(const char *s, size_t maxlen)
97 {
98     size_t len = 0;
99     while (len < maxlen && *s++ != '\0')
100         ++len;
101     return len;
102 }
103
104 int strcmp(const char* s1, const char* s2)
105 {
106     while (1) {
107         int cmp = *s1 - *s2;
108         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
109             return cmp;
110         ++s1;
111         ++s2;
112     }
113 }
114
115 int strncmp(const char* s1, const char* s2, size_t limit)
116 {
117     size_t i = 0;
118     while (i < limit) {
119         int cmp = *s1 - *s2;
120         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
121             return cmp;
122         ++s1;
123         ++s2;
124         ++i;
125     }
126
127     /* limit reached and equal */
128     return 0;
129 }
130
131 char *strcat(char *s1, const char *s2)
132 {
133     char *t1;
134
135     t1 = s1;
136     while (*s1) s1++;
137     while(*s2) *s1++ = *s2++;
138     *s1 = '\0';
139
140     return t1;
141 }
142
143 char *strcpy(char *dest, const char *src)
144 {
145     char *ret = dest;
146
147     while (*src) {
148         *dest++ = *src++;
149     }
150     *dest = '\0';
151
152     return ret;
153 }
154
155 char *strncpy(char *dest, const char *src, size_t limit)
156 {
157     char *ret = dest;
158
159     while (*src != '\0' && limit > 0) {
160         *dest++ = *src++;
161         --limit;
162     }
163     if (limit > 0)
164         *dest = '\0';
165
166     return ret;
167 }
168
169 char *strdup(const char *s1)
170 {
171     char *ret;
172
173     ret = V3_Malloc(strlen(s1) + 1);
174     strcpy(ret, s1);
175
176     return ret;
177 }
178
179 int atoi(const char *buf) 
180 {
181     int ret = 0;
182
183     while (*buf >= '0' && *buf <= '9') {
184        ret *= 10;
185        ret += *buf - '0';
186        buf++;
187     }
188
189     return ret;
190 }
191
192 char *strchr(const char *s, int c)
193 {
194     while (*s != '\0') {
195         if (*s == c)
196             return (char *) s;
197         ++s;
198     }
199     return 0;
200 }
201
202 char *strrchr(const char *s, int c)
203 {
204     size_t len = strlen(s);
205     const char *p = s + len;
206
207     while (p > s) {
208         --p;
209         if (*p == c)
210             return (char*) p;
211     }
212     return 0;
213 }
214
215 char *strpbrk(const char *s, const char *accept)
216 {
217     size_t setLen = strlen(accept);
218
219     while (*s != '\0') {
220         size_t i;
221         for (i = 0; i < setLen; ++i) {
222             if (*s == accept[i])
223                 return (char *) s;
224         }
225         ++s;
226     }
227
228     return 0;
229 }
230
231 #endif