c_string.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "c_string.h"
  2. #include "c_stdlib.h"
  3. // const char *c_strstr(const char * __s1, const char * __s2){
  4. // }
  5. // char *c_strncat(char * s1, const char * s2, size_t n){
  6. // }
  7. // size_t c_strcspn(const char * s1, const char * s2){
  8. // }
  9. // const char *c_strpbrk(const char * s1, const char * s2){
  10. // }
  11. // int c_strcoll(const char * s1, const char * s2){
  12. // }
  13. //
  14. char *c_strdup(const char *c) {
  15. int len = os_strlen(c) + 1;
  16. char *ret = os_malloc(len);
  17. if (ret) {
  18. memcpy(ret, c, len);
  19. }
  20. return ret;
  21. }
  22. /* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */
  23. /*
  24. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  25. *
  26. * Permission to use, copy, modify, and distribute this software for any
  27. * purpose with or without fee is hereby granted, provided that the above
  28. * copyright notice and this permission notice appear in all copies.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  31. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  32. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  33. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  34. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  35. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  36. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  37. */
  38. /*
  39. * Copy src to string dst of size siz. At most siz-1 characters
  40. * will be copied. Always NUL terminates (unless siz == 0).
  41. * Returns strlen(src); if retval >= siz, truncation occurred.
  42. */
  43. size_t
  44. c_strlcpy(char *dst, const char *src, size_t siz)
  45. {
  46. register char *d = dst;
  47. register const char *s = src;
  48. register size_t n = siz;
  49. /* Copy as many bytes as will fit */
  50. if (n != 0 && --n != 0) {
  51. do {
  52. if ((*d++ = *s++) == 0)
  53. break;
  54. } while (--n != 0);
  55. }
  56. /* Not enough room in dst, add NUL and traverse rest of src */
  57. if (n == 0) {
  58. if (siz != 0)
  59. *d = '\0'; /* NUL-terminate dst */
  60. while (*s++)
  61. ;
  62. }
  63. return(s - src - 1); /* count does not include NUL */
  64. }
  65. /* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */
  66. /*
  67. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  68. *
  69. * Permission to use, copy, modify, and distribute this software for any
  70. * purpose with or without fee is hereby granted, provided that the above
  71. * copyright notice and this permission notice appear in all copies.
  72. *
  73. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  74. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  75. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  76. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  77. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  78. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  79. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  80. */
  81. /*
  82. * Appends src to string dst of size siz (unlike strncat, siz is the
  83. * full size of dst, not space left). At most siz-1 characters
  84. * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
  85. * Returns strlen(src) + MIN(siz, strlen(initial dst)).
  86. * If retval >= siz, truncation occurred.
  87. */
  88. size_t
  89. c_strlcat(char *dst, const char *src, size_t siz)
  90. {
  91. register char *d = dst;
  92. register const char *s = src;
  93. register size_t n = siz;
  94. size_t dlen;
  95. /* Find the end of dst and adjust bytes left but don't go past end */
  96. while (n-- != 0 && *d != '\0')
  97. d++;
  98. dlen = d - dst;
  99. n = siz - dlen;
  100. if (n == 0)
  101. return(dlen + strlen(s));
  102. while (*s != '\0') {
  103. if (n != 1) {
  104. *d++ = *s;
  105. n--;
  106. }
  107. s++;
  108. }
  109. *d = '\0';
  110. return(dlen + (s - src)); /* count does not include NUL */
  111. }