dl-string.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 char *ptr = (char *) str;
  24. while (*ptr)
  25. ptr++;
  26. return (ptr - str);
  27. }
  28. static inline char *_dl_strcat(char *dst, const char *src)
  29. {
  30. register char *ptr = dst;
  31. while (*ptr)
  32. ptr++;
  33. while (*src)
  34. *ptr++ = *src++;
  35. *ptr = '\0';
  36. return dst;
  37. }
  38. static inline char * _dl_strcpy(char * dst,const char *src)
  39. {
  40. register char *ptr = dst;
  41. while (*src)
  42. *dst++ = *src++;
  43. *dst = '\0';
  44. return ptr;
  45. }
  46. static inline int _dl_strcmp(const char * s1,const char * s2)
  47. {
  48. register unsigned char c1, c2;
  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. while (len > 0) {
  63. c1 = (unsigned char) *s1++;
  64. c2 = (unsigned char) *s2++;
  65. if (c1 == '\0' || c1 != c2)
  66. return c1 - c2;
  67. len--;
  68. }
  69. return c1 - c2;
  70. }
  71. static inline char * _dl_strchr(const char * str,int c)
  72. {
  73. register char ch;
  74. do {
  75. if ((ch = *str) == c)
  76. return (char *) str;
  77. str++;
  78. }
  79. while (ch);
  80. return 0;
  81. }
  82. static inline char *_dl_strrchr(const char *str, int c)
  83. {
  84. register char *prev = 0;
  85. register char *ptr = (char *) str;
  86. while (*ptr != '\0') {
  87. if (*ptr == c)
  88. prev = ptr;
  89. 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;
  118. register const char *b = src;
  119. while (len--)
  120. *a++ = *b++;
  121. return dst;
  122. }
  123. static inline int _dl_memcmp(const void * s1,const void * s2,size_t len)
  124. {
  125. unsigned char *c1 = (unsigned char *)s1;
  126. unsigned char *c2 = (unsigned char *)s2;
  127. while (len--) {
  128. if (*c1 != *c2)
  129. return *c1 - *c2;
  130. c1++;
  131. c2++;
  132. }
  133. return 0;
  134. }
  135. static inline void * _dl_memset(void * str,int c,size_t len)
  136. {
  137. register char *a = str;
  138. while (len--)
  139. *a++ = c;
  140. return str;
  141. }
  142. static inline char *_dl_get_last_path_component(char *path)
  143. {
  144. char *s;
  145. register char *ptr = path;
  146. register char *prev = 0;
  147. while (*ptr)
  148. ptr++;
  149. s = ptr - 1;
  150. /* strip trailing slashes */
  151. while (s != path && *s == '/') {
  152. *s-- = '\0';
  153. }
  154. /* find last component */
  155. ptr = path;
  156. while (*ptr != '\0') {
  157. if (*ptr == '/')
  158. prev = ptr;
  159. ptr++;
  160. }
  161. s = prev;
  162. if (s == NULL || s[1] == '\0')
  163. return path;
  164. else
  165. return s+1;
  166. }
  167. /* Early on, we can't call printf, so use this to print out
  168. * numbers using the SEND_STDERR() macro. Avoid using mod
  169. * or using long division */
  170. static inline char *_dl_simple_ltoa(char * local, unsigned long i)
  171. {
  172. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  173. char *p = &local[21];
  174. *p-- = '\0';
  175. do {
  176. char temp;
  177. do_rem(temp, i, 10);
  178. *p-- = '0' + temp;
  179. i /= 10;
  180. } while (i > 0);
  181. return p + 1;
  182. }
  183. static inline char *_dl_simple_ltoahex(char * local, unsigned long i)
  184. {
  185. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  186. char *p = &local[21];
  187. *p-- = '\0';
  188. do {
  189. char temp = i & 0xf;
  190. if (temp <= 0x09)
  191. *p-- = '0' + temp;
  192. else
  193. *p-- = 'a' - 0x0a + temp;
  194. i >>= 4;
  195. } while (i > 0);
  196. *p-- = 'x';
  197. *p-- = '0';
  198. return p + 1;
  199. }
  200. #if defined(mc68000) || defined(__arm__) || defined(__mips__) || defined(__sh__) || defined(__powerpc__)
  201. /* On some arches constant strings are referenced through the GOT. */
  202. /* XXX Requires load_addr to be defined. */
  203. #define SEND_STDERR(X) \
  204. { const char *__s = (X); \
  205. if (__s < (const char *) load_addr) __s += load_addr; \
  206. _dl_write (2, __s, _dl_strlen (__s)); \
  207. }
  208. #else
  209. #define SEND_STDERR(X) _dl_write(2, X, _dl_strlen(X));
  210. #endif
  211. #define SEND_ADDRESS_STDERR(X, add_a_newline) { \
  212. char tmp[22], *tmp1; \
  213. _dl_memset(tmp, 0, sizeof(tmp)); \
  214. tmp1=_dl_simple_ltoahex( tmp, (unsigned long)(X)); \
  215. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  216. if (add_a_newline) { \
  217. tmp[0]='\n'; \
  218. _dl_write(2, tmp, 1); \
  219. } \
  220. };
  221. #define SEND_NUMBER_STDERR(X, add_a_newline) { \
  222. char tmp[22], *tmp1; \
  223. _dl_memset(tmp, 0, sizeof(tmp)); \
  224. tmp1=_dl_simple_ltoa( tmp, (unsigned long)(X)); \
  225. _dl_write(2, tmp1, _dl_strlen(tmp1)); \
  226. if (add_a_newline) { \
  227. tmp[0]='\n'; \
  228. _dl_write(2, tmp, 1); \
  229. } \
  230. };
  231. #endif