From: Michael Tremer Date: Tue, 6 Apr 2021 10:48:59 +0000 (+0000) Subject: progressbar: Fallback if output is not a TTY X-Git-Tag: 0.9.28~1285^2~442 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9095651712d3d79866aafadf279d6cc8ea9fb69a;p=pakfire.git progressbar: Fallback if output is not a TTY Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/progressbar.c b/src/libpakfire/progressbar.c index 3293928f9..26b890dd2 100644 --- a/src/libpakfire/progressbar.c +++ b/src/libpakfire/progressbar.c @@ -79,11 +79,26 @@ struct pakfire_progressbar { }; static int pakfire_progressbar_update_terminal_size(struct pakfire_progressbar* p) { - struct winsize w; + int fd = fileno(p->terminal.f); + + // Check if output file is a TTY + int r = isatty(fd); + if (r != 1) { + DEBUG(p->pakfire, "Output file descriptor %d is not a TTY: %s\n", + fd, strerror(errno)); + + // Set a default of 80x20 + p->terminal.cols = 80; + p->terminal.rows = 20; - int r = ioctl(fileno(p->terminal.f), TIOCGWINSZ, &w); + return 0; + } + + struct winsize w; + r = ioctl(fd, TIOCGWINSZ, &w); if (r) { ERROR(p->pakfire, "Could not determine terminal size: %s\n", strerror(errno)); + return r; } // Save result @@ -125,11 +140,12 @@ int pakfire_progressbar_create(struct pakfire_progressbar** progressbar, p->pakfire = pakfire_ref(pakfire); p->nrefs = 1; - // Save output file - if (f) - p->terminal.f = f; - else - p->terminal.f = stderr; + // Set output to stderr if nothing was set + if (!f) + f = stderr; + + // Store output + p->terminal.f = f; // Determine terminal size int r = pakfire_progressbar_update_terminal_size(p);