tech-userlevel archive

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

Re: Why no SIGINFO?



Fixed styling issues, also moved the assignment immediately prior to
the loop. This also prevents the assignment from ever happening if the
code that uses it is never reached.

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:58:07 -0000
@@ -70,6 +70,7 @@
 #include <errno.h>
 #include <fts.h>
 #include <locale.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.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:58:07 -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);
 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:58:07 -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;
        
        if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
                warn("%s", entp->fts_path);
@@ -213,6 +214,7 @@
                }

                if (use_read) {
+                       ptotal = 0;
                        while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
                                wcount = write(to_fd, buf, (size_t)rcount);
                                if (rcount != wcount || wcount == -1) {
@@ -220,6 +222,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 +391,12 @@
 }

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


Home | Main Index | Thread Index | Old Index