diff options
author | Nick Mathewson <nickm@torproject.org> | 2013-07-19 13:03:23 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2013-07-19 13:26:25 -0400 |
commit | 5343ee1a06ebb959fc77753898015186b94a5daa (patch) | |
tree | 22575131739f42d7c4435cd28f42e09763861f81 /src/common/util.c | |
parent | 8f0755fa85b9c1fb38fd122719e483cc11d89069 (diff) | |
download | tor-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.c | 33 |
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 |