Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / src / utils / os_win32.c
blob074096480a405f54efa93f82c02d199434ba17c1
1 /*
2 * wpa_supplicant/hostapd / OS specific functions for Win32 systems
3 * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
16 #include <winsock2.h>
17 #include <wincrypt.h>
19 #include "os.h"
21 void os_sleep(os_time_t sec, os_time_t usec)
23 if (sec)
24 Sleep(sec * 1000);
25 if (usec)
26 Sleep(usec / 1000);
30 int os_get_time(struct os_time *t)
32 #define EPOCHFILETIME (116444736000000000ULL)
33 FILETIME ft;
34 LARGE_INTEGER li;
35 ULONGLONG tt;
37 #ifdef _WIN32_WCE
38 SYSTEMTIME st;
40 GetSystemTime(&st);
41 SystemTimeToFileTime(&st, &ft);
42 #else /* _WIN32_WCE */
43 GetSystemTimeAsFileTime(&ft);
44 #endif /* _WIN32_WCE */
45 li.LowPart = ft.dwLowDateTime;
46 li.HighPart = ft.dwHighDateTime;
47 tt = (li.QuadPart - EPOCHFILETIME) / 10;
48 t->sec = (os_time_t) (tt / 1000000);
49 t->usec = (os_time_t) (tt % 1000000);
51 return 0;
55 int os_mktime(int year, int month, int day, int hour, int min, int sec,
56 os_time_t *t)
58 struct tm tm, *tm1;
59 time_t t_local, t1, t2;
60 os_time_t tz_offset;
62 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
63 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
64 sec > 60)
65 return -1;
67 memset(&tm, 0, sizeof(tm));
68 tm.tm_year = year - 1900;
69 tm.tm_mon = month - 1;
70 tm.tm_mday = day;
71 tm.tm_hour = hour;
72 tm.tm_min = min;
73 tm.tm_sec = sec;
75 t_local = mktime(&tm);
77 /* figure out offset to UTC */
78 tm1 = localtime(&t_local);
79 if (tm1) {
80 t1 = mktime(tm1);
81 tm1 = gmtime(&t_local);
82 if (tm1) {
83 t2 = mktime(tm1);
84 tz_offset = t2 - t1;
85 } else
86 tz_offset = 0;
87 } else
88 tz_offset = 0;
90 *t = (os_time_t) t_local - tz_offset;
91 return 0;
95 int os_daemonize(const char *pid_file)
97 /* TODO */
98 return -1;
102 void os_daemonize_terminate(const char *pid_file)
107 int os_get_random(unsigned char *buf, size_t len)
109 HCRYPTPROV prov;
110 BOOL ret;
112 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
113 CRYPT_VERIFYCONTEXT))
114 return -1;
116 ret = CryptGenRandom(prov, len, buf);
117 CryptReleaseContext(prov, 0);
119 return ret ? 0 : -1;
123 unsigned long os_random(void)
125 return rand();
129 char * os_rel2abs_path(const char *rel_path)
131 return _strdup(rel_path);
135 int os_program_init(void)
137 #ifdef CONFIG_NATIVE_WINDOWS
138 WSADATA wsaData;
139 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
140 printf("Could not find a usable WinSock.dll\n");
141 return -1;
143 #endif /* CONFIG_NATIVE_WINDOWS */
144 return 0;
148 void os_program_deinit(void)
150 #ifdef CONFIG_NATIVE_WINDOWS
151 WSACleanup();
152 #endif /* CONFIG_NATIVE_WINDOWS */
156 int os_setenv(const char *name, const char *value, int overwrite)
158 return -1;
162 int os_unsetenv(const char *name)
164 return -1;
168 char * os_readfile(const char *name, size_t *len)
170 FILE *f;
171 char *buf;
173 f = fopen(name, "rb");
174 if (f == NULL)
175 return NULL;
177 fseek(f, 0, SEEK_END);
178 *len = ftell(f);
179 fseek(f, 0, SEEK_SET);
181 buf = malloc(*len);
182 if (buf == NULL) {
183 fclose(f);
184 return NULL;
187 fread(buf, 1, *len, f);
188 fclose(f);
190 return buf;
194 void * os_zalloc(size_t size)
196 return calloc(1, size);
200 size_t os_strlcpy(char *dest, const char *src, size_t siz)
202 const char *s = src;
203 size_t left = siz;
205 if (left) {
206 /* Copy string up to the maximum size of the dest buffer */
207 while (--left != 0) {
208 if ((*dest++ = *s++) == '\0')
209 break;
213 if (left == 0) {
214 /* Not enough room for the string; force NUL-termination */
215 if (siz != 0)
216 *dest = '\0';
217 while (*s++)
218 ; /* determine total src string length */
221 return s - src - 1;