[PATCH 5/6] Add FormatUInt32() for formatting numbers in a signal safe manner

Chase Douglas chase.douglas at canonical.com
Fri Apr 6 11:25:58 PDT 2012


Signed-off-by: Chase Douglas <chase.douglas at canonical.com>
---
 include/misc.h |    1 +
 os/utils.c     |   19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/include/misc.h b/include/misc.h
index 41c1333..867a72a 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -229,6 +229,7 @@ pad_to_int32(const int bytes)
 }
 
 extern char **xstrtokenize(const char *str, const char *separators);
+extern void FormatUInt32(uint32_t num, char *string);
 
 /**
  * Compare the two version numbers comprising of major.minor.
diff --git a/os/utils.c b/os/utils.c
index ecf78e5..6ff8dbd 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -1784,3 +1784,22 @@ xstrtokenize(const char *str, const char *separators)
     free(list);
     return NULL;
 }
+
+/* Format a number into a string in a signal safe manner. The string should be
+ * at least 11 characters in order to handle all uint32_t values. */
+void
+FormatUInt32(uint32_t num, char *string)
+{
+    uint32_t divisor = 1;
+    int len;
+    int i;
+
+    for (len = 0, divisor = 1; num / divisor; len++, divisor *= 10);
+    if (len == 0)
+        len = 1;
+
+    for (i = len, divisor = 1; i > 0; i--, divisor *= 10)
+        string[len - 1] = '0' + ((num / divisor) % 10);
+
+    string[len] = '\0';
+}
-- 
1.7.9.1



More information about the xorg-devel mailing list