ia64/xen-unstable

view xen/common/string.c @ 19835:edfdeb150f27

Fix buildsystem to detect udev > version 124

udev removed the udevinfo symlink from versions higher than 123 and
xen's build-system could not detect if udev is in place and has the
required version.

Signed-off-by: Marc-A. Dahlhaus <mad@wol.de>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 25 13:02:37 2009 +0100 (2009-06-25)
parents 9fd36167ecfa
children
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_MEMCPY
298 /**
299 * memcpy - Copy one area of memory to another
300 * @dest: Where to copy to
301 * @src: Where to copy from
302 * @count: The size of the area.
303 *
304 * You should not use this function to access IO space, use memcpy_toio()
305 * or memcpy_fromio() instead.
306 */
307 void * memcpy(void * dest,const void *src,size_t count)
308 {
309 char *tmp = (char *) dest, *s = (char *) src;
311 while (count--)
312 *tmp++ = *s++;
314 return dest;
315 }
316 #endif
318 #ifndef __HAVE_ARCH_MEMMOVE
319 /**
320 * memmove - Copy one area of memory to another
321 * @dest: Where to copy to
322 * @src: Where to copy from
323 * @count: The size of the area.
324 *
325 * Unlike memcpy(), memmove() copes with overlapping areas.
326 */
327 void * memmove(void * dest,const void *src,size_t count)
328 {
329 char *tmp, *s;
331 if (dest <= src) {
332 tmp = (char *) dest;
333 s = (char *) src;
334 while (count--)
335 *tmp++ = *s++;
336 }
337 else {
338 tmp = (char *) dest + count;
339 s = (char *) src + count;
340 while (count--)
341 *--tmp = *--s;
342 }
344 return dest;
345 }
346 #endif
348 #ifndef __HAVE_ARCH_MEMCMP
349 /**
350 * memcmp - Compare two areas of memory
351 * @cs: One area of memory
352 * @ct: Another area of memory
353 * @count: The size of the area.
354 */
355 int memcmp(const void * cs,const void * ct,size_t count)
356 {
357 const unsigned char *su1, *su2;
358 int res = 0;
360 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
361 if ((res = *su1 - *su2) != 0)
362 break;
363 return res;
364 }
365 #endif
367 #ifndef __HAVE_ARCH_MEMSCAN
368 /**
369 * memscan - Find a character in an area of memory.
370 * @addr: The memory area
371 * @c: The byte to search for
372 * @size: The size of the area.
373 *
374 * returns the address of the first occurrence of @c, or 1 byte past
375 * the area if @c is not found
376 */
377 void * memscan(void * addr, int c, size_t size)
378 {
379 unsigned char * p = (unsigned char *) addr;
381 while (size) {
382 if (*p == c)
383 return (void *) p;
384 p++;
385 size--;
386 }
387 return (void *) p;
388 }
389 #endif
391 #ifndef __HAVE_ARCH_STRSTR
392 /**
393 * strstr - Find the first substring in a %NUL terminated string
394 * @s1: The string to be searched
395 * @s2: The string to search for
396 */
397 char * strstr(const char * s1,const char * s2)
398 {
399 int l1, l2;
401 l2 = strlen(s2);
402 if (!l2)
403 return (char *) s1;
404 l1 = strlen(s1);
405 while (l1 >= l2) {
406 l1--;
407 if (!memcmp(s1,s2,l2))
408 return (char *) s1;
409 s1++;
410 }
411 return NULL;
412 }
413 #endif
415 #ifndef __HAVE_ARCH_MEMCHR
416 /**
417 * memchr - Find a character in an area of memory.
418 * @s: The memory area
419 * @c: The byte to search for
420 * @n: The size of the area.
421 *
422 * returns the address of the first occurrence of @c, or %NULL
423 * if @c is not found
424 */
425 void *memchr(const void *s, int c, size_t n)
426 {
427 const unsigned char *p = s;
428 while (n-- != 0) {
429 if ((unsigned char)c == *p++) {
430 return (void *)(p-1);
431 }
432 }
433 return NULL;
434 }
436 #endif
438 /*
439 * Local variables:
440 * mode: C
441 * c-set-style: "BSD"
442 * c-basic-offset: 8
443 * tab-width: 8
444 * indent-tabs-mode: t
445 * End:
446 */