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 | |
8e41b68e WD |
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. | |
0f78b815 WD |
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 | 18 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 19 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 | 20 | */ |
ec0e5ac0 | 21 | |
ec0e5ac0 AT |
22 | #include "rsync.h" |
23 | ||
ec0e5ac0 AT |
24 | #ifndef HAVE_STRDUP |
25 | char *strdup(char *s) | |
26 | { | |
204f4f4d WD |
27 | int len = strlen(s) + 1; |
28 | char *ret = (char *)malloc(len); | |
29 | if (ret) | |
30 | memcpy(ret, s, len); | |
31 | return ret; | |
ec0e5ac0 AT |
32 | } |
33 | #endif | |
34 | ||
35 | #ifndef HAVE_GETCWD | |
5a788ade | 36 | char *getcwd(char *buf, int size) |
ec0e5ac0 AT |
37 | { |
38 | return getwd(buf); | |
39 | } | |
40 | #endif | |
41 | ||
42 | ||
43 | #ifndef HAVE_WAITPID | |
5a788ade | 44 | pid_t waitpid(pid_t pid, int *statptr, int options) |
ec0e5ac0 | 45 | { |
e68f3481 | 46 | #ifdef HAVE_WAIT4 |
ec0e5ac0 | 47 | return wait4(pid, statptr, options, NULL); |
e68f3481 DD |
48 | #else |
49 | /* If wait4 is also not available, try wait3 for SVR3 variants */ | |
50 | /* Less ideal because can't actually request a specific pid */ | |
51 | /* At least the WNOHANG option is supported */ | |
52 | /* Code borrowed from apache fragment written by dwd@bell-labs.com */ | |
53 | int tmp_pid, dummystat;; | |
54 | if (kill(pid, 0) == -1) { | |
55 | errno = ECHILD; | |
56 | return -1; | |
57 | } | |
58 | if (statptr == NULL) | |
59 | statptr = &dummystat; | |
60 | while (((tmp_pid = wait3(statptr, options, 0)) != pid) && | |
61 | (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1)) | |
62 | ; | |
63 | return tmp_pid; | |
64 | #endif | |
ec0e5ac0 AT |
65 | } |
66 | #endif | |
67 | ||
9fc310da AT |
68 | |
69 | #ifndef HAVE_MEMMOVE | |
5a788ade | 70 | void *memmove(void *dest, const void *src, size_t n) |
9fc310da | 71 | { |
52d7d788 | 72 | bcopy((char *) src, (char *) dest, n); |
9fc310da AT |
73 | return dest; |
74 | } | |
75 | #endif | |
2b6b4d53 AT |
76 | |
77 | #ifndef HAVE_STRPBRK | |
e0fde757 MP |
78 | /** |
79 | * Find the first ocurrence in @p s of any character in @p accept. | |
80 | * | |
0f78b815 | 81 | * Derived from glibc |
e0fde757 | 82 | **/ |
5a788ade | 83 | char *strpbrk(const char *s, const char *accept) |
2b6b4d53 AT |
84 | { |
85 | while (*s != '\0') { | |
86 | const char *a = accept; | |
87 | while (*a != '\0') { | |
88 | if (*a++ == *s) return (char *)s; | |
89 | } | |
90 | ++s; | |
91 | } | |
92 | ||
93 | return NULL; | |
94 | } | |
95 | #endif | |
7b3d4257 | 96 | |
5a788ade AT |
97 | |
98 | #ifndef HAVE_STRLCPY | |
e0fde757 | 99 | /** |
0f78b815 | 100 | * Like strncpy but does not 0 fill the buffer and always null |
e0fde757 MP |
101 | * terminates. |
102 | * | |
103 | * @param bufsize is the size of the destination buffer. | |
c7677b89 | 104 | * |
e0fde757 MP |
105 | * @return index of the terminating byte. |
106 | **/ | |
5a788ade AT |
107 | size_t strlcpy(char *d, const char *s, size_t bufsize) |
108 | { | |
109 | size_t len = strlen(s); | |
110 | size_t ret = len; | |
72d45525 WD |
111 | if (bufsize > 0) { |
112 | if (len >= bufsize) | |
113 | len = bufsize-1; | |
114 | memcpy(d, s, len); | |
115 | d[len] = 0; | |
116 | } | |
5a788ade AT |
117 | return ret; |
118 | } | |
119 | #endif | |
120 | ||
121 | #ifndef HAVE_STRLCAT | |
e0fde757 | 122 | /** |
0f78b815 | 123 | * Like strncat() but does not 0 fill the buffer and always null |
e0fde757 MP |
124 | * terminates. |
125 | * | |
126 | * @param bufsize length of the buffer, which should be one more than | |
127 | * the maximum resulting string length. | |
128 | **/ | |
5a788ade AT |
129 | size_t strlcat(char *d, const char *s, size_t bufsize) |
130 | { | |
131 | size_t len1 = strlen(d); | |
132 | size_t len2 = strlen(s); | |
133 | size_t ret = len1 + len2; | |
134 | ||
1fb8ec4b WD |
135 | if (len1 < bufsize - 1) { |
136 | if (len2 >= bufsize - len1) | |
137 | len2 = bufsize - len1 - 1; | |
5a788ade AT |
138 | memcpy(d+len1, s, len2); |
139 | d[len1+len2] = 0; | |
140 | } | |
141 | return ret; | |
142 | } | |
143 | #endif | |
b17bc22b | 144 | |
3060d4aa AT |
145 | /* some systems don't take the 2nd argument */ |
146 | int sys_gettimeofday(struct timeval *tv) | |
147 | { | |
4f5b0756 | 148 | #ifdef HAVE_GETTIMEOFDAY_TZ |
3060d4aa AT |
149 | return gettimeofday(tv, NULL); |
150 | #else | |
151 | return gettimeofday(tv); | |
152 | #endif | |
153 | } | |
3a8fad78 WD |
154 | |
155 | /* Return the int64 number as a string. If the human_flag arg is non-zero, | |
156 | * we may output the number in K, M, or G units. We can return up to 4 | |
157 | * buffers at a time. */ | |
158 | char *big_num(int64 num, int human_flag) | |
159 | { | |
160 | static char bufs[4][128]; /* more than enough room */ | |
161 | static unsigned int n; | |
162 | char *s; | |
163 | ||
164 | n = (n + 1) % (sizeof bufs / sizeof bufs[0]); | |
165 | ||
166 | if (human_flag) { | |
167 | char units = '\0'; | |
168 | int mult = human_flag == 1 ? 1000 : 1024; | |
169 | double dnum = 0; | |
170 | if (num > mult*mult*mult) { | |
171 | dnum = (double)num / (mult*mult*mult); | |
172 | units = 'G'; | |
173 | } else if (num > mult*mult) { | |
174 | dnum = (double)num / (mult*mult); | |
175 | units = 'M'; | |
176 | } else if (num > mult) { | |
177 | dnum = (double)num / mult; | |
178 | units = 'K'; | |
179 | } | |
180 | if (units) { | |
181 | snprintf(bufs[n], sizeof bufs[0], "%.2f%c", dnum, units); | |
182 | return bufs[n]; | |
183 | } | |
184 | } | |
185 | ||
186 | s = bufs[n] + sizeof bufs[0] - 1; | |
187 | *s = '\0'; | |
188 | ||
189 | if (!num) | |
190 | *--s = '0'; | |
191 | while (num) { | |
192 | *--s = (char)(num % 10) + '0'; | |
193 | num /= 10; | |
194 | } | |
195 | return s; | |
196 | } |