ppc64: Don't set Kp bit on SLB
[openbios/afaerber.git] / libc / string.c
blob8f62bd7bb6d72226d44f72b386cb7b3e1ca51327
1 /*
2 * linux/lib/string.c
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>
11 * These are buggy as well..
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.
18 #include "config.h"
19 #include "libc/string.h"
20 #include "libc/stdlib.h"
22 /**
23 * strnicmp - Case insensitive, length-limited string comparison
24 * @s1: One string
25 * @s2: The other string
26 * @len: the maximum number of characters to compare
28 int strnicmp(const char *s1, const char *s2, size_t len)
30 /* Yes, Virginia, it had better be unsigned */
31 unsigned char c1, c2;
33 c1 = 0; c2 = 0;
34 if (len) {
35 do {
36 c1 = *s1; c2 = *s2;
37 s1++; s2++;
38 if (!c1)
39 break;
40 if (!c2)
41 break;
42 if (c1 == c2)
43 continue;
44 c1 = tolower(c1);
45 c2 = tolower(c2);
46 if (c1 != c2)
47 break;
48 } while (--len);
50 return (int)c1 - (int)c2;
53 /**
54 * strcpy - Copy a %NUL terminated string
55 * @dest: Where to copy the string to
56 * @src: Where to copy the string from
58 char * strcpy(char * dest,const char *src)
60 char *tmp = dest;
62 while ((*dest++ = *src++) != '\0')
63 /* nothing */;
64 return tmp;
67 /**
68 * strncpy - Copy a length-limited, %NUL-terminated string
69 * @dest: Where to copy the string to
70 * @src: Where to copy the string from
71 * @count: The maximum number of bytes to copy
73 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
74 * However, the result is not %NUL-terminated if the source exceeds
75 * @count bytes.
77 char * strncpy(char * dest,const char *src,size_t count)
79 char *tmp = dest;
81 while (count-- && (*dest++ = *src++) != '\0')
82 /* nothing */;
84 return tmp;
87 /**
88 * strcat - Append one %NUL-terminated string to another
89 * @dest: The string to be appended to
90 * @src: The string to append to it
92 char * strcat(char * dest, const char * src)
94 char *tmp = dest;
96 while (*dest)
97 dest++;
98 while ((*dest++ = *src++) != '\0')
101 return tmp;
105 * strncat - Append a length-limited, %NUL-terminated string to another
106 * @dest: The string to be appended to
107 * @src: The string to append to it
108 * @count: The maximum numbers of bytes to copy
110 * Note that in contrast to strncpy, strncat ensures the result is
111 * terminated.
113 char * strncat(char *dest, const char *src, size_t count)
115 char *tmp = dest;
117 if (count) {
118 while (*dest)
119 dest++;
120 while ((*dest++ = *src++)) {
121 if (--count == 0) {
122 *dest = '\0';
123 break;
128 return tmp;
132 * strcmp - Compare two strings
133 * @cs: One string
134 * @ct: Another string
136 int strcmp(const char * cs,const char * ct)
138 register signed char __res;
140 while (1) {
141 if ((__res = *cs - *ct++) != 0 || !*cs++)
142 break;
145 return __res;
149 * strncmp - Compare two length-limited strings
150 * @cs: One string
151 * @ct: Another string
152 * @count: The maximum number of bytes to compare
154 int strncmp(const char * cs,const char * ct,size_t count)
156 register signed char __res = 0;
158 while (count) {
159 if ((__res = *cs - *ct++) != 0 || !*cs++)
160 break;
161 count--;
164 return __res;
169 * strchr - Find the first occurrence of a character in a string
170 * @s: The string to be searched
171 * @c: The character to search for
173 char * strchr(const char * s, int c)
175 for(; *s != (char) c; ++s)
176 if (*s == '\0')
177 return NULL;
178 return (char *) s;
182 * strrchr - Find the last occurrence of a character in a string
183 * @s: The string to be searched
184 * @c: The character to search for
186 char * strrchr(const char * s, int c)
188 const char *p = s + strlen(s);
189 do {
190 if (*p == (char)c)
191 return (char *)p;
192 } while (--p >= s);
193 return NULL;
197 * strlen - Find the length of a string
198 * @s: The string to be sized
200 size_t strlen(const char * s)
202 const char *sc;
204 for (sc = s; *sc != '\0'; ++sc)
205 /* nothing */;
206 return sc - s;
210 * strnlen - Find the length of a length-limited string
211 * @s: The string to be sized
212 * @count: The maximum number of bytes to search
214 size_t strnlen(const char * s, size_t count)
216 const char *sc;
218 for (sc = s; count-- && *sc != '\0'; ++sc)
219 /* nothing */;
220 return sc - s;
224 * strpbrk - Find the first occurrence of a set of characters
225 * @cs: The string to be searched
226 * @ct: The characters to search for
228 char * strpbrk(const char * cs,const char * ct)
230 const char *sc1,*sc2;
232 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
233 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
234 if (*sc1 == *sc2)
235 return (char *) sc1;
238 return NULL;
242 * strsep - Split a string into tokens
243 * @s: The string to be searched
244 * @ct: The characters to search for
246 * strsep() updates @s to point after the token, ready for the next call.
248 * It returns empty tokens, too, behaving exactly like the libc function
249 * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
250 * Same semantics, slimmer shape. ;)
252 char * strsep(char **s, const char *ct)
254 char *sbegin = *s, *end;
256 if (sbegin == NULL)
257 return NULL;
259 end = strpbrk(sbegin, ct);
260 if (end)
261 *end++ = '\0';
262 *s = end;
264 return sbegin;
268 * memset - Fill a region of memory with the given value
269 * @s: Pointer to the start of the area.
270 * @c: The byte to fill the area with
271 * @count: The size of the area.
273 * Do not use memset() to access IO space, use memset_io() instead.
275 void * memset(void * s,int c,size_t count)
277 char *xs = (char *) s;
279 while (count--)
280 *xs++ = c;
282 return s;
286 * memcpy - Copy one area of memory to another
287 * @dest: Where to copy to
288 * @src: Where to copy from
289 * @count: The size of the area.
291 * You should not use this function to access IO space, use memcpy_toio()
292 * or memcpy_fromio() instead.
294 void * memcpy(void * dest,const void *src,size_t count)
296 char *tmp = (char *) dest, *s = (char *) src;
298 while (count--)
299 *tmp++ = *s++;
301 return dest;
305 * memmove - Copy one area of memory to another
306 * @dest: Where to copy to
307 * @src: Where to copy from
308 * @count: The size of the area.
310 * Unlike memcpy(), memmove() copes with overlapping areas.
312 void * memmove(void * dest,const void *src,size_t count)
314 char *tmp, *s;
316 if (dest <= src) {
317 tmp = (char *) dest;
318 s = (char *) src;
319 while (count--)
320 *tmp++ = *s++;
322 else {
323 tmp = (char *) dest + count;
324 s = (char *) src + count;
325 while (count--)
326 *--tmp = *--s;
329 return dest;
333 * memcmp - Compare two areas of memory
334 * @cs: One area of memory
335 * @ct: Another area of memory
336 * @count: The size of the area.
338 int memcmp(const void * cs,const void * ct,size_t count)
340 const unsigned char *su1, *su2;
341 int res = 0;
343 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
344 if ((res = *su1 - *su2) != 0)
345 break;
346 return res;
349 char *
350 strdup( const char *str )
352 char *p;
353 if( !str )
354 return NULL;
355 p = malloc( strlen(str) + 1 );
356 strcpy( p, str );
357 return p;
361 strcasecmp( const char *cs, const char *ct )
363 register signed char __res;
365 while (1) {
366 char ch1 = toupper(*cs), ch2 = toupper(*ct);
367 ct++;
368 if ((__res = ch1 - ch2) != 0 || !*cs++)
369 break;
371 return __res;
375 strncasecmp( const char *cs, const char *ct, size_t count )
377 register signed char __res = 0;
379 while (count--) {
380 char ch1 = toupper(*cs), ch2 = toupper(*ct);
381 ct++;
382 if ((__res = ch1 - ch2) != 0 || !*cs++)
383 break;
385 return __res;