aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbb/xfuncs.c51
1 files changed, 50 insertions, 1 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 684d0a4fb..00cacaadf 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -229,4 +229,53 @@ int wait4pid(int pid)
229 if (WIFSIGNALED(status)) return WTERMSIG(status); 229 if (WIFSIGNALED(status)) return WTERMSIG(status);
230 return 0; 230 return 0;
231} 231}
232#endif 232#endif
233
234#ifdef L_itoa
235// Largest 32 bit integer is -2 billion plus null terminator.
236// Int should always be 32 bits on a Unix-oid system, see
237// http://www.unix.org/whitepapers/64bit.html
238static char local_buf[12];
239
240void utoa_to_buf(unsigned n, char *buf, int buflen)
241{
242 int i, out = 0;
243 for (i=1000000000; i; i/=10) {
244 int res = n/i;
245
246 if (res || out || i == 1) {
247 out++;
248 n -= res*i;
249 *buf++ = '0' + res;
250 }
251 }
252 *buf = 0;
253}
254
255// Note: uses static buffer, calling it twice in a row will overwrite.
256
257char *utoa(unsigned n)
258{
259 utoa_to_buf(n, local_buf, sizeof(local_buf));
260
261 return local_buf;
262}
263
264void itoa_to_buf(int n, char *buf, int buflen)
265{
266 if (n<0) {
267 n = -n;
268 *buf++ = '-';
269 }
270 utoa_to_buf((unsigned)n, buf, buflen);
271}
272
273// Note: uses static buffer, calling it twice in a row will overwrite.
274
275char *itoa(int n)
276{
277 itoa_to_buf(n, local_buf, sizeof(local_buf));
278
279 return local_buf;
280}
281#endif