Add missing user and sender types to example client's dump code
[libisds.git] / test / simline / unix.c
blobcb214e64aa017a0f0073f3081e552ef6c4fd064c
1 #ifndef _XOPEN_SOURCE
2 /* >= 500: strdup(3) from string.h, strptime(3) from time.h */
3 /* >= 600: setenv(3) */
4 #define _XOPEN_SOURCE 600
5 #endif
6 #include "../test-tools.h"
7 #include <stdlib.h> /* for getenv(), abort() */
8 #include <string.h> /* for strdup() */
9 #include <stdio.h> /* for stderr */
10 #include <time.h>
11 #include "http.h"
13 /* PANIC macro aborts current process without any clean up.
14 * Use it as last resort fatal error solution */
15 #define PANIC(message) { \
16 if (stderr != NULL ) fprintf(stderr, \
17 "SERVER PANIC (%s:%d): %s\n", __FILE__, __LINE__, (message)); \
18 abort(); \
21 static char *tz_orig; /* Copy of original TZ variable */
23 /* Convert UTF-8 @string representation of ISO 8601 date to @time.
24 * XXX: Not all ISO formats are supported */
25 _hidden http_error _server_datestring2tm(const char *string, struct tm *time) {
26 char *offset;
27 if (!string || !time) return HTTP_ERROR_SERVER;
29 /* xsd:date is ISO 8601 string, thus ASCII */
30 offset = strptime(string, "%Y-%m-%d", time);
31 if (offset && *offset == '\0')
32 return HTTP_ERROR_SUCCESS;
34 offset = strptime(string, "%Y%m%d", time);
35 if (offset && *offset == '\0')
36 return HTTP_ERROR_SUCCESS;
38 offset = strptime(string, "%Y-%j", time);
39 if (offset && *offset == '\0')
40 return HTTP_ERROR_SUCCESS;
42 return HTTP_ERROR_SERVER;
45 /* Switches time zone to UTC.
46 * XXX: This is not reentrant and not thread-safe */
47 static void _isds_switch_tz_to_utc(void) {
48 char *tz;
50 tz = getenv("TZ");
51 if (tz) {
52 tz_orig = strdup(tz);
53 if (!tz_orig)
54 PANIC("Can not back original time zone up");
55 } else {
56 tz_orig = NULL;
59 if (setenv("TZ", "", 1))
60 PANIC("Can not change time zone to UTC temporarily");
62 tzset();
66 /* Switches time zone to original value.
67 * XXX: This is not reentrant and not thread-safe */
68 static void _isds_switch_tz_to_native(void) {
69 if (tz_orig) {
70 if (setenv("TZ", tz_orig, 1))
71 PANIC("Can not restore time zone by setting TZ variable");
72 free(tz_orig);
73 tz_orig = NULL;
74 } else {
75 if(unsetenv("TZ"))
76 PANIC("Can not restore time zone by unsetting TZ variable");
78 tzset();
81 /* Convert UTC broken time to time_t.
82 * @broken_utc it time in UTC in broken format. Despite its content is not
83 * touched, it'sw not-const because underlying POSIX function has non-const
84 * signature.
85 * @return (time_t) -1 in case of error */
86 _hidden time_t _isds_timegm(struct tm *broken_utc) {
87 time_t ret;
89 _isds_switch_tz_to_utc();
90 ret = mktime(broken_utc);
91 _isds_switch_tz_to_native();
93 return ret;