replace.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #ifndef __UCLIBC__
  2. #include <stdlib.h>
  3. #include <string.h>
  4. /* like strncpy but does not 0 fill the buffer and always null
  5. terminates. bufsize is the size of the destination buffer */
  6. size_t rep_strlcpy(char *d, const char *s, size_t bufsize)
  7. {
  8. size_t len = strlen(s);
  9. size_t ret = len;
  10. if (bufsize <= 0) return 0;
  11. if (len >= bufsize) len = bufsize-1;
  12. memcpy(d, s, len);
  13. d[len] = 0;
  14. return ret;
  15. }
  16. /* like strncat but does not 0 fill the buffer and always null
  17. terminates. bufsize is the length of the buffer, which should
  18. be one more than the maximum resulting string length */
  19. size_t rep_strlcat(char *d, const char *s, size_t bufsize)
  20. {
  21. size_t len1 = strlen(d);
  22. size_t len2 = strlen(s);
  23. size_t ret = len1 + len2;
  24. if (len1+len2 >= bufsize) {
  25. if (bufsize < (len1+1)) {
  26. return ret;
  27. }
  28. len2 = bufsize - (len1+1);
  29. }
  30. if (len2 > 0) {
  31. memcpy(d+len1, s, len2);
  32. d[len1+len2] = 0;
  33. }
  34. return ret;
  35. }
  36. #endif