MCPcopy Create free account
hub / github.com/Sapd/HeadsetControl / vasprintf

Function vasprintf

src/utility.c:341–380  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

339#define INIT_SZ 128
340
341int vasprintf(char** str, const char* fmt, va_list ap)
342{
343 int ret;
344 va_list ap2;
345 char *string, *newstr;
346 size_t len;
347
348 if ((string = malloc(INIT_SZ)) == NULL)
349 goto fail;
350
351 VA_COPY(ap2, ap);
352 ret = vsnprintf(string, INIT_SZ, fmt, ap2);
353 va_end(ap2);
354 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
355 *str = string;
356 } else if (ret == INT_MAX || ret < 0) { /* Bad length */
357 free(string);
358 goto fail;
359 } else { /* bigger than initial, realloc allowing for nul */
360 len = (size_t)ret + 1;
361 if ((newstr = realloc(string, len)) == NULL) {
362 free(string);
363 goto fail;
364 }
365 VA_COPY(ap2, ap);
366 ret = vsnprintf(newstr, len, fmt, ap2);
367 va_end(ap2);
368 if (ret < 0 || (size_t)ret >= len) { /* failed with realloc'ed string */
369 free(newstr);
370 goto fail;
371 }
372 *str = newstr;
373 }
374 return (ret);
375
376fail:
377 *str = NULL;
378 errno = ENOMEM;
379 return (-1);
380}
381
382int _asprintf(char** str, const char* fmt, ...)
383{

Callers 1

_asprintfFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected