]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/string_utils.h
tree-wide: harden mount option parsing
[mirror_lxc.git] / src / lxc / string_utils.h
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
37ef15bb
CB
2
3#ifndef __LXC_STRING_UTILS_H
4#define __LXC_STRING_UTILS_H
5
b7df06ad
FF
6#include <stdarg.h>
7
37ef15bb
CB
8#include "config.h"
9
37ef15bb
CB
10#include "initutils.h"
11#include "macro.h"
12
a08bfbe3
CB
13#ifndef HAVE_STRLCAT
14#include "include/strlcat.h"
15#endif
16
37ef15bb
CB
17/* convert variadic argument lists to arrays (for execl type argument lists) */
18extern char **lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
19extern 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 */
25extern char *lxc_string_replace(const char *needle, const char *replacement,
26 const char *haystack);
27extern bool lxc_string_in_array(const char *needle, const char **haystack);
28extern char *lxc_string_join(const char *sep, const char **parts,
29 bool use_as_prefix);
30/*
31 * Normalize and split path: Leading and trailing / are removed, multiple
32 * / are compactified, .. and . are resolved (.. on the top level is considered
33 * identical to .).
34 * Examples:
35 * / -> { NULL }
36 * foo/../bar -> { bar, NULL }
37 * ../../ -> { NULL }
38 * ./bar/baz/.. -> { bar, NULL }
39 * foo//bar -> { foo, bar, NULL }
40 */
41extern char **lxc_normalize_path(const char *path);
42
43/* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
44extern char *lxc_deslashify(const char *path);
45extern char *lxc_append_paths(const char *first, const char *second);
46
47/*
48 * Note: the following two functions use strtok(), so they will never
49 * consider an empty element, even if two delimiters are next to
50 * each other.
51 */
52extern bool lxc_string_in_list(const char *needle, const char *haystack,
53 char sep);
54extern char **lxc_string_split(const char *string, char sep);
55extern char **lxc_string_split_and_trim(const char *string, char sep);
56extern char **lxc_string_split_quoted(char *string);
57
58/* Append string to NULL-terminated string array. */
59extern int lxc_append_string(char ***list, char *entry);
60
61/* Some simple array manipulation utilities */
62typedef void (*lxc_free_fn)(void *);
63typedef void *(*lxc_dup_fn)(void *);
64extern int lxc_grow_array(void ***array, size_t *capacity, size_t new_size,
65 size_t capacity_increment);
66extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
67extern size_t lxc_array_len(void **array);
68
69extern void **lxc_append_null_to_array(void **array, size_t count);
70extern void remove_trailing_newlines(char *l);
71
72/* Helper functions to parse numbers. */
73extern int lxc_safe_uint(const char *numstr, unsigned int *converted);
74extern int lxc_safe_int(const char *numstr, int *converted);
75extern int lxc_safe_long(const char *numstr, long int *converted);
76extern int lxc_safe_long_long(const char *numstr, long long int *converted);
77extern int lxc_safe_ulong(const char *numstr, unsigned long *converted);
78extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base);
79/* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
80extern 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 */
fe70edee 86__attribute__((sentinel)) extern char *must_concat(size_t *len, const char *first, ...);
37ef15bb
CB
87__attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
88__attribute__((sentinel)) extern char *must_append_path(char *first, ...);
89
90/* Return copy of string @entry. Do not fail. */
91extern char *must_copy_string(const char *entry);
92
54d423b8 93/* Re-allocate a pointer, do not fail */
37ef15bb
CB
94extern void *must_realloc(void *orig, size_t sz);
95
96extern int lxc_char_left_gc(const char *buffer, size_t len);
97
98extern int lxc_char_right_gc(const char *buffer, size_t len);
99
100extern char *lxc_trim_whitespace_in_place(char *buffer);
101
102extern int lxc_is_line_empty(const char *line);
dca12ddf 103extern void remove_trailing_slashes(char *p);
37ef15bb 104
3473ca76
CB
105static inline bool is_empty_string(const char *s)
106{
107 return !s || strcmp(s, "") == 0;
108}
109
a08bfbe3
CB
110static inline ssize_t safe_strlcat(char *src, const char *append, size_t len)
111{
112 size_t new_len;
113
114 new_len = strlcat(src, append, len);
115 if (new_len >= len)
116 return ret_errno(EINVAL);
117
118 return (ssize_t)new_len;
119}
120
37ef15bb 121#endif /* __LXC_STRING_UTILS_H */