OSDN Git Service

I really really HATE gcc-4.1 and its whimsical inline rules.
[uclinux-h8/uClibc.git] / ldso / include / dl-string.h
1 #ifndef _LINUX_STRING_H_
2 #define _LINUX_STRING_H_
3
4 #include <dl-sysdep.h> // for do_rem
5 #include <features.h>
6
7 static size_t _dl_strlen(const char * str);
8 static char *_dl_strcat(char *dst, const char *src);
9 static char * _dl_strcpy(char * dst,const char *src);
10 static int _dl_strcmp(const char * s1,const char * s2);
11 static int _dl_strncmp(const char * s1,const char * s2,size_t len);
12 static char * _dl_strchr(const char * str,int c);
13 static char *_dl_strrchr(const char *str, int c);
14 static char *_dl_strstr(const char *s1, const char *s2);
15 static void * _dl_memcpy(void * dst, const void * src, size_t len);
16 static int _dl_memcmp(const void * s1,const void * s2,size_t len);
17 static void *_dl_memset(void * str,int c,size_t len);
18 static char *_dl_get_last_path_component(char *path);
19 static char *_dl_simple_ltoa(char * local, unsigned long i);
20 static char *_dl_simple_ltoahex(char * local, unsigned long i);
21
22 #ifndef NULL
23 #define NULL ((void *) 0)
24 #endif
25
26 static __always_inline size_t _dl_strlen(const char * str)
27 {
28         register const char *ptr = (char *) str-1;
29
30         while (*++ptr);
31         return (ptr - str);
32 }
33
34 static __always_inline char * _dl_strcat(char *dst, const char *src)
35 {
36         register char *ptr = dst-1;
37
38         src--;
39         while (*++ptr)
40                 ;/* empty */
41         ptr--;
42         while ((*++ptr = *++src) != 0)
43                 ;/* empty */
44         return dst;
45 }
46
47 static __always_inline char * _dl_strcpy(char * dst,const char *src)
48 {
49         register char *ptr = dst;
50
51         dst--;src--;
52         while ((*++dst = *++src) != 0);
53
54         return ptr;
55 }
56
57 static __always_inline int _dl_strcmp(const char * s1,const char * s2)
58 {
59         register unsigned char c1, c2;
60         s1--;s2--;
61         do {
62                 c1 = (unsigned char) *++s1;
63                 c2 = (unsigned char) *++s2;
64                 if (c1 == '\0')
65                         return c1 - c2;
66         }
67         while (c1 == c2);
68
69         return c1 - c2;
70 }
71
72 static __always_inline int _dl_strncmp(const char * s1,const char * s2,size_t len)
73 {
74         register unsigned char c1 = '\0';
75         register unsigned char c2 = '\0';
76
77         s1--;s2--;
78         while (len > 0) {
79                 c1 = (unsigned char) *++s1;
80                 c2 = (unsigned char) *++s2;
81                 if (c1 == '\0' || c1 != c2)
82                         return c1 - c2;
83                 len--;
84         }
85         return c1 - c2;
86 }
87
88 static __always_inline char * _dl_strchr(const char * str,int c)
89 {
90         register char ch;
91         str--;
92         do {
93                 if ((ch = *++str) == c)
94                         return (char *) str;
95         }
96         while (ch);
97
98         return 0;
99 }
100
101 static inline char * _dl_strrchr(const char *str, int c)
102 {
103     register char *prev = 0;
104     register char *ptr = (char *) str-1;
105
106     while (*++ptr != '\0') {
107         if (*ptr == c)
108             prev = ptr;
109     }
110     if (c == '\0')
111         return(ptr);
112     return(prev);
113 }
114
115 static inline char * _dl_strstr(const char *s1, const char *s2)
116 {
117     register const char *s = s1;
118     register const char *p = s2;
119
120     do {
121         if (!*p) {
122             return (char *) s1;;
123         }
124         if (*p == *s) {
125             ++p;
126             ++s;
127         } else {
128             p = s2;
129             if (!*s) {
130               return NULL;
131             }
132             s = ++s1;
133         }
134     } while (1);
135 }
136
137 static __always_inline void * _dl_memcpy(void * dst, const void * src, size_t len)
138 {
139         register char *a = dst-1;
140         register const char *b = src-1;
141
142         while (len) {
143                 *++a = *++b;
144                 --len;
145         }
146         return dst;
147 }
148
149 static __always_inline int _dl_memcmp(const void * s1,const void * s2,size_t len)
150 {
151         unsigned char *c1 = (unsigned char *)s1-1;
152         unsigned char *c2 = (unsigned char *)s2-1;
153
154         while (len) {
155                 if (*++c1 != *++c2)
156                         return *c1 - *c2;
157                 len--;
158         }
159         return 0;
160 }
161
162 #if defined(powerpc)
163 /* Will generate smaller and faster code due to loop unrolling.*/
164 static __always_inline void * _dl_memset(void *to, int c, size_t n)
165 {
166         unsigned long chunks;
167         unsigned long *tmp_to;
168         unsigned char *tmp_char;
169
170         chunks = n / 4;
171         tmp_to = to + n;
172         c = c << 8 | c;
173         c = c << 16 | c;
174         if (!chunks)
175                 goto lessthan4;
176         do {
177                 *--tmp_to = c;
178         } while (--chunks);
179  lessthan4:
180         n = n % 4;
181         if (!n ) return to;
182         tmp_char = (unsigned char *)tmp_to;
183         do {
184                 *--tmp_char = c;
185         } while (--n);
186         return to;
187 }
188 #else
189 static __always_inline void * _dl_memset(void * str,int c,size_t len)
190 {
191         register char *a = str;
192
193         while (len--)
194                 *a++ = c;
195
196         return str;
197 }
198 #endif
199
200 static __always_inline char * _dl_get_last_path_component(char *path)
201 {
202         register char *ptr = path-1;
203
204         while (*++ptr)
205                 ;/* empty */
206
207         /* strip trailing slashes */
208         while (ptr != path && *--ptr == '/') {
209                 *ptr = '\0';
210         }
211
212         /* find last component */
213         while (ptr != path && *--ptr != '/')
214                 ;/* empty */
215         return ptr == path ? ptr : ptr+1;
216 }
217
218 /* Early on, we can't call printf, so use this to print out
219  * numbers using the SEND_STDERR() macro.  Avoid using mod
220  * or using long division */
221 static __always_inline char * _dl_simple_ltoa(char * local, unsigned long i)
222 {
223         /* 20 digits plus a null terminator should be good for
224          * 64-bit or smaller ints (2^64 - 1)*/
225         char *p = &local[22];
226         *--p = '\0';
227         do {
228             char temp;
229             do_rem(temp, i, 10);
230             *--p = '0' + temp;
231             i /= 10;
232         } while (i > 0);
233         return p;
234 }
235
236 static __always_inline char * _dl_simple_ltoahex(char * local, unsigned long i)
237 {
238         /* 16 digits plus a leading "0x" plus a null terminator,
239          * should be good for 64-bit or smaller ints */
240         char *p = &local[22];
241         *--p = '\0';
242         do {
243                 char temp = i & 0xf;
244                 if (temp <= 0x09)
245                     *--p = '0' + temp;
246                 else
247                     *--p = 'a' - 0x0a + temp;
248                 i >>= 4;
249         } while (i > 0);
250         *--p = 'x';
251         *--p = '0';
252         return p;
253 }
254
255
256
257
258 /* The following macros may be used in dl-startup.c to debug
259  * ldso before ldso has fixed itself up to make function calls */
260
261 /* On some (wierd) arches, none of this stuff works at all, so
262  * disable the whole lot... */
263 #if defined(__mips__)
264
265 # define SEND_STDERR(X)
266 # define SEND_ADDRESS_STDERR(X, add_a_newline)
267 # define SEND_NUMBER_STDERR(X, add_a_newline)
268
269 #else
270
271 /* On some arches constant strings are referenced through the GOT.
272  * This requires that load_addr must already be defined... */
273 #if defined(mc68000) || defined(__arm__) || defined(__mips__)   \
274                      || defined(__sh__) ||  defined(__powerpc__)
275 # define CONSTANT_STRING_GOT_FIXUP(X) \
276         if ((X) < (const char *) load_addr) (X) += load_addr
277 # define NO_EARLY_SEND_STDERR
278 #else
279 # define CONSTANT_STRING_GOT_FIXUP(X)
280 #endif
281
282 #define SEND_STDERR(X) \
283 { \
284         const char *tmp1 = (X); \
285         CONSTANT_STRING_GOT_FIXUP(tmp1); \
286         _dl_write(2, tmp1, _dl_strlen(tmp1)); \
287 }
288
289 #define SEND_ADDRESS_STDERR(ADR, add_a_newline) \
290 { \
291         char tmp[26], v, *tmp2, *tmp1 = tmp; \
292         unsigned long X = (unsigned long)(ADR); \
293         CONSTANT_STRING_GOT_FIXUP(tmp1); \
294         tmp2 = tmp1 + sizeof(tmp); \
295         *--tmp2 = '\0'; \
296         if (add_a_newline) *--tmp2 = '\n'; \
297         do { \
298                 v = (X) & 0xf; \
299                 if (v <= 0x09) \
300                         *--tmp2 = '0' + v; \
301                 else \
302                         *--tmp2 = 'a' - 0x0a + v; \
303                 (X) >>= 4; \
304         } while ((X) > 0); \
305         *--tmp2 = 'x'; \
306         *--tmp2 = '0'; \
307         _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
308 }
309
310 #define SEND_NUMBER_STDERR(NUM, add_a_newline) \
311 { \
312         char tmp[26], v, *tmp2, *tmp1 = tmp; \
313         unsigned long X = (unsigned long)(NUM); \
314         CONSTANT_STRING_GOT_FIXUP(tmp1); \
315         tmp2 = tmp1 + sizeof(tmp); \
316         *--tmp2 = '\0'; \
317         if (add_a_newline) *--tmp2 = '\n'; \
318         do { \
319                 do_rem(v, (X), 10); \
320                 *--tmp2 = '0' + v; \
321                 (X) /= 10; \
322         } while ((X) > 0); \
323         _dl_write(2, tmp2, tmp1 - tmp2 + sizeof(tmp) - 1); \
324 }
325 #endif
326
327 #ifdef __SUPPORT_LD_DEBUG_EARLY__
328 # define SEND_STDERR_DEBUG(X) SEND_STDERR(X)
329 # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline) SEND_NUMBER_STDERR(X, add_a_newline)
330 # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline) SEND_ADDRESS_STDERR(X, add_a_newline)
331 #else
332 # define SEND_STDERR_DEBUG(X)
333 # define SEND_NUMBER_STDERR_DEBUG(X, add_a_newline)
334 # define SEND_ADDRESS_STDERR_DEBUG(X, add_a_newline)
335 #endif
336
337 #endif