From 4d0884a1eaefb7af31f8039f3aa9128362535716 Mon Sep 17 00:00:00 2001 From: Manuel Novoa III Date: Thu, 12 Sep 2002 14:52:26 +0000 Subject: Apply vodz's last_patch53.gz (bb_asprintf.c) and last_patch54.gz (arith.c). --- libbb/bb_asprintf.c | 74 +++-------------------------------------------------- 1 file changed, 3 insertions(+), 71 deletions(-) (limited to 'libbb/bb_asprintf.c') diff --git a/libbb/bb_asprintf.c b/libbb/bb_asprintf.c index c0c5cdeda..9a71be7f5 100644 --- a/libbb/bb_asprintf.c +++ b/libbb/bb_asprintf.c @@ -7,83 +7,15 @@ #include -#ifdef TEST -extern void *xrealloc(void *p, size_t size); -#else -#include "libbb.h" /* busybox source */ -#endif +#include "libbb.h" -/* Exchange glibc vasprintf - minimize allocate memory version */ -/* libc5 and uclibc have not vasprintf function */ -void bb_vasprintf(char **string_ptr, const char *format, va_list args) -{ - int bs = 128; - char stack_buff[128]; - char *buff = stack_buff; - int done; - - /* two or more loop, first - calculate memory size only */ - while(1) { - done = vsnprintf (buff, bs, format, args); -/* Different libc have different interpretation vsnprintf returned value */ - if(done >= 0) { - if(done < bs && buff != stack_buff) { - /* allocated */ - *string_ptr = buff; - return; - } else { - /* true calculate memory size */ - bs = done+1; - } - } else { - /* - * Old libc. Incrementaly test. - * Exact not minimize allocate memory. - */ - bs += 128; - } - buff = xrealloc((buff == stack_buff ? NULL : buff), bs); - } -} - void bb_asprintf(char **string_ptr, const char *format, ...) { va_list p; va_start(p, format); - bb_vasprintf(string_ptr, format, p); + if(vasprintf(string_ptr, format, p)<0) + error_msg_and_die(memory_exhausted); va_end(p); } - -#ifdef TEST -int main(int argc, char **argv) -{ - char *out_buf; - char big_buf[200]; - int i; - - bb_asprintf(&out_buf, "Hi!\nargc=%d argv[0]=%s\n", argc, argv[0]); - printf(out_buf); - free(out_buf); - - for(i=0; i < sizeof(big_buf)-1; i++) - big_buf[i]='x'; - big_buf[i]=0; - bb_asprintf(&out_buf, "Test Big\n%s\n", big_buf); - printf(out_buf); - free(out_buf); - - return 0; -} - -void *xrealloc(void *p, size_t size) -{ - void *p2 = realloc(p, size); - if(p2==0) { - fprintf(stderr, "TEST: memory_exhausted\n"); - exit(1); - } - return p2; -} -#endif -- cgit v1.2.3