2 * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
11 * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
12 * The copyrights to the contents of this file are licensed under the MIT License
13 * (http://www.opensource.org/licenses/mit-license.php)
25 #else /* !HAVE_STDARG_H */
26 #error Not enough var arg support!
27 #endif /* HAVE_STDARG_H */
31 #include "snprintf_compat.h"
32 #include "vasprintf_compat.h"
34 static int printbuf_extend(struct printbuf *p, int min_size);
36 struct printbuf *printbuf_new(void)
40 p = (struct printbuf *)calloc(1, sizeof(struct printbuf));
45 if (!(p->buf = (char *)malloc(p->size)))
55 * Extend the buffer p so it has a size of at least min_size.
57 * If the current size is large enough, nothing is changed.
59 * Note: this does not check the available space! The caller
60 * is responsible for performing those calculations.
62 static int printbuf_extend(struct printbuf *p, int min_size)
67 if (p->size >= min_size)
69 /* Prevent signed integer overflows with large buffers. */
70 if (min_size > INT_MAX - 8)
72 if (p->size > INT_MAX / 2)
73 new_size = min_size + 8;
75 new_size = p->size * 2;
76 if (new_size < min_size + 8)
77 new_size = min_size + 8;
80 MC_DEBUG("printbuf_memappend: realloc "
81 "bpos=%d min_size=%d old_size=%d new_size=%d\n",
82 p->bpos, min_size, p->size, new_size);
83 #endif /* PRINTBUF_DEBUG */
84 if (!(t = (char *)realloc(p->buf, new_size)))
91 int printbuf_memappend(struct printbuf *p, const char *buf, int size)
93 /* Prevent signed integer overflows with large buffers. */
94 if (size > INT_MAX - p->bpos - 1)
96 if (p->size <= p->bpos + size + 1)
98 if (printbuf_extend(p, p->bpos + size + 1) < 0)
101 memcpy(p->buf + p->bpos, buf, size);
103 p->buf[p->bpos] = '\0';
107 int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
113 /* Prevent signed integer overflows with large buffers. */
114 if (len > INT_MAX - offset)
116 size_needed = offset + len;
117 if (pb->size < size_needed)
119 if (printbuf_extend(pb, size_needed) < 0)
123 memset(pb->buf + offset, charvalue, len);
124 if (pb->bpos < size_needed)
125 pb->bpos = size_needed;
130 int sprintbuf(struct printbuf *p, const char *msg, ...)
137 /* user stack buffer first */
139 size = vsnprintf(buf, 128, msg, ap);
141 /* if string is greater than stack buffer, then use dynamic string
142 * with vasprintf. Note: some implementation of vsnprintf return -1
143 * if output is truncated whereas some return the number of bytes that
144 * would have been written - this code handles both cases.
146 if (size == -1 || size > 127)
149 if ((size = vasprintf(&t, msg, ap)) < 0)
155 printbuf_memappend(p, t, size);
161 printbuf_memappend(p, buf, size);
166 void printbuf_reset(struct printbuf *p)
172 void printbuf_free(struct printbuf *p)