Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/gzip make siginfo handler print to /dev/tty.



details:   https://anonhg.NetBSD.org/src/rev/ef988f984887
branches:  trunk
changeset: 359780:ef988f984887
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jan 22 14:00:45 2022 +0000

description:
make siginfo handler print to /dev/tty.

diffstat:

 usr.bin/gzip/gzip.c |  47 ++++++++++++++++++++++++++++++++++-------------
 1 files changed, 34 insertions(+), 13 deletions(-)

diffs (93 lines):

diff -r 0b1124d11da0 -r ef988f984887 usr.bin/gzip/gzip.c
--- a/usr.bin/gzip/gzip.c       Sat Jan 22 13:25:55 2022 +0000
+++ b/usr.bin/gzip/gzip.c       Sat Jan 22 14:00:45 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gzip.c,v 1.117 2021/06/24 07:16:49 simonb Exp $        */
+/*     $NetBSD: gzip.c,v 1.118 2022/01/22 14:00:45 christos Exp $      */
 
 /*
  * Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008, 2009, 2010, 2011, 2015, 2017
@@ -31,7 +31,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1997, 1998, 2003, 2004, 2006, 2008,\
  2009, 2010, 2011, 2015, 2017 Matthew R. Green.  All rights reserved.");
-__RCSID("$NetBSD: gzip.c,v 1.117 2021/06/24 07:16:49 simonb Exp $");
+__RCSID("$NetBSD: gzip.c,v 1.118 2022/01/22 14:00:45 christos Exp $");
 #endif /* not lint */
 
 /*
@@ -65,6 +65,7 @@
 #include <stdarg.h>
 #include <getopt.h>
 #include <time.h>
+#include <paths.h>
 
 #ifndef PRIdOFF
 #define PRIdOFF PRId64
@@ -1708,19 +1709,39 @@
 static void
 check_siginfo(void)
 {
+       static int ttyfd = -2;
+       char buf[2048];
+       int n;
+
        if (print_info == 0)
                return;
-       if (infile) {
-               if (infile_total) {
-                       int pcent = (int)((100.0 * infile_current) / infile_total);
+
+       if (!infile)
+               goto out;
+
+       if (ttyfd == -2)
+               ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC);
+
+       if (ttyfd == -1)
+               goto out;
+
+       if (infile_total) {
+               const double pcent = (100.0 * infile_current) / infile_total;
 
-                       fprintf(stderr, "%s: done %llu/%llu bytes %d%%\n",
-                               infile, (unsigned long long)infile_current,
-                               (unsigned long long)infile_total, pcent);
-               } else
-                       fprintf(stderr, "%s: done %llu bytes\n",
-                               infile, (unsigned long long)infile_current);
+               n = snprintf(buf, sizeof(buf),
+                   "%s: %s: done %ju/%ju bytes (%3.2f%%)\n",
+                   getprogname(), infile, (uintmax_t)infile_current,
+                   (uintmax_t)infile_total, pcent);
+       } else {
+               n = snprintf(buf, sizeof(buf), "%s: %s: done %ju bytes\n",
+                   getprogname(), infile, (uintmax_t)infile_current);
        }
+
+       if (n <= 0)
+               goto out;
+
+       write(ttyfd, buf, (size_t)n);
+out:
        print_info = 0;
 }
 
@@ -1883,7 +1904,7 @@
        uint32_t mtime;
        int ret;
 
-       infile_set("(stdout)", 0);
+       infile_set("<stdout>", 0);
 
        if (fflag == 0 && isatty(STDOUT_FILENO)) {
                maybe_warnx("standard output is a terminal -- ignoring");
@@ -1898,7 +1919,7 @@
        }
 
        if (S_ISREG(sb.st_mode)) {
-               infile_set("(stdout)", sb.st_size);
+               infile_set("<stdout>", sb.st_size);
                mtime = (uint32_t)sb.st_mtime;
        } else {
                systime = time(NULL);



Home | Main Index | Thread Index | Old Index