3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
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.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
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
21 #ifndef __LXC_STRING_UTILS_H
22 #define __LXC_STRING_UTILS_H
28 #include "initutils.h"
31 /* convert variadic argument lists to arrays (for execl type argument lists) */
32 extern char **lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
);
33 extern const char **lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
);
36 * Some simple string functions; if they return pointers, they are allocated
39 extern char *lxc_string_replace(const char *needle
, const char *replacement
,
40 const char *haystack
);
41 extern bool lxc_string_in_array(const char *needle
, const char **haystack
);
42 extern char *lxc_string_join(const char *sep
, const char **parts
,
45 * Normalize and split path: Leading and trailing / are removed, multiple
46 * / are compactified, .. and . are resolved (.. on the top level is considered
50 * foo/../bar -> { bar, NULL }
52 * ./bar/baz/.. -> { bar, NULL }
53 * foo//bar -> { foo, bar, NULL }
55 extern char **lxc_normalize_path(const char *path
);
57 /* remove multiple slashes from the path, e.g. ///foo//bar -> /foo/bar */
58 extern char *lxc_deslashify(const char *path
);
59 extern char *lxc_append_paths(const char *first
, const char *second
);
62 * Note: the following two functions use strtok(), so they will never
63 * consider an empty element, even if two delimiters are next to
66 extern bool lxc_string_in_list(const char *needle
, const char *haystack
,
68 extern char **lxc_string_split(const char *string
, char sep
);
69 extern char **lxc_string_split_and_trim(const char *string
, char sep
);
70 extern char **lxc_string_split_quoted(char *string
);
72 /* Append string to NULL-terminated string array. */
73 extern int lxc_append_string(char ***list
, char *entry
);
75 /* Some simple array manipulation utilities */
76 typedef void (*lxc_free_fn
)(void *);
77 typedef void *(*lxc_dup_fn
)(void *);
78 extern int lxc_grow_array(void ***array
, size_t *capacity
, size_t new_size
,
79 size_t capacity_increment
);
80 extern void lxc_free_array(void **array
, lxc_free_fn element_free_fn
);
81 extern size_t lxc_array_len(void **array
);
83 extern void **lxc_append_null_to_array(void **array
, size_t count
);
84 extern void remove_trailing_newlines(char *l
);
86 /* Helper functions to parse numbers. */
87 extern int lxc_safe_uint(const char *numstr
, unsigned int *converted
);
88 extern int lxc_safe_int(const char *numstr
, int *converted
);
89 extern int lxc_safe_long(const char *numstr
, long int *converted
);
90 extern int lxc_safe_long_long(const char *numstr
, long long int *converted
);
91 extern int lxc_safe_ulong(const char *numstr
, unsigned long *converted
);
92 extern int lxc_safe_uint64(const char *numstr
, uint64_t *converted
, int base
);
93 /* Handles B, kb, MB, GB. Detects overflows and reports -ERANGE. */
94 extern int parse_byte_size_string(const char *s
, int64_t *converted
);
97 * Concatenate all passed-in strings into one path. Do not fail. If any piece
98 * is not prefixed with '/', add a '/'.
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
, ...);
104 /* Return copy of string @entry. Do not fail. */
105 extern char *must_copy_string(const char *entry
);
107 /* Re-allocate a pointer, do not fail */
108 extern void *must_realloc(void *orig
, size_t sz
);
110 extern int lxc_char_left_gc(const char *buffer
, size_t len
);
112 extern int lxc_char_right_gc(const char *buffer
, size_t len
);
114 extern char *lxc_trim_whitespace_in_place(char *buffer
);
116 extern int lxc_is_line_empty(const char *line
);
117 extern void remove_trailing_slashes(char *p
);
119 #endif /* __LXC_STRING_UTILS_H */