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