2 * wpa_supplicant/hostapd / Internal implementation of OS specific functions
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.
8 * This file is an example of operating system specific wrapper functions.
9 * This version implements many of the functions internally, so it can be used
10 * to fill in missing functions from the target system C libraries.
12 * Some of the functions are using standard C library calls in order to keep
13 * this file in working condition to allow the functions to be tested on a
14 * Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for
15 * this file to work correctly. Note that these implementations are only
16 * examples and are not optimized for speed.
21 #undef OS_REJECT_C_LIB_FUNCTIONS
24 void os_sleep(os_time_t sec, os_time_t usec)
33 int os_get_time(struct os_time *t)
37 res = gettimeofday(&tv, NULL);
44 int os_get_reltime(struct os_reltime *t)
48 res = gettimeofday(&tv, NULL);
55 int os_mktime(int year, int month, int day, int hour, int min, int sec,
60 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
61 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
65 os_memset(&tm, 0, sizeof(tm));
66 tm.tm_year = year - 1900;
67 tm.tm_mon = month - 1;
73 *t = (os_time_t) mktime(&tm);
78 int os_gmtime(os_time_t t, struct os_tm *tm)
86 tm->sec = tm2->tm_sec;
87 tm->min = tm2->tm_min;
88 tm->hour = tm2->tm_hour;
89 tm->day = tm2->tm_mday;
90 tm->month = tm2->tm_mon + 1;
91 tm->year = tm2->tm_year + 1900;
96 int os_daemonize(const char *pid_file)
104 FILE *f = fopen(pid_file, "w");
106 fprintf(f, "%u\n", getpid());
115 void os_daemonize_terminate(const char *pid_file)
122 int os_get_random(unsigned char *buf, size_t len)
127 f = fopen("/dev/urandom", "rb");
129 printf("Could not open /dev/urandom.\n");
133 rc = fread(buf, 1, len, f);
136 return rc != len ? -1 : 0;
140 unsigned long os_random(void)
146 char * os_rel2abs_path(const char *rel_path)
148 char *buf = NULL, *cwd, *ret;
149 size_t len = 128, cwd_len, rel_len, ret_len;
151 if (rel_path[0] == '/')
152 return os_strdup(rel_path);
155 buf = os_malloc(len);
158 cwd = getcwd(buf, len);
161 if (errno != ERANGE) {
170 cwd_len = strlen(cwd);
171 rel_len = strlen(rel_path);
172 ret_len = cwd_len + 1 + rel_len + 1;
173 ret = os_malloc(ret_len);
175 os_memcpy(ret, cwd, cwd_len);
177 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
178 ret[ret_len - 1] = '\0';
185 int os_program_init(void)
191 void os_program_deinit(void)
196 int os_setenv(const char *name, const char *value, int overwrite)
198 return setenv(name, value, overwrite);
202 int os_unsetenv(const char *name)
204 #if defined(__FreeBSD__) || defined(__NetBSD__)
208 return unsetenv(name);
213 char * os_readfile(const char *name, size_t *len)
218 f = fopen(name, "rb");
222 fseek(f, 0, SEEK_END);
224 fseek(f, 0, SEEK_SET);
226 buf = os_malloc(*len);
232 if (fread(buf, 1, *len, f) != *len) {
244 void * os_zalloc(size_t size)
246 void *n = os_malloc(size);
248 os_memset(n, 0, size);
253 void * os_malloc(size_t size)
259 void * os_realloc(void *ptr, size_t size)
261 return realloc(ptr, size);
265 void os_free(void *ptr)
271 void * os_memcpy(void *dest, const void *src, size_t n)
281 void * os_memmove(void *dest, const void *src, size_t n)
284 os_memcpy(dest, src, n);
286 /* overlapping areas */
287 char *d = (char *) dest + n;
288 const char *s = (const char *) src + n;
296 void * os_memset(void *s, int c, size_t n)
305 int os_memcmp(const void *s1, const void *s2, size_t n)
307 const unsigned char *p1 = s1, *p2 = s2;
324 char * os_strdup(const char *s)
331 res = os_malloc(len + 1);
333 os_memcpy(res, s, len + 1);
338 size_t os_strlen(const char *s)
347 int os_strcasecmp(const char *s1, const char *s2)
350 * Ignoring case is not required for main functionality, so just use
351 * the case sensitive version of the function.
353 return os_strcmp(s1, s2);
357 int os_strncasecmp(const char *s1, const char *s2, size_t n)
360 * Ignoring case is not required for main functionality, so just use
361 * the case sensitive version of the function.
363 return os_strncmp(s1, s2, n);
367 char * os_strchr(const char *s, int c)
378 char * os_strrchr(const char *s, int c)
393 int os_strcmp(const char *s1, const char *s2)
406 int os_strncmp(const char *s1, const char *s2, size_t n)
425 char * os_strncpy(char *dest, const char *src, size_t n)
441 size_t os_strlcpy(char *dest, const char *src, size_t siz)
447 /* Copy string up to the maximum size of the dest buffer */
448 while (--left != 0) {
449 if ((*dest++ = *s++) == '\0')
455 /* Not enough room for the string; force NUL-termination */
459 ; /* determine total src string length */
466 char * os_strstr(const char *haystack, const char *needle)
468 size_t len = os_strlen(needle);
470 if (os_strncmp(haystack, needle, len) == 0)
471 return (char *) haystack;
479 int os_snprintf(char *str, size_t size, const char *format, ...)
484 /* See http://www.ijs.si/software/snprintf/ for portable
485 * implementation of snprintf.
488 va_start(ap, format);
489 ret = vsnprintf(str, size, format, ap);
492 str[size - 1] = '\0';