]> git.ipfire.org Git - pakfire.git/commitdiff
progressbar: Fallback if output is not a TTY
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 6 Apr 2021 10:48:59 +0000 (10:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 6 Apr 2021 10:48:59 +0000 (10:48 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/progressbar.c

index 3293928f95c0b7d9a04f7d7f9819a8a700e668cc..26b890dd23e5c4ecaa47ed77b305cb033a114ae5 100644 (file)
@@ -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);