[libvirt] [PATCH v2] examples: fix 64-bit integer formatting on Windows

Daniel P. Berrangé posted 1 patch 4 years, 12 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190402092457.30616-1-berrange@redhat.com
examples/admin/client_info.c  | 7 ++++---
examples/admin/list_clients.c | 3 ++-
examples/domtop/domtop.c      | 8 ++++++--
3 files changed, 12 insertions(+), 6 deletions(-)
[libvirt] [PATCH v2] examples: fix 64-bit integer formatting on Windows
Posted by Daniel P. Berrangé 4 years, 12 months ago
The Windows printf functions don't support %llu/%lld for printing 64-bit
integers. For most of libvirt this doesn't matter as we rely on gnulib
which provides a replacement printf that is sane.

The example code is designed to compile against the normal OS headers,
with no use of gnulib and thus has to use the platform specific printf.
To deal with this we must use the macros PRI* macros from inttypes.h
to get the platform specific format string.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 examples/admin/client_info.c  | 7 ++++---
 examples/admin/list_clients.c | 3 ++-
 examples/domtop/domtop.c      | 8 ++++++--
 3 files changed, 12 insertions(+), 6 deletions(-)

Changed in v2:

 - Now actually commit the int64 casts

diff --git a/examples/admin/client_info.c b/examples/admin/client_info.c
index f3f62a656b..7fc6c72bbd 100644
--- a/examples/admin/client_info.c
+++ b/examples/admin/client_info.c
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
+#include <inttypes.h>
 #include <libvirt/libvirt-admin.h>
 
 static const char *
@@ -66,11 +67,11 @@ exampleGetTypedParamValue(virTypedParameterPtr item)
         break;
 
     case VIR_TYPED_PARAM_LLONG:
-        ret = asprintf(&str, "%lld", item->value.l);
+        ret = asprintf(&str, "%" PRId64, (int64_t)item->value.l);
         break;
 
     case VIR_TYPED_PARAM_ULLONG:
-        ret = asprintf(&str, "%llu", item->value.ul);
+        ret = asprintf(&str, "%" PRIu64, (uint64_t)item->value.ul);
         break;
 
     case VIR_TYPED_PARAM_DOUBLE:
@@ -143,7 +144,7 @@ int main(int argc, char **argv)
     if (!(timestr = exampleGetTimeStr(virAdmClientGetTimestamp(clnt))))
         goto cleanup;
 
-    printf("%-15s: %llu\n", "id", virAdmClientGetID(clnt));
+    printf("%-15s: %" PRIu64 "\n", "id", (uint64_t)virAdmClientGetID(clnt));
     printf("%-15s: %s\n", "connection_time", timestr);
     printf("%-15s: %s\n", "transport",
              exampleTransportToString(virAdmClientGetTransport(clnt)));
diff --git a/examples/admin/list_clients.c b/examples/admin/list_clients.c
index 5cf8e4c2a6..2876637d42 100644
--- a/examples/admin/list_clients.c
+++ b/examples/admin/list_clients.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <inttypes.h>
 #include <libvirt/libvirt-admin.h>
 
 static const char *
@@ -96,7 +97,7 @@ int main(int argc, char **argv)
                 exampleGetTimeStr(virAdmClientGetTimestamp(client))))
             goto cleanup;
 
-        printf(" %-5llu %-15s %-15s\n", id,
+        printf(" %-5" PRIu64 " %-15s %-15s\n", (uint64_t)id,
                exampleTransportToString(transport), timestr);
         free(timestr);
     }
diff --git a/examples/domtop/domtop.c b/examples/domtop/domtop.c
index 008065c651..e1e7fbff8b 100644
--- a/examples/domtop/domtop.c
+++ b/examples/domtop/domtop.c
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <sys/time.h>
 #include <unistd.h>
+#include <inttypes.h>
 
 static bool debug;
 static bool run_top;
@@ -226,8 +227,11 @@ print_cpu_usage(const char *dom_name,
             return;
         }
 
-        DEBUG("now_params=%llu then_params=%llu now=%llu then=%llu",
-              now_params[pos].value.ul, then_params[pos].value.ul, now, then);
+        DEBUG("now_params=%" PRIu64 " then_params=%" PRIu64
+              " now=%" PRIu64 " then=%" PRIu64,
+              (uint64_t)now_params[pos].value.ul,
+              (uint64_t)then_params[pos].value.ul,
+              (uint64_t)now, (uint64_t)then);
 
         /* @now_params and @then_params are in nanoseconds, @now and @then are
          * in microseconds. In ideal world, we would translate them both into
-- 
2.20.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v2] examples: fix 64-bit integer formatting on Windows
Posted by Michal Privoznik 4 years, 12 months ago
On 4/2/19 11:24 AM, Daniel P. Berrangé wrote:
> The Windows printf functions don't support %llu/%lld for printing 64-bit
> integers. For most of libvirt this doesn't matter as we rely on gnulib
> which provides a replacement printf that is sane.
> 
> The example code is designed to compile against the normal OS headers,
> with no use of gnulib and thus has to use the platform specific printf.
> To deal with this we must use the macros PRI* macros from inttypes.h
> to get the platform specific format string.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>   examples/admin/client_info.c  | 7 ++++---
>   examples/admin/list_clients.c | 3 ++-
>   examples/domtop/domtop.c      | 8 ++++++--
>   3 files changed, 12 insertions(+), 6 deletions(-)
> 
> Changed in v2:
> 
>   - Now actually commit the int64 casts

ACK

Michal

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list