Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* |
2 | * Reimplementations of standard functions for platforms that don't have them. | |
e0fde757 | 3 | * |
0f78b815 WD |
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 2 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 | * | |
e7c67065 WD |
18 | * You should have received a copy of the GNU General Public License along |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 21 | */ |
ec0e5ac0 | 22 | |
ec0e5ac0 AT |
23 | #include "rsync.h" |
24 | ||
ec0e5ac0 AT |
25 | #ifndef HAVE_STRDUP |
26 | char *strdup(char *s) | |
27 | { | |
28 | int l = strlen(s) + 1; | |
29 | char *ret = (char *)malloc(l); | |
30 | if (ret) | |
31 | strcpy(ret,s); | |
32 | return ret; | |
33 | } | |
34 | #endif | |
35 | ||
36 | #ifndef HAVE_GETCWD | |
5a788ade | 37 | char *getcwd(char *buf, int size) |
ec0e5ac0 AT |
38 | { |
39 | return getwd(buf); | |
40 | } | |
41 | #endif | |
42 | ||
43 | ||
44 | #ifndef HAVE_WAITPID | |
5a788ade | 45 | pid_t waitpid(pid_t pid, int *statptr, int options) |
ec0e5ac0 | 46 | { |
e68f3481 | 47 | #ifdef HAVE_WAIT4 |
ec0e5ac0 | 48 | return wait4(pid, statptr, options, NULL); |
e68f3481 DD |
49 | #else |
50 | /* If wait4 is also not available, try wait3 for SVR3 variants */ | |
51 | /* Less ideal because can't actually request a specific pid */ | |
52 | /* At least the WNOHANG option is supported */ | |
53 | /* Code borrowed from apache fragment written by dwd@bell-labs.com */ | |
54 | int tmp_pid, dummystat;; | |
55 | if (kill(pid, 0) == -1) { | |
56 | errno = ECHILD; | |
57 | return -1; | |
58 | } | |
59 | if (statptr == NULL) | |
60 | statptr = &dummystat; | |
61 | while (((tmp_pid = wait3(statptr, options, 0)) != pid) && | |
62 | (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1)) | |
63 | ; | |
64 | return tmp_pid; | |
65 | #endif | |
ec0e5ac0 AT |
66 | } |
67 | #endif | |
68 | ||
9fc310da AT |
69 | |
70 | #ifndef HAVE_MEMMOVE | |
5a788ade | 71 | void *memmove(void *dest, const void *src, size_t n) |
9fc310da | 72 | { |
52d7d788 | 73 | bcopy((char *) src, (char *) dest, n); |
9fc310da AT |
74 | return dest; |
75 | } | |
76 | #endif | |
2b6b4d53 AT |
77 | |
78 | #ifndef HAVE_STRPBRK | |
e0fde757 MP |
79 | /** |
80 | * Find the first ocurrence in @p s of any character in @p accept. | |
81 | * | |
0f78b815 | 82 | * Derived from glibc |
e0fde757 | 83 | **/ |
5a788ade | 84 | char *strpbrk(const char *s, const char *accept) |
2b6b4d53 AT |
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 | |
7b3d4257 | 97 | |
5a788ade AT |
98 | |
99 | #ifndef HAVE_STRLCPY | |
e0fde757 | 100 | /** |
0f78b815 | 101 | * Like strncpy but does not 0 fill the buffer and always null |
e0fde757 MP |
102 | * terminates. |
103 | * | |
104 | * @param bufsize is the size of the destination buffer. | |
c7677b89 | 105 | * |
e0fde757 MP |
106 | * @return index of the terminating byte. |
107 | **/ | |
5a788ade AT |
108 | size_t strlcpy(char *d, const char *s, size_t bufsize) |
109 | { | |
110 | size_t len = strlen(s); | |
111 | size_t ret = len; | |
72d45525 WD |
112 | if (bufsize > 0) { |
113 | if (len >= bufsize) | |
114 | len = bufsize-1; | |
115 | memcpy(d, s, len); | |
116 | d[len] = 0; | |
117 | } | |
5a788ade AT |
118 | return ret; |
119 | } | |
120 | #endif | |
121 | ||
122 | #ifndef HAVE_STRLCAT | |
e0fde757 | 123 | /** |
0f78b815 | 124 | * Like strncat() but does not 0 fill the buffer and always null |
e0fde757 MP |
125 | * terminates. |
126 | * | |
127 | * @param bufsize length of the buffer, which should be one more than | |
128 | * the maximum resulting string length. | |
129 | **/ | |
5a788ade AT |
130 | size_t strlcat(char *d, const char *s, size_t bufsize) |
131 | { | |
132 | size_t len1 = strlen(d); | |
133 | size_t len2 = strlen(s); | |
134 | size_t ret = len1 + len2; | |
135 | ||
1fb8ec4b WD |
136 | if (len1 < bufsize - 1) { |
137 | if (len2 >= bufsize - len1) | |
138 | len2 = bufsize - len1 - 1; | |
5a788ade AT |
139 | memcpy(d+len1, s, len2); |
140 | d[len1+len2] = 0; | |
141 | } | |
142 | return ret; | |
143 | } | |
144 | #endif | |
b17bc22b | 145 | |
3060d4aa AT |
146 | /* some systems don't take the 2nd argument */ |
147 | int sys_gettimeofday(struct timeval *tv) | |
148 | { | |
4f5b0756 | 149 | #ifdef HAVE_GETTIMEOFDAY_TZ |
3060d4aa AT |
150 | return gettimeofday(tv, NULL); |
151 | #else | |
152 | return gettimeofday(tv); | |
153 | #endif | |
154 | } |