ld_string.h 5.6 KB

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