2005-01-28 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
* lib/buffer.h: Document behavior of buffer_getstr function.
* lib/buffer.c: (buffer_getstr) Fix bug: must handle case where
the string extends beyond the head struct buffer_data.
diff --git a/lib/buffer.c b/lib/buffer.c
index 296fd14..60048bc 100644
--- a/lib/buffer.c
+++ b/lib/buffer.c
@@ -88,7 +88,23 @@
char *
buffer_getstr (struct buffer *b)
{
- return strdup ((char *)b->head->data);
+ size_t totlen = 0;
+ struct buffer_data *data;
+ char *s;
+ char *p;
+
+ for (data = b->head; data; data = data->next)
+ totlen += data->cp - data->sp;
+ if (!(s = malloc(totlen+1)))
+ return NULL;
+ p = s;
+ for (data = b->head; data; data = data->next)
+ {
+ memcpy(p, data->data + data->sp, data->cp - data->sp);
+ p += data->cp - data->sp;
+ }
+ *p = '\0';
+ return s;
}
/* Return 1 if buffer is empty. */