2 * wpa_supplicant/hostapd / OS specific functions for Win32 systems
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
17 void os_sleep(os_time_t sec, os_time_t usec)
26 int os_get_time(struct os_time *t)
28 #define EPOCHFILETIME (116444736000000000ULL)
37 SystemTimeToFileTime(&st, &ft);
38 #else /* _WIN32_WCE */
39 GetSystemTimeAsFileTime(&ft);
40 #endif /* _WIN32_WCE */
41 li.LowPart = ft.dwLowDateTime;
42 li.HighPart = ft.dwHighDateTime;
43 tt = (li.QuadPart - EPOCHFILETIME) / 10;
44 t->sec = (os_time_t) (tt / 1000000);
45 t->usec = (os_time_t) (tt % 1000000);
51 int os_get_reltime(struct os_reltime *t)
53 /* consider using performance counters or so instead */
55 int res = os_get_time(&now);
62 int os_mktime(int year, int month, int day, int hour, int min, int sec,
66 time_t t_local, t1, t2;
69 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
70 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
74 memset(&tm, 0, sizeof(tm));
75 tm.tm_year = year - 1900;
76 tm.tm_mon = month - 1;
82 t_local = mktime(&tm);
84 /* figure out offset to UTC */
85 tm1 = localtime(&t_local);
88 tm1 = gmtime(&t_local);
97 *t = (os_time_t) t_local - tz_offset;
102 int os_gmtime(os_time_t t, struct os_tm *tm)
110 tm->sec = tm2->tm_sec;
111 tm->min = tm2->tm_min;
112 tm->hour = tm2->tm_hour;
113 tm->day = tm2->tm_mday;
114 tm->month = tm2->tm_mon + 1;
115 tm->year = tm2->tm_year + 1900;
120 int os_daemonize(const char *pid_file)
127 void os_daemonize_terminate(const char *pid_file)
132 int os_get_random(unsigned char *buf, size_t len)
137 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
138 CRYPT_VERIFYCONTEXT))
141 ret = CryptGenRandom(prov, len, buf);
142 CryptReleaseContext(prov, 0);
148 unsigned long os_random(void)
154 char * os_rel2abs_path(const char *rel_path)
156 return _strdup(rel_path);
160 int os_program_init(void)
162 #ifdef CONFIG_NATIVE_WINDOWS
164 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
165 printf("Could not find a usable WinSock.dll\n");
168 #endif /* CONFIG_NATIVE_WINDOWS */
173 void os_program_deinit(void)
175 #ifdef CONFIG_NATIVE_WINDOWS
177 #endif /* CONFIG_NATIVE_WINDOWS */
181 int os_setenv(const char *name, const char *value, int overwrite)
187 int os_unsetenv(const char *name)
193 char * os_readfile(const char *name, size_t *len)
198 f = fopen(name, "rb");
202 fseek(f, 0, SEEK_END);
204 fseek(f, 0, SEEK_SET);
212 fread(buf, 1, *len, f);
219 int os_fdatasync(FILE *stream)
226 h = (HANDLE) _get_osfhandle(_fileno(stream));
227 if (h == INVALID_HANDLE_VALUE)
230 if (!FlushFileBuffers(h))
237 void * os_zalloc(size_t size)
239 return calloc(1, size);
243 size_t os_strlcpy(char *dest, const char *src, size_t siz)
249 /* Copy string up to the maximum size of the dest buffer */
250 while (--left != 0) {
251 if ((*dest++ = *s++) == '\0')
257 /* Not enough room for the string; force NUL-termination */
261 ; /* determine total src string length */
268 int os_memcmp_const(const void *a, const void *b, size_t len)
275 for (res = 0, i = 0; i < len; i++)
276 res |= aa[i] ^ bb[i];
282 int os_exec(const char *program, const char *arg, int wait_completion)