ia64/xen-unstable

view xen/common/string.c @ 6832:5959fae4722a

Set NE bit for VMX guest CR0. VMCS guest CR0.NE bit must
be set, else it will cause "vm-entry failed".

Signed-off-by: Chengyuan Li <chengyuan.li@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Sep 14 13:37:50 2005 +0000 (2005-09-14)
parents a01199a95070
children 7d0d88685f79
line source
1 /*
2 * linux/lib/string.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * stupid library routines.. The optimized versions should generally be found
9 * as inline code in <asm-xx/string.h>
10 *
11 * These are buggy as well..
12 *
13 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
14 * - Added strsep() which will replace strtok() soon (because strsep() is
15 * reentrant and should be faster). Use only strsep() in new code, please.
16 */
18 #include <xen/types.h>
19 #include <xen/string.h>
20 #include <xen/ctype.h>
22 #ifndef __HAVE_ARCH_STRNICMP
23 /**
24 * strnicmp - Case insensitive, length-limited string comparison
25 * @s1: One string
26 * @s2: The other string
27 * @len: the maximum number of characters to compare
28 */
29 int strnicmp(const char *s1, const char *s2, size_t len)
30 {
31 /* Yes, Virginia, it had better be unsigned */
32 unsigned char c1, c2;
34 c1 = 0; c2 = 0;
35 if (len) {
36 do {
37 c1 = *s1; c2 = *s2;
38 s1++; s2++;
39 if (!c1)
40 break;
41 if (!c2)
42 break;
43 if (c1 == c2)
44 continue;
45 c1 = tolower(c1);
46 c2 = tolower(c2);
47 if (c1 != c2)
48 break;
49 } while (--len);
50 }
51 return (int)c1 - (int)c2;
52 }
53 #endif
55 char * ___strtok;
57 #ifndef __HAVE_ARCH_STRCPY
58 /**
59 * strcpy - Copy a %NUL terminated string
60 * @dest: Where to copy the string to
61 * @src: Where to copy the string from
62 */
63 char * strcpy(char * dest,const char *src)
64 {
65 char *tmp = dest;
67 while ((*dest++ = *src++) != '\0')
68 /* nothing */;
69 return tmp;
70 }
71 #endif
73 #ifndef __HAVE_ARCH_STRNCPY
74 /**
75 * strncpy - Copy a length-limited, %NUL-terminated string
76 * @dest: Where to copy the string to
77 * @src: Where to copy the string from
78 * @count: The maximum number of bytes to copy
79 *
80 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
81 * However, the result is not %NUL-terminated if the source exceeds
82 * @count bytes.
83 */
84 char * strncpy(char * dest,const char *src,size_t count)
85 {
86 char *tmp = dest;
88 while (count-- && (*dest++ = *src++) != '\0')
89 /* nothing */;
91 return tmp;
92 }
93 #endif
95 #ifndef __HAVE_ARCH_STRLCPY
96 /**
97 * strlcpy - Copy a %NUL terminated string into a sized buffer
98 * @dest: Where to copy the string to
99 * @src: Where to copy the string from
100 * @size: size of destination buffer
101 *
102 * Compatible with *BSD: the result is always a valid
103 * NUL-terminated string that fits in the buffer (unless,
104 * of course, the buffer size is zero). It does not pad
105 * out the result like strncpy() does.
106 */
107 size_t strlcpy(char *dest, const char *src, size_t size)
108 {
109 size_t ret = strlen(src);
111 if (size) {
112 size_t len = (ret >= size) ? size-1 : ret;
113 memcpy(dest, src, len);
114 dest[len] = '\0';
115 }
116 return ret;
117 }
118 EXPORT_SYMBOL(strlcpy);
119 #endif
121 #ifndef __HAVE_ARCH_STRCAT
122 /**
123 * strcat - Append one %NUL-terminated string to another
124 * @dest: The string to be appended to
125 * @src: The string to append to it
126 */
127 char * strcat(char * dest, const char * src)
128 {
129 char *tmp = dest;
131 while (*dest)
132 dest++;
133 while ((*dest++ = *src++) != '\0')
134 ;
136 return tmp;
137 }
138 #endif
140 #ifndef __HAVE_ARCH_STRNCAT
141 /**
142 * strncat - Append a length-limited, %NUL-terminated string to another
143 * @dest: The string to be appended to
144 * @src: The string to append to it
145 * @count: The maximum numbers of bytes to copy
146 *
147 * Note that in contrast to strncpy, strncat ensures the result is
148 * terminated.
149 */
150 char * strncat(char *dest, const char *src, size_t count)
151 {
152 char *tmp = dest;
154 if (count) {
155 while (*dest)
156 dest++;
157 while ((*dest++ = *src++)) {
158 if (--count == 0) {
159 *dest = '\0';
160 break;
161 }
162 }
163 }
165 return tmp;
166 }
167 #endif
169 #ifndef __HAVE_ARCH_STRCMP
170 /**
171 * strcmp - Compare two strings
172 * @cs: One string
173 * @ct: Another string
174 */
175 int strcmp(const char * cs,const char * ct)
176 {
177 register signed char __res;
179 while (1) {
180 if ((__res = *cs - *ct++) != 0 || !*cs++)
181 break;
182 }
184 return __res;
185 }
186 #endif
188 #ifndef __HAVE_ARCH_STRNCMP
189 /**
190 * strncmp - Compare two length-limited strings
191 * @cs: One string
192 * @ct: Another string
193 * @count: The maximum number of bytes to compare
194 */
195 int strncmp(const char * cs,const char * ct,size_t count)
196 {
197 register signed char __res = 0;
199 while (count) {
200 if ((__res = *cs - *ct++) != 0 || !*cs++)
201 break;
202 count--;
203 }
205 return __res;
206 }
207 #endif
209 #ifndef __HAVE_ARCH_STRCHR
210 /**
211 * strchr - Find the first occurrence of a character in a string
212 * @s: The string to be searched
213 * @c: The character to search for
214 */
215 char * strchr(const char * s, int c)
216 {
217 for(; *s != (char) c; ++s)
218 if (*s == '\0')
219 return NULL;
220 return (char *) s;
221 }
222 #endif
224 #ifndef __HAVE_ARCH_STRRCHR
225 /**
226 * strrchr - Find the last occurrence of a character in a string
227 * @s: The string to be searched
228 * @c: The character to search for
229 */
230 char * strrchr(const char * s, int c)
231 {
232 const char *p = s + strlen(s);
233 do {
234 if (*p == (char)c)
235 return (char *)p;
236 } while (--p >= s);
237 return NULL;
238 }
239 #endif
241 #ifndef __HAVE_ARCH_STRLEN
242 /**
243 * strlen - Find the length of a string
244 * @s: The string to be sized
245 */
246 size_t strlen(const char * s)
247 {
248 const char *sc;
250 for (sc = s; *sc != '\0'; ++sc)
251 /* nothing */;
252 return sc - s;
253 }
254 #endif
256 #ifndef __HAVE_ARCH_STRNLEN
257 /**
258 * strnlen - Find the length of a length-limited string
259 * @s: The string to be sized
260 * @count: The maximum number of bytes to search
261 */
262 size_t strnlen(const char * s, size_t count)
263 {
264 const char *sc;
266 for (sc = s; count-- && *sc != '\0'; ++sc)
267 /* nothing */;
268 return sc - s;
269 }
270 #endif
272 #ifndef __HAVE_ARCH_STRSPN
273 /**
274 * strspn - Calculate the length of the initial substring of @s which only
275 * contain letters in @accept
276 * @s: The string to be searched
277 * @accept: The string to search for
278 */
279 size_t strspn(const char *s, const char *accept)
280 {
281 const char *p;
282 const char *a;
283 size_t count = 0;
285 for (p = s; *p != '\0'; ++p) {
286 for (a = accept; *a != '\0'; ++a) {
287 if (*p == *a)
288 break;
289 }
290 if (*a == '\0')
291 return count;
292 ++count;
293 }
295 return count;
296 }
297 #endif
299 #ifndef __HAVE_ARCH_STRPBRK
300 /**
301 * strpbrk - Find the first occurrence of a set of characters
302 * @cs: The string to be searched
303 * @ct: The characters to search for
304 */
305 char * strpbrk(const char * cs,const char * ct)
306 {
307 const char *sc1,*sc2;
309 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
310 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
311 if (*sc1 == *sc2)
312 return (char *) sc1;
313 }
314 }
315 return NULL;
316 }
317 #endif
319 #ifndef __HAVE_ARCH_STRTOK
320 /**
321 * strtok - Split a string into tokens
322 * @s: The string to be searched
323 * @ct: The characters to search for
324 *
325 * WARNING: strtok is deprecated, use strsep instead.
326 */
327 char * strtok(char * s,const char * ct)
328 {
329 char *sbegin, *send;
331 sbegin = s ? s : ___strtok;
332 if (!sbegin) {
333 return NULL;
334 }
335 sbegin += strspn(sbegin,ct);
336 if (*sbegin == '\0') {
337 ___strtok = NULL;
338 return( NULL );
339 }
340 send = strpbrk( sbegin, ct);
341 if (send && *send != '\0')
342 *send++ = '\0';
343 ___strtok = send;
344 return (sbegin);
345 }
346 #endif
348 #ifndef __HAVE_ARCH_STRSEP
349 /**
350 * strsep - Split a string into tokens
351 * @s: The string to be searched
352 * @ct: The characters to search for
353 *
354 * strsep() updates @s to point after the token, ready for the next call.
355 *
356 * It returns empty tokens, too, behaving exactly like the libc function
357 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
358 * Same semantics, slimmer shape. ;)
359 */
360 char * strsep(char **s, const char *ct)
361 {
362 char *sbegin = *s, *end;
364 if (sbegin == NULL)
365 return NULL;
367 end = strpbrk(sbegin, ct);
368 if (end)
369 *end++ = '\0';
370 *s = end;
372 return sbegin;
373 }
374 #endif
376 #ifndef __HAVE_ARCH_MEMSET
377 /**
378 * memset - Fill a region of memory with the given value
379 * @s: Pointer to the start of the area.
380 * @c: The byte to fill the area with
381 * @count: The size of the area.
382 *
383 * Do not use memset() to access IO space, use memset_io() instead.
384 */
385 void * memset(void * s,int c,size_t count)
386 {
387 char *xs = (char *) s;
389 while (count--)
390 *xs++ = c;
392 return s;
393 }
394 #endif
396 #ifndef __HAVE_ARCH_BCOPY
397 /**
398 * bcopy - Copy one area of memory to another
399 * @src: Where to copy from
400 * @dest: Where to copy to
401 * @count: The size of the area.
402 *
403 * Note that this is the same as memcpy(), with the arguments reversed.
404 * memcpy() is the standard, bcopy() is a legacy BSD function.
405 *
406 * You should not use this function to access IO space, use memcpy_toio()
407 * or memcpy_fromio() instead.
408 */
409 char * bcopy(const char * src, char * dest, int count)
410 {
411 char *tmp = dest;
413 while (count--)
414 *tmp++ = *src++;
416 return dest;
417 }
418 #endif
420 #ifndef __HAVE_ARCH_MEMCPY
421 /**
422 * memcpy - Copy one area of memory to another
423 * @dest: Where to copy to
424 * @src: Where to copy from
425 * @count: The size of the area.
426 *
427 * You should not use this function to access IO space, use memcpy_toio()
428 * or memcpy_fromio() instead.
429 */
430 void * memcpy(void * dest,const void *src,size_t count)
431 {
432 char *tmp = (char *) dest, *s = (char *) src;
434 while (count--)
435 *tmp++ = *s++;
437 return dest;
438 }
439 #endif
441 #ifndef __HAVE_ARCH_MEMMOVE
442 /**
443 * memmove - Copy one area of memory to another
444 * @dest: Where to copy to
445 * @src: Where to copy from
446 * @count: The size of the area.
447 *
448 * Unlike memcpy(), memmove() copes with overlapping areas.
449 */
450 void * memmove(void * dest,const void *src,size_t count)
451 {
452 char *tmp, *s;
454 if (dest <= src) {
455 tmp = (char *) dest;
456 s = (char *) src;
457 while (count--)
458 *tmp++ = *s++;
459 }
460 else {
461 tmp = (char *) dest + count;
462 s = (char *) src + count;
463 while (count--)
464 *--tmp = *--s;
465 }
467 return dest;
468 }
469 #endif
471 #ifndef __HAVE_ARCH_MEMCMP
472 /**
473 * memcmp - Compare two areas of memory
474 * @cs: One area of memory
475 * @ct: Another area of memory
476 * @count: The size of the area.
477 */
478 int memcmp(const void * cs,const void * ct,size_t count)
479 {
480 const unsigned char *su1, *su2;
481 int res = 0;
483 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
484 if ((res = *su1 - *su2) != 0)
485 break;
486 return res;
487 }
488 #endif
490 #ifndef __HAVE_ARCH_MEMSCAN
491 /**
492 * memscan - Find a character in an area of memory.
493 * @addr: The memory area
494 * @c: The byte to search for
495 * @size: The size of the area.
496 *
497 * returns the address of the first occurrence of @c, or 1 byte past
498 * the area if @c is not found
499 */
500 void * memscan(void * addr, int c, size_t size)
501 {
502 unsigned char * p = (unsigned char *) addr;
504 while (size) {
505 if (*p == c)
506 return (void *) p;
507 p++;
508 size--;
509 }
510 return (void *) p;
511 }
512 #endif
514 #ifndef __HAVE_ARCH_STRSTR
515 /**
516 * strstr - Find the first substring in a %NUL terminated string
517 * @s1: The string to be searched
518 * @s2: The string to search for
519 */
520 char * strstr(const char * s1,const char * s2)
521 {
522 int l1, l2;
524 l2 = strlen(s2);
525 if (!l2)
526 return (char *) s1;
527 l1 = strlen(s1);
528 while (l1 >= l2) {
529 l1--;
530 if (!memcmp(s1,s2,l2))
531 return (char *) s1;
532 s1++;
533 }
534 return NULL;
535 }
536 #endif
538 #ifndef __HAVE_ARCH_MEMCHR
539 /**
540 * memchr - Find a character in an area of memory.
541 * @s: The memory area
542 * @c: The byte to search for
543 * @n: The size of the area.
544 *
545 * returns the address of the first occurrence of @c, or %NULL
546 * if @c is not found
547 */
548 void *memchr(const void *s, int c, size_t n)
549 {
550 const unsigned char *p = s;
551 while (n-- != 0) {
552 if ((unsigned char)c == *p++) {
553 return (void *)(p-1);
554 }
555 }
556 return NULL;
557 }
559 #endif
561 /*
562 * Local variables:
563 * mode: C
564 * c-set-style: "BSD"
565 * c-basic-offset: 8
566 * tab-width: 8
567 * indent-tabs-mode: t
568 * End:
569 */