MINI2440: start.S removed MINI2440 specific speed
[u-boot-openmoko/mini2440.git] / tools / updater / string.c
blob954fb01e209790f759e34494bcc176054cc5cc1e
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..
14 #include <linux/types.h>
15 #include <linux/string.h>
16 #include <malloc.h>
18 #define __HAVE_ARCH_BCOPY
19 #define __HAVE_ARCH_MEMCMP
20 #define __HAVE_ARCH_MEMCPY
21 #define __HAVE_ARCH_MEMMOVE
22 #define __HAVE_ARCH_MEMSET
23 #define __HAVE_ARCH_STRCAT
24 #define __HAVE_ARCH_STRCMP
25 #define __HAVE_ARCH_STRCPY
26 #define __HAVE_ARCH_STRLEN
27 #define __HAVE_ARCH_STRNCPY
29 char * ___strtok = NULL;
31 #ifndef __HAVE_ARCH_STRCPY
32 char * strcpy(char * dest,const char *src)
34 char *tmp = dest;
36 while ((*dest++ = *src++) != '\0')
37 /* nothing */;
38 return tmp;
40 #endif
42 #ifndef __HAVE_ARCH_STRNCPY
43 char * strncpy(char * dest,const char *src,size_t count)
45 char *tmp = dest;
47 while (count-- && (*dest++ = *src++) != '\0')
48 /* nothing */;
50 return tmp;
52 #endif
54 #ifndef __HAVE_ARCH_STRCAT
55 char * strcat(char * dest, const char * src)
57 char *tmp = dest;
59 while (*dest)
60 dest++;
61 while ((*dest++ = *src++) != '\0')
64 return tmp;
66 #endif
68 #ifndef __HAVE_ARCH_STRNCAT
69 char * strncat(char *dest, const char *src, size_t count)
71 char *tmp = dest;
73 if (count) {
74 while (*dest)
75 dest++;
76 while ((*dest++ = *src++)) {
77 if (--count == 0) {
78 *dest = '\0';
79 break;
84 return tmp;
86 #endif
88 #ifndef __HAVE_ARCH_STRCMP
89 int strcmp(const char * cs,const char * ct)
91 register signed char __res;
93 while (1) {
94 if ((__res = *cs - *ct++) != 0 || !*cs++)
95 break;
98 return __res;
100 #endif
102 #ifndef __HAVE_ARCH_STRNCMP
103 int strncmp(const char * cs,const char * ct,size_t count)
105 register signed char __res = 0;
107 while (count) {
108 if ((__res = *cs - *ct++) != 0 || !*cs++)
109 break;
110 count--;
113 return __res;
115 #endif
117 #ifndef __HAVE_ARCH_STRCHR
118 char * strchr(const char * s, int c)
120 for(; *s != (char) c; ++s)
121 if (*s == '\0')
122 return NULL;
123 return (char *) s;
125 #endif
127 #ifndef __HAVE_ARCH_STRRCHR
128 char * strrchr(const char * s, int c)
130 const char *p = s + strlen(s);
131 do {
132 if (*p == (char)c)
133 return (char *)p;
134 } while (--p >= s);
135 return NULL;
137 #endif
139 #ifndef __HAVE_ARCH_STRLEN
140 size_t strlen(const char * s)
142 const char *sc;
144 for (sc = s; *sc != '\0'; ++sc)
145 /* nothing */;
146 return sc - s;
148 #endif
150 #ifndef __HAVE_ARCH_STRNLEN
151 size_t strnlen(const char * s, size_t count)
153 const char *sc;
155 for (sc = s; count-- && *sc != '\0'; ++sc)
156 /* nothing */;
157 return sc - s;
159 #endif
161 #ifndef __HAVE_ARCH_STRDUP
162 char * strdup(const char *s)
164 char *new;
166 if ((s == NULL) ||
167 ((new = malloc (strlen(s) + 1)) == NULL) ) {
168 return NULL;
171 strcpy (new, s);
172 return new;
174 #endif
176 #ifndef __HAVE_ARCH_STRSPN
177 size_t strspn(const char *s, const char *accept)
179 const char *p;
180 const char *a;
181 size_t count = 0;
183 for (p = s; *p != '\0'; ++p) {
184 for (a = accept; *a != '\0'; ++a) {
185 if (*p == *a)
186 break;
188 if (*a == '\0')
189 return count;
190 ++count;
193 return count;
195 #endif
197 #ifndef __HAVE_ARCH_STRPBRK
198 char * strpbrk(const char * cs,const char * ct)
200 const char *sc1,*sc2;
202 for( sc1 = cs; *sc1 != '\0'; ++sc1) {
203 for( sc2 = ct; *sc2 != '\0'; ++sc2) {
204 if (*sc1 == *sc2)
205 return (char *) sc1;
208 return NULL;
210 #endif
212 #ifndef __HAVE_ARCH_STRTOK
213 char * strtok(char * s,const char * ct)
215 char *sbegin, *send;
217 sbegin = s ? s : ___strtok;
218 if (!sbegin) {
219 return NULL;
221 sbegin += strspn(sbegin,ct);
222 if (*sbegin == '\0') {
223 ___strtok = NULL;
224 return( NULL );
226 send = strpbrk( sbegin, ct);
227 if (send && *send != '\0')
228 *send++ = '\0';
229 ___strtok = send;
230 return (sbegin);
232 #endif
234 #ifndef __HAVE_ARCH_MEMSET
235 void * memset(void * s,char c,size_t count)
237 char *xs = (char *) s;
239 while (count--)
240 *xs++ = c;
242 return s;
244 #endif
246 #ifndef __HAVE_ARCH_BCOPY
247 char * bcopy(const char * src, char * dest, int count)
249 char *tmp = dest;
251 while (count--)
252 *tmp++ = *src++;
254 return dest;
256 #endif
258 #ifndef __HAVE_ARCH_MEMCPY
259 void * memcpy(void * dest,const void *src,size_t count)
261 char *tmp = (char *) dest, *s = (char *) src;
263 while (count--)
264 *tmp++ = *s++;
266 return dest;
268 #endif
270 #ifndef __HAVE_ARCH_MEMMOVE
271 void * memmove(void * dest,const void *src,size_t count)
273 char *tmp, *s;
275 if (dest <= src) {
276 tmp = (char *) dest;
277 s = (char *) src;
278 while (count--)
279 *tmp++ = *s++;
281 else {
282 tmp = (char *) dest + count;
283 s = (char *) src + count;
284 while (count--)
285 *--tmp = *--s;
288 return dest;
290 #endif
292 #ifndef __HAVE_ARCH_MEMCMP
293 int memcmp(const void * cs,const void * ct,size_t count)
295 const unsigned char *su1, *su2;
296 signed char res = 0;
298 for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
299 if ((res = *su1 - *su2) != 0)
300 break;
301 return res;
303 #endif
306 * find the first occurrence of byte 'c', or 1 past the area if none
308 #ifndef __HAVE_ARCH_MEMSCAN
309 void * memscan(void * addr, int c, size_t size)
311 unsigned char * p = (unsigned char *) addr;
313 while (size) {
314 if (*p == c)
315 return (void *) p;
316 p++;
317 size--;
319 return (void *) p;
321 #endif
323 #ifndef __HAVE_ARCH_STRSTR
324 char * strstr(const char * s1,const char * s2)
326 int l1, l2;
328 l2 = strlen(s2);
329 if (!l2)
330 return (char *) s1;
331 l1 = strlen(s1);
332 while (l1 >= l2) {
333 l1--;
334 if (!memcmp(s1,s2,l2))
335 return (char *) s1;
336 s1++;
338 return NULL;
340 #endif