]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
parse-util: add format string macro for outputting permyriad
authorLennart Poettering <lennart@poettering.net>
Wed, 17 Feb 2021 16:29:43 +0000 (17:29 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 18 Feb 2021 21:36:34 +0000 (22:36 +0100)
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.

src/basic/percent-util.h
src/core/cgroup.c
src/core/dbus-cgroup.c

index d806ec007a76f3b25f6afee290ade217e6dc2ac3..24f4c3bdeff48ef885e233eae5a84d6f3aef4645 100644 (file)
@@ -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)
index b0fc67a1255736623b37d633fe116ab0cfecc4f8..644bf50e7d8d2672cd2243c5bc8f6937aadea581 100644 (file)
@@ -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"
index e22233d6ff91f0b1366ee178ef1a847c47b8deb6..5bf9d7f4a84d21001e80ac1eb5f18a54cfcb34ef 100644 (file)
@@ -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;                                               \