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_get_reltime(struct os_reltime *t)
52 /* consider using performance counters or so instead */
54 int res = os_get_time(&now);
61 int os_mktime(int year, int month, int day, int hour, int min, int sec,
65 time_t t_local, t1, t2;
68 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
69 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
73 memset(&tm, 0, sizeof(tm));
74 tm.tm_year = year - 1900;
75 tm.tm_mon = month - 1;
81 t_local = mktime(&tm);
83 /* figure out offset to UTC */
84 tm1 = localtime(&t_local);
87 tm1 = gmtime(&t_local);
96 *t = (os_time_t) t_local - tz_offset;
101 int os_gmtime(os_time_t t, struct os_tm *tm)
109 tm->sec = tm2->tm_sec;
110 tm->min = tm2->tm_min;
111 tm->hour = tm2->tm_hour;
112 tm->day = tm2->tm_mday;
113 tm->month = tm2->tm_mon + 1;
114 tm->year = tm2->tm_year + 1900;
119 int os_daemonize(const char *pid_file)
126 void os_daemonize_terminate(const char *pid_file)
131 int os_get_random(unsigned char *buf, size_t len)
136 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
137 CRYPT_VERIFYCONTEXT))
140 ret = CryptGenRandom(prov, len, buf);
141 CryptReleaseContext(prov, 0);
147 unsigned long os_random(void)
153 char * os_rel2abs_path(const char *rel_path)
155 return _strdup(rel_path);
159 int os_program_init(void)
161 #ifdef CONFIG_NATIVE_WINDOWS
163 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
164 printf("Could not find a usable WinSock.dll\n");
167 #endif /* CONFIG_NATIVE_WINDOWS */
172 void os_program_deinit(void)
174 #ifdef CONFIG_NATIVE_WINDOWS
176 #endif /* CONFIG_NATIVE_WINDOWS */
180 int os_setenv(const char *name, const char *value, int overwrite)
186 int os_unsetenv(const char *name)
192 char * os_readfile(const char *name, size_t *len)
197 f = fopen(name, "rb");
201 fseek(f, 0, SEEK_END);
203 fseek(f, 0, SEEK_SET);
211 fread(buf, 1, *len, f);
218 void * os_zalloc(size_t size)
220 return calloc(1, size);
224 size_t os_strlcpy(char *dest, const char *src, size_t siz)
230 /* Copy string up to the maximum size of the dest buffer */
231 while (--left != 0) {
232 if ((*dest++ = *s++) == '\0')
238 /* Not enough room for the string; force NUL-termination */
242 ; /* determine total src string length */