os_win32.c
Go to the documentation of this file.00001
00016 #include "includes.h"
00017 #include <winsock2.h>
00018 #include <wincrypt.h>
00019
00020 #include "os.h"
00021
00022 void os_sleep(os_time_t sec, os_time_t usec)
00023 {
00024 if (sec)
00025 Sleep(sec * 1000);
00026 if (usec)
00027 Sleep(usec / 1000);
00028 }
00029
00030
00031 int os_get_time(struct os_time *t)
00032 {
00033 #define EPOCHFILETIME (116444736000000000ULL)
00034 FILETIME ft;
00035 LARGE_INTEGER li;
00036 ULONGLONG tt;
00037
00038 #ifdef _WIN32_WCE
00039 SYSTEMTIME st;
00040
00041 GetSystemTime(&st);
00042 SystemTimeToFileTime(&st, &ft);
00043 #else
00044 GetSystemTimeAsFileTime(&ft);
00045 #endif
00046 li.LowPart = ft.dwLowDateTime;
00047 li.HighPart = ft.dwHighDateTime;
00048 tt = (li.QuadPart - EPOCHFILETIME) / 10;
00049 t->sec = (os_time_t) (tt / 1000000);
00050 t->usec = (os_time_t) (tt % 1000000);
00051
00052 return 0;
00053 }
00054
00055
00056 int os_mktime(int year, int month, int day, int hour, int min, int sec,
00057 os_time_t *t)
00058 {
00059 struct tm tm, *tm1;
00060 time_t t_local, t1, t2;
00061 os_time_t tz_offset;
00062
00063 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
00064 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
00065 sec > 60)
00066 return -1;
00067
00068 memset(&tm, 0, sizeof(tm));
00069 tm.tm_year = year - 1900;
00070 tm.tm_mon = month - 1;
00071 tm.tm_mday = day;
00072 tm.tm_hour = hour;
00073 tm.tm_min = min;
00074 tm.tm_sec = sec;
00075
00076 t_local = mktime(&tm);
00077
00078
00079 tm1 = localtime(&t_local);
00080 if (tm1) {
00081 t1 = mktime(tm1);
00082 tm1 = gmtime(&t_local);
00083 if (tm1) {
00084 t2 = mktime(tm1);
00085 tz_offset = t2 - t1;
00086 } else
00087 tz_offset = 0;
00088 } else
00089 tz_offset = 0;
00090
00091 *t = (os_time_t) t_local - tz_offset;
00092 return 0;
00093 }
00094
00095
00096 int os_daemonize(const char *pid_file)
00097 {
00098
00099 return -1;
00100 }
00101
00102
00103 void os_daemonize_terminate(const char *pid_file)
00104 {
00105 }
00106
00107
00108 int os_get_random(unsigned char *buf, size_t len)
00109 {
00110 HCRYPTPROV prov;
00111 BOOL ret;
00112
00113 if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL,
00114 CRYPT_VERIFYCONTEXT))
00115 return -1;
00116
00117 ret = CryptGenRandom(prov, len, buf);
00118 CryptReleaseContext(prov, 0);
00119
00120 return ret ? 0 : -1;
00121 }
00122
00123
00124 unsigned long os_random(void)
00125 {
00126 return rand();
00127 }
00128
00129
00130 char * os_rel2abs_path(const char *rel_path)
00131 {
00132 return _strdup(rel_path);
00133 }
00134
00135
00136 int os_program_init(void)
00137 {
00138 #ifdef CONFIG_NATIVE_WINDOWS
00139 WSADATA wsaData;
00140 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
00141 printf("Could not find a usable WinSock.dll\n");
00142 return -1;
00143 }
00144 #endif
00145 return 0;
00146 }
00147
00148
00149 void os_program_deinit(void)
00150 {
00151 #ifdef CONFIG_NATIVE_WINDOWS
00152 WSACleanup();
00153 #endif
00154 }
00155
00156
00157 int os_setenv(const char *name, const char *value, int overwrite)
00158 {
00159 return -1;
00160 }
00161
00162
00163 int os_unsetenv(const char *name)
00164 {
00165 return -1;
00166 }
00167
00168
00169 char * os_readfile(const char *name, size_t *len)
00170 {
00171 FILE *f;
00172 char *buf;
00173
00174 f = fopen(name, "rb");
00175 if (f == NULL)
00176 return NULL;
00177
00178 fseek(f, 0, SEEK_END);
00179 *len = ftell(f);
00180 fseek(f, 0, SEEK_SET);
00181
00182 buf = malloc(*len);
00183 if (buf == NULL) {
00184 fclose(f);
00185 return NULL;
00186 }
00187
00188 fread(buf, 1, *len, f);
00189 fclose(f);
00190
00191 return buf;
00192 }
00193
00194
00195 void * os_zalloc(size_t size)
00196 {
00197 return calloc(1, size);
00198 }
00199
00200
00201 size_t os_strlcpy(char *dest, const char *src, size_t siz)
00202 {
00203 const char *s = src;
00204 size_t left = siz;
00205
00206 if (left) {
00207
00208 while (--left != 0) {
00209 if ((*dest++ = *s++) == '\0')
00210 break;
00211 }
00212 }
00213
00214 if (left == 0) {
00215
00216 if (siz != 0)
00217 *dest = '\0';
00218 while (*s++)
00219 ;
00220 }
00221
00222 return s - src - 1;
00223 }
00224