From: Lennart Poettering Date: Wed, 17 Feb 2021 16:29:43 +0000 (+0100) Subject: parse-util: add format string macro for outputting permyriad X-Git-Tag: v248-rc1~46^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ead0b2a7973782f4bacaab2d9deed94ac69d867;p=thirdparty%2Fsystemd.git parse-util: add format string macro for outputting permyriad Let's define a set of macros for making output of permyriad values easy. They are printed in pure ASCII, i.e. without the permille/permyriad suffix, using just percent and two places after the dot. --- diff --git a/src/basic/percent-util.h b/src/basic/percent-util.h index d806ec007a7..24f4c3bdeff 100644 --- a/src/basic/percent-util.h +++ b/src/basic/percent-util.h @@ -59,3 +59,6 @@ static inline int UINT32_SCALE_TO_PERMYRIAD(uint32_t scale) { return (int) u; } + +#define PERMYRIAD_AS_PERCENT_FORMAT_STR "%i.%02i%%" +#define PERMYRIAD_AS_PERCENT_FORMAT_VAL(x) ((x)/100), ((x)%100) diff --git a/src/core/cgroup.c b/src/core/cgroup.c index b0fc67a1255..644bf50e7d8 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -21,6 +21,7 @@ #include "nulstr-util.h" #include "parse-util.h" #include "path-util.h" +#include "percent-util.h" #include "process-util.h" #include "procfs-util.h" #include "special.h" diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index e22233d6ff9..5bf9d7f4a84 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -706,9 +706,9 @@ static int bus_cgroup_set_boolean( assert_se(endswith(name, "Scale")); \ \ int scaled = UINT32_SCALE_TO_PERMYRIAD(raw); \ - unit_write_settingf(u, flags, name, "%.*s=%i.%02i%%", \ + unit_write_settingf(u, flags, name, "%.*s=" PERMYRIAD_AS_PERCENT_FORMAT_STR, \ (int)(strlen(name) - strlen("Scale")), name, \ - scaled / 100, scaled % 100); \ + PERMYRIAD_AS_PERCENT_FORMAT_VAL(scaled)); \ } \ \ return 1; \