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.
16 void os_sleep(os_time_t sec, os_time_t usec)
25 int os_get_time(struct os_time *t)
27 #define EPOCHFILETIME (116444736000000000ULL)
36 SystemTimeToFileTime(&st, &ft);
37 #else /* _WIN32_WCE */
38 GetSystemTimeAsFileTime(&ft);
39 #endif /* _WIN32_WCE */
40 li.LowPart = ft.dwLowDateTime;
41 li.HighPart = ft.dwHighDateTime;
42 tt = (li.QuadPart - EPOCHFILETIME) / 10;
43 t->sec = (os_time_t) (tt / 1000000);
44 t->usec = (os_time_t) (tt % 1000000);
50 int os_mktime(int year, int month, int day, int hour, int min, int sec,
54 time_t t_local, t1, t2;
57 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
58 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
62 memset(&tm, 0, sizeof(tm));
63 tm.tm_year = year - 1900;
64 tm.tm_mon = month - 1;
70 t_local = mktime(&tm);
72 /* figure out offset to UTC */
73 tm1 = localtime(&t_local);
76 tm1 = gmtime(&t_local);
85 *t = (os_time_t) t_local - tz_offset;
90 int os_gmtime(os_time_t t, struct os_tm *tm)
98 tm->sec = tm2->tm_sec;
99 tm->min = tm2->tm_min;
100 tm->hour = tm2->tm_hour;
101 tm->day = tm2->tm_mday;
102 tm->month = tm2->tm_mon + 1;
103 tm->year = tm2->tm_year + 1900;
108 int os_daemonize(const char *pid_file)
115 void os_daemonize_terminate(const char *pid_file)
120 int os_get_random(unsigned char *buf, size_t len)
125 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
126 CRYPT_VERIFYCONTEXT))
129 ret = CryptGenRandom(prov, len, buf);
130 CryptReleaseContext(prov, 0);
136 unsigned long os_random(void)
142 char * os_rel2abs_path(const char *rel_path)
144 return _strdup(rel_path);
148 int os_program_init(void)
150 #ifdef CONFIG_NATIVE_WINDOWS
152 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
153 printf("Could not find a usable WinSock.dll\n");
156 #endif /* CONFIG_NATIVE_WINDOWS */
161 void os_program_deinit(void)
163 #ifdef CONFIG_NATIVE_WINDOWS
165 #endif /* CONFIG_NATIVE_WINDOWS */
169 int os_setenv(const char *name, const char *value, int overwrite)
175 int os_unsetenv(const char *name)
181 char * os_readfile(const char *name, size_t *len)
186 f = fopen(name, "rb");
190 fseek(f, 0, SEEK_END);
192 fseek(f, 0, SEEK_SET);
200 fread(buf, 1, *len, f);
207 void * os_zalloc(size_t size)
209 return calloc(1, size);
213 size_t os_strlcpy(char *dest, const char *src, size_t siz)
219 /* Copy string up to the maximum size of the dest buffer */
220 while (--left != 0) {
221 if ((*dest++ = *s++) == '\0')
227 /* Not enough room for the string; force NUL-termination */
231 ; /* determine total src string length */