| 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 | |
| 22 | #include "rsync.h" |
| 23 | #include "itypes.h" |
| 24 | |
| 25 | extern char number_separator; |
| 26 | |
| 27 | #ifndef HAVE_STRDUP |
| 28 | char *strdup(char *s) |
| 29 | { |
| 30 | int len = strlen(s) + 1; |
| 31 | char *ret = (char *)malloc(len); |
| 32 | if (ret) |
| 33 | memcpy(ret, s, len); |
| 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 | /** |
| 82 | * Find the first ocurrence in @p s of any character in @p accept. |
| 83 | * |
| 84 | * Derived from glibc |
| 85 | **/ |
| 86 | char *strpbrk(const char *s, const char *accept) |
| 87 | { |
| 88 | while (*s != '\0') { |
| 89 | const char *a = accept; |
| 90 | while (*a != '\0') { |
| 91 | if (*a++ == *s) return (char *)s; |
| 92 | } |
| 93 | ++s; |
| 94 | } |
| 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | |
| 101 | #ifndef HAVE_STRLCPY |
| 102 | /** |
| 103 | * Like strncpy but does not 0 fill the buffer and always null |
| 104 | * terminates. |
| 105 | * |
| 106 | * @param bufsize is the size of the destination buffer. |
| 107 | * |
| 108 | * @return index of the terminating byte. |
| 109 | **/ |
| 110 | size_t strlcpy(char *d, const char *s, size_t bufsize) |
| 111 | { |
| 112 | size_t len = strlen(s); |
| 113 | size_t ret = len; |
| 114 | if (bufsize > 0) { |
| 115 | if (len >= bufsize) |
| 116 | len = bufsize-1; |
| 117 | memcpy(d, s, len); |
| 118 | d[len] = 0; |
| 119 | } |
| 120 | return ret; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #ifndef HAVE_STRLCAT |
| 125 | /** |
| 126 | * Like strncat() but does not 0 fill the buffer and always null |
| 127 | * terminates. |
| 128 | * |
| 129 | * @param bufsize length of the buffer, which should be one more than |
| 130 | * the maximum resulting string length. |
| 131 | **/ |
| 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 < bufsize - 1) { |
| 139 | if (len2 >= bufsize - len1) |
| 140 | len2 = bufsize - len1 - 1; |
| 141 | memcpy(d+len1, s, len2); |
| 142 | d[len1+len2] = 0; |
| 143 | } |
| 144 | return ret; |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | /* some systems don't take the 2nd argument */ |
| 149 | int sys_gettimeofday(struct timeval *tv) |
| 150 | { |
| 151 | #ifdef HAVE_GETTIMEOFDAY_TZ |
| 152 | return gettimeofday(tv, NULL); |
| 153 | #else |
| 154 | return gettimeofday(tv); |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | #define HUMANIFY(mult) \ |
| 159 | do { \ |
| 160 | if (num >= mult || num <= -mult) { \ |
| 161 | double dnum = (double)num / mult; \ |
| 162 | char units; \ |
| 163 | if (num < 0) \ |
| 164 | dnum = -dnum; \ |
| 165 | if (dnum < mult) \ |
| 166 | units = 'K'; \ |
| 167 | else if ((dnum /= mult) < mult) \ |
| 168 | units = 'M'; \ |
| 169 | else if ((dnum /= mult) < mult) \ |
| 170 | units = 'G'; \ |
| 171 | else { \ |
| 172 | dnum /= mult; \ |
| 173 | units = 'T'; \ |
| 174 | } \ |
| 175 | if (num < 0) \ |
| 176 | dnum = -dnum; \ |
| 177 | snprintf(bufs[n], sizeof bufs[0], "%.2f%c", dnum, units); \ |
| 178 | return bufs[n]; \ |
| 179 | } \ |
| 180 | } while (0) |
| 181 | |
| 182 | /* Return the int64 number as a string. If the human_flag arg is non-zero, |
| 183 | * we may output the number in K, M, G, or T units. If we don't add a unit |
| 184 | * suffix, we will append the fract string, if it is non-NULL. We can |
| 185 | * return up to 4 buffers at a time. */ |
| 186 | char *do_big_num(int64 num, int human_flag, const char *fract) |
| 187 | { |
| 188 | static char bufs[4][128]; /* more than enough room */ |
| 189 | static unsigned int n; |
| 190 | char *s; |
| 191 | int len, negated; |
| 192 | |
| 193 | n = (n + 1) % (sizeof bufs / sizeof bufs[0]); |
| 194 | |
| 195 | if (human_flag > 1) { |
| 196 | if (human_flag == 2) |
| 197 | HUMANIFY(1000); |
| 198 | else |
| 199 | HUMANIFY(1024); |
| 200 | } |
| 201 | |
| 202 | s = bufs[n] + sizeof bufs[0] - 1; |
| 203 | if (fract) { |
| 204 | len = strlen(fract); |
| 205 | s -= len; |
| 206 | strlcpy(s, fract, len + 1); |
| 207 | } else |
| 208 | *s = '\0'; |
| 209 | |
| 210 | len = 0; |
| 211 | |
| 212 | if (!num) |
| 213 | *--s = '0'; |
| 214 | if (num < 0) { |
| 215 | /* A maximum-size negated number can't fit as a positive, |
| 216 | * so do one digit in negated form to start us off. */ |
| 217 | *--s = (char)(-(num % 10)) + '0'; |
| 218 | num = -(num / 10); |
| 219 | len++; |
| 220 | negated = 1; |
| 221 | } else |
| 222 | negated = 0; |
| 223 | |
| 224 | while (num) { |
| 225 | if (human_flag) { |
| 226 | if (len == 3) { |
| 227 | *--s = number_separator; |
| 228 | len = 1; |
| 229 | } else |
| 230 | len++; |
| 231 | } |
| 232 | *--s = (char)(num % 10) + '0'; |
| 233 | num /= 10; |
| 234 | } |
| 235 | |
| 236 | if (negated) |
| 237 | *--s = '-'; |
| 238 | |
| 239 | return s; |
| 240 | } |
| 241 | |
| 242 | /* Return the double number as a string. If the human_flag option is > 1, |
| 243 | * we may output the number in K, M, G, or T units. The buffer we use for |
| 244 | * our result is either a single static buffer defined here, or a buffer |
| 245 | * we get from do_big_num(). */ |
| 246 | char *do_big_dnum(double dnum, int human_flag, int decimal_digits) |
| 247 | { |
| 248 | static char tmp_buf[128]; |
| 249 | #if SIZEOF_INT64 >= 8 |
| 250 | char *fract; |
| 251 | |
| 252 | snprintf(tmp_buf, sizeof tmp_buf, "%.*f", decimal_digits, dnum); |
| 253 | |
| 254 | if (!human_flag || (dnum < 1000.0 && dnum > -1000.0)) |
| 255 | return tmp_buf; |
| 256 | |
| 257 | for (fract = tmp_buf+1; isDigit(fract); fract++) {} |
| 258 | |
| 259 | return do_big_num((int64)dnum, human_flag, fract); |
| 260 | #else |
| 261 | /* A big number might lose digits converting to a too-short int64, |
| 262 | * so let's just return the raw double conversion. */ |
| 263 | snprintf(tmp_buf, sizeof tmp_buf, "%.*f", decimal_digits, dnum); |
| 264 | return tmp_buf; |
| 265 | #endif |
| 266 | } |