tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: Why no SIGINFO?
Here it is. I just put in from, to, bytes done, original size (total bytes) and
percentage. I'll write similar functionality for mv, too.
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 05:42:59 -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 05:42:59 -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 05:42:59 -0000
@@ -81,6 +81,8 @@
struct stat to_stat, *fs;
int ch, checkch, from_fd, rcount, rval, to_fd, tolnk, wcount;
char *p;
+ char infobuf[160];
+ size_t ptotal = 0;
if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
warn("%s", entp->fts_path);
@@ -220,6 +222,15 @@
rval = 1;
break;
}
+ ptotal += wcount;
+ if (pinfo) {
+ snprintf(infobuf, 160,
+ "%s => %s %zu/%zu bytes %d%% written\n",
+ entp->fts_path, to.p_path,
(size_t)ptotal,
+ (size_t)fs->st_size, (int)(100.0 *
ptotal / fs->st_size));
+ (void)write(STDERR_FILENO, infobuf,
strlen(infobuf));
+ pinfo = 0;
+ }
}
if (rcount < 0) {
warn("%s", entp->fts_path);
@@ -381,6 +392,12 @@
}
void
+progress(int unused)
+{
+ pinfo = 1;
+}
+
+void
usage(void)
{
(void)fprintf(stderr,
Home |
Main Index |
Thread Index |
Old Index