From 70921b8e9b66021c38020ab1aad8d61b3634fdf0 Mon Sep 17 00:00:00 2001 From: Alberto Leiva Popper Date: Mon, 6 May 2024 16:10:14 -0600 Subject: [PATCH] Allow --mode=print to read file from stdin Either works: $ fort --mode=print < cert.cer $ fort --mode=print - < cert.cer Progress for #122. --- src/config.c | 4 +--- src/print_file.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/config.c b/src/config.c index 3bf9fcd9..87040334 100644 --- a/src/config.c +++ b/src/config.c @@ -993,9 +993,7 @@ static int validate_config(void) { if (rpki_config.mode == PRINT_FILE) - return (rpki_config.payload == NULL) - ? pr_op_err("Missing file name.") - : 0; + return 0; if (rpki_config.payload != NULL) return pr_op_err("I don't know what '%s' is.", diff --git a/src/print_file.c b/src/print_file.c index b310d41c..7bb4572c 100644 --- a/src/print_file.c +++ b/src/print_file.c @@ -169,9 +169,13 @@ print_file(void) json_t *json = NULL; int error = 0; - file = fopen(filename, "rb"); - if (file == NULL) - return pr_op_err("Cannot open file: %s", strerror(errno)); + if (filename == NULL || strcmp(filename, "-") == 0) { + file = stdin; + } else { + file = fopen(filename, "rb"); + if (file == NULL) + return pr_op_err("Cannot open file: %s", strerror(errno)); + } switch (guess_file_type(file)) { case FT_UNK: @@ -190,7 +194,8 @@ print_file(void) break; } - fclose(file); + if (file != stdin) + fclose(file); if (error) return error; if (json == NULL) -- 2.47.2