os.h revision 281806
1189251Ssam/*
2214734Srpaulo * OS specific functions
3214734Srpaulo * Copyright (c) 2005-2009, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#ifndef OS_H
10189251Ssam#define OS_H
11189251Ssam
12189251Ssamtypedef long os_time_t;
13189251Ssam
14189251Ssam/**
15189251Ssam * os_sleep - Sleep (sec, usec)
16189251Ssam * @sec: Number of seconds to sleep
17189251Ssam * @usec: Number of microseconds to sleep
18189251Ssam */
19189251Ssamvoid os_sleep(os_time_t sec, os_time_t usec);
20189251Ssam
21189251Ssamstruct os_time {
22189251Ssam	os_time_t sec;
23189251Ssam	os_time_t usec;
24189251Ssam};
25189251Ssam
26281806Srpaulostruct os_reltime {
27281806Srpaulo	os_time_t sec;
28281806Srpaulo	os_time_t usec;
29281806Srpaulo};
30281806Srpaulo
31189251Ssam/**
32189251Ssam * os_get_time - Get current time (sec, usec)
33189251Ssam * @t: Pointer to buffer for the time
34189251Ssam * Returns: 0 on success, -1 on failure
35189251Ssam */
36189251Ssamint os_get_time(struct os_time *t);
37189251Ssam
38281806Srpaulo/**
39281806Srpaulo * os_get_reltime - Get relative time (sec, usec)
40281806Srpaulo * @t: Pointer to buffer for the time
41281806Srpaulo * Returns: 0 on success, -1 on failure
42281806Srpaulo */
43281806Srpauloint os_get_reltime(struct os_reltime *t);
44189251Ssam
45189251Ssam
46281806Srpaulo/* Helpers for handling struct os_time */
47189251Ssam
48281806Srpaulostatic inline int os_time_before(struct os_time *a, struct os_time *b)
49281806Srpaulo{
50281806Srpaulo	return (a->sec < b->sec) ||
51281806Srpaulo	       (a->sec == b->sec && a->usec < b->usec);
52281806Srpaulo}
53189251Ssam
54281806Srpaulo
55281806Srpaulostatic inline void os_time_sub(struct os_time *a, struct os_time *b,
56281806Srpaulo			       struct os_time *res)
57281806Srpaulo{
58281806Srpaulo	res->sec = a->sec - b->sec;
59281806Srpaulo	res->usec = a->usec - b->usec;
60281806Srpaulo	if (res->usec < 0) {
61281806Srpaulo		res->sec--;
62281806Srpaulo		res->usec += 1000000;
63281806Srpaulo	}
64281806Srpaulo}
65281806Srpaulo
66281806Srpaulo
67281806Srpaulo/* Helpers for handling struct os_reltime */
68281806Srpaulo
69281806Srpaulostatic inline int os_reltime_before(struct os_reltime *a,
70281806Srpaulo				    struct os_reltime *b)
71281806Srpaulo{
72281806Srpaulo	return (a->sec < b->sec) ||
73281806Srpaulo	       (a->sec == b->sec && a->usec < b->usec);
74281806Srpaulo}
75281806Srpaulo
76281806Srpaulo
77281806Srpaulostatic inline void os_reltime_sub(struct os_reltime *a, struct os_reltime *b,
78281806Srpaulo				  struct os_reltime *res)
79281806Srpaulo{
80281806Srpaulo	res->sec = a->sec - b->sec;
81281806Srpaulo	res->usec = a->usec - b->usec;
82281806Srpaulo	if (res->usec < 0) {
83281806Srpaulo		res->sec--;
84281806Srpaulo		res->usec += 1000000;
85281806Srpaulo	}
86281806Srpaulo}
87281806Srpaulo
88281806Srpaulo
89281806Srpaulostatic inline void os_reltime_age(struct os_reltime *start,
90281806Srpaulo				  struct os_reltime *age)
91281806Srpaulo{
92281806Srpaulo	struct os_reltime now;
93281806Srpaulo
94281806Srpaulo	os_get_reltime(&now);
95281806Srpaulo	os_reltime_sub(&now, start, age);
96281806Srpaulo}
97281806Srpaulo
98281806Srpaulo
99281806Srpaulostatic inline int os_reltime_expired(struct os_reltime *now,
100281806Srpaulo				     struct os_reltime *ts,
101281806Srpaulo				     os_time_t timeout_secs)
102281806Srpaulo{
103281806Srpaulo	struct os_reltime age;
104281806Srpaulo
105281806Srpaulo	os_reltime_sub(now, ts, &age);
106281806Srpaulo	return (age.sec > timeout_secs) ||
107281806Srpaulo	       (age.sec == timeout_secs && age.usec > 0);
108281806Srpaulo}
109281806Srpaulo
110281806Srpaulo
111281806Srpaulostatic inline int os_reltime_initialized(struct os_reltime *t)
112281806Srpaulo{
113281806Srpaulo	return t->sec != 0 || t->usec != 0;
114281806Srpaulo}
115281806Srpaulo
116281806Srpaulo
117189251Ssam/**
118189251Ssam * os_mktime - Convert broken-down time into seconds since 1970-01-01
119189251Ssam * @year: Four digit year
120189251Ssam * @month: Month (1 .. 12)
121189251Ssam * @day: Day of month (1 .. 31)
122189251Ssam * @hour: Hour (0 .. 23)
123189251Ssam * @min: Minute (0 .. 59)
124189251Ssam * @sec: Second (0 .. 60)
125189251Ssam * @t: Buffer for returning calendar time representation (seconds since
126189251Ssam * 1970-01-01 00:00:00)
127189251Ssam * Returns: 0 on success, -1 on failure
128189251Ssam *
129189251Ssam * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
130189251Ssam * which is used by POSIX mktime().
131189251Ssam */
132189251Ssamint os_mktime(int year, int month, int day, int hour, int min, int sec,
133189251Ssam	      os_time_t *t);
134189251Ssam
135252726Srpaulostruct os_tm {
136252726Srpaulo	int sec; /* 0..59 or 60 for leap seconds */
137252726Srpaulo	int min; /* 0..59 */
138252726Srpaulo	int hour; /* 0..23 */
139252726Srpaulo	int day; /* 1..31 */
140252726Srpaulo	int month; /* 1..12 */
141252726Srpaulo	int year; /* Four digit year */
142252726Srpaulo};
143189251Ssam
144252726Srpauloint os_gmtime(os_time_t t, struct os_tm *tm);
145252726Srpaulo
146189251Ssam/**
147189251Ssam * os_daemonize - Run in the background (detach from the controlling terminal)
148189251Ssam * @pid_file: File name to write the process ID to or %NULL to skip this
149189251Ssam * Returns: 0 on success, -1 on failure
150189251Ssam */
151189251Ssamint os_daemonize(const char *pid_file);
152189251Ssam
153189251Ssam/**
154189251Ssam * os_daemonize_terminate - Stop running in the background (remove pid file)
155189251Ssam * @pid_file: File name to write the process ID to or %NULL to skip this
156189251Ssam */
157189251Ssamvoid os_daemonize_terminate(const char *pid_file);
158189251Ssam
159189251Ssam/**
160189251Ssam * os_get_random - Get cryptographically strong pseudo random data
161189251Ssam * @buf: Buffer for pseudo random data
162189251Ssam * @len: Length of the buffer
163189251Ssam * Returns: 0 on success, -1 on failure
164189251Ssam */
165189251Ssamint os_get_random(unsigned char *buf, size_t len);
166189251Ssam
167189251Ssam/**
168189251Ssam * os_random - Get pseudo random value (not necessarily very strong)
169189251Ssam * Returns: Pseudo random value
170189251Ssam */
171189251Ssamunsigned long os_random(void);
172189251Ssam
173189251Ssam/**
174189251Ssam * os_rel2abs_path - Get an absolute path for a file
175189251Ssam * @rel_path: Relative path to a file
176189251Ssam * Returns: Absolute path for the file or %NULL on failure
177189251Ssam *
178189251Ssam * This function tries to convert a relative path of a file to an absolute path
179189251Ssam * in order for the file to be found even if current working directory has
180189251Ssam * changed. The returned value is allocated and caller is responsible for
181189251Ssam * freeing it. It is acceptable to just return the same path in an allocated
182189251Ssam * buffer, e.g., return strdup(rel_path). This function is only used to find
183189251Ssam * configuration files when os_daemonize() may have changed the current working
184189251Ssam * directory and relative path would be pointing to a different location.
185189251Ssam */
186189251Ssamchar * os_rel2abs_path(const char *rel_path);
187189251Ssam
188189251Ssam/**
189189251Ssam * os_program_init - Program initialization (called at start)
190189251Ssam * Returns: 0 on success, -1 on failure
191189251Ssam *
192189251Ssam * This function is called when a programs starts. If there are any OS specific
193189251Ssam * processing that is needed, it can be placed here. It is also acceptable to
194189251Ssam * just return 0 if not special processing is needed.
195189251Ssam */
196189251Ssamint os_program_init(void);
197189251Ssam
198189251Ssam/**
199189251Ssam * os_program_deinit - Program deinitialization (called just before exit)
200189251Ssam *
201189251Ssam * This function is called just before a program exists. If there are any OS
202189251Ssam * specific processing, e.g., freeing resourced allocated in os_program_init(),
203189251Ssam * it should be done here. It is also acceptable for this function to do
204189251Ssam * nothing.
205189251Ssam */
206189251Ssamvoid os_program_deinit(void);
207189251Ssam
208189251Ssam/**
209189251Ssam * os_setenv - Set environment variable
210189251Ssam * @name: Name of the variable
211189251Ssam * @value: Value to set to the variable
212189251Ssam * @overwrite: Whether existing variable should be overwritten
213189251Ssam * Returns: 0 on success, -1 on error
214189251Ssam *
215189251Ssam * This function is only used for wpa_cli action scripts. OS wrapper does not
216189251Ssam * need to implement this if such functionality is not needed.
217189251Ssam */
218189251Ssamint os_setenv(const char *name, const char *value, int overwrite);
219189251Ssam
220189251Ssam/**
221189251Ssam * os_unsetenv - Delete environent variable
222189251Ssam * @name: Name of the variable
223189251Ssam * Returns: 0 on success, -1 on error
224189251Ssam *
225189251Ssam * This function is only used for wpa_cli action scripts. OS wrapper does not
226189251Ssam * need to implement this if such functionality is not needed.
227189251Ssam */
228189251Ssamint os_unsetenv(const char *name);
229189251Ssam
230189251Ssam/**
231189251Ssam * os_readfile - Read a file to an allocated memory buffer
232189251Ssam * @name: Name of the file to read
233189251Ssam * @len: For returning the length of the allocated buffer
234189251Ssam * Returns: Pointer to the allocated buffer or %NULL on failure
235189251Ssam *
236189251Ssam * This function allocates memory and reads the given file to this buffer. Both
237189251Ssam * binary and text files can be read with this function. The caller is
238189251Ssam * responsible for freeing the returned buffer with os_free().
239189251Ssam */
240189251Ssamchar * os_readfile(const char *name, size_t *len);
241189251Ssam
242189251Ssam/**
243281806Srpaulo * os_file_exists - Check whether the specified file exists
244281806Srpaulo * @fname: Path and name of the file
245281806Srpaulo * Returns: 1 if the file exists or 0 if not
246281806Srpaulo */
247281806Srpauloint os_file_exists(const char *fname);
248281806Srpaulo
249281806Srpaulo/**
250189251Ssam * os_zalloc - Allocate and zero memory
251189251Ssam * @size: Number of bytes to allocate
252189251Ssam * Returns: Pointer to allocated and zeroed memory or %NULL on failure
253189251Ssam *
254189251Ssam * Caller is responsible for freeing the returned buffer with os_free().
255189251Ssam */
256189251Ssamvoid * os_zalloc(size_t size);
257189251Ssam
258252726Srpaulo/**
259252726Srpaulo * os_calloc - Allocate and zero memory for an array
260252726Srpaulo * @nmemb: Number of members in the array
261252726Srpaulo * @size: Number of bytes in each member
262252726Srpaulo * Returns: Pointer to allocated and zeroed memory or %NULL on failure
263252726Srpaulo *
264252726Srpaulo * This function can be used as a wrapper for os_zalloc(nmemb * size) when an
265252726Srpaulo * allocation is used for an array. The main benefit over os_zalloc() is in
266252726Srpaulo * having an extra check to catch integer overflows in multiplication.
267252726Srpaulo *
268252726Srpaulo * Caller is responsible for freeing the returned buffer with os_free().
269252726Srpaulo */
270252726Srpaulostatic inline void * os_calloc(size_t nmemb, size_t size)
271252726Srpaulo{
272252726Srpaulo	if (size && nmemb > (~(size_t) 0) / size)
273252726Srpaulo		return NULL;
274252726Srpaulo	return os_zalloc(nmemb * size);
275252726Srpaulo}
276189251Ssam
277252726Srpaulo
278189251Ssam/*
279189251Ssam * The following functions are wrapper for standard ANSI C or POSIX functions.
280189251Ssam * By default, they are just defined to use the standard function name and no
281189251Ssam * os_*.c implementation is needed for them. This avoids extra function calls
282189251Ssam * by allowing the C pre-processor take care of the function name mapping.
283189251Ssam *
284189251Ssam * If the target system uses a C library that does not provide these functions,
285189251Ssam * build_config.h can be used to define the wrappers to use a different
286189251Ssam * function name. This can be done on function-by-function basis since the
287189251Ssam * defines here are only used if build_config.h does not define the os_* name.
288189251Ssam * If needed, os_*.c file can be used to implement the functions that are not
289189251Ssam * included in the C library on the target system. Alternatively,
290189251Ssam * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
291189251Ssam * these functions need to be implemented in os_*.c file for the target system.
292189251Ssam */
293189251Ssam
294189251Ssam#ifdef OS_NO_C_LIB_DEFINES
295189251Ssam
296189251Ssam/**
297189251Ssam * os_malloc - Allocate dynamic memory
298189251Ssam * @size: Size of the buffer to allocate
299189251Ssam * Returns: Allocated buffer or %NULL on failure
300189251Ssam *
301189251Ssam * Caller is responsible for freeing the returned buffer with os_free().
302189251Ssam */
303189251Ssamvoid * os_malloc(size_t size);
304189251Ssam
305189251Ssam/**
306189251Ssam * os_realloc - Re-allocate dynamic memory
307189251Ssam * @ptr: Old buffer from os_malloc() or os_realloc()
308189251Ssam * @size: Size of the new buffer
309189251Ssam * Returns: Allocated buffer or %NULL on failure
310189251Ssam *
311189251Ssam * Caller is responsible for freeing the returned buffer with os_free().
312189251Ssam * If re-allocation fails, %NULL is returned and the original buffer (ptr) is
313189251Ssam * not freed and caller is still responsible for freeing it.
314189251Ssam */
315189251Ssamvoid * os_realloc(void *ptr, size_t size);
316189251Ssam
317189251Ssam/**
318189251Ssam * os_free - Free dynamic memory
319189251Ssam * @ptr: Old buffer from os_malloc() or os_realloc(); can be %NULL
320189251Ssam */
321189251Ssamvoid os_free(void *ptr);
322189251Ssam
323189251Ssam/**
324189251Ssam * os_memcpy - Copy memory area
325189251Ssam * @dest: Destination
326189251Ssam * @src: Source
327189251Ssam * @n: Number of bytes to copy
328189251Ssam * Returns: dest
329189251Ssam *
330189251Ssam * The memory areas src and dst must not overlap. os_memmove() can be used with
331189251Ssam * overlapping memory.
332189251Ssam */
333189251Ssamvoid * os_memcpy(void *dest, const void *src, size_t n);
334189251Ssam
335189251Ssam/**
336189251Ssam * os_memmove - Copy memory area
337189251Ssam * @dest: Destination
338189251Ssam * @src: Source
339189251Ssam * @n: Number of bytes to copy
340189251Ssam * Returns: dest
341189251Ssam *
342189251Ssam * The memory areas src and dst may overlap.
343189251Ssam */
344189251Ssamvoid * os_memmove(void *dest, const void *src, size_t n);
345189251Ssam
346189251Ssam/**
347189251Ssam * os_memset - Fill memory with a constant byte
348189251Ssam * @s: Memory area to be filled
349189251Ssam * @c: Constant byte
350189251Ssam * @n: Number of bytes started from s to fill with c
351189251Ssam * Returns: s
352189251Ssam */
353189251Ssamvoid * os_memset(void *s, int c, size_t n);
354189251Ssam
355189251Ssam/**
356189251Ssam * os_memcmp - Compare memory areas
357189251Ssam * @s1: First buffer
358189251Ssam * @s2: Second buffer
359189251Ssam * @n: Maximum numbers of octets to compare
360189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is
361189251Ssam * found to be less than, to match, or be greater than s2. Only first n
362189251Ssam * characters will be compared.
363189251Ssam */
364189251Ssamint os_memcmp(const void *s1, const void *s2, size_t n);
365189251Ssam
366189251Ssam/**
367189251Ssam * os_strdup - Duplicate a string
368189251Ssam * @s: Source string
369189251Ssam * Returns: Allocated buffer with the string copied into it or %NULL on failure
370189251Ssam *
371189251Ssam * Caller is responsible for freeing the returned buffer with os_free().
372189251Ssam */
373189251Ssamchar * os_strdup(const char *s);
374189251Ssam
375189251Ssam/**
376189251Ssam * os_strlen - Calculate the length of a string
377189251Ssam * @s: '\0' terminated string
378189251Ssam * Returns: Number of characters in s (not counting the '\0' terminator)
379189251Ssam */
380189251Ssamsize_t os_strlen(const char *s);
381189251Ssam
382189251Ssam/**
383189251Ssam * os_strcasecmp - Compare two strings ignoring case
384189251Ssam * @s1: First string
385189251Ssam * @s2: Second string
386189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is
387189251Ssam * found to be less than, to match, or be greatred than s2
388189251Ssam */
389189251Ssamint os_strcasecmp(const char *s1, const char *s2);
390189251Ssam
391189251Ssam/**
392189251Ssam * os_strncasecmp - Compare two strings ignoring case
393189251Ssam * @s1: First string
394189251Ssam * @s2: Second string
395189251Ssam * @n: Maximum numbers of characters to compare
396189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is
397189251Ssam * found to be less than, to match, or be greater than s2. Only first n
398189251Ssam * characters will be compared.
399189251Ssam */
400189251Ssamint os_strncasecmp(const char *s1, const char *s2, size_t n);
401189251Ssam
402189251Ssam/**
403189251Ssam * os_strchr - Locate the first occurrence of a character in string
404189251Ssam * @s: String
405189251Ssam * @c: Character to search for
406189251Ssam * Returns: Pointer to the matched character or %NULL if not found
407189251Ssam */
408189251Ssamchar * os_strchr(const char *s, int c);
409189251Ssam
410189251Ssam/**
411189251Ssam * os_strrchr - Locate the last occurrence of a character in string
412189251Ssam * @s: String
413189251Ssam * @c: Character to search for
414189251Ssam * Returns: Pointer to the matched character or %NULL if not found
415189251Ssam */
416189251Ssamchar * os_strrchr(const char *s, int c);
417189251Ssam
418189251Ssam/**
419189251Ssam * os_strcmp - Compare two strings
420189251Ssam * @s1: First string
421189251Ssam * @s2: Second string
422189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is
423189251Ssam * found to be less than, to match, or be greatred than s2
424189251Ssam */
425189251Ssamint os_strcmp(const char *s1, const char *s2);
426189251Ssam
427189251Ssam/**
428189251Ssam * os_strncmp - Compare two strings
429189251Ssam * @s1: First string
430189251Ssam * @s2: Second string
431189251Ssam * @n: Maximum numbers of characters to compare
432189251Ssam * Returns: An integer less than, equal to, or greater than zero if s1 is
433189251Ssam * found to be less than, to match, or be greater than s2. Only first n
434189251Ssam * characters will be compared.
435189251Ssam */
436189251Ssamint os_strncmp(const char *s1, const char *s2, size_t n);
437189251Ssam
438189251Ssam/**
439189251Ssam * os_strstr - Locate a substring
440189251Ssam * @haystack: String (haystack) to search from
441189251Ssam * @needle: Needle to search from haystack
442189251Ssam * Returns: Pointer to the beginning of the substring or %NULL if not found
443189251Ssam */
444189251Ssamchar * os_strstr(const char *haystack, const char *needle);
445189251Ssam
446189251Ssam/**
447189251Ssam * os_snprintf - Print to a memory buffer
448189251Ssam * @str: Memory buffer to print into
449189251Ssam * @size: Maximum length of the str buffer
450189251Ssam * @format: printf format
451189251Ssam * Returns: Number of characters printed (not including trailing '\0').
452189251Ssam *
453189251Ssam * If the output buffer is truncated, number of characters which would have
454189251Ssam * been written is returned. Since some C libraries return -1 in such a case,
455189251Ssam * the caller must be prepared on that value, too, to indicate truncation.
456189251Ssam *
457189251Ssam * Note: Some C library implementations of snprintf() may not guarantee null
458189251Ssam * termination in case the output is truncated. The OS wrapper function of
459189251Ssam * os_snprintf() should provide this guarantee, i.e., to null terminate the
460189251Ssam * output buffer if a C library version of the function is used and if that
461189251Ssam * function does not guarantee null termination.
462189251Ssam *
463189251Ssam * If the target system does not include snprintf(), see, e.g.,
464189251Ssam * http://www.ijs.si/software/snprintf/ for an example of a portable
465189251Ssam * implementation of snprintf.
466189251Ssam */
467189251Ssamint os_snprintf(char *str, size_t size, const char *format, ...);
468189251Ssam
469189251Ssam#else /* OS_NO_C_LIB_DEFINES */
470189251Ssam
471214734Srpaulo#ifdef WPA_TRACE
472214734Srpaulovoid * os_malloc(size_t size);
473214734Srpaulovoid * os_realloc(void *ptr, size_t size);
474214734Srpaulovoid os_free(void *ptr);
475214734Srpaulochar * os_strdup(const char *s);
476214734Srpaulo#else /* WPA_TRACE */
477189251Ssam#ifndef os_malloc
478189251Ssam#define os_malloc(s) malloc((s))
479189251Ssam#endif
480189251Ssam#ifndef os_realloc
481189251Ssam#define os_realloc(p, s) realloc((p), (s))
482189251Ssam#endif
483189251Ssam#ifndef os_free
484189251Ssam#define os_free(p) free((p))
485189251Ssam#endif
486214734Srpaulo#ifndef os_strdup
487214734Srpaulo#ifdef _MSC_VER
488214734Srpaulo#define os_strdup(s) _strdup(s)
489214734Srpaulo#else
490214734Srpaulo#define os_strdup(s) strdup(s)
491214734Srpaulo#endif
492214734Srpaulo#endif
493214734Srpaulo#endif /* WPA_TRACE */
494189251Ssam
495189251Ssam#ifndef os_memcpy
496189251Ssam#define os_memcpy(d, s, n) memcpy((d), (s), (n))
497189251Ssam#endif
498189251Ssam#ifndef os_memmove
499189251Ssam#define os_memmove(d, s, n) memmove((d), (s), (n))
500189251Ssam#endif
501189251Ssam#ifndef os_memset
502189251Ssam#define os_memset(s, c, n) memset(s, c, n)
503189251Ssam#endif
504189251Ssam#ifndef os_memcmp
505189251Ssam#define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
506189251Ssam#endif
507189251Ssam
508189251Ssam#ifndef os_strlen
509189251Ssam#define os_strlen(s) strlen(s)
510189251Ssam#endif
511189251Ssam#ifndef os_strcasecmp
512189251Ssam#ifdef _MSC_VER
513189251Ssam#define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
514189251Ssam#else
515189251Ssam#define os_strcasecmp(s1, s2) strcasecmp((s1), (s2))
516189251Ssam#endif
517189251Ssam#endif
518189251Ssam#ifndef os_strncasecmp
519189251Ssam#ifdef _MSC_VER
520189251Ssam#define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
521189251Ssam#else
522189251Ssam#define os_strncasecmp(s1, s2, n) strncasecmp((s1), (s2), (n))
523189251Ssam#endif
524189251Ssam#endif
525189251Ssam#ifndef os_strchr
526189251Ssam#define os_strchr(s, c) strchr((s), (c))
527189251Ssam#endif
528189251Ssam#ifndef os_strcmp
529189251Ssam#define os_strcmp(s1, s2) strcmp((s1), (s2))
530189251Ssam#endif
531189251Ssam#ifndef os_strncmp
532189251Ssam#define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
533189251Ssam#endif
534189251Ssam#ifndef os_strrchr
535189251Ssam#define os_strrchr(s, c) strrchr((s), (c))
536189251Ssam#endif
537189251Ssam#ifndef os_strstr
538189251Ssam#define os_strstr(h, n) strstr((h), (n))
539189251Ssam#endif
540189251Ssam
541189251Ssam#ifndef os_snprintf
542189251Ssam#ifdef _MSC_VER
543189251Ssam#define os_snprintf _snprintf
544189251Ssam#else
545189251Ssam#define os_snprintf snprintf
546189251Ssam#endif
547189251Ssam#endif
548189251Ssam
549189251Ssam#endif /* OS_NO_C_LIB_DEFINES */
550189251Ssam
551189251Ssam
552281806Srpaulostatic inline int os_snprintf_error(size_t size, int res)
553281806Srpaulo{
554281806Srpaulo	return res < 0 || (unsigned int) res >= size;
555281806Srpaulo}
556281806Srpaulo
557281806Srpaulo
558252726Srpaulostatic inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
559252726Srpaulo{
560252726Srpaulo	if (size && nmemb > (~(size_t) 0) / size)
561252726Srpaulo		return NULL;
562252726Srpaulo	return os_realloc(ptr, nmemb * size);
563252726Srpaulo}
564252726Srpaulo
565281806Srpaulo/**
566281806Srpaulo * os_remove_in_array - Remove a member from an array by index
567281806Srpaulo * @ptr: Pointer to the array
568281806Srpaulo * @nmemb: Current member count of the array
569281806Srpaulo * @size: The size per member of the array
570281806Srpaulo * @idx: Index of the member to be removed
571281806Srpaulo */
572281806Srpaulostatic inline void os_remove_in_array(void *ptr, size_t nmemb, size_t size,
573281806Srpaulo				      size_t idx)
574281806Srpaulo{
575281806Srpaulo	if (idx < nmemb - 1)
576281806Srpaulo		os_memmove(((unsigned char *) ptr) + idx * size,
577281806Srpaulo			   ((unsigned char *) ptr) + (idx + 1) * size,
578281806Srpaulo			   (nmemb - idx - 1) * size);
579281806Srpaulo}
580252726Srpaulo
581189251Ssam/**
582189251Ssam * os_strlcpy - Copy a string with size bound and NUL-termination
583189251Ssam * @dest: Destination
584189251Ssam * @src: Source
585189251Ssam * @siz: Size of the target buffer
586189251Ssam * Returns: Total length of the target string (length of src) (not including
587189251Ssam * NUL-termination)
588189251Ssam *
589189251Ssam * This function matches in behavior with the strlcpy(3) function in OpenBSD.
590189251Ssam */
591189251Ssamsize_t os_strlcpy(char *dest, const char *src, size_t siz);
592189251Ssam
593281806Srpaulo/**
594281806Srpaulo * os_memcmp_const - Constant time memory comparison
595281806Srpaulo * @a: First buffer to compare
596281806Srpaulo * @b: Second buffer to compare
597281806Srpaulo * @len: Number of octets to compare
598281806Srpaulo * Returns: 0 if buffers are equal, non-zero if not
599281806Srpaulo *
600281806Srpaulo * This function is meant for comparing passwords or hash values where
601281806Srpaulo * difference in execution time could provide external observer information
602281806Srpaulo * about the location of the difference in the memory buffers. The return value
603281806Srpaulo * does not behave like os_memcmp(), i.e., os_memcmp_const() cannot be used to
604281806Srpaulo * sort items into a defined order. Unlike os_memcmp(), execution time of
605281806Srpaulo * os_memcmp_const() does not depend on the contents of the compared memory
606281806Srpaulo * buffers, but only on the total compared length.
607281806Srpaulo */
608281806Srpauloint os_memcmp_const(const void *a, const void *b, size_t len);
609189251Ssam
610281806Srpaulo/**
611281806Srpaulo * os_exec - Execute an external program
612281806Srpaulo * @program: Path to the program
613281806Srpaulo * @arg: Command line argument string
614281806Srpaulo * @wait_completion: Whether to wait until the program execution completes
615281806Srpaulo * Returns: 0 on success, -1 on error
616281806Srpaulo */
617281806Srpauloint os_exec(const char *program, const char *arg, int wait_completion);
618281806Srpaulo
619281806Srpaulo
620189251Ssam#ifdef OS_REJECT_C_LIB_FUNCTIONS
621189251Ssam#define malloc OS_DO_NOT_USE_malloc
622189251Ssam#define realloc OS_DO_NOT_USE_realloc
623189251Ssam#define free OS_DO_NOT_USE_free
624189251Ssam#define memcpy OS_DO_NOT_USE_memcpy
625189251Ssam#define memmove OS_DO_NOT_USE_memmove
626189251Ssam#define memset OS_DO_NOT_USE_memset
627189251Ssam#define memcmp OS_DO_NOT_USE_memcmp
628189251Ssam#undef strdup
629189251Ssam#define strdup OS_DO_NOT_USE_strdup
630189251Ssam#define strlen OS_DO_NOT_USE_strlen
631189251Ssam#define strcasecmp OS_DO_NOT_USE_strcasecmp
632189251Ssam#define strncasecmp OS_DO_NOT_USE_strncasecmp
633189251Ssam#undef strchr
634189251Ssam#define strchr OS_DO_NOT_USE_strchr
635189251Ssam#undef strcmp
636189251Ssam#define strcmp OS_DO_NOT_USE_strcmp
637189251Ssam#undef strncmp
638189251Ssam#define strncmp OS_DO_NOT_USE_strncmp
639189251Ssam#undef strncpy
640189251Ssam#define strncpy OS_DO_NOT_USE_strncpy
641189251Ssam#define strrchr OS_DO_NOT_USE_strrchr
642189251Ssam#define strstr OS_DO_NOT_USE_strstr
643189251Ssam#undef snprintf
644189251Ssam#define snprintf OS_DO_NOT_USE_snprintf
645189251Ssam
646189251Ssam#define strcpy OS_DO_NOT_USE_strcpy
647189251Ssam#endif /* OS_REJECT_C_LIB_FUNCTIONS */
648189251Ssam
649189251Ssam#endif /* OS_H */
650