[PATCHv2 1/9] util: make return value of virUUIDFormat and virMacAddrFormat useful

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Both of these functions returned void, but it's convenient for them to
return a const char* of the char* that is passed in. This was you can
call the function and use the result in the same expression/arg.
---
 src/util/uuid.c       |  6 ++++--
 src/util/uuid.h       |  6 +++---
 src/util/virmacaddr.c | 13 +++++++++++--
 src/util/virmacaddr.h |  4 ++--
 4 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/util/uuid.c b/src/util/uuid.c
index 1efde69..54e7bb6 100644
--- a/src/util/uuid.c
+++ b/src/util/uuid.c
@@ -182,9 +182,10 @@ virUUIDParse(const char *uuidstr, unsigned char *uuid) {
  *
  * Converts the raw UUID into printable format, with embedded '-'
  *
- * Returns 0 in case of success and -1 in case of error.
+ * Returns a pointer to the resulting character string.
  */
-void virUUIDFormat(const unsigned char *uuid, char *uuidstr)
+const char *
+virUUIDFormat(const unsigned char *uuid, char *uuidstr)
 {
     snprintf(uuidstr, VIR_UUID_STRING_BUFLEN,
              "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
@@ -193,6 +194,7 @@ void virUUIDFormat(const unsigned char *uuid, char *uuidstr)
              uuid[8], uuid[9], uuid[10], uuid[11],
              uuid[12], uuid[13], uuid[14], uuid[15]);
     uuidstr[VIR_UUID_STRING_BUFLEN-1] = '\0';
+    return uuidstr;
 }
 
 
diff --git a/src/util/uuid.h b/src/util/uuid.h
index da56a92..54e0573 100644
--- a/src/util/uuid.h
+++ b/src/util/uuid.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007, 2011 Red Hat, Inc.
+ * Copyright (C) 2007, 2011, 2012 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -35,7 +35,7 @@ int virUUIDParse(const char *uuidstr,
                  unsigned char *uuid)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
 
-void virUUIDFormat(const unsigned char *uuid,
-                   char *uuidstr) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+const char *virUUIDFormat(const unsigned char *uuid,
+                          char *uuidstr) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
 #endif /* __VIR_UUID_H__ */
diff --git a/src/util/virmacaddr.c b/src/util/virmacaddr.c
index c07976b..e207927 100644
--- a/src/util/virmacaddr.c
+++ b/src/util/virmacaddr.c
@@ -176,14 +176,23 @@ virMacAddrParse(const char* str, virMacAddrPtr addr)
     return -1;
 }
 
-void virMacAddrFormat(const virMacAddrPtr addr,
-                      char *str)
+/* virMacAddrFormat
+ * Converts the binary mac address in addr into a NULL-terminated
+ * character string in str. It is assumed that the memory pointed to
+ * by str is at least VIR_MAC_STRING_BUFLEN bytes long.
+ *
+ * Returns a pointer to the resulting character string.
+ */
+const char *
+virMacAddrFormat(const virMacAddrPtr addr,
+                 char *str)
 {
     snprintf(str, VIR_MAC_STRING_BUFLEN,
              "%02X:%02X:%02X:%02X:%02X:%02X",
              addr->addr[0], addr->addr[1], addr->addr[2],
              addr->addr[3], addr->addr[4], addr->addr[5]);
     str[VIR_MAC_STRING_BUFLEN-1] = '\0';
+    return str;
 }
 
 void virMacAddrGenerate(const unsigned char prefix[VIR_MAC_PREFIX_BUFLEN],
diff --git a/src/util/virmacaddr.h b/src/util/virmacaddr.h
index fdac362..4c5074c 100644
--- a/src/util/virmacaddr.h
+++ b/src/util/virmacaddr.h
@@ -44,8 +44,8 @@ int virMacAddrCmpRaw(const virMacAddrPtr mac1,
 void virMacAddrSet(virMacAddrPtr dst, const virMacAddrPtr src);
 void virMacAddrSetRaw(virMacAddrPtr dst, const unsigned char s[VIR_MAC_BUFLEN]);
 void virMacAddrGetRaw(virMacAddrPtr src, unsigned char dst[VIR_MAC_BUFLEN]);
-void virMacAddrFormat(const virMacAddrPtr addr,
-                      char *str);
+const char *virMacAddrFormat(const virMacAddrPtr addr,
+                             char *str);
 void virMacAddrGenerate(const unsigned char prefix[VIR_MAC_PREFIX_BUFLEN],
                         virMacAddrPtr addr);
 int virMacAddrParse(const char* str,
-- 
1.7.11.2

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


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]