compat.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Reimplementations of standard functions for platforms that don't have them.
  3. *
  4. * Copyright (C) 1998 Andrew Tridgell
  5. * Copyright (C) 2002 Martin Pool
  6. * Copyright (C) 2004, 2005, 2006 Wayne Davison
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, visit the http://fsf.org website.
  20. */
  21. #include "rsync.h"
  22. #ifndef HAVE_STRDUP
  23. char *strdup(char *s)
  24. {
  25. int len = strlen(s) + 1;
  26. char *ret = (char *)malloc(len);
  27. if (ret)
  28. memcpy(ret, s, len);
  29. return ret;
  30. }
  31. #endif
  32. #ifndef HAVE_GETCWD
  33. char *getcwd(char *buf, int size)
  34. {
  35. return getwd(buf);
  36. }
  37. #endif
  38. #ifndef HAVE_WAITPID
  39. pid_t waitpid(pid_t pid, int *statptr, int options)
  40. {
  41. #ifdef HAVE_WAIT4
  42. return wait4(pid, statptr, options, NULL);
  43. #else
  44. /* If wait4 is also not available, try wait3 for SVR3 variants */
  45. /* Less ideal because can't actually request a specific pid */
  46. /* At least the WNOHANG option is supported */
  47. /* Code borrowed from apache fragment written by dwd@bell-labs.com */
  48. int tmp_pid, dummystat;;
  49. if (kill(pid, 0) == -1) {
  50. errno = ECHILD;
  51. return -1;
  52. }
  53. if (statptr == NULL)
  54. statptr = &dummystat;
  55. while (((tmp_pid = wait3(statptr, options, 0)) != pid) &&
  56. (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
  57. ;
  58. return tmp_pid;
  59. #endif
  60. }
  61. #endif
  62. #ifndef HAVE_MEMMOVE
  63. void *memmove(void *dest, const void *src, size_t n)
  64. {
  65. bcopy((char *) src, (char *) dest, n);
  66. return dest;
  67. }
  68. #endif
  69. #ifndef HAVE_STRPBRK
  70. /**
  71. * Find the first ocurrence in @p s of any character in @p accept.
  72. *
  73. * Derived from glibc
  74. **/
  75. char *strpbrk(const char *s, const char *accept)
  76. {
  77. while (*s != '\0') {
  78. const char *a = accept;
  79. while (*a != '\0') {
  80. if (*a++ == *s) return (char *)s;
  81. }
  82. ++s;
  83. }
  84. return NULL;
  85. }
  86. #endif
  87. #ifndef HAVE_STRLCPY
  88. /**
  89. * Like strncpy but does not 0 fill the buffer and always null
  90. * terminates.
  91. *
  92. * @param bufsize is the size of the destination buffer.
  93. *
  94. * @return index of the terminating byte.
  95. **/
  96. size_t strlcpy(char *d, const char *s, size_t bufsize)
  97. {
  98. size_t len = strlen(s);
  99. size_t ret = len;
  100. if (bufsize > 0) {
  101. if (len >= bufsize)
  102. len = bufsize-1;
  103. memcpy(d, s, len);
  104. d[len] = 0;
  105. }
  106. return ret;
  107. }
  108. #endif
  109. #ifndef HAVE_STRLCAT
  110. /**
  111. * Like strncat() but does not 0 fill the buffer and always null
  112. * terminates.
  113. *
  114. * @param bufsize length of the buffer, which should be one more than
  115. * the maximum resulting string length.
  116. **/
  117. size_t strlcat(char *d, const char *s, size_t bufsize)
  118. {
  119. size_t len1 = strlen(d);
  120. size_t len2 = strlen(s);
  121. size_t ret = len1 + len2;
  122. if (len1 < bufsize - 1) {
  123. if (len2 >= bufsize - len1)
  124. len2 = bufsize - len1 - 1;
  125. memcpy(d+len1, s, len2);
  126. d[len1+len2] = 0;
  127. }
  128. return ret;
  129. }
  130. #endif
  131. /* some systems don't take the 2nd argument */
  132. int sys_gettimeofday(struct timeval *tv)
  133. {
  134. #ifdef HAVE_GETTIMEOFDAY_TZ
  135. return gettimeofday(tv, NULL);
  136. #else
  137. return gettimeofday(tv);
  138. #endif
  139. }