specifier: add helper for escaping '%' characters to avoid making them subject for expansion

This is ultimately just a wrapper around strreplace(), but it makes
things a bit more self-descriptive.
This commit is contained in:
Lennart Poettering
2017-11-29 12:32:56 +01:00
parent 9d73565ac0
commit e82f30d17f
4 changed files with 108 additions and 0 deletions
+30
View File
@@ -32,6 +32,7 @@
#include "macro.h"
#include "specifier.h"
#include "string-util.h"
#include "strv.h"
/*
* Generic infrastructure for replacing %x style specifiers in
@@ -191,3 +192,32 @@ int specifier_kernel_release(char specifier, void *data, void *userdata, char **
*ret = n;
return 0;
}
int specifier_escape_strv(char **l, char ***ret) {
char **z, **p, **q;
assert(ret);
if (strv_isempty(l)) {
*ret = NULL;
return 0;
}
z = new(char*, strv_length(l)+1);
if (!z)
return -ENOMEM;
for (p = l, q = z; *p; p++, q++) {
*q = specifier_escape(*p);
if (!*q) {
strv_free(z);
return -ENOMEM;
}
}
*q = NULL;
*ret = z;
return 0;
}