]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/string_utils.h
tree-wide: use lxc_drop_groups() instead of lxc_setgroups(0, NULL)
[mirror_lxc.git] / src / lxc / string_utils.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_STRING_UTILS_H
4 #define __LXC_STRING_UTILS_H
5
6 #include <stdarg.h>
7
8 #include "config.h"
9
10 #include "initutils.h"
11 #include "macro.h"
12
13 #ifndef HAVE_STRLCAT
14 #include "include/strlcat.h"
15 #endif
16
17 /* convert variadic argument lists to arrays (for execl type argument lists) */
18 __hidden extern char **lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
19 __hidden extern const char **lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);
20
21 /*
22 * Some simple string functions; if they return pointers, they are allocated
23 * buffers.
24 */
25 __hidden extern char *lxc_string_replace(const char *needle, const char *replacement,
26 const char *haystack);
27 __hidden extern bool lxc_string_in_array(const char *needle, const char **haystack);
28 __hidden extern char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix);
29 /*
30 * Normalize and split path: Leading and trailing / are removed, multiple
31 * / are compactified, .. and . are resolved (.. on the top level is considered
32 * identical to .).
33 * Examples:
34 * / -> { NULL }
35 * foo/../bar -> { bar, NULL }
36 * ../../ -> { NULL }
37 * ./bar/baz/.. -> { bar, NULL }
38 * foo//bar -> { foo, bar, NULL }
39 */
40 __hidden extern char **lxc_normalize_path(const char *path);
41
42 /* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
43 __hidden extern char *lxc_deslashify(const char *path);
44 __hidden extern char *lxc_append_paths(const char *first, const char *second);
45
46 /*
47 * Note: the following two functions use strtok(), so they will never
48 * consider an empty element, even if two delimiters are next to
49 * each other.
50 */
51 __hidden extern bool lxc_string_in_list(const char *needle, const char *haystack, char sep);
52 __hidden extern char **lxc_string_split(const char *string, char sep);
53 __hidden extern char **lxc_string_split_and_trim(const char *string, char sep);
54 __hidden extern char **lxc_string_split_quoted(char *string);
55
56 /* Append string to NULL-terminated string array. */
57 __hidden extern int lxc_append_string(char ***list, char *entry);
58
59 /* Some simple array manipulation utilities */
60 typedef void (*lxc_free_fn)(void *);
61 typedef void *(*lxc_dup_fn)(void *);
62 __hidden extern int lxc_grow_array(void ***array, size_t *capacity, size_t new_size,
63 size_t capacity_increment);
64 __hidden extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
65 __hidden extern size_t lxc_array_len(void **array);
66
67 __hidden extern void **lxc_append_null_to_array(void **array, size_t count);
68 __hidden extern void remove_trailing_newlines(char *l);
69
70 /* Helper functions to parse numbers. */
71 __hidden extern int lxc_safe_uint(const char *numstr, unsigned int *converted);
72 __hidden extern int lxc_safe_int(const char *numstr, int *converted);
73 __hidden extern int lxc_safe_long(const char *numstr, long int *converted);
74 __hidden extern int lxc_safe_long_long(const char *numstr, long long int *converted);
75 __hidden extern int lxc_safe_ulong(const char *numstr, unsigned long *converted);
76 __hidden extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base);
77 __hidden extern int lxc_safe_int64_residual(const char *numstr, int64_t *converted, int base,
78 char *residual, size_t residual_len);
79 /* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
80 __hidden extern int parse_byte_size_string(const char *s, int64_t *converted);
81
82 /*
83 * Concatenate all passed-in strings into one path. Do not fail. If any piece
84 * is not prefixed with '/', add a '/'.
85 */
86 __hidden __attribute__((sentinel)) extern char *must_concat(size_t *len, const char *first, ...);
87 __hidden __attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
88 __hidden __attribute__((sentinel)) extern char *must_append_path(char *first, ...);
89
90 #define must_make_path_relative(__first__, ...) \
91 ({ \
92 char *__ptr__; \
93 if (*__first__ == '/') \
94 __ptr__ = must_make_path(".", __first__, __VA_ARGS__); \
95 else \
96 __ptr__ = must_make_path(__first__, __VA_ARGS__); \
97 __ptr__; \
98 })
99
100 /* Return copy of string @entry. Do not fail. */
101 __hidden extern char *must_copy_string(const char *entry);
102
103 /* Re-allocate a pointer, do not fail */
104 __hidden extern void *must_realloc(void *orig, size_t sz);
105
106 __hidden extern int lxc_char_left_gc(const char *buffer, size_t len);
107
108 __hidden extern int lxc_char_right_gc(const char *buffer, size_t len);
109
110 __hidden extern char *lxc_trim_whitespace_in_place(char *buffer);
111
112 __hidden extern int lxc_is_line_empty(const char *line);
113 __hidden extern void remove_trailing_slashes(char *p);
114
115 static inline bool is_empty_string(const char *s)
116 {
117 return !s || strcmp(s, "") == 0;
118 }
119
120 #define maybe_empty(s) ((!is_empty_string(s)) ? (s) : ("(null)"))
121
122 static inline ssize_t safe_strlcat(char *src, const char *append, size_t len)
123 {
124 size_t new_len;
125
126 new_len = strlcat(src, append, len);
127 if (new_len >= len)
128 return ret_errno(EINVAL);
129
130 return (ssize_t)new_len;
131 }
132
133 static inline bool strnequal(const char *str, const char *eq, size_t len)
134 {
135 return strncmp(str, eq, len) == 0;
136 }
137
138 static inline bool strequal(const char *str, const char *eq)
139 {
140 return strcmp(str, eq) == 0;
141 }
142
143 #endif /* __LXC_STRING_UTILS_H */