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 hash table
[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  * $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
18 #include <palacios/vmm_string.h>
19 #include <palacios/vmm.h>
20
21
22
23 static float e = 0.00000001;
24
25 double ceil(double x) {
26   if ((double)(x - (int)x) == 0) {
27     return (int)x;
28   }
29   return (int)(x + e) + 1;
30 }
31
32 #if 0
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
46 void* memcpy(void *dst, const void* src, size_t n)
47 {
48     unsigned char* d = (unsigned char*) dst;
49     const unsigned char* s = (const unsigned char*) src;
50
51     while (n > 0) {
52         *d++ = *s++;
53         --n;
54     }
55
56     return dst;
57 }
58
59
60 int memcmp(const void *s1_, const void *s2_, size_t n)
61 {
62     const signed char *s1 = s1_, *s2 = s2_;
63
64     while (n > 0) {
65         int cmp = *s1 - *s2;
66         if (cmp != 0)
67             return cmp;
68         ++s1;
69         ++s2;
70     }
71
72     return 0;
73 }
74
75 size_t strlen(const char* s)
76 {
77     size_t len = 0;
78     while (*s++ != '\0')
79         ++len;
80     return len;
81 }
82
83 /*
84  * This it a GNU extension.
85  * It is like strlen(), but it will check at most maxlen
86  * characters for the terminating nul character,
87  * returning maxlen if it doesn't find a nul.
88  * This is very useful for checking the length of untrusted
89  * strings (e.g., from user space).
90  */
91 size_t strnlen(const char *s, size_t maxlen)
92 {
93     size_t len = 0;
94     while (len < maxlen && *s++ != '\0')
95         ++len;
96     return len;
97 }
98
99 int strcmp(const char* s1, const char* s2)
100 {
101     while (1) {
102         int cmp = *s1 - *s2;
103         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
104             return cmp;
105         ++s1;
106         ++s2;
107     }
108 }
109
110 int strncmp(const char* s1, const char* s2, size_t limit)
111 {
112     size_t i = 0;
113     while (i < limit) {
114         int cmp = *s1 - *s2;
115         if (cmp != 0 || *s1 == '\0' || *s2 == '\0')
116             return cmp;
117         ++s1;
118         ++s2;
119         ++i;
120     }
121
122     /* limit reached and equal */
123     return 0;
124 }
125
126 char *strcat(char *s1, const char *s2)
127 {
128     char *t1;
129
130     t1 = s1;
131     while (*s1) s1++;
132     while(*s2) *s1++ = *s2++;
133     *s1 = '\0';
134
135     return t1;
136 }
137
138 char *strcpy(char *dest, const char *src)
139 {
140     char *ret = dest;
141
142     while (*src) {
143         *dest++ = *src++;
144     }
145     *dest = '\0';
146
147     return ret;
148 }
149
150 char *strncpy(char *dest, const char *src, size_t limit)
151 {
152     char *ret = dest;
153
154     while (*src != '\0' && limit > 0) {
155         *dest++ = *src++;
156         --limit;
157     }
158     if (limit > 0)
159         *dest = '\0';
160
161     return ret;
162 }
163
164 char *strdup(const char *s1)
165 {
166     char *ret;
167
168     ret = V3_Malloc(strlen(s1) + 1);
169     strcpy(ret, s1);
170
171     return ret;
172 }
173
174 int atoi(const char *buf) 
175 {
176     int ret = 0;
177
178     while (*buf >= '0' && *buf <= '9') {
179        ret *= 10;
180        ret += *buf - '0';
181        buf++;
182     }
183
184     return ret;
185 }
186
187 char *strchr(const char *s, int c)
188 {
189     while (*s != '\0') {
190         if (*s == c)
191             return (char *) s;
192         ++s;
193     }
194     return 0;
195 }
196
197 char *strrchr(const char *s, int c)
198 {
199     size_t len = strlen(s);
200     const char *p = s + len;
201
202     while (p > s) {
203         --p;
204         if (*p == c)
205             return (char*) p;
206     }
207     return 0;
208 }
209
210 char *strpbrk(const char *s, const char *accept)
211 {
212     size_t setLen = strlen(accept);
213
214     while (*s != '\0') {
215         size_t i;
216         for (i = 0; i < setLen; ++i) {
217             if (*s == accept[i])
218                 return (char *) s;
219         }
220         ++s;
221     }
222
223     return 0;
224 }
225
226 #endif