Deleted Added
full compact
bsd-asprintf.c (181110) bsd-asprintf.c (181111)
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
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 vasprintf(char **str, const char *fmt, va_list ap)
42int
43vasprintf(char **str, const char *fmt, va_list ap)
43{
44 int ret = -1;
45 va_list ap2;
46 char *string, *newstr;
47 size_t len;
48
49 VA_COPY(ap2, ap);
50 if ((string = malloc(INIT_SZ)) == NULL)
51 goto fail;
52
53 ret = vsnprintf(string, INIT_SZ, fmt, ap2);
54 if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
55 *str = string;
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;
56 } else if (ret == INT_MAX) { /* shouldn't happen */
57 } else if (ret == INT_MAX || ret < 0) { /* Bad length */
58 free(string);
57 goto fail;
58 } else { /* bigger than initial, realloc allowing for nul */
59 len = (size_t)ret + 1;
60 if ((newstr = realloc(string, len)) == NULL) {
61 free(string);
62 goto fail;
63 } else {
64 va_end(ap2);

--- 35 unchanged lines hidden ---
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 ---