]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: be more strict about empty tables
authorKarel Zak <kzak@redhat.com>
Mon, 19 Sep 2016 12:07:38 +0000 (14:07 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 19 Sep 2016 12:07:38 +0000 (14:07 +0200)
and don't print extra \n for empty table.

Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/src/table.c
libsmartcols/src/table_print.c

index c41c6a12cae10b4284a64deb1b982875f559548f..9aae75c9588fd6d28be4dba98e54545472f2d082 100644 (file)
@@ -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) {
index 41b56d1d64aff8c68bea2e7b8ac45ee9aad7f0d0..7612682cd20fe9c0c3ffabf8567ddf960d13b2c9 100644 (file)
@@ -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);