Rework LuCI build system
[project/luci.git] / libs / luci-lib-nixio / axTLS / ssl / os_port.c
1 /*
2 * Copyright (c) 2007, Cameron Rich
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of the axTLS project nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /**
32 * @file os_port.c
33 *
34 * OS specific functions.
35 */
36 #include <time.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include "os_port.h"
41
42 #ifdef WIN32
43 /**
44 * gettimeofday() not in Win32
45 */
46 EXP_FUNC void STDCALL gettimeofday(struct timeval* t, void* timezone)
47 {
48 #if defined(_WIN32_WCE)
49 t->tv_sec = time(NULL);
50 t->tv_usec = 0; /* 1sec precision only */
51 #else
52 struct _timeb timebuffer;
53 _ftime(&timebuffer);
54 t->tv_sec = (long)timebuffer.time;
55 t->tv_usec = 1000 * timebuffer.millitm; /* 1ms precision */
56 #endif
57 }
58
59 /**
60 * strcasecmp() not in Win32
61 */
62 EXP_FUNC int STDCALL strcasecmp(const char *s1, const char *s2)
63 {
64 while (tolower(*s1) == tolower(*s2++))
65 {
66 if (*s1++ == '\0')
67 {
68 return 0;
69 }
70 }
71
72 return *(unsigned char *)s1 - *(unsigned char *)(s2 - 1);
73 }
74
75
76 EXP_FUNC int STDCALL getdomainname(char *buf, int buf_size)
77 {
78 HKEY hKey;
79 unsigned long datatype;
80 unsigned long bufferlength = buf_size;
81
82 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
83 TEXT("SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
84 0, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
85 return -1;
86
87 RegQueryValueEx(hKey, "Domain", NULL, &datatype, buf, &bufferlength);
88 RegCloseKey(hKey);
89 return 0;
90 }
91 #endif
92
93 #undef malloc
94 #undef realloc
95 #undef calloc
96
97 static const char * out_of_mem_str = "out of memory";
98 static const char * file_open_str = "Could not open file \"%s\"";
99
100 /*
101 * Some functions that call display some error trace and then call abort().
102 * This just makes life much easier on embedded systems, since we're
103 * suffering major trauma...
104 */
105 EXP_FUNC void * STDCALL ax_malloc(size_t s)
106 {
107 void *x;
108
109 if ((x = malloc(s)) == NULL)
110 exit_now(out_of_mem_str);
111
112 return x;
113 }
114
115 EXP_FUNC void * STDCALL ax_realloc(void *y, size_t s)
116 {
117 void *x;
118
119 if ((x = realloc(y, s)) == NULL)
120 exit_now(out_of_mem_str);
121
122 return x;
123 }
124
125 EXP_FUNC void * STDCALL ax_calloc(size_t n, size_t s)
126 {
127 void *x;
128
129 if ((x = calloc(n, s)) == NULL)
130 exit_now(out_of_mem_str);
131
132 return x;
133 }
134
135 EXP_FUNC int STDCALL ax_open(const char *pathname, int flags)
136 {
137 int x;
138
139 if ((x = open(pathname, flags)) < 0)
140 exit_now(file_open_str, pathname);
141
142 return x;
143 }
144
145 /**
146 * This is a call which will deliberately exit an application, but will
147 * display some information before dying.
148 */
149 void exit_now(const char *format, ...)
150 {
151 va_list argp;
152
153 va_start(argp, format);
154 vfprintf(stderr, format, argp);
155 va_end(argp);
156 abort();
157 }
158