From: Alberto Leiva Popper Date: Mon, 6 May 2024 22:10:14 +0000 (-0600) Subject: Allow --mode=print to read file from stdin X-Git-Tag: 1.6.2~28 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=70921b8e9b66021c38020ab1aad8d61b3634fdf0;p=thirdparty%2FFORT-validator.git Allow --mode=print to read file from stdin Either works: $ fort --mode=print < cert.cer $ fort --mode=print - < cert.cer Progress for #122. --- 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)