/
compat.c
164 lines (141 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
* OpenConnect (SSL + DTLS) VPN client
*
* Copyright © 2008-2012 Intel Corporation.
*
* Authors: David Woodhouse <dwmw2@infradead.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to:
*
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "openconnect-internal.h"
#ifdef __sun__
#include <sys/time.h>
time_t openconnect__time(time_t *t)
{
time_t s = gethrtime() / 1000000000LL;
if (t)
*t = s;
return s;
}
#endif
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef HAVE_ASPRINTF
#include <stdarg.h>
#include <errno.h>
static int oc_vasprintf(char **strp, const char *fmt, va_list ap)
{
va_list ap2;
char *res = NULL;
int len = 160, len2;
int ret = 0;
int errno_save = -ENOMEM;
res = malloc(160);
if (!res)
goto err;
/* Use a copy of 'ap', preserving it in case we need to retry into
a larger buffer. 160 characters should be sufficient for most
strings in openconnect. */
#ifdef HAVE_VA_COPY
va_copy(ap2, ap);
61
#elif defined(HAVE___VA_COPY)
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
__va_copy(ap2, ap);
#else
#error No va_copy()!
// You could try this.
ap2 = ap;
// Or this
*ap2 = *ap;
#endif
len = vsnprintf(res, 160, fmt, ap2);
va_end(ap2);
if (len < 0) {
printf_err:
errno_save = errno;
free(res);
res = NULL;
goto err;
}
80
if (len >= 0 && len < 160)
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
goto out;
free(res);
res = malloc(len+1);
if (!res)
goto err;
len2 = vsnprintf(res, len+1, fmt, ap);
if (len2 < 0 || len2 > len)
goto printf_err;
ret = 0;
goto out;
err:
errno = errno_save;
ret = -1;
out:
*strp = res;
return ret;
}
int openconnect__asprintf(char **strp, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = oc_vasprintf(strp, fmt, ap);
va_end(ap);
return ret;
}
#endif
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef HAVE_GETLINE
ssize_t openconnect__getline(char **lineptr, size_t *n, FILE *stream)
{
int len = 0;
if (!*lineptr) {
*n = 2;
*lineptr = malloc(*n);
if (!*lineptr)
return -1;
}
while (fgets((*lineptr) + len, (*n) - len, stream)) {
len += strlen((*lineptr) + len);
if ((*lineptr)[len-1] == '\n')
break;
*n *= 2;
134
realloc_inplace(*lineptr, *n);
135
136
137
138
139
140
141
142
if (!*lineptr)
return -1;
}
if (len)
return len;
return -1;
}
#endif
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ifndef HAVE_STRCASESTR
#include <ctype.h>
char *openconnect__strcasestr(const char *haystack, const char *needle)
{
int hlen = strlen(haystack);
int nlen = strlen(needle);
int i, j;
for (i = 0; i < hlen - nlen + 1; i++) {
for (j = 0; j < nlen; j++) {
if (tolower(haystack[i + j]) !=
tolower(needle[j]))
break;
}
if (j == nlen)
return (char *)haystack + i;
}
return NULL;
}
#endif