]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/string_utils.h
Merge pull request #3059 from brauner/2019-06-21/seccomp_notify
[mirror_lxc.git] / src / lxc / string_utils.h
CommitLineData
37ef15bb
CB
1/* liblxcapi
2 *
3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
5 *
84fa5aca
CB
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10
11 * This library is distributed in the hope that it will be useful,
37ef15bb 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
84fa5aca
CB
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37ef15bb
CB
19 */
20
21#ifndef __LXC_STRING_UTILS_H
22#define __LXC_STRING_UTILS_H
23
b7df06ad
FF
24#include <stdarg.h>
25
37ef15bb
CB
26#include "config.h"
27
37ef15bb
CB
28#include "initutils.h"
29#include "macro.h"
30
31/* convert variadic argument lists to arrays (for execl type argument lists) */
32extern char **lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup);
33extern const char **lxc_va_arg_list_to_argv_const(va_list ap, size_t skip);
34
35/*
36 * Some simple string functions; if they return pointers, they are allocated
37 * buffers.
38 */
39extern char *lxc_string_replace(const char *needle, const char *replacement,
40 const char *haystack);
41extern bool lxc_string_in_array(const char *needle, const char **haystack);
42extern char *lxc_string_join(const char *sep, const char **parts,
43 bool use_as_prefix);
44/*
45 * Normalize and split path: Leading and trailing / are removed, multiple
46 * / are compactified, .. and . are resolved (.. on the top level is considered
47 * identical to .).
48 * Examples:
49 * / -> { NULL }
50 * foo/../bar -> { bar, NULL }
51 * ../../ -> { NULL }
52 * ./bar/baz/.. -> { bar, NULL }
53 * foo//bar -> { foo, bar, NULL }
54 */
55extern char **lxc_normalize_path(const char *path);
56
57/* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
58extern char *lxc_deslashify(const char *path);
59extern char *lxc_append_paths(const char *first, const char *second);
60
61/*
62 * Note: the following two functions use strtok(), so they will never
63 * consider an empty element, even if two delimiters are next to
64 * each other.
65 */
66extern bool lxc_string_in_list(const char *needle, const char *haystack,
67 char sep);
68extern char **lxc_string_split(const char *string, char sep);
69extern char **lxc_string_split_and_trim(const char *string, char sep);
70extern char **lxc_string_split_quoted(char *string);
71
72/* Append string to NULL-terminated string array. */
73extern int lxc_append_string(char ***list, char *entry);
74
75/* Some simple array manipulation utilities */
76typedef void (*lxc_free_fn)(void *);
77typedef void *(*lxc_dup_fn)(void *);
78extern int lxc_grow_array(void ***array, size_t *capacity, size_t new_size,
79 size_t capacity_increment);
80extern void lxc_free_array(void **array, lxc_free_fn element_free_fn);
81extern size_t lxc_array_len(void **array);
82
83extern void **lxc_append_null_to_array(void **array, size_t count);
84extern void remove_trailing_newlines(char *l);
85
86/* Helper functions to parse numbers. */
87extern int lxc_safe_uint(const char *numstr, unsigned int *converted);
88extern int lxc_safe_int(const char *numstr, int *converted);
89extern int lxc_safe_long(const char *numstr, long int *converted);
90extern int lxc_safe_long_long(const char *numstr, long long int *converted);
91extern int lxc_safe_ulong(const char *numstr, unsigned long *converted);
92extern int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base);
93/* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
94extern int parse_byte_size_string(const char *s, int64_t *converted);
95
96/*
97 * Concatenate all passed-in strings into one path. Do not fail. If any piece
98 * is not prefixed with '/', add a '/'.
99 */
100__attribute__((sentinel)) extern char *must_concat(const char *first, ...);
101__attribute__((sentinel)) extern char *must_make_path(const char *first, ...);
102__attribute__((sentinel)) extern char *must_append_path(char *first, ...);
103
104/* Return copy of string @entry. Do not fail. */
105extern char *must_copy_string(const char *entry);
106
54d423b8 107/* Re-allocate a pointer, do not fail */
37ef15bb
CB
108extern void *must_realloc(void *orig, size_t sz);
109
110extern int lxc_char_left_gc(const char *buffer, size_t len);
111
112extern int lxc_char_right_gc(const char *buffer, size_t len);
113
114extern char *lxc_trim_whitespace_in_place(char *buffer);
115
116extern int lxc_is_line_empty(const char *line);
dca12ddf 117extern void remove_trailing_slashes(char *p);
37ef15bb
CB
118
119#endif /* __LXC_STRING_UTILS_H */