ia64/xen-unstable

view xen/common/string.c @ 9706:3c05406f5e0a

In some cases, say for instance for some bizzare reason
the tree was checked out of CVS, which doens't neccessarily
store file permissions, mkbuildtree may not be executable.
So run them explicitly via bash.

Signed-Off-By: Horms <horms@verge.net.au>
author kaf24@firebug.cl.cam.ac.uk
date Thu Apr 13 11:24:00 2006 +0100 (2006-04-13)
parents 7d0d88685f79
children 647c06ba0b49
line source
1 /*
2 * linux/lib/string.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 #include <xen/types.h>
8 #include <xen/string.h>
9 #include <xen/ctype.h>
11 #ifndef __HAVE_ARCH_STRNICMP
12 /**
13 * strnicmp - Case insensitive, length-limited string comparison
14 * @s1: One string
15 * @s2: The other string
16 * @len: the maximum number of characters to compare
17 */
18 int strnicmp(const char *s1, const char *s2, size_t len)
19 {
20 /* Yes, Virginia, it had better be unsigned */
21 unsigned char c1, c2;
23 c1 = 0; c2 = 0;
24 if (len) {
25 do {
26 c1 = *s1; c2 = *s2;
27 s1++; s2++;
28 if (!c1)
29 break;
30 if (!c2)
31 break;
32 if (c1 == c2)
33 continue;
34 c1 = tolower(c1);
35 c2 = tolower(c2);
36 if (c1 != c2)
37 break;
38 } while (--len);
39 }
40 return (int)c1 - (int)c2;
41 }
42 #endif
44 #ifndef __HAVE_ARCH_STRCPY
45 /**
46 * strcpy - Copy a %NUL terminated string
47 * @dest: Where to copy the string to
48 * @src: Where to copy the string from
49 */
50 char * strcpy(char * dest,const char *src)
51 {
52 char *tmp = dest;
54 while ((*dest++ = *src++) != '\0')
55 /* nothing */;
56 return tmp;
57 }
58 #endif
60 #ifndef __HAVE_ARCH_STRNCPY
61 /**
62 * strncpy - Copy a length-limited, %NUL-terminated string
63 * @dest: Where to copy the string to
64 * @src: Where to copy the string from
65 * @count: The maximum number of bytes to copy
66 *
67 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
68 * However, the result is not %NUL-terminated if the source exceeds
69 * @count bytes.
70 */
71 char * strncpy(char * dest,const char *src,size_t count)
72 {
73 char *tmp = dest;
75 while (count-- && (*dest++ = *src++) != '\0')
76 /* nothing */;
78 return tmp;
79 }
80 #endif
82 #ifndef __HAVE_ARCH_STRLCPY
83 /**
84 * strlcpy - Copy a %NUL terminated string into a sized buffer
85 * @dest: Where to copy the string to
86 * @src: Where to copy the string from
87 * @size: size of destination buffer
88 *
89 * Compatible with *BSD: the result is always a valid
90 * NUL-terminated string that fits in the buffer (unless,
91 * of course, the buffer size is zero). It does not pad
92 * out the result like strncpy() does.
93 */
94 size_t strlcpy(char *dest, const char *src, size_t size)
95 {
96 size_t ret = strlen(src);
98 if (size) {
99 size_t len = (ret >= size) ? size-1 : ret;
100 memcpy(dest, src, len);
101 dest[len] = '\0';
102 }
103 return ret;
104 }
105 EXPORT_SYMBOL(strlcpy);
106 #endif
108 #ifndef __HAVE_ARCH_STRCAT
109 /**
110 * strcat - Append one %NUL-terminated string to another
111 * @dest: The string to be appended to
112 * @src: The string to append to it
113 */
114 char * strcat(char * dest, const char * src)
115 {
116 char *tmp = dest;
118 while (*dest)
119 dest++;
120 while ((*dest++ = *src++) != '\0')
121 ;
123 return tmp;
124 }
125 #endif
127 #ifndef __HAVE_ARCH_STRNCAT
128 /**
129 * strncat - Append a length-limited, %NUL-terminated string to another
130 * @dest: The string to be appended to
131 * @src: The string to append to it
132 * @count: The maximum numbers of bytes to copy
133 *
134 * Note that in contrast to strncpy, strncat ensures the result is
135 * terminated.
136 */
137 char * strncat(char *dest, const char *src, size_t count)
138 {
139 char *tmp = dest;
141 if (count) {
142 while (*dest)
143 dest++;
144 while ((*dest++ = *src++)) {
145 if (--count == 0) {
146 *dest = '\0';
147 break;
148 }
149 }
150 }
152 return tmp;
153 }
154 #endif
156 #ifndef __HAVE_ARCH_STRCMP
157 /**
158 * strcmp - Compare two strings
159 * @cs: One string
160 * @ct: Another string
161 */
162 int strcmp(const char * cs,const char * ct)
163 {
164 register signed char __res;
166 while (1) {
167 if ((__res = *cs - *ct++) != 0 || !*cs++)
168 break;
169 }
171 return __res;
172 }
173 #endif
175 #ifndef __HAVE_ARCH_STRNCMP
176 /**
177 * strncmp - Compare two length-limited strings
178 * @cs: One string
179 * @ct: Another string
180 * @count: The maximum number of bytes to compare
181 */
182 int strncmp(const char * cs,const char * ct,size_t count)
183 {
184 register signed char __res = 0;
186 while (count) {
187 if ((__res = *cs - *ct++) != 0 || !*cs++)
188 break;
189 count--;
190 }
192 return __res;
193 }
194 #endif
196 #ifndef __HAVE_ARCH_STRCHR
197 /**
198 * strchr - Find the first occurrence of a character in a string
199 * @s: The string to be searched
200 * @c: The character to search for
201 */
202 char * strchr(const char * s, int c)
203 {
204 for(; *s != (char) c; ++s)
205 if (*s == '\0')
206 return NULL;
207 return (char *) s;
208 }
209 #endif
211 #ifndef __HAVE_ARCH_STRRCHR
212 /**
213 * strrchr - Find the last occurrence of a character in a string
214 * @s: The string to be searched
215 * @c: The character to search for
216 */
217 char * strrchr(const char * s, int c)
218 {
219 const char *p = s + strlen(s);
220 do {
221 if (*p == (char)c)
222 return (char *)p;
223 } while (--p >= s);
224 return NULL;
225 }
226 #endif
228 #ifndef __HAVE_ARCH_STRLEN
229 /**
230 * strlen - Find the length of a string
231 * @s: The string to be sized
232 */
233 size_t strlen(const char * s)
234 {
235 const char *sc;
237 for (sc = s; *sc != '\0'; ++sc)
238 /* nothing */;
239 return sc - s;
240 }
241 #endif
243 #ifndef __HAVE_ARCH_STRNLEN
244 /**
245 * strnlen - Find the length of a length-limited string
246 * @s: The string to be sized
247 * @count: The maximum number of bytes to search
248 */
249 size_t strnlen(const char * s, size_t count)
250 {
251 const char *sc;
253 for (sc = s; count-- && *sc != '\0'; ++sc)
254 /* nothing */;
255 return sc - s;
256 }
257 #endif
259 #ifndef __HAVE_ARCH_STRSPN
260 /**
261 * strspn - Calculate the length of the initial substring of @s which only
262 * contain letters in @accept
263 * @s: The string to be searched
264 * @accept: The string to search for
265 */
266 size_t strspn(const char *s, const char *accept)
267 {
268 const char *p;
269 const char *a;
270 size_t count = 0;
272 for (p = s; *p != '\0'; ++p) {
273 for (a = accept; *a != '\0'; ++a) {
274 if (*p == *a)
275 break;
276 }
277 if (*a == '\0')
278 return count;
279 ++count;
280 }
282 return count;
283 }
284 #endif
286 #ifndef __HAVE_ARCH_STRPBRK
287 /**
288 * strpbrk - Find the first occurrence of a set of characters
289 * @cs: The string to be searched
290 * @ct: The characters to search for
291 */
292 char * strpbrk(const char * cs,const char * ct)
293 {
294 const char *sc1,*sc2;
296 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
297 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
298 if (*sc1 == *sc2)
299 return (char *) sc1;
300 }
301 }
302 return NULL;
303 }
304 #endif
306 #ifndef __HAVE_ARCH_STRSEP
307 /**
308 * strsep - Split a string into tokens
309 * @s: The string to be searched
310 * @ct: The characters to search for
311 *
312 * strsep() updates @s to point after the token, ready for the next call.
313 *
314 * It returns empty tokens, too, behaving exactly like the libc function
315 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
316 * Same semantics, slimmer shape. ;)
317 */
318 char * strsep(char **s, const char *ct)
319 {
320 char *sbegin = *s, *end;
322 if (sbegin == NULL)
323 return NULL;
325 end = strpbrk(sbegin, ct);
326 if (end)
327 *end++ = '\0';
328 *s = end;
330 return sbegin;
331 }
332 #endif
334 #ifndef __HAVE_ARCH_MEMSET
335 /**
336 * memset - Fill a region of memory with the given value
337 * @s: Pointer to the start of the area.
338 * @c: The byte to fill the area with
339 * @count: The size of the area.
340 *
341 * Do not use memset() to access IO space, use memset_io() instead.
342 */
343 void * memset(void * s,int c,size_t count)
344 {
345 char *xs = (char *) s;
347 while (count--)
348 *xs++ = c;
350 return s;
351 }
352 #endif
354 #ifndef __HAVE_ARCH_BCOPY
355 /**
356 * bcopy - Copy one area of memory to another
357 * @src: Where to copy from
358 * @dest: Where to copy to
359 * @count: The size of the area.
360 *
361 * Note that this is the same as memcpy(), with the arguments reversed.
362 * memcpy() is the standard, bcopy() is a legacy BSD function.
363 *
364 * You should not use this function to access IO space, use memcpy_toio()
365 * or memcpy_fromio() instead.
366 */
367 char * bcopy(const char * src, char * dest, int count)
368 {
369 char *tmp = dest;
371 while (count--)
372 *tmp++ = *src++;
374 return dest;
375 }
376 #endif
378 #ifndef __HAVE_ARCH_MEMCPY
379 /**
380 * memcpy - Copy one area of memory to another
381 * @dest: Where to copy to
382 * @src: Where to copy from
383 * @count: The size of the area.
384 *
385 * You should not use this function to access IO space, use memcpy_toio()
386 * or memcpy_fromio() instead.
387 */
388 void * memcpy(void * dest,const void *src,size_t count)
389 {
390 char *tmp = (char *) dest, *s = (char *) src;
392 while (count--)
393 *tmp++ = *s++;
395 return dest;
396 }
397 #endif
399 #ifndef __HAVE_ARCH_MEMMOVE
400 /**
401 * memmove - Copy one area of memory to another
402 * @dest: Where to copy to
403 * @src: Where to copy from
404 * @count: The size of the area.
405 *
406 * Unlike memcpy(), memmove() copes with overlapping areas.
407 */
408 void * memmove(void * dest,const void *src,size_t count)
409 {
410 char *tmp, *s;
412 if (dest <= src) {
413 tmp = (char *) dest;
414 s = (char *) src;
415 while (count--)
416 *tmp++ = *s++;
417 }
418 else {
419 tmp = (char *) dest + count;
420 s = (char *) src + count;
421 while (count--)
422 *--tmp = *--s;
423 }
425 return dest;
426 }
427 #endif
429 #ifndef __HAVE_ARCH_MEMCMP
430 /**
431 * memcmp - Compare two areas of memory
432 * @cs: One area of memory
433 * @ct: Another area of memory
434 * @count: The size of the area.
435 */
436 int memcmp(const void * cs,const void * ct,size_t count)
437 {
438 const unsigned char *su1, *su2;
439 int res = 0;
441 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
442 if ((res = *su1 - *su2) != 0)
443 break;
444 return res;
445 }
446 #endif
448 #ifndef __HAVE_ARCH_MEMSCAN
449 /**
450 * memscan - Find a character in an area of memory.
451 * @addr: The memory area
452 * @c: The byte to search for
453 * @size: The size of the area.
454 *
455 * returns the address of the first occurrence of @c, or 1 byte past
456 * the area if @c is not found
457 */
458 void * memscan(void * addr, int c, size_t size)
459 {
460 unsigned char * p = (unsigned char *) addr;
462 while (size) {
463 if (*p == c)
464 return (void *) p;
465 p++;
466 size--;
467 }
468 return (void *) p;
469 }
470 #endif
472 #ifndef __HAVE_ARCH_STRSTR
473 /**
474 * strstr - Find the first substring in a %NUL terminated string
475 * @s1: The string to be searched
476 * @s2: The string to search for
477 */
478 char * strstr(const char * s1,const char * s2)
479 {
480 int l1, l2;
482 l2 = strlen(s2);
483 if (!l2)
484 return (char *) s1;
485 l1 = strlen(s1);
486 while (l1 >= l2) {
487 l1--;
488 if (!memcmp(s1,s2,l2))
489 return (char *) s1;
490 s1++;
491 }
492 return NULL;
493 }
494 #endif
496 #ifndef __HAVE_ARCH_MEMCHR
497 /**
498 * memchr - Find a character in an area of memory.
499 * @s: The memory area
500 * @c: The byte to search for
501 * @n: The size of the area.
502 *
503 * returns the address of the first occurrence of @c, or %NULL
504 * if @c is not found
505 */
506 void *memchr(const void *s, int c, size_t n)
507 {
508 const unsigned char *p = s;
509 while (n-- != 0) {
510 if ((unsigned char)c == *p++) {
511 return (void *)(p-1);
512 }
513 }
514 return NULL;
515 }
517 #endif
519 /*
520 * Local variables:
521 * mode: C
522 * c-set-style: "BSD"
523 * c-basic-offset: 8
524 * tab-width: 8
525 * indent-tabs-mode: t
526 * End:
527 */