LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
os.h
Go to the documentation of this file.
1 /*
2  * OS specific functions
3  * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #ifndef OS_H
16 #define OS_H
17 
18 typedef long os_time_t;
19 
25 void os_sleep(os_time_t sec, os_time_t usec);
26 
27 struct os_time {
30 };
31 
37 int os_get_time(struct os_time *t);
38 
39 
40 /* Helper macros for handling struct os_time */
41 
42 #define os_time_before(a, b) \
43  ((a)->sec < (b)->sec || \
44  ((a)->sec == (b)->sec && (a)->usec < (b)->usec))
45 
46 #define os_time_sub(a, b, res) do { \
47  (res)->sec = (a)->sec - (b)->sec; \
48  (res)->usec = (a)->usec - (b)->usec; \
49  if ((res)->usec < 0) { \
50  (res)->sec--; \
51  (res)->usec += 1000000; \
52  } \
53 } while (0)
54 
70 int os_mktime(int year, int month, int day, int hour, int min, int sec,
71  os_time_t *t);
72 
73 
79 int os_daemonize(const char *pid_file);
80 
85 void os_daemonize_terminate(const char *pid_file);
86 
93 int os_get_random(unsigned char *buf, size_t len);
94 
99 unsigned long os_random(void);
100 
114 char * os_rel2abs_path(const char *rel_path);
115 
124 int os_program_init(void);
125 
134 void os_program_deinit(void);
135 
146 int os_setenv(const char *name, const char *value, int overwrite);
147 
156 int os_unsetenv(const char *name);
157 
168 char * os_readfile(const char *name, size_t *len);
169 
177 void * os_zalloc(size_t size);
178 
179 
180 /*
181  * The following functions are wrapper for standard ANSI C or POSIX functions.
182  * By default, they are just defined to use the standard function name and no
183  * os_*.c implementation is needed for them. This avoids extra function calls
184  * by allowing the C pre-processor take care of the function name mapping.
185  *
186  * If the target system uses a C library that does not provide these functions,
187  * build_config.h can be used to define the wrappers to use a different
188  * function name. This can be done on function-by-function basis since the
189  * defines here are only used if build_config.h does not define the os_* name.
190  * If needed, os_*.c file can be used to implement the functions that are not
191  * included in the C library on the target system. Alternatively,
192  * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
193  * these functions need to be implemented in os_*.c file for the target system.
194  */
195 
196 #ifdef OS_NO_C_LIB_DEFINES
197 
205 void * os_malloc(size_t size);
206 
217 void * os_realloc(void *ptr, size_t size);
218 
223 void os_free(void *ptr);
224 
235 void * os_memcpy(void *dest, const void *src, size_t n);
236 
246 void * os_memmove(void *dest, const void *src, size_t n);
247 
255 void * os_memset(void *s, int c, size_t n);
256 
266 int os_memcmp(const void *s1, const void *s2, size_t n);
267 
275 char * os_strdup(const char *s);
276 
282 size_t os_strlen(const char *s);
283 
291 int os_strcasecmp(const char *s1, const char *s2);
292 
302 int os_strncasecmp(const char *s1, const char *s2, size_t n);
303 
310 char * os_strchr(const char *s, int c);
311 
318 char * os_strrchr(const char *s, int c);
319 
327 int os_strcmp(const char *s1, const char *s2);
328 
338 int os_strncmp(const char *s1, const char *s2, size_t n);
339 
347 char * os_strncpy(char *dest, const char *src, size_t n);
348 
355 char * os_strstr(const char *haystack, const char *needle);
356 
378 int os_snprintf(char *str, size_t size, const char *format, ...);
379 
380 #else /* OS_NO_C_LIB_DEFINES */
381 
382 #ifdef WPA_TRACE
383 void * os_malloc(size_t size);
384 void * os_realloc(void *ptr, size_t size);
385 void os_free(void *ptr);
386 char * os_strdup(const char *s);
387 #else /* WPA_TRACE */
388 #ifndef os_malloc
389 #define os_malloc(s) malloc((s))
390 #endif
391 #ifndef os_realloc
392 #define os_realloc(p, s) realloc((p), (s))
393 #endif
394 #ifndef os_free
395 #define os_free(p) free((p))
396 #endif
397 #ifndef os_strdup
398 #ifdef _MSC_VER
399 #define os_strdup(s) _strdup(s)
400 #else
401 #define os_strdup(s) strdup(s)
402 #endif
403 #endif
404 #endif /* WPA_TRACE */
405 
406 #ifndef os_memcpy
407 #define os_memcpy(d, s, n) memcpy((d), (s), (n))
408 #endif
409 #ifndef os_memmove
410 #define os_memmove(d, s, n) memmove((d), (s), (n))
411 #endif
412 #ifndef os_memset
413 #define os_memset(s, c, n) memset(s, c, n)
414 #endif
415 #ifndef os_memcmp
416 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
417 #endif
418 
419 #ifndef os_strlen
420 #define os_strlen(s) strlen(s)
421 #endif
422 #ifndef os_strcasecmp
423 #ifdef _MSC_VER
424 #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
425 #else
426 #define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
427 #endif
428 #endif
429 #ifndef os_strncasecmp
430 #ifdef _MSC_VER
431 #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
432 #else
433 #define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
434 #endif
435 #endif
436 #ifndef os_strchr
437 #define os_strchr(s, c) strchr((s), (c))
438 #endif
439 #ifndef os_strcmp
440 #define os_strcmp(s1, s2) strcmp((s1), (s2))
441 #endif
442 #ifndef os_strncmp
443 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
444 #endif
445 #ifndef os_strncpy
446 #define os_strncpy(d, s, n) strncpy((d), (s), (n))
447 #endif
448 #ifndef os_strrchr
449 #define os_strrchr(s, c) strrchr((s), (c))
450 #endif
451 #ifndef os_strstr
452 #define os_strstr(h, n) strstr((h), (n))
453 #endif
454 
455 #ifndef os_snprintf
456 #ifdef _MSC_VER
457 #define os_snprintf _snprintf
458 #else
459 #define os_snprintf snprintf
460 #endif
461 #endif
462 
463 #endif /* OS_NO_C_LIB_DEFINES */
464 
465 
476 size_t os_strlcpy(char *dest, const char *src, size_t siz);
477 
478 
479 #ifdef OS_REJECT_C_LIB_FUNCTIONS
480 #define malloc OS_DO_NOT_USE_malloc
481 #define realloc OS_DO_NOT_USE_realloc
482 #define free OS_DO_NOT_USE_free
483 #define memcpy OS_DO_NOT_USE_memcpy
484 #define memmove OS_DO_NOT_USE_memmove
485 #define memset OS_DO_NOT_USE_memset
486 #define memcmp OS_DO_NOT_USE_memcmp
487 #undef strdup
488 #define strdup OS_DO_NOT_USE_strdup
489 #define strlen OS_DO_NOT_USE_strlen
490 #define strcasecmp OS_DO_NOT_USE_strcasecmp
491 #define strncasecmp OS_DO_NOT_USE_strncasecmp
492 #undef strchr
493 #define strchr OS_DO_NOT_USE_strchr
494 #undef strcmp
495 #define strcmp OS_DO_NOT_USE_strcmp
496 #undef strncmp
497 #define strncmp OS_DO_NOT_USE_strncmp
498 #undef strncpy
499 #define strncpy OS_DO_NOT_USE_strncpy
500 #define strrchr OS_DO_NOT_USE_strrchr
501 #define strstr OS_DO_NOT_USE_strstr
502 #undef snprintf
503 #define snprintf OS_DO_NOT_USE_snprintf
504 
505 #define strcpy OS_DO_NOT_USE_strcpy
506 #endif /* OS_REJECT_C_LIB_FUNCTIONS */
507 
508 #endif /* OS_H */
#define os_strchr(s, c)
Definition: os.h:437
char * os_readfile(const char *name, size_t *len)
Definition: os_unix.c:284
Definition: os.h:27
void os_sleep(os_time_t sec, os_time_t usec)
Definition: os_unix.c:41
int os_program_init(void)
Definition: os_unix.c:233
#define os_memset(s, c, n)
Definition: os.h:413
void * os_zalloc(size_t size)
Definition: os_unix.c:316
#define os_snprintf
Definition: os.h:459
int os_get_random(unsigned char *buf, size_t len)
Definition: os_unix.c:166
#define os_strdup(s)
Definition: os.h:401
#define os_strcmp(s1, s2)
Definition: os.h:440
os_time_t usec
Definition: os.h:29
#define os_strncmp(s1, s2, n)
Definition: os.h:443
#define os_strncasecmp(s1, s2, n)
Definition: os.h:433
#define os_malloc(s)
Definition: os.h:389
void os_daemonize_terminate(const char *pid_file)
Definition: os_unix.c:159
void os_program_deinit(void)
Definition: os_unix.c:242
#define os_strcasecmp(s1, s2)
Definition: os.h:426
int os_get_time(struct os_time *t)
Definition: os_unix.c:50
int os_mktime(int year, int month, int day, int hour, int min, int sec, os_time_t *t)
Definition: os_unix.c:61
#define os_memcpy(d, s, n)
Definition: os.h:407
int os_daemonize(const char *pid_file)
Definition: os_unix.c:136
size_t os_strlcpy(char *dest, const char *src, size_t siz)
Definition: os_unix.c:323
#define os_memmove(d, s, n)
Definition: os.h:410
#define os_strncpy(d, s, n)
Definition: os.h:446
#define os_strstr(h, n)
Definition: os.h:452
int os_setenv(const char *name, const char *value, int overwrite)
Definition: os_unix.c:266
int os_unsetenv(const char *name)
Definition: os_unix.c:272
#define os_realloc(p, s)
Definition: os.h:392
#define os_strlen(s)
Definition: os.h:420
char * os_rel2abs_path(const char *rel_path)
Definition: os_unix.c:190
#define os_free(p)
Definition: os.h:395
os_time_t sec
Definition: os.h:28
long os_time_t
Definition: os.h:18
#define os_strrchr(s, c)
Definition: os.h:449
#define os_memcmp(s1, s2, n)
Definition: os.h:416
unsigned long os_random(void)
Definition: os_unix.c:184