ia64/xen-unstable

view xen/common/string.c @ 16456:ad632e4f26d4

Revert 16450:5e8e82e80. Instead remove all arch-specific handling of
memcmp() and hence always use common/lib.c version.
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Sat Nov 24 22:19:03 2007 +0000 (2007-11-24)
parents 5e8e82e80f3b
children 9fd36167ecfa
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_STRLCPY
45 /**
46 * strlcpy - Copy a %NUL terminated string into a sized buffer
47 * @dest: Where to copy the string to
48 * @src: Where to copy the string from
49 * @size: size of destination buffer
50 *
51 * Compatible with *BSD: the result is always a valid
52 * NUL-terminated string that fits in the buffer (unless,
53 * of course, the buffer size is zero). It does not pad
54 * out the result like strncpy() does.
55 */
56 size_t strlcpy(char *dest, const char *src, size_t size)
57 {
58 size_t ret = strlen(src);
60 if (size) {
61 size_t len = (ret >= size) ? size-1 : ret;
62 memcpy(dest, src, len);
63 dest[len] = '\0';
64 }
65 return ret;
66 }
67 EXPORT_SYMBOL(strlcpy);
68 #endif
70 #ifndef __HAVE_ARCH_STRLCAT
71 /**
72 * strlcat - Append a %NUL terminated string into a sized buffer
73 * @dest: Where to copy the string to
74 * @src: Where to copy the string from
75 * @size: size of destination buffer
76 *
77 * Compatible with *BSD: the result is always a valid
78 * NUL-terminated string that fits in the buffer (unless,
79 * of course, the buffer size is zero).
80 */
81 size_t strlcat(char *dest, const char *src, size_t size)
82 {
83 size_t slen = strlen(src);
84 size_t dlen = strnlen(dest, size);
85 char *p = dest + dlen;
87 while ((p - dest) < size)
88 if ((*p++ = *src++) == '\0')
89 break;
91 if (dlen < size)
92 *(p-1) = '\0';
94 return slen + dlen;
95 }
96 EXPORT_SYMBOL(strlcat);
97 #endif
99 #ifndef __HAVE_ARCH_STRCMP
100 /**
101 * strcmp - Compare two strings
102 * @cs: One string
103 * @ct: Another string
104 */
105 int strcmp(const char * cs,const char * ct)
106 {
107 register signed char __res;
109 while (1) {
110 if ((__res = *cs - *ct++) != 0 || !*cs++)
111 break;
112 }
114 return __res;
115 }
116 #endif
118 #ifndef __HAVE_ARCH_STRNCMP
119 /**
120 * strncmp - Compare two length-limited strings
121 * @cs: One string
122 * @ct: Another string
123 * @count: The maximum number of bytes to compare
124 */
125 int strncmp(const char * cs,const char * ct,size_t count)
126 {
127 register signed char __res = 0;
129 while (count) {
130 if ((__res = *cs - *ct++) != 0 || !*cs++)
131 break;
132 count--;
133 }
135 return __res;
136 }
137 #endif
139 #ifndef __HAVE_ARCH_STRCHR
140 /**
141 * strchr - Find the first occurrence of a character in a string
142 * @s: The string to be searched
143 * @c: The character to search for
144 */
145 char * strchr(const char * s, int c)
146 {
147 for(; *s != (char) c; ++s)
148 if (*s == '\0')
149 return NULL;
150 return (char *) s;
151 }
152 #endif
154 #ifndef __HAVE_ARCH_STRRCHR
155 /**
156 * strrchr - Find the last occurrence of a character in a string
157 * @s: The string to be searched
158 * @c: The character to search for
159 */
160 char * strrchr(const char * s, int c)
161 {
162 const char *p = s + strlen(s);
163 do {
164 if (*p == (char)c)
165 return (char *)p;
166 } while (--p >= s);
167 return NULL;
168 }
169 #endif
171 #ifndef __HAVE_ARCH_STRLEN
172 /**
173 * strlen - Find the length of a string
174 * @s: The string to be sized
175 */
176 size_t strlen(const char * s)
177 {
178 const char *sc;
180 for (sc = s; *sc != '\0'; ++sc)
181 /* nothing */;
182 return sc - s;
183 }
184 #endif
186 #ifndef __HAVE_ARCH_STRNLEN
187 /**
188 * strnlen - Find the length of a length-limited string
189 * @s: The string to be sized
190 * @count: The maximum number of bytes to search
191 */
192 size_t strnlen(const char * s, size_t count)
193 {
194 const char *sc;
196 for (sc = s; count-- && *sc != '\0'; ++sc)
197 /* nothing */;
198 return sc - s;
199 }
200 #endif
202 #ifndef __HAVE_ARCH_STRSPN
203 /**
204 * strspn - Calculate the length of the initial substring of @s which only
205 * contain letters in @accept
206 * @s: The string to be searched
207 * @accept: The string to search for
208 */
209 size_t strspn(const char *s, const char *accept)
210 {
211 const char *p;
212 const char *a;
213 size_t count = 0;
215 for (p = s; *p != '\0'; ++p) {
216 for (a = accept; *a != '\0'; ++a) {
217 if (*p == *a)
218 break;
219 }
220 if (*a == '\0')
221 return count;
222 ++count;
223 }
225 return count;
226 }
227 #endif
229 #ifndef __HAVE_ARCH_STRPBRK
230 /**
231 * strpbrk - Find the first occurrence of a set of characters
232 * @cs: The string to be searched
233 * @ct: The characters to search for
234 */
235 char * strpbrk(const char * cs,const char * ct)
236 {
237 const char *sc1,*sc2;
239 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
240 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
241 if (*sc1 == *sc2)
242 return (char *) sc1;
243 }
244 }
245 return NULL;
246 }
247 #endif
249 #ifndef __HAVE_ARCH_STRSEP
250 /**
251 * strsep - Split a string into tokens
252 * @s: The string to be searched
253 * @ct: The characters to search for
254 *
255 * strsep() updates @s to point after the token, ready for the next call.
256 *
257 * It returns empty tokens, too, behaving exactly like the libc function
258 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
259 * Same semantics, slimmer shape. ;)
260 */
261 char * strsep(char **s, const char *ct)
262 {
263 char *sbegin = *s, *end;
265 if (sbegin == NULL)
266 return NULL;
268 end = strpbrk(sbegin, ct);
269 if (end)
270 *end++ = '\0';
271 *s = end;
273 return sbegin;
274 }
275 #endif
277 #ifndef __HAVE_ARCH_MEMSET
278 /**
279 * memset - Fill a region of memory with the given value
280 * @s: Pointer to the start of the area.
281 * @c: The byte to fill the area with
282 * @count: The size of the area.
283 *
284 * Do not use memset() to access IO space, use memset_io() instead.
285 */
286 void * memset(void * s,int c,size_t count)
287 {
288 char *xs = (char *) s;
290 while (count--)
291 *xs++ = c;
293 return s;
294 }
295 #endif
297 #ifndef __HAVE_ARCH_BCOPY
298 /**
299 * bcopy - Copy one area of memory to another
300 * @src: Where to copy from
301 * @dest: Where to copy to
302 * @count: The size of the area.
303 *
304 * Note that this is the same as memcpy(), with the arguments reversed.
305 * memcpy() is the standard, bcopy() is a legacy BSD function.
306 *
307 * You should not use this function to access IO space, use memcpy_toio()
308 * or memcpy_fromio() instead.
309 */
310 char * bcopy(const char * src, char * dest, int count)
311 {
312 char *tmp = dest;
314 while (count--)
315 *tmp++ = *src++;
317 return dest;
318 }
319 #endif
321 #ifndef __HAVE_ARCH_MEMCPY
322 /**
323 * memcpy - Copy one area of memory to another
324 * @dest: Where to copy to
325 * @src: Where to copy from
326 * @count: The size of the area.
327 *
328 * You should not use this function to access IO space, use memcpy_toio()
329 * or memcpy_fromio() instead.
330 */
331 void * memcpy(void * dest,const void *src,size_t count)
332 {
333 char *tmp = (char *) dest, *s = (char *) src;
335 while (count--)
336 *tmp++ = *s++;
338 return dest;
339 }
340 #endif
342 #ifndef __HAVE_ARCH_MEMMOVE
343 /**
344 * memmove - Copy one area of memory to another
345 * @dest: Where to copy to
346 * @src: Where to copy from
347 * @count: The size of the area.
348 *
349 * Unlike memcpy(), memmove() copes with overlapping areas.
350 */
351 void * memmove(void * dest,const void *src,size_t count)
352 {
353 char *tmp, *s;
355 if (dest <= src) {
356 tmp = (char *) dest;
357 s = (char *) src;
358 while (count--)
359 *tmp++ = *s++;
360 }
361 else {
362 tmp = (char *) dest + count;
363 s = (char *) src + count;
364 while (count--)
365 *--tmp = *--s;
366 }
368 return dest;
369 }
370 #endif
372 #ifndef __HAVE_ARCH_MEMCMP
373 /**
374 * memcmp - Compare two areas of memory
375 * @cs: One area of memory
376 * @ct: Another area of memory
377 * @count: The size of the area.
378 */
379 int memcmp(const void * cs,const void * ct,size_t count)
380 {
381 const unsigned char *su1, *su2;
382 int res = 0;
384 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
385 if ((res = *su1 - *su2) != 0)
386 break;
387 return res;
388 }
389 #endif
391 #ifndef __HAVE_ARCH_MEMSCAN
392 /**
393 * memscan - Find a character in an area of memory.
394 * @addr: The memory area
395 * @c: The byte to search for
396 * @size: The size of the area.
397 *
398 * returns the address of the first occurrence of @c, or 1 byte past
399 * the area if @c is not found
400 */
401 void * memscan(void * addr, int c, size_t size)
402 {
403 unsigned char * p = (unsigned char *) addr;
405 while (size) {
406 if (*p == c)
407 return (void *) p;
408 p++;
409 size--;
410 }
411 return (void *) p;
412 }
413 #endif
415 #ifndef __HAVE_ARCH_STRSTR
416 /**
417 * strstr - Find the first substring in a %NUL terminated string
418 * @s1: The string to be searched
419 * @s2: The string to search for
420 */
421 char * strstr(const char * s1,const char * s2)
422 {
423 int l1, l2;
425 l2 = strlen(s2);
426 if (!l2)
427 return (char *) s1;
428 l1 = strlen(s1);
429 while (l1 >= l2) {
430 l1--;
431 if (!memcmp(s1,s2,l2))
432 return (char *) s1;
433 s1++;
434 }
435 return NULL;
436 }
437 #endif
439 #ifndef __HAVE_ARCH_MEMCHR
440 /**
441 * memchr - Find a character in an area of memory.
442 * @s: The memory area
443 * @c: The byte to search for
444 * @n: The size of the area.
445 *
446 * returns the address of the first occurrence of @c, or %NULL
447 * if @c is not found
448 */
449 void *memchr(const void *s, int c, size_t n)
450 {
451 const unsigned char *p = s;
452 while (n-- != 0) {
453 if ((unsigned char)c == *p++) {
454 return (void *)(p-1);
455 }
456 }
457 return NULL;
458 }
460 #endif
462 /*
463 * Local variables:
464 * mode: C
465 * c-set-style: "BSD"
466 * c-basic-offset: 8
467 * tab-width: 8
468 * indent-tabs-mode: t
469 * End:
470 */