aboutsummaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorBrent Cook <bcook@openbsd.org>2015-10-14 23:53:52 -0500
committerBrent Cook <bcook@openbsd.org>2015-10-15 09:16:38 -0500
commit4298ac9305d89cadf90ebb01f0e85bb74fea2c1e (patch)
treee58171fce41da919e0005b167fab19185c4d002d /crypto
parent1dd79f5d8f617afdb0f5d51ac0278d7b4f84b82f (diff)
downloadportable-4298ac9305d89cadf90ebb01f0e85bb74fea2c1e.tar.gz
portable-4298ac9305d89cadf90ebb01f0e85bb74fea2c1e.tar.bz2
portable-4298ac9305d89cadf90ebb01f0e85bb74fea2c1e.zip
include timegm fallback
Diffstat (limited to 'crypto')
-rw-r--r--crypto/Makefile.am6
-rw-r--r--crypto/compat/timegm.c71
2 files changed, 77 insertions, 0 deletions
diff --git a/crypto/Makefile.am b/crypto/Makefile.am
index e1e785d..40d82cd 100644
--- a/crypto/Makefile.am
+++ b/crypto/Makefile.am
@@ -73,6 +73,12 @@ if !HAVE_INET_PTON
73libcompat_la_SOURCES += compat/inet_pton.c 73libcompat_la_SOURCES += compat/inet_pton.c
74endif 74endif
75 75
76if !HAVE_TIMEGM
77if !HAVE__MKGMTIME
78libcompat_la_SOURCES += compat/timegm.c
79endif
80endif
81
76if !HAVE_REALLOCARRAY 82if !HAVE_REALLOCARRAY
77libcompat_la_SOURCES += compat/reallocarray.c 83libcompat_la_SOURCES += compat/reallocarray.c
78endif 84endif
diff --git a/crypto/compat/timegm.c b/crypto/compat/timegm.c
new file mode 100644
index 0000000..2007140
--- /dev/null
+++ b/crypto/compat/timegm.c
@@ -0,0 +1,71 @@
1/*
2 * timegm shims based on example code from in the glibc timegm manpage.
3 *
4 * These should be replaced with lockless versions that do not require
5 * modifying global state.
6 */
7
8#include <time.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12
13#ifdef _WIN32
14#include <windows.h>
15time_t
16timegm(struct tm *tm)
17{
18 time_t ret;
19 char *tz, *buf;
20 static volatile HANDLE mtx = NULL;
21
22 if (!mtx) {
23 HANDLE p = CreateMutex(NULL, FALSE, NULL);
24 if (InterlockedCompareExchangePointer(
25 (void **)&mtx, (void *)p, NULL))
26 CloseHandle(p);
27 }
28 WaitForSingleObject(mtx, INFINITE);
29 tz = getenv("TZ");
30 if (tz) {
31 if (asprintf(&buf, "TZ=%s", tz) == -1)
32 buf = NULL;
33 }
34 putenv("TZ=UTC");
35 tzset();
36 ret = mktime(tm);
37 if (buf) {
38 putenv(buf);
39 free(buf);
40 } else
41 putenv("TZ=");
42 tzset();
43 ReleaseMutex(mtx);
44 return ret;
45}
46#else
47#include <pthread.h>
48time_t
49timegm(struct tm *tm)
50{
51 time_t ret;
52 char *tz;
53 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
54
55 pthread_mutex_lock(&mtx);
56 tz = getenv("TZ");
57 if (tz)
58 tz = strdup(tz);
59 setenv("TZ", "", 1);
60 tzset();
61 ret = mktime(tm);
62 if (tz) {
63 setenv("TZ", tz, 1);
64 free(tz);
65 } else
66 unsetenv("TZ");
67 tzset();
68 pthread_mutex_unlock(&mtx);
69 return ret;
70}
71#endif