Commit d80bbcb0 authored by Peter Hutterer's avatar Peter Hutterer

tools: record: drop quotes from os-release information

Signed-off-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
parent 4d92ea11
Pipeline #122496 passed with stages
in 28 minutes and 45 seconds
......@@ -312,3 +312,29 @@ error:
free(result);
return -1;
}
/**
* Strip any of the characters in what from the beginning and end of the
* input string.
*
* @return a newly allocated string with none of "what" at the beginning or
* end of string
*/
static inline char *
strstrip(const char *input, const char *what)
{
char *str, *last;
str = safe_strdup(&input[strspn(input, what)]);
last = str;
for (char *c = str; *c != '\0'; c++) {
if (!strchr(what, *c))
last = c + 1;
}
*last = '\0';
return str;
}
......@@ -1033,6 +1033,44 @@ START_TEST(strjoin_test)
}
END_TEST
START_TEST(strstrip_test)
{
struct strstrip_test {
const char *string;
const char *expected;
const char *what;
} tests[] = {
{ "foo", "foo", "1234" },
{ "\"bar\"", "bar", "\"" },
{ "'bar'", "bar", "'" },
{ "\"bar\"", "\"bar\"", "'" },
{ "'bar'", "'bar'", "\"" },
{ "\"bar\"", "bar", "\"" },
{ "\"\"", "", "\"" },
{ "\"foo\"bar\"", "foo\"bar", "\"" },
{ "\"'foo\"bar\"", "foo\"bar", "\"'" },
{ "abcfooabcbarbca", "fooabcbar", "abc" },
{ "xxxxfoo", "foo", "x" },
{ "fooyyyy", "foo", "y" },
{ "xxxxfooyyyy", "foo", "xy" },
{ "x xfooy y", " xfooy ", "xy" },
{ " foo\n", "foo", " \n" },
{ "", "", "abc" },
{ "", "", "" },
{ NULL , NULL, NULL }
};
struct strstrip_test *t = tests;
while (t->string) {
char *str;
str = strstrip(t->string, t->what);
ck_assert_str_eq(str, t->expected);
free(str);
t++;
}
}
END_TEST
START_TEST(list_test_insert)
{
struct list_test {
......@@ -1138,6 +1176,7 @@ litest_utils_suite(void)
tcase_add_test(tc, strsplit_test);
tcase_add_test(tc, kvsplit_double_test);
tcase_add_test(tc, strjoin_test);
tcase_add_test(tc, strstrip_test);
tcase_add_test(tc, time_conversion);
tcase_add_test(tc, list_test_insert);
......
......@@ -1447,9 +1447,9 @@ print_system_header(struct record_context *ctx)
osrstr[strlen(osrstr) - 1] = '\0'; /* linebreak */
if (!distro && strneq(osrstr, "ID=", 3))
distro = safe_strdup(&osrstr[3]);
distro = strstrip(&osrstr[3], "\"'");
else if (!version && strneq(osrstr, "VERSION_ID=", 11))
version = safe_strdup(&osrstr[11]);
version = strstrip(&osrstr[11], "\"'");
if (distro && version) {
iprintf(ctx, "os: \"%s:%s\"\n", distro, version);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment