util: use extract_first_word() instead of strsep()

This commit is contained in:
Yu Watanabe
2019-06-20 06:51:34 +09:00
parent fe2e4b6961
commit 31a9be2372
2 changed files with 36 additions and 22 deletions
+21 -11
View File
@@ -91,10 +91,9 @@ int cg_read_event(
const char *controller,
const char *path,
const char *event,
char **val) {
char **ret) {
_cleanup_free_ char *events = NULL, *content = NULL;
char *p, *line;
int r;
r = cg_get_path(controller, path, "cgroup.events", &events);
@@ -105,22 +104,33 @@ int cg_read_event(
if (r < 0)
return r;
p = content;
while ((line = strsep(&p, "\n"))) {
char *key;
for (const char *p = content;;) {
_cleanup_free_ char *line = NULL, *key = NULL, *val = NULL;
const char *q;
key = strsep(&line, " ");
if (!key || !line)
r = extract_first_word(&p, &line, "\n", 0);
if (r < 0)
return r;
if (r == 0)
return -ENOENT;
q = line;
r = extract_first_word(&q, &key, " ", 0);
if (r < 0)
return r;
if (r == 0)
return -EINVAL;
if (strcmp(key, event))
if (!streq(key, event))
continue;
*val = strdup(line);
val = strdup(q);
if (!val)
return -ENOMEM;
*ret = TAKE_PTR(val);
return 0;
}
return -ENOENT;
}
bool cg_ns_supported(void) {