summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-03-01 09:30:55 +0100
committerSven Eden <yamakuzure@gmx.net>2018-05-30 07:59:10 +0200
commit6cd40c23b77d77ed06913007c305699fb2fea85c (patch)
treedfa279614e5ebcabda1d8523ec3397a3a23a833f /src
parentfb2e0a5e8629b4e29fa1cd8576525d2dd3510b40 (diff)
basic/cgroup-util: simplify cg_get_keyed_attribute(), add test
I didn't like the nested loop where we'd count what we have acquired already, since we should always know that.
Diffstat (limited to 'src')
-rw-r--r--src/basic/cgroup-util.c23
1 files changed, 6 insertions, 17 deletions
diff --git a/src/basic/cgroup-util.c b/src/basic/cgroup-util.c
index 9e5c540c6..452a8e2f7 100644
--- a/src/basic/cgroup-util.c
+++ b/src/basic/cgroup-util.c
@@ -2148,7 +2148,7 @@ int cg_get_keyed_attribute(
_cleanup_free_ char *filename = NULL, *contents = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *p;
- size_t n, i;
+ size_t n, i, n_done = 0;
char **v;
int r;
@@ -2175,42 +2175,31 @@ int cg_get_keyed_attribute(
for (p = contents; *p;) {
const char *w = NULL;
- size_t n_done = 0;
- for (i = 0; i < n; i++) {
- if (v[i])
- n_done ++;
- else {
+ for (i = 0; i < n; i++)
+ if (!v[i]) {
w = first_word(p, keys[i]);
if (w)
break;
}
- }
if (w) {
- char *c;
size_t l;
l = strcspn(w, NEWLINE);
- c = strndup(w, l);
- if (!c) {
+ v[i] = strndup(w, l);
+ if (!v[i]) {
r = -ENOMEM;
goto fail;
}
- v[i] = c;
n_done++;
-
if (n_done >= n)
goto done;
p = w + l;
- } else {
- if (n_done >= n)
- goto done;
-
+ } else
p += strcspn(p, NEWLINE);
- }
p += strspn(p, NEWLINE);
}