summaryrefslogtreecommitdiff
path: root/trace.c
diff options
context:
space:
mode:
authorPierre Habouzit <madcoder@debian.org>2007-09-20 00:42:13 +0200
committerJunio C Hamano <gitster@pobox.com>2007-09-21 03:31:18 -0700
commit7a33bcbe802080f3a926e93d66b65ff7c5e8c5ed (patch)
treeed4a77eef3125b2b9bceb5adb63d8df51fcacc9f /trace.c
parent663af3422a648e87945e4d8c0cc3e13671f2bbde (diff)
downloadgit-7a33bcbe802080f3a926e93d66b65ff7c5e8c5ed.tar.gz
sq_quote_argv and add_to_string rework with strbuf's.
* sq_quote_buf is made public, and works on a strbuf. * sq_quote_argv also works on a strbuf. * make sq_quote_argv take a "maxlen" argument to check the buffer won't grow too big. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'trace.c')
-rw-r--r--trace.c51
1 files changed, 31 insertions, 20 deletions
diff --git a/trace.c b/trace.c
index 91548a56ec..69fa05e644 100644
--- a/trace.c
+++ b/trace.c
@@ -64,7 +64,7 @@ static const char err_msg[] = "Could not trace into fd given by "
void trace_printf(const char *fmt, ...)
{
- char buf[8192];
+ struct strbuf buf;
va_list ap;
int fd, len, need_close = 0;
@@ -72,12 +72,22 @@ void trace_printf(const char *fmt, ...)
if (!fd)
return;
+ strbuf_init(&buf, 0);
va_start(ap, fmt);
- len = vsnprintf(buf, sizeof(buf), fmt, ap);
+ len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
va_end(ap);
- if (len >= sizeof(buf))
- die("unreasonnable trace length");
- write_or_whine_pipe(fd, buf, len, err_msg);
+ if (len >= strbuf_avail(&buf)) {
+ strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
+ va_start(ap, fmt);
+ len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
+ va_end(ap);
+ if (len >= strbuf_avail(&buf))
+ die("broken vsnprintf");
+ }
+ strbuf_setlen(&buf, len);
+
+ write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
+ strbuf_release(&buf);
if (need_close)
close(fd);
@@ -85,31 +95,32 @@ void trace_printf(const char *fmt, ...)
void trace_argv_printf(const char **argv, int count, const char *fmt, ...)
{
- char buf[8192];
+ struct strbuf buf;
va_list ap;
- char *argv_str;
- size_t argv_len;
int fd, len, need_close = 0;
fd = get_trace_fd(&need_close);
if (!fd)
return;
+ strbuf_init(&buf, 0);
va_start(ap, fmt);
- len = vsnprintf(buf, sizeof(buf), fmt, ap);
+ len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
va_end(ap);
- if (len >= sizeof(buf))
- die("unreasonnable trace length");
-
- /* Get the argv string. */
- argv_str = sq_quote_argv(argv, count);
- argv_len = strlen(argv_str);
-
- write_or_whine_pipe(fd, buf, len, err_msg);
- write_or_whine_pipe(fd, argv_str, argv_len, err_msg);
- write_or_whine_pipe(fd, "\n", 1, err_msg);
+ if (len >= strbuf_avail(&buf)) {
+ strbuf_grow(&buf, len - strbuf_avail(&buf) + 128);
+ va_start(ap, fmt);
+ len = vsnprintf(buf.buf, strbuf_avail(&buf), fmt, ap);
+ va_end(ap);
+ if (len >= strbuf_avail(&buf))
+ die("broken vsnprintf");
+ }
+ strbuf_setlen(&buf, len);
- free(argv_str);
+ sq_quote_argv(&buf, argv, count, 0);
+ strbuf_addch(&buf, '\n');
+ write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
+ strbuf_release(&buf);
if (need_close)
close(fd);