[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / string.c
blob190007a47a4daa0eb0bcdcd7ea4bd7dfddbabfe9
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.
13 FILE_LICENCE ( GPL2_ONLY );
16 * stupid library routines.. The optimized versions should generally be found
17 * as inline code in <asm-xx/string.h>
19 * These are buggy as well..
21 * * Fri Jun 25 1999, Ingo Oeser <ioe@informatik.tu-chemnitz.de>
22 * - Added strsep() which will replace strtok() soon (because strsep() is
23 * reentrant and should be faster). Use only strsep() in new code, please.
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
31 /* *** FROM string.c *** */
33 #ifndef __HAVE_ARCH_STRCPY
34 /**
35 * strcpy - Copy a %NUL terminated string
36 * @dest: Where to copy the string to
37 * @src: Where to copy the string from
39 char * strcpy(char * dest,const char *src)
41 char *tmp = dest;
43 while ((*dest++ = *src++) != '\0')
44 /* nothing */;
45 return tmp;
47 #endif
49 #ifndef __HAVE_ARCH_STRNCPY
50 /**
51 * strncpy - Copy a length-limited, %NUL-terminated string
52 * @dest: Where to copy the string to
53 * @src: Where to copy the string from
54 * @count: The maximum number of bytes to copy
56 * Note that unlike userspace strncpy, this does not %NUL-pad the buffer.
57 * However, the result is not %NUL-terminated if the source exceeds
58 * @count bytes.
60 char * strncpy(char * dest,const char *src,size_t count)
62 char *tmp = dest;
64 while (count-- && (*dest++ = *src++) != '\0')
65 /* nothing */;
67 return tmp;
69 #endif
71 #ifndef __HAVE_ARCH_STRCAT
72 /**
73 * strcat - Append one %NUL-terminated string to another
74 * @dest: The string to be appended to
75 * @src: The string to append to it
77 char * strcat(char * dest, const char * src)
79 char *tmp = dest;
81 while (*dest)
82 dest++;
83 while ((*dest++ = *src++) != '\0')
86 return tmp;
88 #endif
90 #ifndef __HAVE_ARCH_STRCMP
91 /**
92 * strcmp - Compare two strings
93 * @cs: One string
94 * @ct: Another string
96 int strcmp(const char * cs,const char * ct)
98 register signed char __res;
100 while (1) {
101 if ((__res = *cs - *ct++) != 0 || !*cs++)
102 break;
105 return __res;
107 #endif
109 #ifndef __HAVE_ARCH_STRNCMP
111 * strncmp - Compare two length-limited strings
112 * @cs: One string
113 * @ct: Another string
114 * @count: The maximum number of bytes to compare
116 int strncmp(const char * cs,const char * ct,size_t count)
118 register signed char __res = 0;
120 while (count) {
121 if ((__res = *cs - *ct++) != 0 || !*cs++)
122 break;
123 count--;
126 return __res;
128 #endif
130 #ifndef __HAVE_ARCH_STRCASECMP
131 int strcasecmp(const char *a, const char *b)
133 while (*a && *b && (*a & ~0x20) == (*b & ~0x20)) {a++; b++; }
134 return((*a & ~0x20) - (*b & ~0x20));
136 #endif
138 #ifndef __HAVE_ARCH_STRCHR
140 * strchr - Find the first occurrence of a character in a string
141 * @s: The string to be searched
142 * @c: The character to search for
144 char * strchr(const char * s, int c)
146 for(; *s != (char) c; ++s)
147 if (*s == '\0')
148 return NULL;
149 return (char *) s;
151 #endif
153 #ifndef __HAVE_ARCH_STRRCHR
155 * strrchr - Find the last occurrence of a character in a string
156 * @s: The string to be searched
157 * @c: The character to search for
159 char * strrchr(const char * s, int c)
161 const char *p = s + strlen(s);
162 do {
163 if (*p == (char)c)
164 return (char *)p;
165 } while (--p >= s);
166 return NULL;
168 #endif
170 #ifndef __HAVE_ARCH_STRLEN
172 * strlen - Find the length of a string
173 * @s: The string to be sized
175 size_t strlen(const char * s)
177 const char *sc;
179 for (sc = s; *sc != '\0'; ++sc)
180 /* nothing */;
181 return sc - s;
183 #endif
185 #ifndef __HAVE_ARCH_STRNLEN
187 * strnlen - Find the length of a length-limited string
188 * @s: The string to be sized
189 * @count: The maximum number of bytes to search
191 size_t strnlen(const char * s, size_t count)
193 const char *sc;
195 for (sc = s; count-- && *sc != '\0'; ++sc)
196 /* nothing */;
197 return sc - s;
199 #endif
201 #ifndef __HAVE_ARCH_MEMSET
203 * memset - Fill a region of memory with the given value
204 * @s: Pointer to the start of the area.
205 * @c: The byte to fill the area with
206 * @count: The size of the area.
208 * Do not use memset() to access IO space, use memset_io() instead.
210 void * memset(void * s,int c,size_t count)
212 char *xs = (char *) s;
214 while (count--)
215 *xs++ = c;
217 return s;
219 #endif
221 #ifndef __HAVE_ARCH_MEMCPY
223 * memcpy - Copy one area of memory to another
224 * @dest: Where to copy to
225 * @src: Where to copy from
226 * @count: The size of the area.
228 * You should not use this function to access IO space, use memcpy_toio()
229 * or memcpy_fromio() instead.
231 void * memcpy(void * dest,const void *src,size_t count)
233 char *tmp = (char *) dest, *s = (char *) src;
235 while (count--)
236 *tmp++ = *s++;
238 return dest;
240 #endif
242 #ifndef __HAVE_ARCH_MEMMOVE
244 * memmove - Copy one area of memory to another
245 * @dest: Where to copy to
246 * @src: Where to copy from
247 * @count: The size of the area.
249 * Unlike memcpy(), memmove() copes with overlapping areas.
251 void * memmove(void * dest,const void *src,size_t count)
253 char *tmp, *s;
255 if (dest <= src) {
256 tmp = (char *) dest;
257 s = (char *) src;
258 while (count--)
259 *tmp++ = *s++;
261 else {
262 tmp = (char *) dest + count;
263 s = (char *) src + count;
264 while (count--)
265 *--tmp = *--s;
268 return dest;
270 #endif
272 #ifndef __HAVE_ARCH_MEMCMP
274 * memcmp - Compare two areas of memory
275 * @cs: One area of memory
276 * @ct: Another area of memory
277 * @count: The size of the area.
279 int memcmp(const void * cs,const void * ct,size_t count)
281 const unsigned char *su1, *su2;
282 int res = 0;
284 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
285 if ((res = *su1 - *su2) != 0)
286 break;
287 return res;
289 #endif
291 #ifndef __HAVE_ARCH_STRSTR
293 * strstr - Find the first substring in a %NUL terminated string
294 * @s1: The string to be searched
295 * @s2: The string to search for
297 char * strstr(const char * s1,const char * s2)
299 int l1, l2;
301 l2 = strlen(s2);
302 if (!l2)
303 return (char *) s1;
304 l1 = strlen(s1);
305 while (l1 >= l2) {
306 l1--;
307 if (!memcmp(s1,s2,l2))
308 return (char *) s1;
309 s1++;
311 return NULL;
313 #endif
315 #ifndef __HAVE_ARCH_MEMCHR
317 * memchr - Find a character in an area of memory.
318 * @s: The memory area
319 * @c: The byte to search for
320 * @n: The size of the area.
322 * returns the address of the first occurrence of @c, or %NULL
323 * if @c is not found
325 void * memchr(const void *s, int c, size_t n)
327 const unsigned char *p = s;
328 while (n-- != 0) {
329 if ((unsigned char)c == *p++) {
330 return (void *)(p-1);
333 return NULL;
336 #endif
338 char * strndup(const char *s, size_t n)
340 size_t len = strlen(s);
341 char *new;
343 if (len>n)
344 len = n;
345 new = malloc(len+1);
346 if (new) {
347 new[len] = '\0';
348 memcpy(new,s,len);
350 return new;
353 char * strdup(const char *s) {
354 return strndup(s, ~((size_t)0));