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