/components/security/linksecurity/tee/csky/ck802t/test/test_bed/libs/libc/string.c

https://github.com/alibaba/AliOS-Things · C · 131 lines · 105 code · 22 blank · 4 comment · 50 complexity · ef1dcb8d1668dec798118338adb30efc MD5 · raw file

  1. /**
  2. * Copyright (C) 2015-2017 Alibaba Group Holding Limited
  3. *
  4. */
  5. #include "test_bed.h"
  6. int8_t *strcpy(int8_t *dest, const int8_t *src)
  7. {
  8. register int8_t *ret = dest;
  9. if (NULL == dest || NULL == src) {
  10. return NULL;
  11. }
  12. while ((*dest++ = *src++))
  13. ;
  14. return ret;
  15. }
  16. int8_t *strncpy(int8_t *dest, const int8_t *src, size_t n)
  17. {
  18. register int8_t *ret = dest;
  19. if (NULL == dest || NULL == src) {
  20. return NULL;
  21. }
  22. while (n && (*dest++ = *src++))
  23. n--;
  24. while (n--) {
  25. *dest++ = '\0';
  26. }
  27. return ret;
  28. }
  29. int32_t strcmp(const int8_t *s1, const int8_t *s2)
  30. {
  31. register int32_t ret = 0;
  32. if (NULL == s1 || NULL == s2) {
  33. while (1)
  34. ; /* FIXME(junlin) should be panic? */
  35. }
  36. while (!(ret = (*s1 - *s2++)) && *s1++)
  37. ;
  38. return ret;
  39. }
  40. int32_t strncmp(const int8_t *s1, const int8_t *s2, size_t n)
  41. {
  42. register int32_t ret = 0;
  43. if (NULL == s1 || NULL == s2) {
  44. while (1)
  45. ; /* FIXME(junlin) should be panic? */
  46. }
  47. while (n-- && !(ret = (*s1 - *s2++)) && *s1++)
  48. ;
  49. return ret;
  50. }
  51. size_t strlen(const int8_t *s)
  52. {
  53. register int32_t ret = 0;
  54. if (NULL == s) {
  55. return 0;
  56. }
  57. while (s[ret++])
  58. ;
  59. return ret - 1;
  60. }
  61. int8_t *strrchr(const int8_t *s, int32_t c)
  62. {
  63. register int8_t *ret = NULL;
  64. if (NULL == s) {
  65. return NULL;
  66. }
  67. do {
  68. if (*s == (int8_t)c) {
  69. ret = (int8_t *)s;
  70. }
  71. } while (*s++);
  72. return ret;
  73. }
  74. void *memset(void *s, int32_t c, size_t n)
  75. {
  76. int8_t *xs = s;
  77. if (NULL == s) {
  78. return NULL;
  79. }
  80. while (n--) {
  81. *xs++ = (int8_t)c;
  82. }
  83. return s;
  84. }
  85. void *memcpy(void *dest, const void *src, size_t n)
  86. {
  87. register const int8_t *s = src;
  88. register int8_t * d = dest;
  89. if (NULL == dest || NULL == src) {
  90. return NULL;
  91. }
  92. while (n--) {
  93. d[n] = s[n];
  94. }
  95. return dest;
  96. }
  97. int32_t memcmp(const void *s1, const void *s2, size_t n)
  98. {
  99. register const int8_t *s = s1;
  100. register const int8_t *d = s2;
  101. register int32_t ret = 0;
  102. if (NULL == s1 || NULL == s2) {
  103. while (1)
  104. ; /* FIXME(junlin) should be panic? */
  105. }
  106. while (n-- && !(ret = (*s++ - *d++)))
  107. ;
  108. return ret;
  109. }