string.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef _LINUX_STRING_H_
  2. #define _LINUX_STRING_H_
  3. #include <sys/types.h> /* for size_t */
  4. extern void *_dl_malloc(int size);
  5. extern char *_dl_getenv(char *symbol, char **envp);
  6. extern void _dl_unsetenv(char *symbol, char **envp);
  7. extern char *_dl_strdup(const char *string);
  8. extern char *_dl_get_last_path_component(char *path);
  9. extern size_t _dl_strlen(const char * str);
  10. extern char * _dl_strcpy(char * dst,const char *src);
  11. extern int _dl_strcmp(const char * s1,const char * s2);
  12. extern int _dl_strncmp(const char * s1,const char * s2,size_t len);
  13. extern char * _dl_strchr(const char * str,int c);
  14. extern char *_dl_strrchr(const char *str, int c);
  15. extern void * _dl_memcpy(void * dst, const void * src, size_t len);
  16. extern int _dl_memcmp(const void * s1,const void * s2,size_t len);
  17. extern void * _dl_memset(void * str,int c,size_t len);
  18. #ifndef NULL
  19. #define NULL ((void *) 0)
  20. #endif
  21. static inline size_t _dl_strlen_inline(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_strcpy_inline(char * dst,const char *src)
  29. {
  30. register char *ptr = dst;
  31. while (*src)
  32. *dst++ = *src++;
  33. *dst = '\0';
  34. return ptr;
  35. }
  36. static inline int _dl_strcmp_inline(const char * s1,const char * s2)
  37. {
  38. unsigned register char c1, c2;
  39. do {
  40. c1 = (unsigned char) *s1++;
  41. c2 = (unsigned char) *s2++;
  42. if (c1 == '\0')
  43. return c1 - c2;
  44. }
  45. while (c1 == c2);
  46. return c1 - c2;
  47. }
  48. static inline int _dl_strncmp_inline(const char * s1,const char * s2,size_t len)
  49. {
  50. unsigned register char c1 = '\0';
  51. unsigned register char c2 = '\0';
  52. while (len > 0) {
  53. c1 = (unsigned char) *s1++;
  54. c2 = (unsigned char) *s2++;
  55. if (c1 == '\0' || c1 != c2)
  56. return c1 - c2;
  57. len--;
  58. }
  59. return c1 - c2;
  60. }
  61. static inline char * _dl_strchr_inline(const char * str,int c)
  62. {
  63. register char ch;
  64. do {
  65. if ((ch = *str) == c)
  66. return (char *) str;
  67. str++;
  68. }
  69. while (ch);
  70. return 0;
  71. }
  72. static inline char *_dl_strrchr_inline(const char *str, int c)
  73. {
  74. register char *prev = 0;
  75. register char *ptr = (char *) str;
  76. while (*ptr != '\0') {
  77. if (*ptr == c)
  78. prev = ptr;
  79. ptr++;
  80. }
  81. if (c == '\0')
  82. return(ptr);
  83. return(prev);
  84. }
  85. static inline void * _dl_memcpy_inline(void * dst, const void * src, size_t len)
  86. {
  87. register char *a = dst;
  88. register const char *b = src;
  89. while (len--)
  90. *a++ = *b++;
  91. return dst;
  92. }
  93. static inline int _dl_memcmp_inline(const void * s1,const void * s2,size_t len)
  94. {
  95. unsigned char *c1 = (unsigned char *)s1;
  96. unsigned char *c2 = (unsigned char *)s2;
  97. while (len--) {
  98. if (*c1 != *c2)
  99. return *c1 - *c2;
  100. c1++;
  101. c2++;
  102. }
  103. return 0;
  104. }
  105. static inline void * _dl_memset_inline(void * str,int c,size_t len)
  106. {
  107. register char *a = str;
  108. while (len--)
  109. *a++ = c;
  110. return str;
  111. }
  112. static inline char *_dl_get_last_path_component_inline(char *path)
  113. {
  114. char *s;
  115. register char *ptr = path;
  116. register char *prev = 0;
  117. while (*ptr)
  118. ptr++;
  119. s = ptr - 1;
  120. /* strip trailing slashes */
  121. while (s != path && *s == '/') {
  122. *s-- = '\0';
  123. }
  124. /* find last component */
  125. ptr = path;
  126. while (*ptr != '\0') {
  127. if (*ptr == '/')
  128. prev = ptr;
  129. ptr++;
  130. }
  131. s = prev;
  132. if (s == NULL || s[1] == '\0')
  133. return path;
  134. else
  135. return s+1;
  136. }
  137. /* Early on, we can't call printf, so use this to print out
  138. * numbers using the SEND_STDERR() macro */
  139. static inline char *_dl_simple_ltoa_inline(char * local, unsigned long i)
  140. {
  141. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  142. char *p = &local[21];
  143. *p-- = '\0';
  144. do {
  145. *p-- = '0' + i % 10;
  146. i /= 10;
  147. } while (i > 0);
  148. return p + 1;
  149. }
  150. static inline char *_dl_simple_ltoahex_inline(char * local, unsigned long i)
  151. {
  152. /* 21 digits plus null terminator, good for 64-bit or smaller ints */
  153. char *p = &local[21];
  154. *p-- = '\0';
  155. do {
  156. char temp = i % 0x10;
  157. if (temp <= 0x09)
  158. *p-- = '0' + temp;
  159. else
  160. *p-- = 'a' - 0x0a + temp;
  161. i /= 0x10;
  162. } while (i > 0);
  163. *p-- = 'x';
  164. *p-- = '0';
  165. return p + 1;
  166. }
  167. #endif