Deleted Added
sdiff udiff text old ( 181110 ) new ( 181111 )
full compact
1/*
2 * Copyright (c) 2004 Darren Tucker.
3 *
4 * Based originally on asprintf.c from OpenBSD:
5 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above

--- 25 unchanged lines hidden (view full) ---

34# else
35# define VA_COPY(dest, src) (dest) = (src)
36# endif
37# endif
38#endif
39
40#define INIT_SZ 128
41
42int
43vasprintf(char **str, const char *fmt, va_list ap)
44{
45 int ret = -1;
46 va_list ap2;
47 char *string, *newstr;
48 size_t len;
49
50 VA_COPY(ap2, ap);
51 if ((string = malloc(INIT_SZ)) == NULL)
52 goto fail;
53
54 ret = vsnprintf(string, INIT_SZ, fmt, ap2);
55 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
56 *str = string;
57 } else if (ret == INT_MAX || ret < 0) { /* Bad length */
58 free(string);
59 goto fail;
60 } else { /* bigger than initial, realloc allowing for nul */
61 len = (size_t)ret + 1;
62 if ((newstr = realloc(string, len)) == NULL) {
63 free(string);
64 goto fail;
65 } else {
66 va_end(ap2);

--- 35 unchanged lines hidden ---