string.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #ifndef __STRING_H
  2. #define __STRING_H
  3. #include "asmc_types.h"
  4. int strcmp(const char *x, const char *y) {
  5. while (1) {
  6. unsigned char a = *x;
  7. unsigned char b = *y;
  8. if (a < b) return 0-1;
  9. if (b < a) return 1;
  10. if (a == 0) return 0;
  11. x = x + 1;
  12. y = y + 1;
  13. }
  14. }
  15. // From PDClib
  16. void * memcpy( void * s1, const void * s2, size_t n )
  17. {
  18. char * dest = (char *) s1;
  19. const char * src = (const char *) s2;
  20. while ( n-- )
  21. {
  22. *dest = *src;
  23. dest += 1;
  24. src += 1;
  25. }
  26. return s1;
  27. }
  28. // From PDClib
  29. size_t strlen( const char * s )
  30. {
  31. size_t rc = 0;
  32. while ( s[rc] )
  33. {
  34. ++rc;
  35. }
  36. return rc;
  37. }
  38. // From PDClib
  39. void * memmove( void * s1, const void * s2, size_t n )
  40. {
  41. char * dest = (char *) s1;
  42. const char * src = (const char *) s2;
  43. if ( dest <= src )
  44. {
  45. while ( n-- )
  46. {
  47. *dest = *src;
  48. dest += 1;
  49. src += 1;
  50. }
  51. }
  52. else
  53. {
  54. src += n;
  55. dest += n;
  56. while ( n-- )
  57. {
  58. dest -= 1;
  59. src -= 1;
  60. *dest = *src;
  61. }
  62. }
  63. return s1;
  64. }
  65. // From PDClib
  66. void * memset( void * s, int c, size_t n )
  67. {
  68. unsigned char * p = (unsigned char *) s;
  69. while ( n-- )
  70. {
  71. *p = (unsigned char) c;
  72. p += 1;
  73. }
  74. return s;
  75. }
  76. // From PDClib
  77. int memcmp( const void * s1, const void * s2, size_t n )
  78. {
  79. const unsigned char * p1 = (const unsigned char *) s1;
  80. const unsigned char * p2 = (const unsigned char *) s2;
  81. while ( n-- )
  82. {
  83. if ( *p1 != *p2 )
  84. {
  85. return *p1 - *p2;
  86. }
  87. p1 += 1;
  88. p2 += 1;
  89. }
  90. return 0;
  91. }
  92. // From PDClib
  93. char * strcpy( char * s1, const char * s2 )
  94. {
  95. char * rc = s1;
  96. while ( ( *s1++ = *s2++ ) );
  97. return rc;
  98. }
  99. // From PDClib
  100. char * strchr( const char * s, int c )
  101. {
  102. do
  103. {
  104. if ( *s == (char) c )
  105. {
  106. return (char *) s;
  107. }
  108. } while ( *s++ );
  109. return NULL;
  110. }
  111. // From PDClib
  112. int strncmp( const char * s1, const char * s2, size_t n )
  113. {
  114. while ( n && *s1 && ( *s1 == *s2 ) )
  115. {
  116. ++s1;
  117. ++s2;
  118. --n;
  119. }
  120. if ( n == 0 )
  121. {
  122. return 0;
  123. }
  124. else
  125. {
  126. return ( *(unsigned char *)s1 - *(unsigned char *)s2 );
  127. }
  128. }
  129. // From PDClib
  130. char * strrchr( const char * s, int c )
  131. {
  132. size_t i = 0;
  133. while ( s[i++] );
  134. do
  135. {
  136. if ( s[--i] == (char) c )
  137. {
  138. return (char *) s + i;
  139. }
  140. } while ( i );
  141. return NULL;
  142. }
  143. // From PDClib
  144. void * memchr( const void * s, int c, size_t n )
  145. {
  146. const unsigned char * p = (const unsigned char *) s;
  147. while ( n-- )
  148. {
  149. if ( *p == (unsigned char) c )
  150. {
  151. return (void *) p;
  152. }
  153. ++p;
  154. }
  155. return NULL;
  156. }
  157. // From PDClib
  158. char * strcat( char * s1, const char * s2 )
  159. {
  160. char * rc = s1;
  161. if ( *s1 )
  162. {
  163. while ( *++s1 );
  164. }
  165. while ( (*s1++ = *s2++) );
  166. return rc;
  167. }
  168. // From PDClib
  169. char * strncpy( char * s1, const char * s2, size_t n )
  170. {
  171. char * rc = s1;
  172. while ( n && ( *s1++ = *s2++ ) )
  173. {
  174. /* Cannot do "n--" in the conditional as size_t is unsigned and we have
  175. to check it again for >0 in the next loop below, so we must not risk
  176. underflow.
  177. */
  178. --n;
  179. }
  180. /* Checking against 1 as we missed the last --n in the loop above. */
  181. while ( n-- > 1 )
  182. {
  183. *s1++ = '\0';
  184. }
  185. return rc;
  186. }
  187. #include "stdlib.h"
  188. char *strdup(const char *s) {
  189. char *r = malloc(strlen(s+1));
  190. strcpy(r, s);
  191. return r;
  192. }
  193. #endif