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
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.
27 #undef OS_REJECT_C_LIB_FUNCTIONS
30 void os_sleep(os_time_t sec
, os_time_t usec
)
39 int os_get_time(struct os_time
*t
)
43 res
= gettimeofday(&tv
, NULL
);
50 int os_mktime(int year
, int month
, int day
, int hour
, int min
, int sec
,
55 if (year
< 1970 || month
< 1 || month
> 12 || day
< 1 || day
> 31 ||
56 hour
< 0 || hour
> 23 || min
< 0 || min
> 59 || sec
< 0 ||
60 os_memset(&tm
, 0, sizeof(tm
));
61 tm
.tm_year
= year
- 1900;
62 tm
.tm_mon
= month
- 1;
68 *t
= (os_time_t
) mktime(&tm
);
73 int os_daemonize(const char *pid_file
)
81 FILE *f
= fopen(pid_file
, "w");
83 fprintf(f
, "%u\n", getpid());
92 void os_daemonize_terminate(const char *pid_file
)
99 int os_get_random(unsigned char *buf
, size_t len
)
104 f
= fopen("/dev/urandom", "rb");
106 printf("Could not open /dev/urandom.\n");
110 rc
= fread(buf
, 1, len
, f
);
113 return rc
!= len
? -1 : 0;
117 unsigned long os_random(void)
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
);
132 buf
= os_malloc(len
);
135 cwd
= getcwd(buf
, len
);
138 if (errno
!= ERANGE
) {
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
);
152 os_memcpy(ret
, cwd
, cwd_len
);
154 os_memcpy(ret
+ cwd_len
+ 1, rel_path
, rel_len
);
155 ret
[ret_len
- 1] = '\0';
162 int os_program_init(void)
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__)
185 return unsetenv(name
);
190 char * os_readfile(const char *name
, size_t *len
)
195 f
= fopen(name
, "rb");
199 fseek(f
, 0, SEEK_END
);
201 fseek(f
, 0, SEEK_SET
);
203 buf
= os_malloc(*len
);
209 fread(buf
, 1, *len
, f
);
216 void * os_zalloc(size_t size
)
218 void *n
= os_malloc(size
);
220 os_memset(n
, 0, size
);
225 void * os_malloc(size_t size
)
231 void * os_realloc(void *ptr
, size_t size
)
233 return realloc(ptr
, size
);
237 void os_free(void *ptr
)
243 void * os_memcpy(void *dest
, const void *src
, size_t n
)
253 void * os_memmove(void *dest
, const void *src
, size_t n
)
256 os_memcpy(dest
, src
, n
);
258 /* overlapping areas */
259 char *d
= (char *) dest
+ n
;
260 const char *s
= (const char *) src
+ n
;
268 void * os_memset(void *s
, int c
, size_t n
)
277 int os_memcmp(const void *s1
, const void *s2
, size_t n
)
279 const unsigned char *p1
= s1
, *p2
= s2
;
296 char * os_strdup(const char *s
)
303 res
= os_malloc(len
+ 1);
305 os_memcpy(res
, s
, len
+ 1);
310 size_t os_strlen(const char *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
)
350 char * os_strrchr(const char *s
, int c
)
365 int os_strcmp(const char *s1
, const char *s2
)
378 int os_strncmp(const char *s1
, const char *s2
, size_t n
)
397 char * os_strncpy(char *dest
, const char *src
, size_t n
)
413 char * os_strstr(const char *haystack
, const char *needle
)
415 size_t len
= os_strlen(needle
);
417 if (os_strncmp(haystack
, needle
, len
) == 0)
418 return (char *) haystack
;
426 int os_snprintf(char *str
, size_t size
, const char *format
, ...)
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
);
439 str
[size
- 1] = '\0';