rev |
line source |
jws@381
|
1 /*
|
jws@381
|
2 * linux/lib/string.c
|
jws@381
|
3 *
|
jws@381
|
4 * Copyright (C) 1991, 1992 Linus Torvalds
|
jws@381
|
5 */
|
jws@381
|
6
|
kaf24@1248
|
7 #include <xen/types.h>
|
kaf24@1248
|
8 #include <xen/string.h>
|
kaf24@1248
|
9 #include <xen/ctype.h>
|
jws@381
|
10
|
jws@381
|
11 #ifndef __HAVE_ARCH_STRNICMP
|
jws@381
|
12 /**
|
jws@381
|
13 * strnicmp - Case insensitive, length-limited string comparison
|
jws@381
|
14 * @s1: One string
|
jws@381
|
15 * @s2: The other string
|
jws@381
|
16 * @len: the maximum number of characters to compare
|
jws@381
|
17 */
|
jws@381
|
18 int strnicmp(const char *s1, const char *s2, size_t len)
|
jws@381
|
19 {
|
jws@381
|
20 /* Yes, Virginia, it had better be unsigned */
|
jws@381
|
21 unsigned char c1, c2;
|
jws@381
|
22
|
jws@381
|
23 c1 = 0; c2 = 0;
|
jws@381
|
24 if (len) {
|
jws@381
|
25 do {
|
jws@381
|
26 c1 = *s1; c2 = *s2;
|
jws@381
|
27 s1++; s2++;
|
jws@381
|
28 if (!c1)
|
jws@381
|
29 break;
|
jws@381
|
30 if (!c2)
|
jws@381
|
31 break;
|
jws@381
|
32 if (c1 == c2)
|
jws@381
|
33 continue;
|
jws@381
|
34 c1 = tolower(c1);
|
jws@381
|
35 c2 = tolower(c2);
|
jws@381
|
36 if (c1 != c2)
|
jws@381
|
37 break;
|
jws@381
|
38 } while (--len);
|
jws@381
|
39 }
|
jws@381
|
40 return (int)c1 - (int)c2;
|
jws@381
|
41 }
|
jws@381
|
42 #endif
|
jws@381
|
43
|
kaf24@3475
|
44 #ifndef __HAVE_ARCH_STRLCPY
|
kaf24@3475
|
45 /**
|
kaf24@3475
|
46 * strlcpy - Copy a %NUL terminated string into a sized buffer
|
kaf24@3475
|
47 * @dest: Where to copy the string to
|
kaf24@3475
|
48 * @src: Where to copy the string from
|
kaf24@3475
|
49 * @size: size of destination buffer
|
kaf24@3475
|
50 *
|
kaf24@3475
|
51 * Compatible with *BSD: the result is always a valid
|
kaf24@3475
|
52 * NUL-terminated string that fits in the buffer (unless,
|
kaf24@3475
|
53 * of course, the buffer size is zero). It does not pad
|
kaf24@3475
|
54 * out the result like strncpy() does.
|
kaf24@3475
|
55 */
|
kaf24@3475
|
56 size_t strlcpy(char *dest, const char *src, size_t size)
|
kaf24@3475
|
57 {
|
kaf24@3475
|
58 size_t ret = strlen(src);
|
kaf24@3475
|
59
|
kaf24@3475
|
60 if (size) {
|
kaf24@3475
|
61 size_t len = (ret >= size) ? size-1 : ret;
|
kaf24@3475
|
62 memcpy(dest, src, len);
|
kaf24@3475
|
63 dest[len] = '\0';
|
kaf24@3475
|
64 }
|
kaf24@3475
|
65 return ret;
|
kaf24@3475
|
66 }
|
kaf24@3475
|
67 EXPORT_SYMBOL(strlcpy);
|
kaf24@3475
|
68 #endif
|
kaf24@3475
|
69
|
kfraser@13703
|
70 #ifndef __HAVE_ARCH_STRLCAT
|
jws@381
|
71 /**
|
kfraser@13703
|
72 * strlcat - Append a %NUL terminated string into a sized buffer
|
kfraser@13703
|
73 * @dest: Where to copy the string to
|
kfraser@13703
|
74 * @src: Where to copy the string from
|
kfraser@13703
|
75 * @size: size of destination buffer
|
jws@381
|
76 *
|
kfraser@13703
|
77 * Compatible with *BSD: the result is always a valid
|
kfraser@13703
|
78 * NUL-terminated string that fits in the buffer (unless,
|
kfraser@13703
|
79 * of course, the buffer size is zero).
|
jws@381
|
80 */
|
kfraser@13703
|
81 size_t strlcat(char *dest, const char *src, size_t size)
|
jws@381
|
82 {
|
kfraser@13703
|
83 size_t slen = strlen(src);
|
kfraser@13703
|
84 size_t dlen = strnlen(dest, size);
|
kfraser@13703
|
85 char *p = dest + dlen;
|
jws@381
|
86
|
kfraser@13703
|
87 while ((p - dest) < size)
|
kfraser@13703
|
88 if ((*p++ = *src++) == '\0')
|
kfraser@13703
|
89 break;
|
jws@381
|
90
|
kfraser@13703
|
91 if (dlen < size)
|
kfraser@13703
|
92 *(p-1) = '\0';
|
kfraser@13703
|
93
|
kfraser@13703
|
94 return slen + dlen;
|
jws@381
|
95 }
|
kfraser@13703
|
96 EXPORT_SYMBOL(strlcat);
|
jws@381
|
97 #endif
|
jws@381
|
98
|
jws@381
|
99 #ifndef __HAVE_ARCH_STRCMP
|
jws@381
|
100 /**
|
jws@381
|
101 * strcmp - Compare two strings
|
jws@381
|
102 * @cs: One string
|
jws@381
|
103 * @ct: Another string
|
jws@381
|
104 */
|
jws@381
|
105 int strcmp(const char * cs,const char * ct)
|
jws@381
|
106 {
|
jws@381
|
107 register signed char __res;
|
jws@381
|
108
|
jws@381
|
109 while (1) {
|
jws@381
|
110 if ((__res = *cs - *ct++) != 0 || !*cs++)
|
jws@381
|
111 break;
|
jws@381
|
112 }
|
jws@381
|
113
|
jws@381
|
114 return __res;
|
jws@381
|
115 }
|
jws@381
|
116 #endif
|
jws@381
|
117
|
jws@381
|
118 #ifndef __HAVE_ARCH_STRNCMP
|
jws@381
|
119 /**
|
jws@381
|
120 * strncmp - Compare two length-limited strings
|
jws@381
|
121 * @cs: One string
|
jws@381
|
122 * @ct: Another string
|
jws@381
|
123 * @count: The maximum number of bytes to compare
|
jws@381
|
124 */
|
jws@381
|
125 int strncmp(const char * cs,const char * ct,size_t count)
|
jws@381
|
126 {
|
jws@381
|
127 register signed char __res = 0;
|
jws@381
|
128
|
jws@381
|
129 while (count) {
|
jws@381
|
130 if ((__res = *cs - *ct++) != 0 || !*cs++)
|
jws@381
|
131 break;
|
jws@381
|
132 count--;
|
jws@381
|
133 }
|
jws@381
|
134
|
jws@381
|
135 return __res;
|
jws@381
|
136 }
|
jws@381
|
137 #endif
|
jws@381
|
138
|
jws@381
|
139 #ifndef __HAVE_ARCH_STRCHR
|
jws@381
|
140 /**
|
jws@381
|
141 * strchr - Find the first occurrence of a character in a string
|
jws@381
|
142 * @s: The string to be searched
|
jws@381
|
143 * @c: The character to search for
|
jws@381
|
144 */
|
jws@381
|
145 char * strchr(const char * s, int c)
|
jws@381
|
146 {
|
jws@381
|
147 for(; *s != (char) c; ++s)
|
jws@381
|
148 if (*s == '\0')
|
jws@381
|
149 return NULL;
|
jws@381
|
150 return (char *) s;
|
jws@381
|
151 }
|
jws@381
|
152 #endif
|
jws@381
|
153
|
jws@381
|
154 #ifndef __HAVE_ARCH_STRRCHR
|
jws@381
|
155 /**
|
jws@381
|
156 * strrchr - Find the last occurrence of a character in a string
|
jws@381
|
157 * @s: The string to be searched
|
jws@381
|
158 * @c: The character to search for
|
jws@381
|
159 */
|
jws@381
|
160 char * strrchr(const char * s, int c)
|
jws@381
|
161 {
|
jws@381
|
162 const char *p = s + strlen(s);
|
jws@381
|
163 do {
|
jws@381
|
164 if (*p == (char)c)
|
jws@381
|
165 return (char *)p;
|
jws@381
|
166 } while (--p >= s);
|
jws@381
|
167 return NULL;
|
jws@381
|
168 }
|
jws@381
|
169 #endif
|
jws@381
|
170
|
jws@381
|
171 #ifndef __HAVE_ARCH_STRLEN
|
jws@381
|
172 /**
|
jws@381
|
173 * strlen - Find the length of a string
|
jws@381
|
174 * @s: The string to be sized
|
jws@381
|
175 */
|
jws@381
|
176 size_t strlen(const char * s)
|
jws@381
|
177 {
|
jws@381
|
178 const char *sc;
|
jws@381
|
179
|
jws@381
|
180 for (sc = s; *sc != '\0'; ++sc)
|
jws@381
|
181 /* nothing */;
|
jws@381
|
182 return sc - s;
|
jws@381
|
183 }
|
jws@381
|
184 #endif
|
jws@381
|
185
|
jws@381
|
186 #ifndef __HAVE_ARCH_STRNLEN
|
jws@381
|
187 /**
|
jws@381
|
188 * strnlen - Find the length of a length-limited string
|
jws@381
|
189 * @s: The string to be sized
|
jws@381
|
190 * @count: The maximum number of bytes to search
|
jws@381
|
191 */
|
jws@381
|
192 size_t strnlen(const char * s, size_t count)
|
jws@381
|
193 {
|
jws@381
|
194 const char *sc;
|
jws@381
|
195
|
jws@381
|
196 for (sc = s; count-- && *sc != '\0'; ++sc)
|
jws@381
|
197 /* nothing */;
|
jws@381
|
198 return sc - s;
|
jws@381
|
199 }
|
jws@381
|
200 #endif
|
jws@381
|
201
|
jws@381
|
202 #ifndef __HAVE_ARCH_STRSPN
|
jws@381
|
203 /**
|
jws@381
|
204 * strspn - Calculate the length of the initial substring of @s which only
|
jws@381
|
205 * contain letters in @accept
|
jws@381
|
206 * @s: The string to be searched
|
jws@381
|
207 * @accept: The string to search for
|
jws@381
|
208 */
|
jws@381
|
209 size_t strspn(const char *s, const char *accept)
|
jws@381
|
210 {
|
jws@381
|
211 const char *p;
|
jws@381
|
212 const char *a;
|
jws@381
|
213 size_t count = 0;
|
jws@381
|
214
|
jws@381
|
215 for (p = s; *p != '\0'; ++p) {
|
jws@381
|
216 for (a = accept; *a != '\0'; ++a) {
|
jws@381
|
217 if (*p == *a)
|
jws@381
|
218 break;
|
jws@381
|
219 }
|
jws@381
|
220 if (*a == '\0')
|
jws@381
|
221 return count;
|
jws@381
|
222 ++count;
|
jws@381
|
223 }
|
jws@381
|
224
|
jws@381
|
225 return count;
|
jws@381
|
226 }
|
jws@381
|
227 #endif
|
jws@381
|
228
|
jws@381
|
229 #ifndef __HAVE_ARCH_STRPBRK
|
jws@381
|
230 /**
|
jws@381
|
231 * strpbrk - Find the first occurrence of a set of characters
|
jws@381
|
232 * @cs: The string to be searched
|
jws@381
|
233 * @ct: The characters to search for
|
jws@381
|
234 */
|
jws@381
|
235 char * strpbrk(const char * cs,const char * ct)
|
jws@381
|
236 {
|
jws@381
|
237 const char *sc1,*sc2;
|
jws@381
|
238
|
jws@381
|
239 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
|
jws@381
|
240 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
|
jws@381
|
241 if (*sc1 == *sc2)
|
jws@381
|
242 return (char *) sc1;
|
jws@381
|
243 }
|
jws@381
|
244 }
|
jws@381
|
245 return NULL;
|
jws@381
|
246 }
|
jws@381
|
247 #endif
|
jws@381
|
248
|
jws@381
|
249 #ifndef __HAVE_ARCH_STRSEP
|
jws@381
|
250 /**
|
jws@381
|
251 * strsep - Split a string into tokens
|
jws@381
|
252 * @s: The string to be searched
|
jws@381
|
253 * @ct: The characters to search for
|
jws@381
|
254 *
|
jws@381
|
255 * strsep() updates @s to point after the token, ready for the next call.
|
jws@381
|
256 *
|
jws@381
|
257 * It returns empty tokens, too, behaving exactly like the libc function
|
jws@381
|
258 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
|
jws@381
|
259 * Same semantics, slimmer shape. ;)
|
jws@381
|
260 */
|
jws@381
|
261 char * strsep(char **s, const char *ct)
|
jws@381
|
262 {
|
jws@381
|
263 char *sbegin = *s, *end;
|
jws@381
|
264
|
jws@381
|
265 if (sbegin == NULL)
|
jws@381
|
266 return NULL;
|
jws@381
|
267
|
jws@381
|
268 end = strpbrk(sbegin, ct);
|
jws@381
|
269 if (end)
|
jws@381
|
270 *end++ = '\0';
|
jws@381
|
271 *s = end;
|
jws@381
|
272
|
jws@381
|
273 return sbegin;
|
jws@381
|
274 }
|
jws@381
|
275 #endif
|
jws@381
|
276
|
jws@381
|
277 #ifndef __HAVE_ARCH_MEMSET
|
jws@381
|
278 /**
|
jws@381
|
279 * memset - Fill a region of memory with the given value
|
jws@381
|
280 * @s: Pointer to the start of the area.
|
jws@381
|
281 * @c: The byte to fill the area with
|
jws@381
|
282 * @count: The size of the area.
|
jws@381
|
283 *
|
jws@381
|
284 * Do not use memset() to access IO space, use memset_io() instead.
|
jws@381
|
285 */
|
jws@381
|
286 void * memset(void * s,int c,size_t count)
|
jws@381
|
287 {
|
jws@381
|
288 char *xs = (char *) s;
|
jws@381
|
289
|
jws@381
|
290 while (count--)
|
jws@381
|
291 *xs++ = c;
|
jws@381
|
292
|
jws@381
|
293 return s;
|
jws@381
|
294 }
|
jws@381
|
295 #endif
|
jws@381
|
296
|
kaf24@3466
|
297 #ifndef __HAVE_ARCH_MEMCPY
|
jws@381
|
298 /**
|
jws@381
|
299 * memcpy - Copy one area of memory to another
|
jws@381
|
300 * @dest: Where to copy to
|
jws@381
|
301 * @src: Where to copy from
|
jws@381
|
302 * @count: The size of the area.
|
jws@381
|
303 *
|
jws@381
|
304 * You should not use this function to access IO space, use memcpy_toio()
|
jws@381
|
305 * or memcpy_fromio() instead.
|
jws@381
|
306 */
|
jws@381
|
307 void * memcpy(void * dest,const void *src,size_t count)
|
jws@381
|
308 {
|
jws@381
|
309 char *tmp = (char *) dest, *s = (char *) src;
|
jws@381
|
310
|
jws@381
|
311 while (count--)
|
jws@381
|
312 *tmp++ = *s++;
|
jws@381
|
313
|
jws@381
|
314 return dest;
|
jws@381
|
315 }
|
kaf24@3466
|
316 #endif
|
jws@381
|
317
|
jws@381
|
318 #ifndef __HAVE_ARCH_MEMMOVE
|
jws@381
|
319 /**
|
jws@381
|
320 * memmove - Copy one area of memory to another
|
jws@381
|
321 * @dest: Where to copy to
|
jws@381
|
322 * @src: Where to copy from
|
jws@381
|
323 * @count: The size of the area.
|
jws@381
|
324 *
|
jws@381
|
325 * Unlike memcpy(), memmove() copes with overlapping areas.
|
jws@381
|
326 */
|
jws@381
|
327 void * memmove(void * dest,const void *src,size_t count)
|
jws@381
|
328 {
|
jws@381
|
329 char *tmp, *s;
|
jws@381
|
330
|
jws@381
|
331 if (dest <= src) {
|
jws@381
|
332 tmp = (char *) dest;
|
jws@381
|
333 s = (char *) src;
|
jws@381
|
334 while (count--)
|
jws@381
|
335 *tmp++ = *s++;
|
jws@381
|
336 }
|
jws@381
|
337 else {
|
jws@381
|
338 tmp = (char *) dest + count;
|
jws@381
|
339 s = (char *) src + count;
|
jws@381
|
340 while (count--)
|
jws@381
|
341 *--tmp = *--s;
|
jws@381
|
342 }
|
jws@381
|
343
|
jws@381
|
344 return dest;
|
jws@381
|
345 }
|
jws@381
|
346 #endif
|
jws@381
|
347
|
jws@381
|
348 #ifndef __HAVE_ARCH_MEMCMP
|
jws@381
|
349 /**
|
jws@381
|
350 * memcmp - Compare two areas of memory
|
jws@381
|
351 * @cs: One area of memory
|
jws@381
|
352 * @ct: Another area of memory
|
jws@381
|
353 * @count: The size of the area.
|
jws@381
|
354 */
|
jws@381
|
355 int memcmp(const void * cs,const void * ct,size_t count)
|
jws@381
|
356 {
|
jws@381
|
357 const unsigned char *su1, *su2;
|
jws@381
|
358 int res = 0;
|
jws@381
|
359
|
jws@381
|
360 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
|
jws@381
|
361 if ((res = *su1 - *su2) != 0)
|
jws@381
|
362 break;
|
jws@381
|
363 return res;
|
jws@381
|
364 }
|
jws@381
|
365 #endif
|
jws@381
|
366
|
jws@381
|
367 #ifndef __HAVE_ARCH_MEMSCAN
|
jws@381
|
368 /**
|
jws@381
|
369 * memscan - Find a character in an area of memory.
|
jws@381
|
370 * @addr: The memory area
|
jws@381
|
371 * @c: The byte to search for
|
jws@381
|
372 * @size: The size of the area.
|
jws@381
|
373 *
|
jws@381
|
374 * returns the address of the first occurrence of @c, or 1 byte past
|
jws@381
|
375 * the area if @c is not found
|
jws@381
|
376 */
|
jws@381
|
377 void * memscan(void * addr, int c, size_t size)
|
jws@381
|
378 {
|
jws@381
|
379 unsigned char * p = (unsigned char *) addr;
|
jws@381
|
380
|
jws@381
|
381 while (size) {
|
jws@381
|
382 if (*p == c)
|
jws@381
|
383 return (void *) p;
|
jws@381
|
384 p++;
|
jws@381
|
385 size--;
|
jws@381
|
386 }
|
jws@381
|
387 return (void *) p;
|
jws@381
|
388 }
|
jws@381
|
389 #endif
|
jws@381
|
390
|
jws@381
|
391 #ifndef __HAVE_ARCH_STRSTR
|
jws@381
|
392 /**
|
jws@381
|
393 * strstr - Find the first substring in a %NUL terminated string
|
jws@381
|
394 * @s1: The string to be searched
|
jws@381
|
395 * @s2: The string to search for
|
jws@381
|
396 */
|
jws@381
|
397 char * strstr(const char * s1,const char * s2)
|
jws@381
|
398 {
|
jws@381
|
399 int l1, l2;
|
jws@381
|
400
|
jws@381
|
401 l2 = strlen(s2);
|
jws@381
|
402 if (!l2)
|
jws@381
|
403 return (char *) s1;
|
jws@381
|
404 l1 = strlen(s1);
|
jws@381
|
405 while (l1 >= l2) {
|
jws@381
|
406 l1--;
|
jws@381
|
407 if (!memcmp(s1,s2,l2))
|
jws@381
|
408 return (char *) s1;
|
jws@381
|
409 s1++;
|
jws@381
|
410 }
|
jws@381
|
411 return NULL;
|
jws@381
|
412 }
|
jws@381
|
413 #endif
|
jws@381
|
414
|
jws@381
|
415 #ifndef __HAVE_ARCH_MEMCHR
|
jws@381
|
416 /**
|
jws@381
|
417 * memchr - Find a character in an area of memory.
|
jws@381
|
418 * @s: The memory area
|
jws@381
|
419 * @c: The byte to search for
|
jws@381
|
420 * @n: The size of the area.
|
jws@381
|
421 *
|
jws@381
|
422 * returns the address of the first occurrence of @c, or %NULL
|
jws@381
|
423 * if @c is not found
|
jws@381
|
424 */
|
jws@381
|
425 void *memchr(const void *s, int c, size_t n)
|
jws@381
|
426 {
|
jws@381
|
427 const unsigned char *p = s;
|
jws@381
|
428 while (n-- != 0) {
|
jws@381
|
429 if ((unsigned char)c == *p++) {
|
jws@381
|
430 return (void *)(p-1);
|
jws@381
|
431 }
|
jws@381
|
432 }
|
jws@381
|
433 return NULL;
|
jws@381
|
434 }
|
jws@381
|
435
|
jws@381
|
436 #endif
|
kaf24@3952
|
437
|
kaf24@3952
|
438 /*
|
kaf24@3952
|
439 * Local variables:
|
kaf24@3952
|
440 * mode: C
|
kaf24@3952
|
441 * c-set-style: "BSD"
|
kaf24@3952
|
442 * c-basic-offset: 8
|
kaf24@3952
|
443 * tab-width: 8
|
kaf24@3952
|
444 * indent-tabs-mode: t
|
kaf24@4026
|
445 * End:
|
kaf24@3952
|
446 */
|