dl-string.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #ifndef _LINUX_STRING_H_
  2. #define _LINUX_STRING_H_
  3. #include <dl-sysdep.h> // for do_rem
  4. static size_t _dl_strlen(const char * str);
  5. static char *_dl_strcat(char *dst, const char *src);
  6. static char * _dl_strcpy(char * dst,const char *src);
  7. static int _dl_strcmp(const char * s1,const char * s2);
  8. static int _dl_strncmp(const char * s1,const char * s2,size_t len);
  9. static char * _dl_strchr(const char * str,int c);
  10. static char *_dl_strrchr(const char *str, int c);
  11. static char *_dl_strstr(const char *s1, const char *s2);
  12. static void * _dl_memcpy(void * dst, const void * src, size_t len);
  13. static int _dl_memcmp(const void * s1,const void * s2,size_t len);
  14. static void *_dl_memset(void * str,int c,size_t len);
  15. static char *_dl_get_last_path_component(char *path);
  16. static char *_dl_simple_ltoa(char * local, unsigned long i);
  17. static char *_dl_simple_ltoahex(char * local, unsigned long i);
  18. #ifndef NULL
  19. #define NULL ((void *) 0)
  20. #endif
  21. static inline size_t _dl_strlen(const char * str)
  22. {
  23. register const char *ptr = (char *) str-1;
  24. while (*++ptr);
  25. return (ptr - str);
  26. }
  27. static inline char *_dl_strcat(char *dst, const char *src)
  28. {
  29. register char *ptr = dst-1;
  30. src--;
  31. while (*++ptr)
  32. ;/* empty */
  33. ptr--;
  34. while ((*++ptr = *++src) != 0)
  35. ;/* empty */
  36. return dst;
  37. }
  38. static inline char * _dl_strcpy(char * dst,const char *src)
  39. {
  40. register char *ptr = dst;
  41. dst--;src--;
  42. while ((*++dst = *++src) != 0);
  43. return ptr;
  44. }
  45. static inline int _dl_strcmp(const char * s1,const char * s2)
  46. {
  47. register unsigned char c1, c2;
  48. s1--;s2--;
  49. do {
  50. c1 = (unsigned char) *++s1;
  51. c2 = (unsigned char) *++s2;
  52. if (c1 == '\0')
  53. return c1 - c2;
  54. }
  55. while (c1 == c2);
  56. return c1 - c2;
  57. }
  58. static inline int _dl_strncmp(const char * s1,const char * s2,size_t len)
  59. {
  60. register unsigned char c1 = '\0';
  61. register unsigned char c2 = '\0';
  62. s1--;s2--;
  63. while (len > 0) {
  64. c1 = (unsigned char) *++s1;
  65. c2 = (unsigned char) *++s2;
  66. if (c1 == '\0' || c1 != c2)
  67. return c1 - c2;
  68. len--;
  69. }
  70. return c1 - c2;
  71. }
  72. static inline char * _dl_strchr(const char * str,int c)
  73. {
  74. register char ch;
  75. str--;
  76. do {
  77. if ((ch = *++str) == c)
  78. return (char *) str;
  79. }
  80. while (ch);
  81. return 0;
  82. }
  83. static inline char *_dl_strrchr(const char *str, int c)
  84. {
  85. register char *prev = 0;
  86. register char *ptr = (char *) str-1;
  87. while (*++ptr != '\0') {
  88. if (*ptr == c)
  89. prev = ptr;
  90. }
  91. if (c == '\0')
  92. return(ptr);
  93. return(prev);
  94. }
  95. static inline char *_dl_strstr(const char *s1, const char *s2)
  96. {
  97. register const char *s = s1;
  98. register const char *p = s2;
  99. do {
  100. if (!*p) {
  101. return (char *) s1;;
  102. }
  103. if (*p == *s) {
  104. ++p;
  105. ++s;
  106. } else {
  107. p = s2;
  108. if (!*s) {
  109. return NULL;
  110. }
  111. s = ++s1;
  112. }
  113. } while (1);
  114. }
  115. static inline void * _dl_memcpy(void * dst, const void * src, size_t len)
  116. {
  117. register char *a = dst-1;
  118. register const char *b = src-1;
  119. while (len) {
  120. *++a = *++b;
  121. --len;
  122. }
  123. return dst;
  124. }
  125. static inline int _dl_memcmp(const void * s1,const void * s2,size_t len)
  126. {
  127. unsigned char *c1 = (unsigned char *)s1-1;
  128. unsigned char *c2 = (unsigned char *)s2-1;
  129. while (len) {
  130. if (*++c1 != *++c2)
  131. return *c1 - *c2;
  132. len--;
  133. }
  134. return 0;
  135. }
  136. #if defined(powerpc)
  137. /* Will generate smaller and faster code due to loop unrolling.*/
  138. static inline void *_dl_memset(void *to, int c, size_t n)
  139. {
  140. unsigned long chunks;
  141. unsigned long *tmp_to;
  142. unsigned char *tmp_char;
  143. chunks = n / 4;
  144. tmp_to = to + n;
  145. c = c << 8 | c;
  146. c = c << 16 | c;
  147. if (!chunks)
  148. goto lessthan4;
  149. do {
  150. *--tmp_to = c;
  151. } while (--chunks);
  152. lessthan4:
  153. n = n % 4;
  154. if (!n ) return to;
  155. tmp_char = (unsigned char *)tmp_to;
  156. do {
  157. *--tmp_char = c;
  158. } while (--n);
  159. return to;
  160. }
  161. #else
  162. static inline void * _dl_memset(void * str,int c,size_t len)
  163. {
  164. register char *a = str;
  165. while (len--)
  166. *a++ = c;
  167. return str;
  168. }
  169. #endif
  170. static inline char *_dl_get_last_path_component(char *path)
  171. {
  172. register char *ptr = path-1;
  173. while (*++ptr)
  174. ;/* empty */
  175. /* strip trailing slashes */
  176. while (ptr != path && *--ptr == '/') {
  177. *ptr = '\0';
  178. }
  179. /* find last component */
  180. while (ptr != path && *--ptr != '/')
  181. ;/* empty */
  182. return ptr == path ? ptr : ptr+1;
  183. }
  184. /* Early on, we can't call printf, so use this to print out
  185. * numbers using the SEND_STDERR() macro. Avoid using mod
  186. * or using long division */
  187. static inline char *_dl_simple_ltoa(char * local, unsigned long i)
  188. {
  189. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  190. char *p = &local[22];
  191. *--p = '\0';
  192. do {
  193. char temp;
  194. do_rem(temp, i, 10);
  195. *--p = '0' + temp;
  196. i /= 10;
  197. } while (i > 0);
  198. return p;
  199. }
  200. static inline char *_dl_simple_ltoahex(char * local, unsigned long i)
  201. {
  202. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  203. char *p = &local[22];
  204. *--p = '\0';
  205. do {
  206. char temp = i & 0xf;
  207. if (temp <= 0x09)
  208. *--p = '0' + temp;
  209. else
  210. *--p = 'a' - 0x0a + temp;
  211. i >>= 4;
  212. } while (i > 0);
  213. *--p = 'x';
  214. *--p = '0';
  215. return p;
  216. }
  217. #if defined(mc68000) || defined(__arm__) || defined(__mips__) || defined(__sh__) || defined(__powerpc__)
  218. /* On some arches constant strings are referenced through the GOT. */
  219. /* XXX Requires load_addr to be defined. */
  220. #define SEND_STDERR(X) \
  221. { const char *__s = (X); \
  222. if (__s < (const char *) load_addr) __s += load_addr; \
  223. _dl_write (2, __s, _dl_strlen (__s)); \
  224. }
  225. #else
  226. #define SEND_STDERR(X) _dl_write(2, X, _dl_strlen(X));
  227. #endif
  228. /* Some targets may have to override this to something that doesn't
  229. reference constant strings through the GOT. This macro should be
  230. preferred over SEND_STDERR for constant strings before we complete
  231. bootstrap. */
  232. #ifndef SEND_EARLY_STDERR
  233. # define SEND_EARLY_STDERR(S) SEND_STDERR(S)
  234. #endif
  235. #define SEND_ADDRESS_STDERR(X, add_a_newline) { \
  236. char tmp[22], *tmp1; \
  237. _dl_memset(tmp, 0, sizeof(tmp)); \
  238. tmp1=_dl_simple_ltoahex( tmp, (unsigned long)(X)); \
  239. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  240. if (add_a_newline) { \
  241. tmp[0]='\n'; \
  242. _dl_write(2, tmp, 1); \
  243. } \
  244. };
  245. #define SEND_NUMBER_STDERR(X, add_a_newline) { \
  246. char tmp[22], *tmp1; \
  247. _dl_memset(tmp, 0, sizeof(tmp)); \
  248. tmp1=_dl_simple_ltoa( tmp, (unsigned long)(X)); \
  249. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  250. if (add_a_newline) { \
  251. tmp[0]='\n'; \
  252. _dl_write(2, tmp, 1); \
  253. } \
  254. };
  255. #endif