1
0
mirror of https://github.com/systemd/systemd synced 2026-04-23 15:34:50 +02:00

Compare commits

..

No commits in common. "d4782b51ec448ad6e26d119cb17a7576fe719760" and "c86efe34df92e304e1c21d3ae06c0935be01369c" have entirely different histories.

3 changed files with 57 additions and 74 deletions

View File

@ -319,13 +319,13 @@ static inline int __coverity_check_and_return__(int condition) {
sizeof(type) <= 4 ? 10U : \ sizeof(type) <= 4 ? 10U : \
sizeof(type) <= 8 ? 20U : sizeof(int[-2*(sizeof(type) > 8)]))) sizeof(type) <= 8 ? 20U : sizeof(int[-2*(sizeof(type) > 8)])))
#define DECIMAL_STR_WIDTH(x) \ #define DECIMAL_STR_WIDTH(x) \
({ \ ({ \
typeof(x) _x_ = (x); \ typeof(x) _x_ = (x); \
size_t ans = IS_SIGNED_INTEGER_TYPE(_x_) ? 2 : 1; \ size_t ans = 1; \
while ((_x_ /= 10) != 0) \ while ((_x_ /= 10) != 0) \
ans++; \ ans++; \
ans; \ ans; \
}) })
#define SWAP_TWO(x, y) do { \ #define SWAP_TWO(x, y) do { \

View File

@ -33,7 +33,6 @@
#include "dissect-image.h" #include "dissect-image.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "format-table.h"
#include "format-util.h" #include "format-util.h"
#include "fs-util.h" #include "fs-util.h"
#include "fsprg.h" #include "fsprg.h"
@ -85,7 +84,6 @@ enum {
}; };
static OutputMode arg_output = OUTPUT_SHORT; static OutputMode arg_output = OUTPUT_SHORT;
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
static bool arg_utc = false; static bool arg_utc = false;
static bool arg_follow = false; static bool arg_follow = false;
static bool arg_full = true; static bool arg_full = true;
@ -579,8 +577,6 @@ static int parse_argv(int argc, char *argv[]) {
if (IN_SET(arg_output, OUTPUT_EXPORT, OUTPUT_JSON, OUTPUT_JSON_PRETTY, OUTPUT_JSON_SSE, OUTPUT_JSON_SEQ, OUTPUT_CAT)) if (IN_SET(arg_output, OUTPUT_EXPORT, OUTPUT_JSON, OUTPUT_JSON_PRETTY, OUTPUT_JSON_SSE, OUTPUT_JSON_SEQ, OUTPUT_CAT))
arg_quiet = true; arg_quiet = true;
if (OUTPUT_MODE_IS_JSON(arg_output))
arg_json_format_flags = output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO;
break; break;
case 'l': case 'l':
@ -1445,9 +1441,8 @@ finish:
} }
static int list_boots(sd_journal *j) { static int list_boots(sd_journal *j) {
_cleanup_(table_unrefp) Table *table = NULL; int w, i, count;
BootId *id, *all_ids; BootId *id, *all_ids;
int count, i, r;
assert(j); assert(j);
@ -1457,30 +1452,23 @@ static int list_boots(sd_journal *j) {
if (count == 0) if (count == 0)
return count; return count;
table = table_new(OUTPUT_MODE_IS_JSON(arg_output) ? "index" : "idx", "boot id", "first entry", "last entry"); pager_open(arg_pager_flags);
if (!table)
return log_oom();
if (arg_full) /* numbers are one less, but we need an extra char for the sign */
table_set_width(table, 0); w = DECIMAL_STR_WIDTH(count - 1) + 1;
i = 0; i = 0;
LIST_FOREACH(boot_list, id, all_ids) { LIST_FOREACH(boot_list, id, all_ids) {
r = table_add_many(table, char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX];
TABLE_INT, i - count + 1,
TABLE_SET_ALIGN_PERCENT, 100, printf("% *i " SD_ID128_FORMAT_STR " %s—%s\n",
TABLE_ID128, id->id, w, i - count + 1,
TABLE_TIMESTAMP, id->first, SD_ID128_FORMAT_VAL(id->id),
TABLE_TIMESTAMP, id->last); format_timestamp_maybe_utc(a, sizeof(a), id->first),
if (r < 0) format_timestamp_maybe_utc(b, sizeof(b), id->last));
return table_log_add_error(r);
i++; i++;
} }
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, !arg_quiet);
if (r < 0)
return table_log_print_error(r);
boot_id_free_all(all_ids); boot_id_free_all(all_ids);
return 0; return 0;

View File

@ -396,30 +396,28 @@ TEST(table) {
_cleanup_(table_unrefp) Table *t = NULL; _cleanup_(table_unrefp) Table *t = NULL;
_cleanup_free_ char *formatted = NULL; _cleanup_free_ char *formatted = NULL;
assert_se(t = table_new("one", "two", "three", "four")); assert_se(t = table_new("one", "two", "three"));
assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(3), 100) >= 0); assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "xxx", TABLE_STRING, "xxx",
TABLE_STRING, "yyy", TABLE_STRING, "yyy",
TABLE_BOOLEAN, true, TABLE_BOOLEAN, true) >= 0);
TABLE_INT, -1) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "a long field", TABLE_STRING, "a long field",
TABLE_STRING, "yyy", TABLE_STRING, "yyy",
TABLE_SET_UPPERCASE, 1, TABLE_SET_UPPERCASE, 1,
TABLE_BOOLEAN, false, TABLE_BOOLEAN, false) >= 0);
TABLE_INT, -999999) >= 0);
assert_se(table_format(t, &formatted) >= 0); assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO THREE FOUR\n" "ONE TWO THREE\n"
"xxx yyy yes -1\n" "xxx yyy yes\n"
"a long field YYY no -999999\n")); "a long field YYY no\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -429,20 +427,20 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO THREE FOUR\n" "ONE TWO THREE\n"
"xxx yyy yes -1\n" "xxx yyy yes\n"
"a long field YYY no -999999\n")); "a long field YYY no\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
table_set_width(t, 15); table_set_width(t, 12);
assert_se(table_format(t, &formatted) >= 0); assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO TH… FO\n" "ONE TWO THR\n"
"xxx yyy yes -1\n" "xxx yyy yes\n"
"a … YYY no -9…\n")); "a … YYY no\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -451,9 +449,9 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"… … …\n" "… … …\n"
"… … …\n" "… … …\n"
"… … …\n")); "… … …\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -462,9 +460,9 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"… … …\n" "… … …\n"
"… … …\n" "… … …\n"
"… … …\n")); "… … …\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -475,9 +473,9 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO THREE FOUR\n" "ONE TWO THREE\n"
"a long field YYY no -999999\n" "a long field YYY no\n"
"xxx yyy yes -1\n")); "xxx yyy yes\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -486,30 +484,27 @@ TEST(table) {
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "fäää", TABLE_STRING, "fäää",
TABLE_STRING, "uuu", TABLE_STRING, "uuu",
TABLE_BOOLEAN, true, TABLE_BOOLEAN, true) >= 0);
TABLE_INT, 42) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "fäää", TABLE_STRING, "fäää",
TABLE_STRING, "zzz", TABLE_STRING, "zzz",
TABLE_BOOLEAN, false, TABLE_BOOLEAN, false) >= 0);
TABLE_INT, 0) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_EMPTY, TABLE_EMPTY,
TABLE_SIZE, (uint64_t) 4711, TABLE_SIZE, (uint64_t) 4711,
TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE, TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
TABLE_INT64, (uint64_t) -123456789) >= 0);
assert_se(table_format(t, &formatted) >= 0); assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"a long field YYY no -999999\n" "a long field YYY no\n"
"fäää zzz no 0\n" "fäää zzz no\n"
"fäää uuu yes 42\n" "fäää uuu yes\n"
"xxx yyy yes -1\n" "xxx yyy yes\n"
" 4.6K 5min -123456789\n")); " 4.6K 5min\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -520,17 +515,17 @@ TEST(table) {
if (isatty(STDOUT_FILENO)) if (isatty(STDOUT_FILENO))
assert_se(streq(formatted, assert_se(streq(formatted,
"no a long f… no a long f… a long fi…\n" " no a long f… no a long f… a long fi…\n"
"no fäää no fäää fäää\n" " no fäää no fäää fäää\n"
"yes fäää yes fäää fäää\n" " yes fäää yes fäää fäää\n"
"yes xxx yes xxx xxx\n" " yes xxx yes xxx xxx\n"
"5min 5min \n")); "5min 5min \n"));
else else
assert_se(streq(formatted, assert_se(streq(formatted,
"no a long field no a long field a long field\n" " no a long field no a long field a long field\n"
"no fäää no fäää fäää\n" " no fäää no fäää fäää\n"
"yes fäää yes fäää fäää\n" " yes fäää yes fäää fäää\n"
"yes xxx yes xxx xxx\n" " yes xxx yes xxx xxx\n"
"5min 5min \n")); "5min 5min \n"));
} }