summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2011-01-25 00:24:17 +0100
committerKarel Zak <kzak@redhat.com>2011-01-25 00:24:17 +0100
commit1f51db365f83309eb34de781fbae60b49098d3b0 (patch)
tree0f214e8a37d34de90269519bd1da33d9ff4a037e
parent0bbeaccb7aad5257f4e8d11324f4aecd7cbf3f51 (diff)
downloadutil-linux-old-1f51db365f83309eb34de781fbae60b49098d3b0.tar.gz
include: [tt] enlarge output buffer
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--include/tt.h1
-rw-r--r--lib/tt.c25
2 files changed, 22 insertions, 4 deletions
diff --git a/include/tt.h b/include/tt.h
index b27f51ea..400d6ba5 100644
--- a/include/tt.h
+++ b/include/tt.h
@@ -48,6 +48,7 @@ struct tt_line {
struct tt *table;
char const **data;
void *userdata;
+ ssize_t data_sz; /* strlen of all data */
struct list_head ln_lines; /* table lines */
diff --git a/lib/tt.c b/lib/tt.c
index ffa5897d..3bcdea9e 100644
--- a/lib/tt.c
+++ b/lib/tt.c
@@ -276,7 +276,13 @@ int tt_line_set_data(struct tt_line *ln, int colnum, const char *data)
cl = tt_get_column(ln->table, colnum);
if (!cl)
return -1;
+
+ if (ln->data[cl->seqnum])
+ ln->data_sz -= strlen(ln->data[cl->seqnum]);
+
ln->data[cl->seqnum] = data;
+ if (data)
+ ln->data_sz += strlen(data);
return 0;
}
@@ -632,6 +638,8 @@ static void print_tree(struct tt *tb, char *buf, size_t bufsz)
int tt_print_table(struct tt *tb)
{
char *line;
+ size_t line_sz;
+ struct list_head *p;
if (!tb)
return -1;
@@ -640,15 +648,24 @@ int tt_print_table(struct tt *tb)
if (tb->termwidth <= 0)
tb->termwidth = 80;
}
- line = malloc(tb->termwidth);
+
+ line_sz = tb->termwidth;
+
+ list_for_each(p, &tb->tb_lines) {
+ struct tt_line *ln = list_entry(p, struct tt_line, ln_lines);
+ if (ln->data_sz > line_sz)
+ line_sz = ln->data_sz;
+ }
+
+ line = malloc(line_sz);
if (!line)
return -1;
if (!(tb->flags & TT_FL_RAW))
- recount_widths(tb, line, tb->termwidth);
+ recount_widths(tb, line, line_sz);
if (tb->flags & TT_FL_TREE)
- print_tree(tb, line, tb->termwidth);
+ print_tree(tb, line, line_sz);
else
- print_table(tb, line, tb->termwidth);
+ print_table(tb, line, line_sz);
free(line);
return 0;