summaryrefslogtreecommitdiff
path: root/lib/libform
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2013-11-21 15:40:17 +0000
committerchristos <christos@NetBSD.org>2013-11-21 15:40:17 +0000
commit1bebdac1c24776643bac115a7e4d1a18c1909267 (patch)
tree891c319f938575ff65f7a5aade1768dbf13c5d7d /lib/libform
parentca5e306e663dff3e26d4d324df564f669bcbc943 (diff)
Simplify previous.
Diffstat (limited to 'lib/libform')
-rw-r--r--lib/libform/field.c65
1 files changed, 28 insertions, 37 deletions
diff --git a/lib/libform/field.c b/lib/libform/field.c
index adafbf4996d..7dd7a26789d 100644
--- a/lib/libform/field.c
+++ b/lib/libform/field.c
@@ -1,4 +1,4 @@
-/* $NetBSD: field.c,v 1.26 2013/11/21 09:40:19 blymn Exp $ */
+/* $NetBSD: field.c,v 1.27 2013/11/21 15:40:17 christos Exp $ */
/*-
* Copyright (c) 1998-1999 Brett Lymn
* (blymn@baea.com.au, brett_lymn@yahoo.com.au)
@@ -29,8 +29,9 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: field.c,v 1.26 2013/11/21 09:40:19 blymn Exp $");
+__RCSID("$NetBSD: field.c,v 1.27 2013/11/21 15:40:17 christos Exp $");
+#include <sys/param.h>
#include <stdlib.h>
#include <strings.h>
#include <stdarg.h>
@@ -472,7 +473,7 @@ field_buffer(FIELD *field, int buffer)
char *reformat, *p;
_FORMI_FIELD_LINES *linep;
- size_t bufsize;
+ size_t bufsize, pos;
if (field == NULL)
return NULL;
@@ -492,45 +493,35 @@ field_buffer(FIELD *field, int buffer)
if (_formi_sync_buffer(field) != E_OK)
return NULL;
- if ((field->opts & O_REFORMAT) != O_REFORMAT) {
+ if ((field->opts & O_REFORMAT) != O_REFORMAT)
return field->buffers[buffer].string;
- } else {
- if (field->row_count > 1) {
-
- /*
- * compute reformat buffer size
- * each line length plus one line feed
- * except for last line without line feed
- * but plus one NUL character
- */
- bufsize = 0;
- for (linep=field->alines; linep; linep=linep->next)
- bufsize += strlen(linep->string)+1;
-
- reformat = (char *)malloc(bufsize);
- if (reformat == NULL)
- return NULL;
- /*
- * foreach row copy line, append newline, no
- * newline on last row.
- */
- p = reformat;
- for (linep=field->alines; linep; linep=linep->next) {
- strcpy(p, linep->string);
- p += strlen(linep->string);
- if (linep->next)
- *p++ = '\n';
+ if (field->row_count <= 1)
+ return strdup(field->buffers[buffer].string);
+
+ /*
+ * create a single string containing each line,
+ * separated by newline, last line having no
+ * newline, but NUL terminated.
+ */
+ bufsize = pos = 0;
+ reformat = NULL;
+ for (linep = field->alines; linep; linep = linep->next) {
+ size_t len = strlen(linep->string);
+ if (len + 1 >= bufsize - pos) {
+ bufsize += MAX(1024, 2 * len);
+ p = realloc(reformat, bufsize);
+ if (p == NULL) {
+ free(reformat);
+ return NULL;
}
- *p = '\0';
-
- return reformat;
- } else {
- asprintf(&reformat, "%s",
- field->buffers[buffer].string);
- return reformat;
+ reformat = p;
}
+ memcpy(reformat + pos, linep->string, len);
+ pos += len;
+ reformat[pos++] = linep->next ? '\n' : '\0';
}
+ return reformat;
}
/*