Adding debian version 3.70~pre8+dfsg-1.
[syslinux-debian/hramrach.git] / gpxe / src / core / string.c
blob2e17bdcb013eed2ec8333fce3fbb531a41ea6768
1 /*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2004 Tobias Lorenz
5 * string handling functions
6 * based on linux/lib/string.c
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * stupid library routines.. The optimized versions should generally be found
15 * as inline code in <asm-xx/string.h>
17 * These are buggy as well..
19 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
20 * - Added strsep() which will replace strtok() soon (because strsep() is
21 * reentrant and should be faster). Use only strsep() in new code, please.
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
29 /* *** FROM string.c *** */
31 #ifndef __HAVE_ARCH_STRCPY
32 /**
33 * strcpy - Copy a %NUL terminated string
34 * @dest: Where to copy the string to
35 * @src: Where to copy the string from
37 char * strcpy(char * dest,const char *src)
39 char *tmp = dest;
41 while ((*dest++ = *src++) != '\0')
42 /* nothing */;
43 return tmp;
45 #endif
47 #ifndef __HAVE_ARCH_STRNCPY
48 /**
49 * strncpy - Copy a length-limited, %NUL-terminated string
50 * @dest: Where to copy the string to
51 * @src: Where to copy the string from
52 * @count: The maximum number of bytes to copy
54 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
55 * However, the result is not %NUL-terminated if the source exceeds
56 * @count bytes.
58 char * strncpy(char * dest,const char *src,size_t count)
60 char *tmp = dest;
62 while (count-- && (*dest++ = *src++) != '\0')
63 /* nothing */;
65 return tmp;
67 #endif
69 #ifndef __HAVE_ARCH_STRCAT
70 /**
71 * strcat - Append one %NUL-terminated string to another
72 * @dest: The string to be appended to
73 * @src: The string to append to it
75 char * strcat(char * dest, const char * src)
77 char *tmp = dest;
79 while (*dest)
80 dest++;
81 while ((*dest++ = *src++) != '\0')
84 return tmp;
86 #endif
88 #ifndef __HAVE_ARCH_STRCMP
89 /**
90 * strcmp - Compare two strings
91 * @cs: One string
92 * @ct: Another string
94 int strcmp(const char * cs,const char * ct)
96 register signed char __res;
98 while (1) {
99 if ((__res = *cs - *ct++) != 0 || !*cs++)
100 break;
103 return __res;
105 #endif
107 #ifndef __HAVE_ARCH_STRNCMP
109 * strncmp - Compare two length-limited strings
110 * @cs: One string
111 * @ct: Another string
112 * @count: The maximum number of bytes to compare
114 int strncmp(const char * cs,const char * ct,size_t count)
116 register signed char __res = 0;
118 while (count) {
119 if ((__res = *cs - *ct++) != 0 || !*cs++)
120 break;
121 count--;
124 return __res;
126 #endif
128 #ifndef __HAVE_ARCH_STRCASECMP
129 int strcasecmp(const char *a, const char *b)
131 while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
132 return((*a & ~0x20) - (*b & ~0x20));
134 #endif
136 #ifndef __HAVE_ARCH_STRCHR
138 * strchr - Find the first occurrence of a character in a string
139 * @s: The string to be searched
140 * @c: The character to search for
142 char * strchr(const char * s, int c)
144 for(; *s != (char) c; ++s)
145 if (*s == '\0')
146 return NULL;
147 return (char *) s;
149 #endif
151 #ifndef __HAVE_ARCH_STRRCHR
153 * strrchr - Find the last occurrence of a character in a string
154 * @s: The string to be searched
155 * @c: The character to search for
157 char * strrchr(const char * s, int c)
159 const char *p = s + strlen(s);
160 do {
161 if (*p == (char)c)
162 return (char *)p;
163 } while (--p >= s);
164 return NULL;
166 #endif
168 #ifndef __HAVE_ARCH_STRLEN
170 * strlen - Find the length of a string
171 * @s: The string to be sized
173 size_t strlen(const char * s)
175 const char *sc;
177 for (sc = s; *sc != '\0'; ++sc)
178 /* nothing */;
179 return sc - s;
181 #endif
183 #ifndef __HAVE_ARCH_STRNLEN
185 * strnlen - Find the length of a length-limited string
186 * @s: The string to be sized
187 * @count: The maximum number of bytes to search
189 size_t strnlen(const char * s, size_t count)
191 const char *sc;
193 for (sc = s; count-- && *sc != '\0'; ++sc)
194 /* nothing */;
195 return sc - s;
197 #endif
199 #ifndef __HAVE_ARCH_MEMSET
201 * memset - Fill a region of memory with the given value
202 * @s: Pointer to the start of the area.
203 * @c: The byte to fill the area with
204 * @count: The size of the area.
206 * Do not use memset() to access IO space, use memset_io() instead.
208 void * memset(void * s,int c,size_t count)
210 char *xs = (char *) s;
212 while (count--)
213 *xs++ = c;
215 return s;
217 #endif
219 #ifndef __HAVE_ARCH_MEMCPY
221 * memcpy - Copy one area of memory to another
222 * @dest: Where to copy to
223 * @src: Where to copy from
224 * @count: The size of the area.
226 * You should not use this function to access IO space, use memcpy_toio()
227 * or memcpy_fromio() instead.
229 void * memcpy(void * dest,const void *src,size_t count)
231 char *tmp = (char *) dest, *s = (char *) src;
233 while (count--)
234 *tmp++ = *s++;
236 return dest;
238 #endif
240 #ifndef __HAVE_ARCH_MEMMOVE
242 * memmove - Copy one area of memory to another
243 * @dest: Where to copy to
244 * @src: Where to copy from
245 * @count: The size of the area.
247 * Unlike memcpy(), memmove() copes with overlapping areas.
249 void * memmove(void * dest,const void *src,size_t count)
251 char *tmp, *s;
253 if (dest <= src) {
254 tmp = (char *) dest;
255 s = (char *) src;
256 while (count--)
257 *tmp++ = *s++;
259 else {
260 tmp = (char *) dest + count;
261 s = (char *) src + count;
262 while (count--)
263 *--tmp = *--s;
266 return dest;
268 #endif
270 #ifndef __HAVE_ARCH_MEMCMP
272 * memcmp - Compare two areas of memory
273 * @cs: One area of memory
274 * @ct: Another area of memory
275 * @count: The size of the area.
277 int memcmp(const void * cs,const void * ct,size_t count)
279 const unsigned char *su1, *su2;
280 int res = 0;
282 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
283 if ((res = *su1 - *su2) != 0)
284 break;
285 return res;
287 #endif
289 #ifndef __HAVE_ARCH_STRSTR
291 * strstr - Find the first substring in a %NUL terminated string
292 * @s1: The string to be searched
293 * @s2: The string to search for
295 char * strstr(const char * s1,const char * s2)
297 int l1, l2;
299 l2 = strlen(s2);
300 if (!l2)
301 return (char *) s1;
302 l1 = strlen(s1);
303 while (l1 >= l2) {
304 l1--;
305 if (!memcmp(s1,s2,l2))
306 return (char *) s1;
307 s1++;
309 return NULL;
311 #endif
313 #ifndef __HAVE_ARCH_MEMCHR
315 * memchr - Find a character in an area of memory.
316 * @s: The memory area
317 * @c: The byte to search for
318 * @n: The size of the area.
320 * returns the address of the first occurrence of @c, or %NULL
321 * if @c is not found
323 void * memchr(const void *s, int c, size_t n)
325 const unsigned char *p = s;
326 while (n-- != 0) {
327 if ((unsigned char)c == *p++) {
328 return (void *)(p-1);
331 return NULL;
334 #endif
336 char * strndup(const char *s, size_t n)
338 size_t len = strlen(s);
339 char *new;
341 if (len>n)
342 len = n;
343 new = malloc(len+1);
344 if (new) {
345 new[len] = '\0';
346 memcpy(new,s,len);
348 return new;
351 char * strdup(const char *s) {
352 return strndup(s, ~((size_t)0));