diff options
| author | Brent Cook <bcook@openbsd.org> | 2015-10-14 23:53:52 -0500 |
|---|---|---|
| committer | Brent Cook <bcook@openbsd.org> | 2015-10-15 09:16:38 -0500 |
| commit | 4298ac9305d89cadf90ebb01f0e85bb74fea2c1e (patch) | |
| tree | e58171fce41da919e0005b167fab19185c4d002d /crypto | |
| parent | 1dd79f5d8f617afdb0f5d51ac0278d7b4f84b82f (diff) | |
| download | portable-4298ac9305d89cadf90ebb01f0e85bb74fea2c1e.tar.gz portable-4298ac9305d89cadf90ebb01f0e85bb74fea2c1e.tar.bz2 portable-4298ac9305d89cadf90ebb01f0e85bb74fea2c1e.zip | |
include timegm fallback
Diffstat (limited to 'crypto')
| -rw-r--r-- | crypto/Makefile.am | 6 | ||||
| -rw-r--r-- | crypto/compat/timegm.c | 71 |
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 | |||
| 73 | libcompat_la_SOURCES += compat/inet_pton.c | 73 | libcompat_la_SOURCES += compat/inet_pton.c |
| 74 | endif | 74 | endif |
| 75 | 75 | ||
| 76 | if !HAVE_TIMEGM | ||
| 77 | if !HAVE__MKGMTIME | ||
| 78 | libcompat_la_SOURCES += compat/timegm.c | ||
| 79 | endif | ||
| 80 | endif | ||
| 81 | |||
| 76 | if !HAVE_REALLOCARRAY | 82 | if !HAVE_REALLOCARRAY |
| 77 | libcompat_la_SOURCES += compat/reallocarray.c | 83 | libcompat_la_SOURCES += compat/reallocarray.c |
| 78 | endif | 84 | endif |
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> | ||
| 15 | time_t | ||
| 16 | timegm(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> | ||
| 48 | time_t | ||
| 49 | timegm(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 | ||
