2 * OpenConnect (SSL + DTLS) VPN client
4 * Copyright © 2008-2012 Intel Corporation.
6 * Authors: David Woodhouse <dwmw2@infradead.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1, as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA
25 #include "openconnect-internal.h"
30 time_t openconnect__time(time_t *t)
32 time_t s = gethrtime() / 1000000000LL;
44 static int oc_vasprintf(char **strp, const char *fmt, va_list ap)
50 int errno_save = -ENOMEM;
56 /* Use a copy of 'ap', preserving it in case we need to retry into
57 a larger buffer. 160 characters should be sufficient for most
58 strings in openconnect. */
61 #elif defined (HAVE___VA_COPY)
65 // You could try this.
70 len = vsnprintf(res, 160, fmt, ap2);
80 if (len >=0 && len < 160)
88 len2 = vsnprintf(res, len+1, fmt, ap);
89 if (len2 < 0 || len2 > len)
103 int openconnect__asprintf(char **strp, const char *fmt, ...)
109 ret = oc_vasprintf(strp, fmt, ap);
116 ssize_t openconnect__getline(char **lineptr, size_t *n, FILE *stream)
122 *lineptr = malloc(*n);
127 while (fgets((*lineptr) + len, (*n) - len, stream)) {
129 len += strlen((*lineptr) + len);
130 if ((*lineptr)[len-1] == '\n')
134 *lineptr = realloc(*lineptr, *n);
144 #ifndef HAVE_STRCASESTR
147 char *openconnect__strcasestr(const char *haystack, const char *needle)
149 int hlen = strlen(haystack);
150 int nlen = strlen(needle);
153 for (i = 0; i < hlen - nlen + 1; i++) {
154 for (j = 0; j < nlen; j++) {
155 if (tolower(haystack[i + j]) !=
160 return (char *)haystack + i;