Add patch from 9.3.5 to 9.3.5-P1. This is for adding randomization
[dragonfly.git] / contrib / wpa_supplicant-0.5.8 / os_internal.c
blobb2183ea38f4df98f6ece6c21aa717a1c1278bb0f
1 /*
2 * wpa_supplicant/hostapd / Internal implementation of OS specific functions
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.
14 * This file is an example of operating system specific wrapper functions.
15 * This version implements many of the functions internally, so it can be used
16 * to fill in missing functions from the target system C libraries.
18 * Some of the functions are using standard C library calls in order to keep
19 * this file in working condition to allow the functions to be tested on a
20 * Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for
21 * this file to work correctly. Note that these implementations are only
22 * examples and are not optimized for speed.
25 #include "includes.h"
27 #undef OS_REJECT_C_LIB_FUNCTIONS
28 #include "os.h"
30 void os_sleep(os_time_t sec, os_time_t usec)
32 if (sec)
33 sleep(sec);
34 if (usec)
35 usleep(usec);
39 int os_get_time(struct os_time *t)
41 int res;
42 struct timeval tv;
43 res = gettimeofday(&tv, NULL);
44 t->sec = tv.tv_sec;
45 t->usec = tv.tv_usec;
46 return res;
50 int os_mktime(int year, int month, int day, int hour, int min, int sec,
51 os_time_t *t)
53 struct tm tm;
55 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
56 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
57 sec > 60)
58 return -1;
60 os_memset(&tm, 0, sizeof(tm));
61 tm.tm_year = year - 1900;
62 tm.tm_mon = month - 1;
63 tm.tm_mday = day;
64 tm.tm_hour = hour;
65 tm.tm_min = min;
66 tm.tm_sec = sec;
68 *t = (os_time_t) mktime(&tm);
69 return 0;
73 int os_daemonize(const char *pid_file)
75 if (daemon(0, 0)) {
76 perror("daemon");
77 return -1;
80 if (pid_file) {
81 FILE *f = fopen(pid_file, "w");
82 if (f) {
83 fprintf(f, "%u\n", getpid());
84 fclose(f);
88 return -0;
92 void os_daemonize_terminate(const char *pid_file)
94 if (pid_file)
95 unlink(pid_file);
99 int os_get_random(unsigned char *buf, size_t len)
101 FILE *f;
102 size_t rc;
104 f = fopen("/dev/urandom", "rb");
105 if (f == NULL) {
106 printf("Could not open /dev/urandom.\n");
107 return -1;
110 rc = fread(buf, 1, len, f);
111 fclose(f);
113 return rc != len ? -1 : 0;
117 unsigned long os_random(void)
119 return random();
123 char * os_rel2abs_path(const char *rel_path)
125 char *buf = NULL, *cwd, *ret;
126 size_t len = 128, cwd_len, rel_len, ret_len;
128 if (rel_path[0] == '/')
129 return os_strdup(rel_path);
131 for (;;) {
132 buf = os_malloc(len);
133 if (buf == NULL)
134 return NULL;
135 cwd = getcwd(buf, len);
136 if (cwd == NULL) {
137 os_free(buf);
138 if (errno != ERANGE) {
139 return NULL;
141 len *= 2;
142 } else {
143 break;
147 cwd_len = strlen(cwd);
148 rel_len = strlen(rel_path);
149 ret_len = cwd_len + 1 + rel_len + 1;
150 ret = os_malloc(ret_len);
151 if (ret) {
152 os_memcpy(ret, cwd, cwd_len);
153 ret[cwd_len] = '/';
154 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
155 ret[ret_len - 1] = '\0';
157 os_free(buf);
158 return ret;
162 int os_program_init(void)
164 return 0;
168 void os_program_deinit(void)
173 int os_setenv(const char *name, const char *value, int overwrite)
175 return setenv(name, value, overwrite);
179 int os_unsetenv(const char *name)
181 #if defined(__FreeBSD__) || defined(__NetBSD__)
182 unsetenv(name);
183 return 0;
184 #else
185 return unsetenv(name);
186 #endif
190 char * os_readfile(const char *name, size_t *len)
192 FILE *f;
193 char *buf;
195 f = fopen(name, "rb");
196 if (f == NULL)
197 return NULL;
199 fseek(f, 0, SEEK_END);
200 *len = ftell(f);
201 fseek(f, 0, SEEK_SET);
203 buf = os_malloc(*len);
204 if (buf == NULL) {
205 fclose(f);
206 return NULL;
209 fread(buf, 1, *len, f);
210 fclose(f);
212 return buf;
216 void * os_zalloc(size_t size)
218 void *n = os_malloc(size);
219 if (n)
220 os_memset(n, 0, size);
221 return n;
225 void * os_malloc(size_t size)
227 return malloc(size);
231 void * os_realloc(void *ptr, size_t size)
233 return realloc(ptr, size);
237 void os_free(void *ptr)
239 free(ptr);
243 void * os_memcpy(void *dest, const void *src, size_t n)
245 char *d = dest;
246 const char *s = src;
247 while (n--)
248 *d++ = *s++;
249 return dest;
253 void * os_memmove(void *dest, const void *src, size_t n)
255 if (dest < src)
256 os_memcpy(dest, src, n);
257 else {
258 /* overlapping areas */
259 char *d = (char *) dest + n;
260 const char *s = (const char *) src + n;
261 while (n--)
262 *--d = *--s;
264 return dest;
268 void * os_memset(void *s, int c, size_t n)
270 char *p = s;
271 while (n--)
272 *p++ = c;
273 return s;
277 int os_memcmp(const void *s1, const void *s2, size_t n)
279 const unsigned char *p1 = s1, *p2 = s2;
281 if (n == 0)
282 return 0;
284 while (*p1 == *p2) {
285 p1++;
286 p2++;
287 n--;
288 if (n == 0)
289 return 0;
292 return *p1 - *p2;
296 char * os_strdup(const char *s)
298 char *res;
299 size_t len;
300 if (s == NULL)
301 return NULL;
302 len = os_strlen(s);
303 res = os_malloc(len + 1);
304 if (res)
305 os_memcpy(res, s, len + 1);
306 return res;
310 size_t os_strlen(const char *s)
312 const char *p = s;
313 while (*p)
314 p++;
315 return p - s;
319 int os_strcasecmp(const char *s1, const char *s2)
322 * Ignoring case is not required for main functionality, so just use
323 * the case sensitive version of the function.
325 return os_strcmp(s1, s2);
329 int os_strncasecmp(const char *s1, const char *s2, size_t n)
332 * Ignoring case is not required for main functionality, so just use
333 * the case sensitive version of the function.
335 return os_strncmp(s1, s2, n);
339 char * os_strchr(const char *s, int c)
341 while (*s) {
342 if (*s == c)
343 return (char *) s;
344 s++;
346 return NULL;
350 char * os_strrchr(const char *s, int c)
352 const char *p = s;
353 while (*p)
354 p++;
355 p--;
356 while (p >= s) {
357 if (*p == c)
358 return (char *) p;
359 p--;
361 return NULL;
365 int os_strcmp(const char *s1, const char *s2)
367 while (*s1 == *s2) {
368 if (*s1 == '\0')
369 break;
370 s1++;
371 s2++;
374 return *s1 - *s2;
378 int os_strncmp(const char *s1, const char *s2, size_t n)
380 if (n == 0)
381 return 0;
383 while (*s1 == *s2) {
384 if (*s1 == '\0')
385 break;
386 s1++;
387 s2++;
388 n--;
389 if (n == 0)
390 return 0;
393 return *s1 - *s2;
397 char * os_strncpy(char *dest, const char *src, size_t n)
399 char *d = dest;
401 while (n--) {
402 *d = *src;
403 if (*src == '\0')
404 break;
405 d++;
406 src++;
409 return dest;
413 char * os_strstr(const char *haystack, const char *needle)
415 size_t len = os_strlen(needle);
416 while (*haystack) {
417 if (os_strncmp(haystack, needle, len) == 0)
418 return (char *) haystack;
419 haystack++;
422 return NULL;
426 int os_snprintf(char *str, size_t size, const char *format, ...)
428 va_list ap;
429 int ret;
431 /* See http://www.ijs.si/software/snprintf/ for portable
432 * implementation of snprintf.
435 va_start(ap, format);
436 ret = vsnprintf(str, size, format, ap);
437 va_end(ap);
438 if (size > 0)
439 str[size - 1] = '\0';
440 return ret;