]> git.meshlink.io Git - meshlink/blob - src/dropin.c
Update astylerc and reformat the code.
[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
84         if(status > len - 1) {
85                 len = status;
86                 va_copy(aq, ap);
87                 status = vsnprintf(*buf, len, fmt, aq);
88                 va_end(aq);
89         }
90
91         return status;
92 }
93 #endif
94
95 #ifndef HAVE_GETTIMEOFDAY
96 int gettimeofday(struct timeval *tv, void *tz) {
97 #ifdef HAVE_MINGW
98         FILETIME ft;
99         GetSystemTimeAsFileTime(&ft);
100         uint64_t lt = (uint64_t)ft.dwLowDateTime | ((uint64_t)ft.dwHighDateTime << 32);
101         lt -= 116444736000000000ULL;
102         tv->tv_sec = lt / 10000000;
103         tv->tv_usec = (lt / 10) % 1000000;
104 #else
105 #warning No high resolution time source!
106         tv->tv_sec = time(NULL);
107         tv->tv_usec = 0;
108 #endif
109         return 0;
110 }
111 #endif
112
113 #ifndef HAVE_USLEEP
114 int usleep(long long usec) {
115         struct timeval tv = {usec / 1000000, (usec / 1000) % 1000};
116         select(0, NULL, NULL, NULL, &tv);
117         return 0;
118 }
119 #endif