aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/vsprintf.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 38e612e66da..385c40291cd 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -212,6 +212,26 @@ char *put_dec(char *buf, unsigned long long num)
212 } 212 }
213} 213}
214 214
215/*
216 * Convert passed number to decimal string.
217 * Returns the length of string. On buffer overflow, returns 0.
218 *
219 * If speed is not important, use snprintf(). It's easy to read the code.
220 */
221int num_to_str(char *buf, int size, unsigned long long num)
222{
223 char tmp[21]; /* Enough for 2^64 in decimal */
224 int idx, len;
225
226 len = put_dec(tmp, num) - tmp;
227
228 if (len > size)
229 return 0;
230 for (idx = 0; idx < len; ++idx)
231 buf[idx] = tmp[len - idx - 1];
232 return len;
233}
234
215#define ZEROPAD 1 /* pad with zero */ 235#define ZEROPAD 1 /* pad with zero */
216#define SIGN 2 /* unsigned/signed long */ 236#define SIGN 2 /* unsigned/signed long */
217#define PLUS 4 /* show plus */ 237#define PLUS 4 /* show plus */