summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-02-20 11:45:07 +0100
committerLennart Poettering <lennart@poettering.net>2023-02-20 16:49:45 +0100
commitebb93f3ccfbab668c4e3f77041d97162fadb742b (patch)
tree78d0506ae45a59350b36d7775d2d8bd1d6862fad /src/basic
parentc52c4d6974e3bc6d8ffdd3b73207617e06cd5157 (diff)
downloadsystemd-ebb93f3ccfbab668c4e3f77041d97162fadb742b.tar.gz
cap-list: rework capability_set_to_string()
Let's use strextend_with_separator() and CAPABILITY_TO_STRING().
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/cap-list.c37
1 files changed, 15 insertions, 22 deletions
diff --git a/src/basic/cap-list.c b/src/basic/cap-list.c
index 7843efc994..3b506ed596 100644
--- a/src/basic/cap-list.c
+++ b/src/basic/cap-list.c
@@ -10,6 +10,7 @@
#include "macro.h"
#include "parse-util.h"
#include "stdio-util.h"
+#include "string-util.h"
static const struct capability_name* lookup_capability(register const char *str, register GPERF_LEN_TYPE len);
@@ -73,35 +74,27 @@ int capability_list_length(void) {
int capability_set_to_string(uint64_t set, char **ret) {
_cleanup_free_ char *str = NULL;
- size_t n = 0;
assert(ret);
- for (unsigned i = 0; i <= cap_last_cap(); i++)
- if (set & (UINT64_C(1) << i)) {
- const char *p;
- char buf[2 + 16 + 1];
- size_t add;
+ for (unsigned i = 0; i <= cap_last_cap(); i++) {
+ const char *p;
- p = capability_to_name(i);
- if (!p) {
- xsprintf(buf, "0x%x", i);
- p = buf;
- }
+ if (!FLAGS_SET(set, UINT64_C(1) << i))
+ continue;
- add = strlen(p);
+ p = CAPABILITY_TO_STRING(i);
+ assert(p);
- if (!GREEDY_REALLOC(str, n + add + 2))
- return -ENOMEM;
-
- strcpy(mempcpy(str + n, p, add), " ");
- n += add + 1;
- }
-
- if (!GREEDY_REALLOC(str, n + 1))
- return -ENOMEM;
+ if (!strextend_with_separator(&str, " ", p))
+ return -ENOMEM;
+ }
- str[n > 0 ? n - 1 : 0] = '\0'; /* truncate the last space, if it's there */
+ if (!str) {
+ str = new0(char, 1);
+ if (!str)
+ return -ENOMEM;
+ }
*ret = TAKE_PTR(str);
return 0;