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_mktime(int year, int month, int day, int hour, int min, int sec,
49 if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
50 hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 ||
54 os_memset(&tm, 0, sizeof(tm));
55 tm.tm_year = year - 1900;
56 tm.tm_mon = month - 1;
62 *t = (os_time_t) mktime(&tm);
67 int os_gmtime(os_time_t t, struct os_tm *tm)
75 tm->sec = tm2->tm_sec;
76 tm->min = tm2->tm_min;
77 tm->hour = tm2->tm_hour;
78 tm->day = tm2->tm_mday;
79 tm->month = tm2->tm_mon + 1;
80 tm->year = tm2->tm_year + 1900;
85 int os_daemonize(const char *pid_file)
93 FILE *f = fopen(pid_file, "w");
95 fprintf(f, "%u\n", getpid());
104 void os_daemonize_terminate(const char *pid_file)
111 int os_get_random(unsigned char *buf, size_t len)
116 f = fopen("/dev/urandom", "rb");
118 printf("Could not open /dev/urandom.\n");
122 rc = fread(buf, 1, len, f);
125 return rc != len ? -1 : 0;
129 unsigned long os_random(void)
135 char * os_rel2abs_path(const char *rel_path)
137 char *buf = NULL, *cwd, *ret;
138 size_t len = 128, cwd_len, rel_len, ret_len;
140 if (rel_path[0] == '/')
141 return os_strdup(rel_path);
144 buf = os_malloc(len);
147 cwd = getcwd(buf, len);
150 if (errno != ERANGE) {
159 cwd_len = strlen(cwd);
160 rel_len = strlen(rel_path);
161 ret_len = cwd_len + 1 + rel_len + 1;
162 ret = os_malloc(ret_len);
164 os_memcpy(ret, cwd, cwd_len);
166 os_memcpy(ret + cwd_len + 1, rel_path, rel_len);
167 ret[ret_len - 1] = '\0';
174 int os_program_init(void)
180 void os_program_deinit(void)
185 int os_setenv(const char *name, const char *value, int overwrite)
187 return setenv(name, value, overwrite);
191 int os_unsetenv(const char *name)
193 #if defined(__FreeBSD__) || defined(__NetBSD__)
197 return unsetenv(name);
202 char * os_readfile(const char *name, size_t *len)
207 f = fopen(name, "rb");
211 fseek(f, 0, SEEK_END);
213 fseek(f, 0, SEEK_SET);
215 buf = os_malloc(*len);
221 if (fread(buf, 1, *len, f) != *len) {
233 void * os_zalloc(size_t size)
235 void *n = os_malloc(size);
237 os_memset(n, 0, size);
242 void * os_malloc(size_t size)
248 void * os_realloc(void *ptr, size_t size)
250 return realloc(ptr, size);
254 void os_free(void *ptr)
260 void * os_memcpy(void *dest, const void *src, size_t n)
270 void * os_memmove(void *dest, const void *src, size_t n)
273 os_memcpy(dest, src, n);
275 /* overlapping areas */
276 char *d = (char *) dest + n;
277 const char *s = (const char *) src + n;
285 void * os_memset(void *s, int c, size_t n)
294 int os_memcmp(const void *s1, const void *s2, size_t n)
296 const unsigned char *p1 = s1, *p2 = s2;
313 char * os_strdup(const char *s)
320 res = os_malloc(len + 1);
322 os_memcpy(res, s, len + 1);
327 size_t os_strlen(const char *s)
336 int os_strcasecmp(const char *s1, const char *s2)
339 * Ignoring case is not required for main functionality, so just use
340 * the case sensitive version of the function.
342 return os_strcmp(s1, s2);
346 int os_strncasecmp(const char *s1, const char *s2, size_t n)
349 * Ignoring case is not required for main functionality, so just use
350 * the case sensitive version of the function.
352 return os_strncmp(s1, s2, n);
356 char * os_strchr(const char *s, int c)
367 char * os_strrchr(const char *s, int c)
382 int os_strcmp(const char *s1, const char *s2)
395 int os_strncmp(const char *s1, const char *s2, size_t n)
414 char * os_strncpy(char *dest, const char *src, size_t n)
430 size_t os_strlcpy(char *dest, const char *src, size_t siz)
436 /* Copy string up to the maximum size of the dest buffer */
437 while (--left != 0) {
438 if ((*dest++ = *s++) == '\0')
444 /* Not enough room for the string; force NUL-termination */
448 ; /* determine total src string length */
455 char * os_strstr(const char *haystack, const char *needle)
457 size_t len = os_strlen(needle);
459 if (os_strncmp(haystack, needle, len) == 0)
460 return (char *) haystack;
468 int os_snprintf(char *str, size_t size, const char *format, ...)
473 /* See http://www.ijs.si/software/snprintf/ for portable
474 * implementation of snprintf.
477 va_start(ap, format);
478 ret = vsnprintf(str, size, format, ap);
481 str[size - 1] = '\0';