summaryrefslogtreecommitdiffstats
path: root/libsmartcols
diff options
context:
space:
mode:
authorKarel Zak2016-09-19 14:07:38 +0200
committerKarel Zak2016-09-19 14:07:38 +0200
commit4e91ebca3e237fb0e7b3aae05505f4ce3b52146a (patch)
tree5601602635dbe1f843b35329fe5723c67e9817ef /libsmartcols
parentlibsmartcols: fix comment (diff)
downloadkernel-qcow2-util-linux-4e91ebca3e237fb0e7b3aae05505f4ce3b52146a.tar.gz
kernel-qcow2-util-linux-4e91ebca3e237fb0e7b3aae05505f4ce3b52146a.tar.xz
kernel-qcow2-util-linux-4e91ebca3e237fb0e7b3aae05505f4ce3b52146a.zip
libsmartcols: be more strict about empty tables
and don't print extra \n for empty table. Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libsmartcols')
-rw-r--r--libsmartcols/src/table.c2
-rw-r--r--libsmartcols/src/table_print.c19
2 files changed, 15 insertions, 6 deletions
diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c
index c41c6a12c..9aae75c95 100644
--- a/libsmartcols/src/table.c
+++ b/libsmartcols/src/table.c
@@ -448,7 +448,7 @@ struct libscols_column *scols_table_get_column(struct libscols_table *tb,
*/
int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln)
{
- if (!tb || !ln)
+ if (!tb || !ln || tb->ncols == 0)
return -EINVAL;
if (tb->ncols > ln->ncells) {
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
index 41b56d1d6..7612682cd 100644
--- a/libsmartcols/src/table_print.c
+++ b/libsmartcols/src/table_print.c
@@ -1520,7 +1520,7 @@ int scols_table_print_range_to_string( struct libscols_table *tb,
#endif
}
-static int __scols_print_table(struct libscols_table *tb)
+static int __scols_print_table(struct libscols_table *tb, int *is_empty)
{
int rc = 0;
struct libscols_buffer *buf;
@@ -1529,9 +1529,17 @@ static int __scols_print_table(struct libscols_table *tb)
return -EINVAL;
DBG(TAB, ul_debugobj(tb, "printing"));
+ if (is_empty)
+ *is_empty = 0;
+ if (list_empty(&tb->tb_columns)) {
+ DBG(TAB, ul_debugobj(tb, "error -- no columns"));
+ return -EINVAL;
+ }
if (list_empty(&tb->tb_lines)) {
- DBG(TAB, ul_debugobj(tb, "ignore -- empty table"));
+ DBG(TAB, ul_debugobj(tb, "ignore -- no lines"));
+ if (is_empty)
+ *is_empty = 1;
return 0;
}
@@ -1570,9 +1578,10 @@ done:
*/
int scols_print_table(struct libscols_table *tb)
{
- int rc = __scols_print_table(tb);
+ int empty = 0;
+ int rc = __scols_print_table(tb, &empty);
- if (rc == 0)
+ if (rc == 0 && !empty)
fputc('\n', tb->out);
return rc;
}
@@ -1605,7 +1614,7 @@ int scols_print_table_to_string(struct libscols_table *tb, char **data)
old_stream = scols_table_get_stream(tb);
scols_table_set_stream(tb, stream);
- rc = __scols_print_table(tb);
+ rc = __scols_print_table(tb, NULL);
fclose(stream);
scols_table_set_stream(tb, old_stream);