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