2 dropin.c -- a set of drop-in replacements for libc functions
3 Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #ifndef HAVE_GET_CURRENT_DIR_NAME
26 Replacement for the GNU get_current_dir_name function:
28 get_current_dir_name will malloc(3) an array big enough to hold the
29 current directory name. If the environment variable PWD is set, and
30 its value is correct, then that value will be returned.
32 char *get_current_dir_name(void) {
37 /* Start with 100 bytes. If this turns out to be insufficient to
38 contain the working directory, double the size. */
42 errno = 0; /* Success */
43 r = getcwd(buf, size);
45 /* getcwd returns NULL and sets errno to ERANGE if the bufferspace
46 is insufficient to contain the entire working directory. */
47 while(r == NULL && errno == ERANGE) {
49 size <<= 1; /* double the size */
51 r = getcwd(buf, size);
59 int asprintf(char **buf, const char *fmt, ...) {
63 result = vasprintf(buf, fmt, ap);
68 int vasprintf(char **buf, const char *fmt, va_list ap) {
77 status = vsnprintf(*buf, len, fmt, aq);
81 *buf = xrealloc(*buf, status + 1);
83 if(status > len - 1) {
86 status = vsnprintf(*buf, len, fmt, aq);
94 #ifndef HAVE_GETTIMEOFDAY
95 int gettimeofday(struct timeval *tv, void *tz) {
98 GetSystemTimeAsFileTime(&ft);
99 uint64_t lt = (uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32);
100 lt -= 116444736000000000ULL;
101 tv->tv_sec = lt / 10000000;
102 tv->tv_usec = (lt / 10) % 1000000;
104 #warning No high resolution time source!
105 tv->tv_sec = time(NULL);
113 int usleep(long long usec) {
114 struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
115 select(0, NULL, NULL, NULL, &tv);