Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / core / stringextra.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2004 Tobias Lorenz
4  *
5  *  string handling functions
6  *  based on linux/lib/string.c
7  *
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.
11  */
12
13 FILE_LICENCE ( GPL2_ONLY );
14
15 /*
16  * stupid library routines.. The optimized versions should generally be found
17  * as inline code in <asm-xx/string.h>
18  *
19  * These are buggy as well..
20  *
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.
24  */
25
26 /*
27  * these are the standard string functions that are currently not used by
28  * any code in etherboot. put into a separate file to avoid linking them in
29  * with the rest of string.o
30  * if anything ever does want to use a function of these, consider moving
31  * the function in question back into string.c
32  */
33  
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38
39 /* *** FROM string.c *** */
40
41 #ifndef __HAVE_ARCH_STRNICMP
42 /**
43  * strnicmp - Case insensitive, length-limited string comparison
44  * @s1: One string
45  * @s2: The other string
46  * @len: the maximum number of characters to compare
47  */
48 int strnicmp(const char *s1, const char *s2, size_t len)
49 {
50         /* Yes, Virginia, it had better be unsigned */
51         unsigned char c1, c2;
52
53         c1 = 0; c2 = 0;
54         if (len) {
55                 do {
56                         c1 = *s1; c2 = *s2;
57                         s1++; s2++;
58                         if (!c1)
59                                 break;
60                         if (!c2)
61                                 break;
62                         if (c1 == c2)
63                                 continue;
64                         c1 = tolower(c1);
65                         c2 = tolower(c2);
66                         if (c1 != c2)
67                                 break;
68                 } while (--len);
69         }
70         return (int)c1 - (int)c2;
71 }
72 #endif
73
74 char * ___strtok;
75
76 #ifndef __HAVE_ARCH_STRNCAT
77 /**
78  * strncat - Append a length-limited, %NUL-terminated string to another
79  * @dest: The string to be appended to
80  * @src: The string to append to it
81  * @count: The maximum numbers of bytes to copy
82  *
83  * Note that in contrast to strncpy, strncat ensures the result is
84  * terminated.
85  */
86 char * strncat(char *dest, const char *src, size_t count)
87 {
88         char *tmp = dest;
89
90         if (count) {
91                 while (*dest)
92                         dest++;
93                 while ((*dest++ = *src++)) {
94                         if (--count == 0) {
95                                 *dest = '\0';
96                                 break;
97                         }
98                 }
99         }
100
101         return tmp;
102 }
103 #endif
104
105 #ifndef __HAVE_ARCH_STRSPN
106 /**
107  * strspn - Calculate the length of the initial substring of @s which only
108  *      contain letters in @accept
109  * @s: The string to be searched
110  * @accept: The string to search for
111  */
112 size_t strspn(const char *s, const char *accept)
113 {
114         const char *p;
115         const char *a;
116         size_t count = 0;
117
118         for (p = s; *p != '\0'; ++p) {
119                 for (a = accept; *a != '\0'; ++a) {
120                         if (*p == *a)
121                                 break;
122                 }
123                 if (*a == '\0')
124                         return count;
125                 ++count;
126         }
127
128         return count;
129 }
130 #endif
131
132 #ifndef __HAVE_ARCH_STRCSPN
133 /**
134  * strcspn - Calculate the length of the initial substring of @s which only
135  *      contain letters not in @reject
136  * @s: The string to be searched
137  * @accept: The string to search for
138  */
139 size_t strcspn(const char *s, const char *reject)
140 {
141         const char *p;
142         const char *r;
143         size_t count = 0;
144
145         for (p = s; *p != '\0'; ++p) {
146                 for (r = reject; *r != '\0'; ++r) {
147                         if (*p == *r)
148                                 return count;
149                 }
150                 ++count;
151         }
152
153         return count;
154 }
155 #endif
156
157 #ifndef __HAVE_ARCH_STRPBRK
158 /**
159  * strpbrk - Find the first occurrence of a set of characters
160  * @cs: The string to be searched
161  * @ct: The characters to search for
162  */
163 char * strpbrk(const char * cs,const char * ct)
164 {
165         const char *sc1,*sc2;
166
167         for( sc1 = cs; *sc1 != '\0'; ++sc1) {
168                 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
169                         if (*sc1 == *sc2)
170                                 return (char *) sc1;
171                 }
172         }
173         return NULL;
174 }
175 #endif
176
177 #ifndef __HAVE_ARCH_STRTOK
178 /**
179  * strtok - Split a string into tokens
180  * @s: The string to be searched
181  * @ct: The characters to search for
182  *
183  * WARNING: strtok is deprecated, use strsep instead.
184  */
185 char * strtok(char * s,const char * ct)
186 {
187         char *sbegin, *send;
188
189         sbegin  = s ? s : ___strtok;
190         if (!sbegin) {
191                 return NULL;
192         }
193         sbegin += strspn(sbegin,ct);
194         if (*sbegin == '\0') {
195                 ___strtok = NULL;
196                 return( NULL );
197         }
198         send = strpbrk( sbegin, ct);
199         if (send && *send != '\0')
200                 *send++ = '\0';
201         ___strtok = send;
202         return (sbegin);
203 }
204 #endif
205
206 #ifndef __HAVE_ARCH_STRSEP
207 /**
208  * strsep - Split a string into tokens
209  * @s: The string to be searched
210  * @ct: The characters to search for
211  *
212  * strsep() updates @s to point after the token, ready for the next call.
213  *
214  * It returns empty tokens, too, behaving exactly like the libc function
215  * of that name. In fact, it was stolen from glibc2 and de-fancy-fied.
216  * Same semantics, slimmer shape. ;)
217  */
218 char * strsep(char **s, const char *ct)
219 {
220         char *sbegin = *s, *end;
221
222         if (sbegin == NULL)
223                 return NULL;
224
225         end = strpbrk(sbegin, ct);
226         if (end)
227                 *end++ = '\0';
228         *s = end;
229
230         return sbegin;
231 }
232 #endif
233
234 #ifndef __HAVE_ARCH_BCOPY
235 /**
236  * bcopy - Copy one area of memory to another
237  * @src: Where to copy from
238  * @dest: Where to copy to
239  * @count: The size of the area.
240  *
241  * Note that this is the same as memcpy(), with the arguments reversed.
242  * memcpy() is the standard, bcopy() is a legacy BSD function.
243  *
244  * You should not use this function to access IO space, use memcpy_toio()
245  * or memcpy_fromio() instead.
246  */
247 char * bcopy(const char * src, char * dest, int count)
248 {
249         return memmove(dest,src,count);
250 }
251 #endif
252
253 #ifndef __HAVE_ARCH_MEMSCAN
254 /**
255  * memscan - Find a character in an area of memory.
256  * @addr: The memory area
257  * @c: The byte to search for
258  * @size: The size of the area.
259  *
260  * returns the address of the first occurrence of @c, or 1 byte past
261  * the area if @c is not found
262  */
263 void * memscan(const void * addr, int c, size_t size)
264 {
265         unsigned char * p = (unsigned char *) addr;
266
267         while (size) {
268                 if (*p == c)
269                         return (void *) p;
270                 p++;
271                 size--;
272         }
273         return (void *) p;
274 }
275 #endif