]> git.meshlink.io Git - meshlink/blob - src/dropin.c
Remove unused functions and #includes.
[meshlink] / src / dropin.c
1 /*
2     dropin.c -- a set of drop-in replacements for libc functions
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
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.
9
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.
14
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.
18 */
19
20 #include "system.h"
21
22 #include "xalloc.h"
23
24 #ifndef HAVE_GET_CURRENT_DIR_NAME
25 /*
26   Replacement for the GNU get_current_dir_name function:
27
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.
31 */
32 char *get_current_dir_name(void) {
33         size_t size;
34         char *buf;
35         char *r;
36
37         /* Start with 100 bytes.  If this turns out to be insufficient to
38            contain the working directory, double the size.  */
39         size = 100;
40         buf = xmalloc(size);
41
42         errno = 0;                                      /* Success */
43         r = getcwd(buf, size);
44
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) {
48                 free(buf);
49                 size <<= 1;                             /* double the size */
50                 buf = xmalloc(size);
51                 r = getcwd(buf, size);
52         }
53
54         return buf;
55 }
56 #endif
57
58 #ifndef HAVE_ASPRINTF
59 int asprintf(char **buf, const char *fmt, ...) {
60         int result;
61         va_list ap;
62         va_start(ap, fmt);
63         result = vasprintf(buf, fmt, ap);
64         va_end(ap);
65         return result;
66 }
67
68 int vasprintf(char **buf, const char *fmt, va_list ap) {
69         int status;
70         va_list aq;
71         int len;
72
73         len = 4096;
74         *buf = xmalloc(len);
75
76         va_copy(aq, ap);
77         status = vsnprintf(*buf, len, fmt, aq);
78         va_end(aq);
79
80         if(status >= 0)
81                 *buf = xrealloc(*buf, status + 1);
82
83         if(status > len - 1) {
84                 len = status;
85                 va_copy(aq, ap);
86                 status = vsnprintf(*buf, len, fmt, aq);
87                 va_end(aq);
88         }
89
90         return status;
91 }
92 #endif
93
94 #ifndef HAVE_GETTIMEOFDAY
95 int gettimeofday(struct timeval *tv, void *tz) {
96 #ifdef HAVE_MINGW
97         FILETIME ft;
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;
103 #else
104 #warning No high resolution time source!
105         tv->tv_sec = time(NULL);
106         tv->tv_usec = 0;
107 #endif
108         return 0;
109 }
110 #endif
111
112 #ifndef HAVE_USLEEP
113 int usleep(long long usec) {
114         struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
115         select(0, NULL, NULL, NULL, &tv);
116         return 0;
117 }
118 #endif