chiark / gitweb /
util: replace decimal_str_max() by a typesafe macro DECIMAL_STR_WIDTH()
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Apr 2013 03:04:53 +0000 (05:04 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 16 Apr 2013 03:04:53 +0000 (05:04 +0200)
DECIMAL_STR_WIDTH() now works on any numeric type, and is easier to
distingish from DECIMAL_STR_MAX().

This also replaces another manual implementaiton of ulog10 by this macro.

src/shared/cgroup-show.c
src/shared/util.h
src/systemctl/systemctl.c

index 9ee532ca22271c3dee9f5d0385668975b7f9d4a5..0e82375ea91f13dd4d0b8dd6e7ee7bc87276f9c2 100644 (file)
@@ -40,17 +40,6 @@ static int compare(const void *a, const void *b) {
         return 0;
 }
 
-static unsigned ilog10(unsigned long ul) {
-        int n = 0;
-
-        while (ul > 0) {
-                n++;
-                ul /= 10;
-        }
-
-        return n;
-}
-
 static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsigned n_columns, bool extra, bool more, bool kernel_threads, OutputFlags flags) {
         unsigned i, m, pid_width;
         pid_t biggest = 0;
@@ -71,7 +60,7 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi
                         pids[m++] = pids[i];
         }
         n_pids = m;
-        pid_width = ilog10(biggest);
+        pid_width = DECIMAL_STR_WIDTH(biggest);
 
         /* And sort */
         qsort(pids, n_pids, sizeof(pid_t), compare);
index b33fdb5b7a7f09ce090f16a3552e21a4be614104..683ff5a4fe03e4af1f8e6de4c2b672876b6c3511 100644 (file)
@@ -649,19 +649,21 @@ static inline bool logind_running(void) {
         return access("/run/systemd/seats/", F_OK) >= 0;
 }
 
-static inline unsigned decimal_str_max(unsigned x) {
-        unsigned ans = 1;
-        while (x /= 10)
-                ans ++;
-        return ans;
-}
+#define DECIMAL_STR_WIDTH(x)                            \
+        ({                                              \
+                typeof(x) _x_ = (x);                    \
+                unsigned ans = 1;                       \
+                while (_x_ /= 10)                       \
+                        ans++;                          \
+                ans;                                    \
+        })
 
 int unlink_noerrno(const char *path);
 
-#define alloca0(n)                                                      \
-        ({                                                              \
-                char *__new;                                            \
-                size_t __len = n;                                       \
-                __new = alloca(__len);                                  \
-                (void *) memset(__new, 0, __len);                       \
+#define alloca0(n)                                      \
+        ({                                              \
+                char *_new_;                            \
+                size_t _len_ = n;                       \
+                _new_ = alloca(_len_);                  \
+                (void *) memset(_new_, 0, _len_);       \
         })
index 36567224f886c15d4ac84f60a051303992675803..2f43052f9af029187bcc7c0996008147bca653e4 100644 (file)
@@ -1178,7 +1178,7 @@ static void list_jobs_print(struct job_info* jobs, size_t n) {
 
                 for (i = 0, j = jobs; i < n; i++, j++) {
                         assert(j->name && j->type && j->state);
-                        l0 = MAX(l0, decimal_str_max(j->id));
+                        l0 = MAX(l0, DECIMAL_STR_WIDTH(j->id));
                         l1 = MAX(l1, strlen(j->name));
                         l2 = MAX(l2, strlen(j->type));
                         l3 = MAX(l3, strlen(j->state));