tech-userlevel archive

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

Re: Why no SIGINFO?



> Why snprintf and write rather than fprintf (and possibly fflush)?
> You're not executing inside a signal handler at this point (are you? at
> least not the SIGINFO handler), so the only difference I can see is the
> length clipping, and I think that's actually undesirable.
> (Furthermore, if you do stick with snprintf, I'd say the 160 should
> actually be sizeof(infobuf).)

I actually realized this same thing this morning. I originally wrote
the code to be in the handler, then moved it. Here is the much revised
version, let me know if the diff gets screwed up again. I also fixed
the moot cast and potential race condition. Left out the fflush
(shouldn't it be line buffered and flush on the newline? Which needs
to be printed anyways.)


Index: cp.c
===================================================================
RCS file: /cvsroot/src/bin/cp/cp.c,v
retrieving revision 1.57
diff -u -r1.57 cp.c
--- cp.c        18 Aug 2011 08:11:58 -0000      1.57
+++ cp.c        4 Jan 2012 15:00:46 -0000
@@ -74,6 +74,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <signal.h>

 #include "extern.h"

@@ -88,6 +89,7 @@
 uid_t myuid;
 int Hflag, Lflag, Rflag, Pflag, fflag, iflag, lflag, pflag, rflag,
vflag, Nflag;
 mode_t myumask;
+sig_atomic_t pinfo;

 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };

@@ -210,6 +212,9 @@
        /* Set end of argument list for fts(3). */
        argv[argc] = NULL;
        
+       pinfo = 0;
+       (void)signal(SIGINFO, progress);
+       
        /*
         * Cp has two distinct cases:
         *
Index: extern.h
===================================================================
RCS file: /cvsroot/src/bin/cp/extern.h,v
retrieving revision 1.16
diff -u -r1.16 extern.h
--- extern.h    6 Feb 2011 12:37:49 -0000       1.16
+++ extern.h    4 Jan 2012 15:00:46 -0000
@@ -44,6 +44,7 @@
 extern uid_t myuid;
 extern int Rflag, rflag, Hflag, Lflag, Pflag, fflag, iflag, lflag,
pflag, Nflag;
 extern mode_t myumask;
+extern sig_atomic_t pinfo;

 #include <sys/cdefs.h>

@@ -54,6 +55,7 @@
 int    copy_special(struct stat *, int);
 int    set_utimes(const char *, struct stat *);
 int    setfile(struct stat *, int);
+void progress(int unused);
 void   usage(void) __attribute__((__noreturn__));
 __END_DECLS

Index: utils.c
===================================================================
RCS file: /cvsroot/src/bin/cp/utils.c,v
retrieving revision 1.40
diff -u -r1.40 utils.c
--- utils.c     3 Aug 2011 04:11:15 -0000       1.40
+++ utils.c     4 Jan 2012 15:00:46 -0000
@@ -81,6 +81,7 @@
        struct stat to_stat, *fs;
        int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
        char *p;
+       size_t ptotal = 0;
        
        if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
                warn("%s", entp->fts_path);
@@ -220,6 +221,14 @@
                                        rval = 1;
                                        break;
                                }
+                               ptotal += wcount;
+                               if (pinfo) {
+                                       pinfo = 0;
+                                       fprintf(stderr,
+                                       "%s => %s %zu/%zu bytes %d%% written\n",
+                                       entp->fts_path, to.p_path, ptotal,
+                                       (size_t)fs->st_size, (int)((100.0 * 
ptotal) / fs->st_size));
+                               }
                        }
                        if (rcount < 0) {
                                warn("%s", entp->fts_path);
@@ -381,6 +390,12 @@
 }

 void
+progress(int unused)
+{
+       pinfo = 1;
+}      
+
+void
 usage(void)
 {
        (void)fprintf(stderr,


Home | Main Index | Thread Index | Old Index