Debian Codegraph @

src/common/util.h

raw symbols SCIP
1 /*
2 * Copyright 2009 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Lukas Kuklinek <[email protected]>
21 */
22
23
24 #ifndef OSCAP_UTIL_H_
25 #define OSCAP_UTIL_H_
26
27 #include "oscap_platforms.h"
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdbool.h>
31 #include <assert.h>
32 #include "public/oscap.h"
33 #include <stdarg.h>
34 #include <string.h>
35 #include "oscap_export.h"
36 #include "oscap_pcre.h"
37
38 #ifndef __attribute__nonnull__
39 #define __attribute__nonnull__(x) assert((x) != NULL)
40 #endif
41
42 /**
43 * Function pointer to an object destructor.
44 */
45 typedef void (*oscap_destruct_func) (void *);
46
47 /**
48 * Pointer to a clone function.
49 */
50 typedef void* (*oscap_clone_func) (void *);
51
52 /**
53 * Function pointer to an object consumer.
54 */
55 typedef void (*oscap_consumer_func) (void *, void *);
56
57 /**
58 * Generate a getter function with an optional conversion.
59 * Header of the generated function will look like this (substitute the capital letter sequences with the actual parametres):
60 * @code RTYPE SNAME_MNAME(const struct SNAME*); @endcode
61 * @param RTYPE return type
62 * @param CONV conversion expression, i.e. either a type conversion of form '(type)' or an unary function identifier
63 * @param SNAME name of the structure of which we are getting a member (without the struct keyword)
64 * @param MNAME member name affects how the resultant function will be called
65 * @param MEXP expression to get the member
66 */
67 #define OSCAP_GENERIC_GETTER_CONV(RTYPE,CONV,SNAME,MNAME,MEXP) \
68 RTYPE SNAME##_get_##MNAME(const struct SNAME* item) { return (CONV(item->MEXP)); }
69
70 /**
71 * Generate a getter function.
72 * @param RTYPE return type
73 * @param SNAME name of the structure of which we are getting a member
74 * @param MNAME member name affects how the resultant function will be called
75 * @param MEXP expression to get the member
76 * @see OSCAP_GENERIC_GETTER_CONV
77 */
78 #define OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MEXP) \
79 OSCAP_GENERIC_GETTER_CONV(RTYPE,,SNAME,MNAME,MEXP)
80
81 /**
82 * Generate a getter function with an explicit conversion to the target data type.
83 * @param RTYPE return type
84 * @param SNAME name of the structure of which we are getting a member
85 * @param MNAME member name affects how the resultant function will be called
86 * @param MEXP expression to get the member
87 * @see OSCAP_GENERIC_GETTER_CONV
88 */
89 #define OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MEXP) \
90 OSCAP_GENERIC_GETTER_CONV(RTYPE,(RTYPE),SNAME,MNAME,MEXP)
91
92 /**
93 * Generate a getter function with an explicit conversion to the target data type.
94 * Member name and second part of the generated function's name must match.
95 * @param RTYPE return type
96 * @param SNAME name of the structure of which we are getting a member
97 * @param MNAME member name affects how the resultant function will be called
98 * @see OSCAP_GENERIC_GETTER_FORCE
99 */
100 #define OSCAP_GETTER_FORCE(RTYPE,SNAME,MNAME) \
101 OSCAP_GENERIC_GETTER_FORCE(RTYPE,SNAME,MNAME,MNAME)
102
103 /**
104 * Generate a getter function.
105 * Member name and second part of the generated function's name must match.
106 * @param RTYPE return type
107 * @param SNAME name of the structure of which we are getting a member
108 * @param MNAME member name affects how the resultant function will be called
109 * @see OSCAP_GENERIC_GETTER
110 */
111 #define OSCAP_GETTER(RTYPE,SNAME,MNAME) \
112 OSCAP_GENERIC_GETTER(RTYPE,SNAME,MNAME,MNAME)
113
114 #define ITERATOR_CAST(x) ((struct oscap_iterator*)(x))
115 #define OSCAP_ITERATOR(n) struct n##_iterator*
116 #define OSCAP_ITERATOR_FWD(n) struct n##_iterator;
117 #define OSCAP_ITERATOR_HAS_MORE(n) bool n##_iterator_has_more(OSCAP_ITERATOR(n) it) { return oscap_iterator_has_more(ITERATOR_CAST(it)); }
118 #define OSCAP_ITERATOR_NEXT(t,n) t n##_iterator_next(OSCAP_ITERATOR(n) it) { return oscap_iterator_next(ITERATOR_CAST(it)); }
119 #define OSCAP_ITERATOR_FREE(n) void n##_iterator_free(OSCAP_ITERATOR(n) it) { oscap_iterator_free(ITERATOR_CAST(it)); }
120 #define OSCAP_ITERATOR_RESET(n) void n##_iterator_reset(OSCAP_ITERATOR(n) it) { oscap_iterator_reset(ITERATOR_CAST(it)); }
121 #define OSCAP_ITERATOR_DETACH(t,n) t n##_iterator_detach(OSCAP_ITERATOR(n) it) { return oscap_iterator_detach(ITERATOR_CAST(it)); }
122 #define OSCAP_ITERATOR_GEN_T(t,n) OSCAP_ITERATOR_FWD(n) OSCAP_ITERATOR_HAS_MORE(n) OSCAP_ITERATOR_RESET(n) OSCAP_ITERATOR_NEXT(t,n) OSCAP_ITERATOR_FREE(n)
123 #define OSCAP_ITERATOR_GEN(n) OSCAP_ITERATOR_GEN_T(struct n*,n)
124
125 #define OSCAP_ITERATOR_REMOVE_T(t,n,destructor) \
126 void n##_iterator_remove(OSCAP_ITERATOR(n) it) { destructor(oscap_iterator_detach(ITERATOR_CAST(it))); }
127 #define OSCAP_ITERATOR_REMOVE(n,destructor) OSCAP_ITERATOR_REMOVE_T(struct n*,n,destructor)
128 #define OSCAP_ITERATOR_REMOVE_F(n) OSCAP_ITERATOR_REMOVE(n, n##_free)
129
130
131 /**
132 * Generate an iterator getter function.
133 * Signature of the generated function will be as follows (substitute uppercase strings with actual params):
134 * @code struct ITYPE_iterator* SNAME_MNAME(const struct SNAME*); @endcode
135 * @param ITYPE iterator type (without the '_iterator' suffix)
136 * @param SNAME name of type the structure containing the list
137 * @param MANME list itself
138 * @param CONV convert expression
139 */
140 #define OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,CONV) \
141 struct ITYPE##_iterator* SNAME##_get_##MNAME(const struct SNAME* item) \
142 { return oscap_iterator_new((CONV(item))->MNAME); }
143
144 /**
145 * Generate an iterator getter function.
146 * Signature of the generated function will be as follows (substitute uppercase strings with actual params):
147 * @code struct ITYPE_iterator* SNAME_MNAME(const struct SNAME*); @endcode
148 * @param ITYPE iterator type (without the '_iterator' suffix)
149 * @param SNAME name of type the structure containing the list
150 * @param MANME list itself
151 */
152 #define OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_IGETTER_CONV(ITYPE,SNAME,MNAME,)
153
154 /**
155 * Generate an iterator and its getter.
156 * @see OSCAP_IGETTER
157 * @see OSCAP_ITERATOR_GEN
158 */
159 #define OSCAP_IGETTER_GEN(ITYPE,SNAME,MNAME) OSCAP_IGETTER(ITYPE,SNAME,MNAME) OSCAP_ITERATOR_GEN(ITYPE)
160
161 /**
162 * Generate a getter function from a hash table.
163 * Signature of the generated function will be as follows (substitute uppercase strings with actual params):
164 * @code RTYPE SNAME_MNAME(const struct SNAME* item, const char* key); @endcode
165 * @param RTYPE type of the hash table item
166 * @param SNAME name of the structure
167 * @param MNAME structure member name
168 * @param MEXP expression to get the member variable (i.e. the hash table)
169 */
170 #define OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MEXP) \
171 RTYPE SNAME##_get_##MNAME(const struct SNAME* item, const char* key) \
172 { return oscap_htable_get(item->MEXP, key); }
173
174 /**
175 * Generate a getter function from a hash table.
176 * Signature of the generated function will be as follows (substitute uppercase strings with actual params):
177 * @code RTYPE SNAME_MNAME(const struct SNAME* item, const char* key); @endcode
178 * @param RTYPE type of the hash table item
179 * @param SNAME name of the structure
180 * @param MNAME structure member name
181 */
182 #define OSCAP_HGETTER(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(RTYPE,SNAME,MNAME,MNAME)
183
184 /**
185 * Generate a getter function from a hash table.
186 * Signature of the generated function will be as follows (substitute uppercase strings with actual params):
187 * @code struct RTYPE* SNAME_MNAME(const struct SNAME* item, const char* key); @endcode
188 * @param RTYPE type of the hash table item
189 * @param SNAME name of the structure
190 * @param MNAME structure member name
191 */
192 #define OSCAP_HGETTER_STRUCT(RTYPE,SNAME,MNAME) OSCAP_HGETTER_EXP(struct RTYPE*,SNAME,MNAME,MNAME)
193
194 #define OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) bool SNAME##_set_##MNAME(struct SNAME *obj, MTYPE newval)
195
196 /**
197 * Generate a setter function with a check.
198 * Signature of the generated function will be as follows (substitute uppercase strings with actual params):
199 * @code bool SNAME_get_MNAME(struct SNAME *obj, MTYPE *item); @endcode
200 * @param SNAME Name of the structure.
201 * @param MTYPE Type of the member item to set.
202 * @param MNAME Name of the member item to set.
203 * @param CHECK Code to check validity of the assignment (you can use obj and item variables here).
204 * @param DELETER Function used to delete the old value (or empty string).
205 * @param ASSIGNER Function used to assign value to the item.
206 */
207 #define OSCAP_SETTER_GENERIC_CHECK(SNAME, MTYPE, MNAME, CHECK, DELETER, ASSIGNER) \
208 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
209 { if (!(CHECK)) return false; DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
210
211 /**
212 * Generate a setter function without a check.
213 * @see OSCAP_SETTER_GENERIC_CHECK
214 */
215 #define OSCAP_SETTER_GENERIC(SNAME, MTYPE, MNAME, DELETER, ASSIGNER) \
216 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
217 { DELETER(obj->MNAME); obj->MNAME = ASSIGNER(newval); return true; }
218
219 /**
220 * Generate a setter function without a check that does not delete the previous value.
221 * @see OSCAP_SETTER_GENERIC_CHECK
222 */
223 #define OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, ASSIGNER) \
224 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) \
225 { obj->MNAME = ASSIGNER(newval); return true; }
226
227 /**
228 * Generate a setter function using a simple assignment.
229 * @see OSCAP_SETTER_GENERIC_CHECK
230 */
231 #define OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME) \
232 OSCAP_SETTER_GENERIC_NODELETE(SNAME, MTYPE, MNAME, )
233
234 /**
235 * Assign a string value to a structure member.
236 * @param SNAME Structure name
237 * @param MNAME Name of structure member containing the string to be set.
238 */
239 #define OSCAP_SETTER_STRING(SNAME, MNAME) \
240 OSCAP_SETTER_GENERIC(SNAME, const char *, MNAME, free, oscap_strdup)
241
242 /**
243 * Define both, getter and setter for a string structure member.
244 */
245 #define OSCAP_ACCESSOR_STRING(SNAME, MNAME) \
246 OSCAP_GETTER(const char*, SNAME, MNAME) OSCAP_SETTER_STRING(SNAME, MNAME)
247
248 /**
249 * Define both, getter and setter for a text structure member.
250 */
251 #define OSCAP_ACCESSOR_TEXT(SNAME, MNAME) \
252 OSCAP_GETTER(struct oscap_text *, SNAME, MNAME) \
253 OSCAP_SETTER_GENERIC(SNAME, struct oscap_text*, MNAME, oscap_text_free, )
254
255 /**
256 * Define both, getter and setter for a structure member using simple get/set.
257 */
258 #define OSCAP_ACCESSOR_SIMPLE(MTYPE, SNAME, MNAME) \
259 OSCAP_GETTER(MTYPE, SNAME, MNAME) OSCAP_SETTER_SIMPLE(SNAME, MTYPE, MNAME)
260
261 /**
262 * Define both, getter and setter for a structure member using simple get/set
263 * using different expression to het the member.
264 */
265 #define OSCAP_ACCESSOR_EXP(MTYPE, SNAME, MNAME, MEXP) \
266 OSCAP_GENERIC_GETTER(MTYPE, SNAME, MNAME, MEXP) \
267 OSCAP_SETTER_HEADER(SNAME, MTYPE, MNAME) { obj->MEXP = newval; return true; }
268
269 /**
270 * Generate function to insert an item into a list.
271 * Signature of the generated function will be as follows (substitute uppercase strings with actual params):
272 * @code bool SNAME_add_FNAME(struct SNAME *obj, struct MTYPE *item); @endcode
273 * @param SNAME structure typename (w/o the struct keyword)
274 * @param FNAME how should function name part after the _add_ look like
275 * @param MTYPE list member type
276 * @param MNAME name of the list within the structure
277 */
278 #define OSCAP_INSERTER(SNAME, FNAME, MTYPE, MNAME) \
279 bool SNAME##_add_##FNAME(struct SNAME *obj, struct MTYPE *item) \
280 { oscap_list_add(obj->MNAME, item); return true; }
281
282 /* Generate iterator getter and list inserter */
283 #define OSCAP_IGETINS(ITYPE, SNAME, MNAME, FNAME) \
284 OSCAP_IGETTER(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
285 /* Generate iterator getter, list inserter, and iterator manipulation functions. */
286 #define OSCAP_IGETINS_GEN(ITYPE, SNAME, MNAME, FNAME) \
287 OSCAP_IGETTER_GEN(ITYPE, SNAME, MNAME) OSCAP_INSERTER(SNAME, FNAME, ITYPE, MNAME)
288
289 /**
290 * Define mapping between symbolic constant and its string representation.
291 *
292 * It is supposed to define array of these structures, ending with element
293 * with the @a string member set to NULL. Value of such member also defines
294 * the default value for strings not defined elsewhere.
295 */
296 struct oscap_string_map {
297 const int value; /* integer/enum value */
298 const char *string; /* string representation of the value */
299 };
300
301 /**
302 * Use strdup on string, if string is NULL, return NULL
303 * @param str String we want to duplicate
304 */
305 static inline char *oscap_strdup(const char *str) {
306 if (str == NULL)
307 return NULL;
308
309 #ifdef _MSC_VER
310 return _strdup(str);
311 #else
312 return strdup(str);
313 #endif
314 }
315
316 /**
317 * Removes all occurrences of substr from str by shifting the contents to
318 * the left. If string is NULL, does nothing.
319 * @param str String we want to process
320 * @param substr Sub-string we want to get rid of
321 */
322 static inline void oscap_strrm(char *str, const char *substr) {
323 if (str == NULL)
324 return;
325
326 size_t sublen = strlen(substr);
327 char *ptr = strstr(str, substr);
328 while (ptr != NULL) {
329 memmove(ptr, ptr+sublen, strlen(ptr) - sublen + 1);
330 ptr = strstr(str, substr);
331 }
332 }
333
334 /// Just like strcmp except it's NULL-safe. Use the standard strcmp directly if possible.
335 static inline int oscap_strcmp(const char *s1, const char *s2) {
336 if (s1 == NULL) s1 = "";
337 if (s2 == NULL) s2 = "";
338 return strcmp(s1, s2);
339 }
340
341 /// Check for string equality. Use the standard strcmp directly if possible.
342 static inline bool oscap_streq(const char *s1, const char *s2) {
343 return oscap_strcmp(s1, s2) == 0;
344 }
345
346 /// Check whether str starts with "prefix"
347 static inline bool oscap_str_startswith(const char *str, const char *prefix) {
348 return strncmp(str, prefix, strlen(prefix)) == 0;
349 }
350
351 /// Check whether str ends with "suffix"
352 static inline bool oscap_str_endswith(const char *str, const char *suffix) {
353 const size_t str_len = strlen(str);
354 const size_t suffix_len = strlen(suffix);
355 if (suffix_len > str_len)
356 return false;
357 return strncmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
358 }
359
360 /// Allocate aligned memory
361 static inline void *oscap_aligned_malloc(size_t size, size_t alignment) {
362 #ifdef WIN32
363 return _aligned_malloc(size, alignment);
364 #else
365 void *ptr = NULL;
366 int ret = posix_memalign(&ptr, alignment, size);
367 if (ret == 0)
368 return ptr;
369 return NULL;
370 #endif
371 }
372
373 /// Free aligned memory
374 static inline void oscap_aligned_free(void *memblock) {
375 #ifdef WIN32
376 _aligned_free(memblock);
377 #else
378 free(memblock);
379 #endif
380 }
381
382 /// Trim whitespace (modifies its argument!)
383 char *oscap_trim(char *str);
384 /// Print to a newly allocated string using a va_list.
385 char *oscap_vsprintf(const char *fmt, va_list ap);
386
387 /**
388 * Generates a pseudorandom string of a given length.
389 * If charset string is not NULL and its length is greater than 0,
390 * it will be used as a dictionary, otherwise a default alphanumeric set
391 * will be the base for the generated string.
392 * Caller is responsible for freeing the returned string.
393 * @param len desired string length (must be greater than 0)
394 * @param charset a dictionary string, could be NULL
395 * @return A random string of desired length.
396 */
397 char *oscap_generate_random_string(size_t len, char *charset);
398
399 /**
400 * Join 2 paths in an intelligent way.
401 * Both paths are allowed to be NULL.
402 * Caller is responsible for freeing the returned pointer.
403 * @param path1 first path
404 * @param path2 second path
405 * @return Join of path1 and path2. The first path is separated by the second
406 * path by exactly 1 slash separator.
407 */
408 char *oscap_path_join(const char *path1, const char *path2);
409
410 /// In a list of key-value pairs (odd indicies are keys, even values), find a value for given key
411 const char *oscap_strlist_find_value(char ** const kvalues, const char *key);
412 /// Right trim @a ch characters (modifies its first argument!)
413 char *oscap_rtrim(char *str, char ch);
414 /// Convert string to uppercase (modifies its first argument!)
415 void oscap_strtoupper(char *str);
416
417 // check pointer equality
418 bool oscap_ptr_cmp(void *node1, void *node2);
419
420 /**
421 * A helper function to expand given shorthand IPv6
422 *
423 * example:
424 * oscap_expand_ipv6("::1") returns "0:0:0:0:0:0:0:1"
425 *
426 * @par
427 * Asset Identification specification requires fully expanded IPv6 addresses.
428 * Despite this being greatly discouraged by RFC 5952 we have to comply and
429 * expand given IPv6, otherwise the resulting AI content would be invalid.
430 */
431 char *oscap_expand_ipv6(const char *input);
432
433 #ifndef OSCAP_CONCAT
434 # define OSCAP_CONCAT1(a,b) a ## b
435 # define OSCAP_CONCAT(a,b) OSCAP_CONCAT1(a,b)
436 #endif
437
438 /**
439 * Mark global symbols.
440 * This macro can be used to wrap global variables and helps distinguish
441 * global variables and local varibles.
442 * It adds '___G_' prefix to variable name.
443 */
444 #define OSCAP_GSYM(s) OSCAP_CONCAT(___G_, s)
445
446 #define protect_errno \
447 for (int OSCAP_CONCAT(__e,__LINE__)=errno, OSCAP_CONCAT(__s,__LINE__)=1; OSCAP_CONCAT(__s,__LINE__)--; errno=OSCAP_CONCAT(__e,__LINE__))
448
449 /**
450 * Convert a string to an enumeration constant.
451 * @param map An array of oscap_string_map structures that defines mapping between constants and strings.
452 * @param str string to be converted
453 * @memberof oscap_string_map
454 */
455 int oscap_string_to_enum(const struct oscap_string_map *map, const char *str);
456
457 /**
458 * Convert an enumeration constant to its corresponding string representation.
459 * @param map An array of oscap_string_map structures that defines mapping between constants and strings.
460 * @param val value to be converted
461 * @memberof oscap_string_map
462 */
463 const char *oscap_enum_to_string(const struct oscap_string_map *map, int val);
464
465 /**
466 * Split a string.
467 * Split string using given delimiter.
468 * Produces NULL-terminated array of strings.
469 * Modifies its first argument!
470 * @param str String we want to split
471 * @param delim Delimiter of string parts
472 */
473 char **oscap_split(char *str, const char *delim);
474
475 /**
476 * compare two strings ignoring case
477 * @param s1 first string
478 * @param s2 second string
479 * @return an integer less than, equal to, or greater than zero if s1 is,
480 * after ignoring case, found to be less than, to match, or be greater
481 * than s2, respectively.
482 */
483 int oscap_strcasecmp(const char *s1, const char *s2);
484
485 /**
486 * compare two strings ignoring case
487 * @param s1 first string
488 * @param s2 second string
489 * @param n compare no more than n bytes of s1 and s2
490 * @return an integer less than, equal to, or greater than zero if s1 is,
491 * after ignoring case, found to be less than, to match, or be greater
492 * than s2, respectively.
493 */
494 int oscap_strncasecmp(const char *s1, const char *s2, size_t n);
495
496 /**
497 * Get string describing error number
498 * @param errnum error number
499 * @param buf buffer to hold error string
500 * @param buflen size of buffer
501 */
502 char *oscap_strerror_r(int errnum, char *buf, size_t buflen);
503
504 #ifndef OS_WINDOWS
505 /**
506 * Open file for reading with prefix added to its name.
507 * Caller is responsible for closing the handle.
508 * @param prefix the prefix, might be NULL or empty
509 * @param path the file name with the path
510 * @return FILE* handle of opened file
511 * NULL on failure
512 */
513 FILE *oscap_fopen_with_prefix(const char *prefix, const char *path);
514 #endif
515
516 #ifdef OS_WINDOWS
517 /**
518 * Convert wide character string to a C string (UTF-16 to UTF-8)
519 * It is a wrapper around Windows API WideCharToMultiByte().
520 * Caller is responsible for freein the returned string.
521 * @param wstr wide string
522 * @return C string
523 */
524 char *oscap_windows_wstr_to_str(const wchar_t *wstr);
525
526 /**
527 * Convert C string to wide character string (UTF-8 to UTF-16)
528 * It is a wrapper around Windows API MultiByteToWideChar().
529 * Caller is responsible for freeing the returned string.
530 * @param str C string
531 * @return wide string
532 */
533 wchar_t *oscap_windows_str_to_wstr(const char *str);
534
535 /**
536 * Get formated message about Windows API errors.
537 * It is a wrapper around Windows API FormatMessage().
538 * Caller is responsible for freeing the returned string.
539 * @param error_code Error code returned by a Windows API call.
540 * @return Error message in a form of a C string.
541 */
542 char *oscap_windows_error_message(unsigned long error_code);
543 #endif
544
545 /**
546 * Open a file for writing.
547 * The main difference from fopen() is that if the file exists but its opening
548 * for writing fails as permission denied, it will attempt to open it again
549 * without the O_CREAT flag. This is useful when writing to world-writeable
550 * directories with sticky bit such as /tmp on systems with fs.protected_regular
551 * turned on.
552 * @param filename name of the file to be opened
553 * @return file descriptor or -1 on error
554 */
555 int oscap_open_writable(const char *filename);
556
557 /**
558 * Check if a path starts with the given prefix
559 * @param path file system path
560 * @param prefix file system path that will be tested if it's a prefix of the path parameter
561 * @return true or false
562 */
563 bool oscap_path_startswith(const char *path, const char *prefix);
564
565 /**
566 * Concatenate 2 strings
567 * Convenience wrapper over strncat.
568 * @param str1 string 1
569 * @param str2 string 2
570 * @return string 1 with string 2 appended inside
571 */
572 char *oscap_concat(char *str1, char *str2);
573
574 /**
575 * Remove excess leading and trailing whitespace from a string
576 * Caller is responsible for freeing the returned string.
577 * @param str input string
578 * @return A new string with excess leading and trailing whitespace removed, or NULL if input is NULL
579 */
580 char *oscap_remove_excess_whitespace(const char *str);
581
582 #endif /* OSCAP_UTIL_H_ */