From d2620c60c54761c09dc679f2db07c296c355436a Mon Sep 17 00:00:00 2001 From: Kirill Primak Date: Wed, 6 Nov 2024 23:38:01 +0300 Subject: [PATCH] main: refuse to dump the result into tty Closes: https://codeberg.org/vyivel/dulcepan/issues/17 --- src/main.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 1f16cdf..660d9e4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -167,8 +168,9 @@ static void help(const char *prog) { " -o Specify the output file path.\n" "\n" "If the output file path is not specified, the resuling image will be printed to\n" - "the standard output. If the output file format is not specified, it is guessed\n" - "from the output file path if it's specified, and assumed to be PNG otherwise.\n" + "the standard output, which is expected to not be a terminal. If the output file\n" + "format is not specified, it is guessed from the output file path if it's\n" + "specified, and assumed to be PNG otherwise.\n" "\n" "Supported formats: png, ppm.\n" "\n" @@ -210,6 +212,14 @@ int main(int argc, char **argv) { } } + if (state.output_path == NULL && isatty(STDOUT_FILENO)) { + fprintf(stderr, + "Refusing to run as the standard output is a terminal and there's no output file\n" + "path specified.\n\n"); + help(argv[0]); + exit(1); + } + if (state.output_format == DP_FILE_UNKNOWN) { if (state.output_path != NULL) { const char *ext = dp_ext_from_path(state.output_path);