string_brom.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* string.c - common string routines */
  2. /*
  3. * Copyright (c) 2014 Wind River Systems, Inc.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #include <string.h>
  8. #include <stdint.h>
  9. #include <sys/types.h>
  10. #include <soc.h>
  11. /**
  12. *
  13. * @brief Copy a string
  14. *
  15. * @return pointer to destination buffer <d>
  16. */
  17. char *strcpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s)
  18. {
  19. return pbrom_libc_api->p_strcpy(d, s);
  20. }
  21. /**
  22. *
  23. * @brief Copy part of a string
  24. *
  25. * @return pointer to destination buffer <d>
  26. */
  27. char *strncpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s, size_t n)
  28. {
  29. return pbrom_libc_api->p_strncpy(d, s, n);
  30. }
  31. /**
  32. *
  33. * @brief String scanning operation
  34. *
  35. * @return pointer to 1st instance of found byte, or NULL if not found
  36. */
  37. char *strchr(const char *s, int c)
  38. {
  39. return pbrom_libc_api->p_strchr(s, c);
  40. }
  41. /**
  42. *
  43. * @brief String scanning operation
  44. *
  45. * @return pointer to last instance of found byte, or NULL if not found
  46. */
  47. char *strrchr(const char *s, int c)
  48. {
  49. return pbrom_libc_api->p_strrchr(s, c);
  50. }
  51. /**
  52. *
  53. * @brief Get string length
  54. *
  55. * @return number of bytes in string <s>
  56. */
  57. size_t strlen(const char *s)
  58. {
  59. return pbrom_libc_api->p_strlen(s);
  60. }
  61. /**
  62. *
  63. * @brief Get fixed-size string length
  64. *
  65. * @return number of bytes in fixed-size string <s>
  66. */
  67. size_t strnlen(const char *s, size_t maxlen)
  68. {
  69. return pbrom_libc_api->p_strnlen(s, maxlen);
  70. }
  71. /**
  72. *
  73. * @brief Compare two strings
  74. *
  75. * @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
  76. */
  77. int strcmp(const char *s1, const char *s2)
  78. {
  79. return pbrom_libc_api->p_strcmp(s1, s2);
  80. }
  81. /**
  82. *
  83. * @brief Compare part of two strings
  84. *
  85. * @return negative # if <s1> < <s2>, 0 if <s1> == <s2>, else positive #
  86. */
  87. int strncmp(const char *s1, const char *s2, size_t n)
  88. {
  89. return pbrom_libc_api->p_strncmp(s1, s2, n);
  90. }
  91. /**
  92. * @brief Separate `str` by any char in `sep` and return NULL terminated
  93. * sections. Consecutive `sep` chars in `str` are treated as a single
  94. * separator.
  95. *
  96. * @return pointer to NULL terminated string or NULL on errors.
  97. */
  98. char *strtok_r(char *str, const char *sep, char **state)
  99. {
  100. return pbrom_libc_api->p_strtok_r(str, sep, state);
  101. }
  102. char *strcat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src)
  103. {
  104. return pbrom_libc_api->p_strcat(dest, src);
  105. }
  106. char *strncat(char *_MLIBC_RESTRICT dest, const char *_MLIBC_RESTRICT src,
  107. size_t n)
  108. {
  109. return pbrom_libc_api->p_strncat(dest, src, n);
  110. }
  111. /**
  112. *
  113. * @brief Compare two memory areas
  114. *
  115. * @return negative # if <m1> < <m2>, 0 if <m1> == <m2>, else positive #
  116. */
  117. int memcmp(const void *m1, const void *m2, size_t n)
  118. {
  119. return pbrom_libc_api->p_memcmp(m1, m2, n);
  120. }
  121. /**
  122. *
  123. * @brief Copy bytes in memory with overlapping areas
  124. *
  125. * @return pointer to destination buffer <d>
  126. */
  127. void *memmove(void *d, const void *s, size_t n)
  128. {
  129. return pbrom_libc_api->p_memmove(d, s, n);
  130. }
  131. /**
  132. *
  133. * @brief Copy bytes in memory
  134. *
  135. * @return pointer to start of destination buffer
  136. */
  137. void *memcpy(void *_MLIBC_RESTRICT d, const void *_MLIBC_RESTRICT s, size_t n)
  138. {
  139. return pbrom_libc_api->p_memcpy(d, s, n);
  140. }
  141. /**
  142. *
  143. * @brief Set bytes in memory
  144. *
  145. * @return pointer to start of buffer
  146. */
  147. void *memset(void *buf, int c, size_t n)
  148. {
  149. return pbrom_libc_api->p_memset(buf, c, n);
  150. }
  151. /**
  152. *
  153. * @brief Scan byte in memory
  154. *
  155. * @return pointer to start of found byte
  156. */
  157. void *memchr(const void *s, int c, size_t n)
  158. {
  159. return pbrom_libc_api->p_memchr(s, c, n);
  160. }