2 * Reimplementations of standard functions for platforms that don't have them.
4 * Copyright (C) 1998 Andrew Tridgell
5 * Copyright (C) 2002 Martin Pool
6 * Copyright (C) 2004-2020 Wayne Davison
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.
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.
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.
25 static char number_separator
;
30 int len
= strlen(s
) + 1;
31 char *ret
= (char *)malloc(len
);
39 char *getcwd(char *buf
, int size
)
47 pid_t
waitpid(pid_t pid
, int *statptr
, int options
)
50 return wait4(pid
, statptr
, options
, NULL
);
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) {
63 while (((tmp_pid
= wait3(statptr
, options
, 0)) != pid
) &&
64 (tmp_pid
!= -1) && (tmp_pid
!= 0) && (pid
!= -1))
73 void *memmove(void *dest
, const void *src
, size_t n
)
75 bcopy((char *) src
, (char *) dest
, n
);
82 * Find the first occurrence in @p s of any character in @p accept.
86 char *strpbrk(const char *s
, const char *accept
)
89 const char *a
= accept
;
91 if (*a
++ == *s
) return (char *)s
;
103 * Like strncpy but does not 0 fill the buffer and always null
106 * @param bufsize is the size of the destination buffer.
108 * @return index of the terminating byte.
110 size_t strlcpy(char *d
, const char *s
, size_t bufsize
)
112 size_t len
= strlen(s
);
126 * Like strncat() but does not 0 fill the buffer and always null
129 * @param bufsize length of the buffer, which should be one more than
130 * the maximum resulting string length.
132 size_t strlcat(char *d
, const char *s
, size_t bufsize
)
134 size_t len1
= strlen(d
);
135 size_t len2
= strlen(s
);
136 size_t ret
= len1
+ len2
;
138 if (len1
< bufsize
- 1) {
139 if (len2
>= bufsize
- len1
)
140 len2
= bufsize
- len1
- 1;
141 memcpy(d
+len1
, s
, len2
);
148 /* some systems don't take the 2nd argument */
149 int sys_gettimeofday(struct timeval
*tv
)
151 #ifdef HAVE_GETTIMEOFDAY_TZ
152 return gettimeofday(tv
, NULL
);
154 return gettimeofday(tv
);
158 #define HUMANIFY(mult) \
160 if (num >= mult || num <= -mult) { \
161 double dnum = (double)num / mult; \
167 else if ((dnum /= mult) < mult) \
169 else if ((dnum /= mult) < mult) \
177 snprintf(bufs[n], sizeof bufs[0], "%.2f%c", dnum, units); \
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
)
188 static char bufs
[4][128]; /* more than enough room */
189 static unsigned int n
;
193 if (human_flag
&& !number_separator
) {
195 snprintf(buf
, sizeof buf
, "%f", 3.14);
196 if (strchr(buf
, '.') != NULL
)
197 number_separator
= ',';
199 number_separator
= '.';
202 n
= (n
+ 1) % (sizeof bufs
/ sizeof bufs
[0]);
204 if (human_flag
> 1) {
211 s
= bufs
[n
] + sizeof bufs
[0] - 1;
215 strlcpy(s
, fract
, len
+ 1);
224 /* A maximum-size negated number can't fit as a positive,
225 * so do one digit in negated form to start us off. */
226 *--s
= (char)(-(num
% 10)) + '0';
236 *--s
= number_separator
;
241 *--s
= (char)(num
% 10) + '0';
251 /* Return the double number as a string. If the human_flag option is > 1,
252 * we may output the number in K, M, G, or T units. The buffer we use for
253 * our result is either a single static buffer defined here, or a buffer
254 * we get from do_big_num(). */
255 char *do_big_dnum(double dnum
, int human_flag
, int decimal_digits
)
257 static char tmp_buf
[128];
258 #if SIZEOF_INT64 >= 8
261 snprintf(tmp_buf
, sizeof tmp_buf
, "%.*f", decimal_digits
, dnum
);
263 if (!human_flag
|| (dnum
< 1000.0 && dnum
> -1000.0))
266 for (fract
= tmp_buf
+1; isDigit(fract
); fract
++) {}
268 return do_big_num((int64
)dnum
, human_flag
, fract
);
270 /* A big number might lose digits converting to a too-short int64,
271 * so let's just return the raw double conversion. */
272 snprintf(tmp_buf
, sizeof tmp_buf
, "%.*f", decimal_digits
, dnum
);