3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
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.
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.
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.
23 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
37 #include <sys/mount.h>
38 #include <sys/param.h>
39 #include <sys/prctl.h>
41 #include <sys/types.h>
49 #include "namespace.h"
51 #include "string_utils.h"
54 #include "include/strlcpy.h"
58 #include "include/strlcat.h"
61 lxc_log_define(string_utils
, lxc
);
63 char **lxc_va_arg_list_to_argv(va_list ap
, size_t skip
, int do_strdup
)
66 size_t count
= 1 + skip
;
69 /* first determine size of argument list, we don't want to reallocate
74 char *arg
= va_arg(ap2
, char *);
81 result
= calloc(count
, sizeof(char *));
87 char *arg
= va_arg(ap
, char *);
90 arg
= do_strdup
? strdup(arg
) : arg
;
93 result
[count
++] = arg
;
96 /* calloc has already set last element to NULL*/
104 const char **lxc_va_arg_list_to_argv_const(va_list ap
, size_t skip
)
106 return (const char **)lxc_va_arg_list_to_argv(ap
, skip
, 0);
109 char *lxc_string_replace(const char *needle
, const char *replacement
,
110 const char *haystack
)
112 ssize_t len
= -1, saved_len
= -1;
114 size_t replacement_len
= strlen(replacement
);
115 size_t needle_len
= strlen(needle
);
117 /* should be executed exactly twice */
118 while (len
== -1 || result
== NULL
) {
124 result
= calloc(1, len
+ 1);
133 for (last_p
= (char *)haystack
, p
= strstr(last_p
, needle
); p
;
134 last_p
= p
, p
= strstr(last_p
, needle
)) {
135 part_len
= (ssize_t
)(p
- last_p
);
136 if (result
&& part_len
> 0)
137 memcpy(&result
[len
], last_p
, part_len
);
141 if (result
&& replacement_len
> 0)
142 memcpy(&result
[len
], replacement
,
145 len
+= replacement_len
;
149 part_len
= strlen(last_p
);
150 if (result
&& part_len
> 0)
151 memcpy(&result
[len
], last_p
, part_len
);
156 /* make sure we did the same thing twice,
157 * once for calculating length, the other
158 * time for copying data */
159 if (saved_len
!= len
) {
164 /* make sure we didn't overwrite any buffer,
165 * due to calloc the string should be 0-terminated */
166 if (result
[len
] != '\0') {
174 bool lxc_string_in_array(const char *needle
, const char **haystack
)
176 for (; haystack
&& *haystack
; haystack
++)
177 if (!strcmp(needle
, *haystack
))
183 char *lxc_string_join(const char *sep
, const char **parts
, bool use_as_prefix
)
187 size_t sep_len
= strlen(sep
);
188 size_t result_len
= use_as_prefix
* sep_len
;
191 /* calculate new string length */
192 for (p
= (char **)parts
; *p
; p
++)
193 result_len
+= (p
> (char **)parts
) * sep_len
+ strlen(*p
);
195 buf_len
= result_len
+ 1;
196 result
= calloc(buf_len
, 1);
201 (void)strlcpy(result
, sep
, buf_len
);
203 for (p
= (char **)parts
; *p
; p
++) {
204 if (p
> (char **)parts
)
205 (void)strlcat(result
, sep
, buf_len
);
207 (void)strlcat(result
, *p
, buf_len
);
213 char **lxc_normalize_path(const char *path
)
217 size_t components_len
= 0;
220 components
= lxc_string_split(path
, '/');
224 for (p
= components
; *p
; p
++)
227 /* resolve '.' and '..' */
228 for (pos
= 0; pos
< components_len
;) {
229 if (!strcmp(components
[pos
], ".") ||
230 (!strcmp(components
[pos
], "..") && pos
== 0)) {
231 /* eat this element */
232 free(components
[pos
]);
233 memmove(&components
[pos
], &components
[pos
+ 1],
234 sizeof(char *) * (components_len
- pos
));
236 } else if (!strcmp(components
[pos
], "..")) {
237 /* eat this and the previous element */
238 free(components
[pos
- 1]);
239 free(components
[pos
]);
240 memmove(&components
[pos
- 1], &components
[pos
+ 1],
241 sizeof(char *) * (components_len
- pos
));
252 char *lxc_deslashify(const char *path
)
262 parts
= lxc_normalize_path(dup
);
268 /* We'll end up here if path == "///" or path == "". */
272 lxc_free_array((void **)parts
, free
);
276 n
= strcspn(dup
, "/");
279 lxc_free_array((void **)parts
, free
);
289 p
= lxc_string_join("/", (const char **)parts
, *dup
== '/');
291 lxc_free_array((void **)parts
, free
);
295 char *lxc_append_paths(const char *first
, const char *second
)
300 const char *pattern
= "%s%s";
302 len
= strlen(first
) + strlen(second
) + 1;
303 if (second
[0] != '/') {
308 result
= calloc(1, len
);
312 ret
= snprintf(result
, len
, pattern
, first
, second
);
313 if (ret
< 0 || (size_t)ret
>= len
) {
321 bool lxc_string_in_list(const char *needle
, const char *haystack
, char _sep
)
324 char sep
[2] = { _sep
, '\0' };
327 if (!haystack
|| !needle
)
330 len
= strlen(haystack
);
331 str
= alloca(len
+ 1);
332 (void)strlcpy(str
, haystack
, len
+ 1);
334 lxc_iterate_parts(token
, str
, sep
)
335 if (strcmp(needle
, token
) == 0)
341 char **lxc_string_split(const char *string
, char _sep
)
344 char sep
[2] = {_sep
, '\0'};
345 char **tmp
= NULL
, **result
= NULL
;
346 size_t result_capacity
= 0;
347 size_t result_count
= 0;
352 return calloc(1, sizeof(char *));
354 len
= strlen(string
);
355 str
= alloca(len
+ 1);
356 (void)strlcpy(str
, string
, len
+ 1);
358 lxc_iterate_parts(token
, str
, sep
) {
359 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
363 result
[result_count
] = strdup(token
);
364 if (!result
[result_count
])
370 /* if we allocated too much, reduce it */
371 tmp
= realloc(result
, (result_count
+ 1) * sizeof(char *));
377 /* Make sure we don't return uninitialized memory. */
378 if (result_count
== 0)
385 lxc_free_array((void **)result
, free
);
390 static bool complete_word(char ***result
, char *start
, char *end
, size_t *cap
,
395 r
= lxc_grow_array((void ***)result
, cap
, 2 + *cnt
, 16);
399 (*result
)[*cnt
] = strndup(start
, end
- start
);
400 if (!(*result
)[*cnt
])
409 * Given a a string 'one two "three four"', split into three words,
410 * one, two, and "three four"
412 char **lxc_string_split_quoted(char *string
)
414 char *nextword
= string
, *p
, state
;
415 char **result
= NULL
;
416 size_t result_capacity
= 0;
417 size_t result_count
= 0;
419 if (!string
|| !*string
)
420 return calloc(1, sizeof(char *));
422 // TODO I'm *not* handling escaped quote
424 for (p
= string
; *p
; p
++) {
429 else if (*p
== '"' || *p
== '\'') {
439 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
447 complete_word(&result
, nextword
+1, p
, &result_capacity
, &result_count
);
456 complete_word(&result
, nextword
, p
, &result_capacity
, &result_count
);
458 return realloc(result
, (result_count
+ 1) * sizeof(char *));
461 char **lxc_string_split_and_trim(const char *string
, char _sep
)
464 char sep
[2] = { _sep
, '\0' };
465 char **result
= NULL
;
466 size_t result_capacity
= 0;
467 size_t result_count
= 0;
473 return calloc(1, sizeof(char *));
475 len
= strlen(string
);
476 str
= alloca(len
+ 1);
477 (void)strlcpy(str
, string
, len
+ 1);
479 lxc_iterate_parts(token
, str
, sep
) {
480 while (token
[0] == ' ' || token
[0] == '\t')
484 while (i
> 0 && (token
[i
- 1] == ' ' || token
[i
- 1] == '\t')) {
489 r
= lxc_grow_array((void ***)&result
, &result_capacity
, result_count
+ 1, 16);
493 result
[result_count
] = strdup(token
);
494 if (!result
[result_count
])
500 /* if we allocated too much, reduce it */
501 return realloc(result
, (result_count
+ 1) * sizeof(char *));
505 lxc_free_array((void **)result
, free
);
510 void lxc_free_array(void **array
, lxc_free_fn element_free_fn
)
514 for (p
= array
; p
&& *p
; p
++)
520 int lxc_grow_array(void ***array
, size_t *capacity
, size_t new_size
, size_t capacity_increment
)
525 /* first time around, catch some trivial mistakes of the user
526 * only initializing one of these */
527 if (!*array
|| !*capacity
) {
532 new_capacity
= *capacity
;
533 while (new_size
+ 1 > new_capacity
)
534 new_capacity
+= capacity_increment
;
536 if (new_capacity
!= *capacity
) {
537 /* we have to reallocate */
538 new_array
= realloc(*array
, new_capacity
* sizeof(void *));
542 memset(&new_array
[*capacity
], 0, (new_capacity
- (*capacity
)) * sizeof(void *));
544 *capacity
= new_capacity
;
547 /* array has sufficient elements */
551 size_t lxc_array_len(void **array
)
556 for (p
= array
; p
&& *p
; p
++)
562 void **lxc_append_null_to_array(void **array
, size_t count
)
566 /* Append NULL to the array */
568 temp
= realloc(array
, (count
+ 1) * sizeof(*array
));
571 for (i
= 0; i
< count
; i
++)
584 static int lxc_append_null_to_list(void ***list
)
590 for (; (*list
)[newentry
]; newentry
++) {
594 tmp
= realloc(*list
, (newentry
+ 2) * sizeof(void **));
599 (*list
)[newentry
+ 1] = NULL
;
604 int lxc_append_string(char ***list
, char *entry
)
609 newentry
= lxc_append_null_to_list((void ***)list
);
613 copy
= strdup(entry
);
617 (*list
)[newentry
] = copy
;
622 int lxc_safe_uint(const char *numstr
, unsigned int *converted
)
625 unsigned long int uli
;
627 while (isspace(*numstr
))
634 uli
= strtoul(numstr
, &err
, 0);
635 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
638 if (err
== numstr
|| *err
!= '\0')
644 *converted
= (unsigned int)uli
;
648 int lxc_safe_ulong(const char *numstr
, unsigned long *converted
)
651 unsigned long int uli
;
653 while (isspace(*numstr
))
660 uli
= strtoul(numstr
, &err
, 0);
661 if (errno
== ERANGE
&& uli
== ULONG_MAX
)
664 if (err
== numstr
|| *err
!= '\0')
671 int lxc_safe_uint64(const char *numstr
, uint64_t *converted
, int base
)
676 while (isspace(*numstr
))
683 u
= strtoull(numstr
, &err
, base
);
684 if (errno
== ERANGE
&& u
== UINT64_MAX
)
687 if (err
== numstr
|| *err
!= '\0')
694 int lxc_safe_int(const char *numstr
, int *converted
)
700 sli
= strtol(numstr
, &err
, 0);
701 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
704 if (errno
!= 0 && sli
== 0)
707 if (err
== numstr
|| *err
!= '\0')
710 if (sli
> INT_MAX
|| sli
< INT_MIN
)
713 *converted
= (int)sli
;
717 int lxc_safe_long(const char *numstr
, long int *converted
)
723 sli
= strtol(numstr
, &err
, 0);
724 if (errno
== ERANGE
&& (sli
== LONG_MAX
|| sli
== LONG_MIN
))
727 if (errno
!= 0 && sli
== 0)
730 if (err
== numstr
|| *err
!= '\0')
737 int lxc_safe_long_long(const char *numstr
, long long int *converted
)
740 signed long long int sli
;
743 sli
= strtoll(numstr
, &err
, 0);
744 if (errno
== ERANGE
&& (sli
== LLONG_MAX
|| sli
== LLONG_MIN
))
747 if (errno
!= 0 && sli
== 0)
750 if (err
== numstr
|| *err
!= '\0')
757 char *must_concat(const char *first
, ...)
761 size_t cur_len
, it_len
;
763 dest
= must_copy_string(first
);
764 cur_len
= it_len
= strlen(first
);
766 va_start(args
, first
);
767 while ((cur
= va_arg(args
, char *)) != NULL
) {
768 it_len
= strlen(cur
);
770 dest
= must_realloc(dest
, cur_len
+ it_len
+ 1);
772 (void)memcpy(dest
+ cur_len
, cur
, it_len
);
781 char *must_make_path(const char *first
, ...)
785 size_t full_len
= strlen(first
);
788 dest
= must_copy_string(first
);
790 va_start(args
, first
);
791 while ((cur
= va_arg(args
, char *)) != NULL
) {
792 full_len
+= strlen(cur
);
796 buf_len
= full_len
+ 1;
797 dest
= must_realloc(dest
, buf_len
);
800 (void)strlcat(dest
, "/", buf_len
);
801 (void)strlcat(dest
, cur
, buf_len
);
808 char *must_append_path(char *first
, ...)
816 full_len
= strlen(first
);
817 va_start(args
, first
);
818 while ((cur
= va_arg(args
, char *)) != NULL
) {
819 full_len
+= strlen(cur
);
823 buf_len
= full_len
+ 1;
824 dest
= must_realloc(dest
, buf_len
);
827 (void)strlcat(dest
, "/", buf_len
);
828 (void)strlcat(dest
, cur
, buf_len
);
835 char *must_copy_string(const char *entry
)
849 void *must_realloc(void *orig
, size_t sz
)
854 ret
= realloc(orig
, sz
);
860 int parse_byte_size_string(const char *s
, int64_t *converted
)
864 int64_t mltpl
, overflow
;
866 char dup
[INTTYPE_TO_STRLEN(int64_t)];
867 char suffix
[3] = {0};
869 if (!s
|| !strcmp(s
, ""))
872 end
= stpncpy(dup
, s
, sizeof(dup
) - 1);
876 if (isdigit(*(end
- 1)))
878 else if (isalpha(*(end
- 1)))
883 if (suffix_len
> 0 && (end
- 2) == dup
&& !isdigit(*(end
- 2)))
886 if (suffix_len
> 0 && isalpha(*(end
- 2)))
889 if (suffix_len
> 0) {
890 memcpy(suffix
, end
- suffix_len
, suffix_len
);
891 *(suffix
+ suffix_len
) = '\0';
892 *(end
- suffix_len
) = '\0';
894 dup
[lxc_char_right_gc(dup
, strlen(dup
))] = '\0';
896 ret
= lxc_safe_long_long(dup
, &conv
);
900 if (suffix_len
!= 2) {
905 if (strcasecmp(suffix
, "KB") == 0)
907 else if (strcasecmp(suffix
, "MB") == 0)
909 else if (strcasecmp(suffix
, "GB") == 0)
910 mltpl
= 1024 * 1024 * 1024;
914 overflow
= conv
* mltpl
;
915 if (conv
!= 0 && (overflow
/ conv
) != mltpl
)
918 *converted
= overflow
;
922 void remove_trailing_newlines(char *l
)
929 while (--p
>= l
&& *p
== '\n')
933 int lxc_char_left_gc(const char *buffer
, size_t len
)
937 for (i
= 0; i
< len
; i
++) {
938 if (buffer
[i
] == ' ' ||
948 int lxc_char_right_gc(const char *buffer
, size_t len
)
952 for (i
= len
- 1; i
>= 0; i
--) {
953 if (buffer
[i
] == ' ' ||
965 char *lxc_trim_whitespace_in_place(char *buffer
)
967 buffer
+= lxc_char_left_gc(buffer
, strlen(buffer
));
968 buffer
[lxc_char_right_gc(buffer
, strlen(buffer
))] = '\0';
972 int lxc_is_line_empty(const char *line
)
975 size_t len
= strlen(line
);
977 for (i
= 0; i
< len
; i
++)
978 if (line
[i
] != ' ' && line
[i
] != '\t' &&
979 line
[i
] != '\n' && line
[i
] != '\r' &&
980 line
[i
] != '\f' && line
[i
] != '\0')
985 void remove_trailing_slashes(char *p
)
988 while (--l
>= 0 && (p
[l
] == '/' || p
[l
] == '\n'))