aboutsummaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2013-07-19 13:03:23 -0400
committerNick Mathewson <nickm@torproject.org>2013-07-19 13:26:25 -0400
commit5343ee1a06ebb959fc77753898015186b94a5daa (patch)
tree22575131739f42d7c4435cd28f42e09763861f81 /src/common/util.c
parent8f0755fa85b9c1fb38fd122719e483cc11d89069 (diff)
downloadtor-5343ee1a06ebb959fc77753898015186b94a5daa.tar.gz
tor-5343ee1a06ebb959fc77753898015186b94a5daa.zip
Add a signal-safe decimal formatting function
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/common/util.c b/src/common/util.c
index 0e8d34eafd..25ea133711 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -3441,6 +3441,39 @@ format_hex_number_sigsafe(unsigned int x, char *buf, int buf_len)
return len;
}
+/** As format_hex_number_sigsafe, but format the number in base 10. */
+int
+format_dec_number_sigsafe(unsigned long x, char *buf, int buf_len)
+{
+ int len;
+ unsigned long tmp;
+ char *cp;
+
+ tmp = x;
+ len = 1;
+ while (tmp > 9) {
+ tmp /= 10;
+ ++len;
+ }
+
+ if (len >= buf_len)
+ return 0;
+
+ cp = buf + len;
+ *cp = '\0';
+ do {
+ unsigned digit = x % 10;
+ tor_assert(cp > buf);
+ --cp;
+ *cp = '0' + digit;
+ x /= 10;
+ } while (x);
+
+ tor_assert(cp == buf);
+
+ return len;
+}
+
#ifndef _WIN32
/** Format <b>child_state</b> and <b>saved_errno</b> as a hex string placed in
* <b>hex_errno</b>. Called between fork and _exit, so must be signal-handler