]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/replace-var.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2012 Lennart Poettering
11 #include "alloc-util.h"
13 #include "replace-var.h"
14 #include "string-util.h"
17 * Generic infrastructure for replacing @FOO@ style variables in
18 * strings. Will call a callback for each replacement.
21 static int get_variable(const char *b
, char **r
) {
31 k
= strspn(b
+ 1, UPPERCASE_LETTERS
"_");
32 if (k
<= 0 || b
[k
+1] != '@')
35 t
= strndup(b
+ 1, k
);
43 char *replace_var(const char *text
, char *(*lookup
)(const char *variable
, void *userdata
), void *userdata
) {
59 _cleanup_free_
char *v
= NULL
, *n
= NULL
;
64 k
= get_variable(f
, &v
);
72 n
= lookup(v
, userdata
);
79 nl
= l
- skip
+ strlen(n
);
80 a
= realloc(r
, nl
+ 1);