static char * _strdup_vprintf(const char *fmt, va_list ap) { /* Guess we need no more than 100 bytes. */ int n, size = 200; char *p,*np; //va_list ap; if ((p = (char *) ortp_malloc (size)) == NULL) return NULL; while (1) { /* Try to print in the allocated space. */ //va_start(ap, fmt); n = vsnprintf (p, size, fmt, ap); //va_end(ap); /* If that worked, return the string. */ if (n > -1 && n < size) return p; //printf("Reallocing space.\n"); /* Else try again with more space. */ if (n > -1) /* glibc 2.1 */ size = n + 1; /* precisely what is needed */ else /* glibc 2.0 */ size *= 2; /* twice the old size */ if ((np = (char *) ortp_realloc (p, size)) == NULL) { free(p); return NULL; } else { p = np; } } }
char * ortp_strcat_vprintf(char* dst, const char *fmt, va_list ap){ char *ret; unsigned long dstlen, retlen; ret=ortp_strdup_vprintf(fmt, ap); dstlen = strlen(dst); retlen = strlen(ret); if ((dst = ortp_realloc(dst, dstlen+retlen+1)) != NULL){ strncat(dst,ret,retlen); dst[dstlen+retlen] = '\0'; ortp_free(ret); return dst; } else { ortp_free(ret); return NULL; } }
char * ortp_strdup_vprintf(const char *fmt, va_list ap) { /* Guess we need no more than 100 bytes. */ int n, size = 200; char *p,*np; #ifndef WIN32 va_list cap;/*copy of our argument list: a va_list cannot be re-used (SIGSEGV on linux 64 bits)*/ #endif if ((p = (char *) ortp_malloc (size)) == NULL) return NULL; while (1) { /* Try to print in the allocated space. */ #ifndef WIN32 va_copy(cap,ap); n = vsnprintf (p, size, fmt, cap); va_end(cap); #else /*this works on 32 bits, luckily*/ n = vsnprintf (p, size, fmt, ap); #endif /* If that worked, return the string. */ if (n > -1 && n < size) return p; //printf("Reallocing space.\n"); /* Else try again with more space. */ if (n > -1) /* glibc 2.1 */ size = n + 1; /* precisely what is needed */ else /* glibc 2.0 */ size *= 2; /* twice the old size */ if ((np = (char *) ortp_realloc (p, size)) == NULL) { free(p); return NULL; } else { p = np; } } }