Module Name:    src
Committed By:   christos
Date:           Wed Jan  4 15:58:37 UTC 2012

Modified Files:
        src/bin/cp: cp.c extern.h utils.c

Log Message:
Add siginfo support from Daniel Loffgren.


To generate a diff of this commit:
cvs rdiff -u -r1.57 -r1.58 src/bin/cp/cp.c
cvs rdiff -u -r1.16 -r1.17 src/bin/cp/extern.h
cvs rdiff -u -r1.40 -r1.41 src/bin/cp/utils.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/cp/cp.c
diff -u src/bin/cp/cp.c:1.57 src/bin/cp/cp.c:1.58
--- src/bin/cp/cp.c:1.57	Thu Aug 18 04:11:58 2011
+++ src/bin/cp/cp.c	Wed Jan  4 10:58:37 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: cp.c,v 1.57 2011/08/18 08:11:58 manu Exp $ */
+/* $NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $ */
 
 /*
  * Copyright (c) 1988, 1993, 1994
@@ -43,7 +43,7 @@ __COPYRIGHT(
 #if 0
 static char sccsid[] = "@(#)cp.c	8.5 (Berkeley) 4/29/95";
 #else
-__RCSID("$NetBSD: cp.c,v 1.57 2011/08/18 08:11:58 manu Exp $");
+__RCSID("$NetBSD: cp.c,v 1.58 2012/01/04 15:58:37 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -70,6 +70,7 @@ __RCSID("$NetBSD: cp.c,v 1.57 2011/08/18
 #include <errno.h>
 #include <fts.h>
 #include <locale.h>
+#include <signal.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -88,11 +89,18 @@ PATH_T to = { .p_end = to.p_path, .targe
 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 };
 
-int 	main(int, char *[]);
-int 	copy(char *[], enum op, int);
+static int copy(char *[], enum op, int);
+
+static void
+progress(int sig __unused)
+{
+
+	pinfo++;
+}
 
 int
 main(int argc, char *argv[])
@@ -210,6 +218,8 @@ main(int argc, char *argv[])
 	/* Set end of argument list for fts(3). */
 	argv[argc] = NULL;     
 	
+	(void)signal(SIGINFO, progress);
+	
 	/*
 	 * Cp has two distinct cases:
 	 *
@@ -310,7 +320,7 @@ popdne(void)
 	return rv;
 }
 
-int
+static int
 copy(char *argv[], enum op type, int fts_options)
 {
 	struct stat to_stat;

Index: src/bin/cp/extern.h
diff -u src/bin/cp/extern.h:1.16 src/bin/cp/extern.h:1.17
--- src/bin/cp/extern.h:1.16	Sun Feb  6 07:37:49 2011
+++ src/bin/cp/extern.h	Wed Jan  4 10:58:37 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: extern.h,v 1.16 2011/02/06 12:37:49 darcy Exp $ */
+/* $NetBSD: extern.h,v 1.17 2012/01/04 15:58:37 christos Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -44,6 +44,7 @@ extern PATH_T to;
 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>
 

Index: src/bin/cp/utils.c
diff -u src/bin/cp/utils.c:1.40 src/bin/cp/utils.c:1.41
--- src/bin/cp/utils.c:1.40	Wed Aug  3 00:11:15 2011
+++ src/bin/cp/utils.c	Wed Jan  4 10:58:37 2012
@@ -1,4 +1,4 @@
-/* $NetBSD: utils.c,v 1.40 2011/08/03 04:11:15 manu Exp $ */
+/* $NetBSD: utils.c,v 1.41 2012/01/04 15:58:37 christos Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)utils.c	8.3 (Berkeley) 4/1/94";
 #else
-__RCSID("$NetBSD: utils.c,v 1.40 2011/08/03 04:11:15 manu Exp $");
+__RCSID("$NetBSD: utils.c,v 1.41 2012/01/04 15:58:37 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -74,6 +74,22 @@ set_utimes(const char *file, struct stat
     return (0);
 }
 
+struct finfo {
+	const char *from;
+	const char *to;
+	size_t size;
+};
+
+static void
+progress(const struct finfo *fi, size_t written)
+{
+	int pcent = (int)((100.0 * written) / fi->size);
+
+	pinfo = 0;
+	(void)fprintf(stderr, "%s => %s %zu/%zu bytes %d%% written\n",
+	    fi->from, fi->to, written, fi->size, pcent);
+}
+
 int
 copy_file(FTSENT *entp, int dne)
 {
@@ -81,6 +97,7 @@ copy_file(FTSENT *entp, int dne)
 	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);
@@ -164,6 +181,12 @@ copy_file(FTSENT *entp, int dne)
 	 * now if it's empty, so let's not bother.
 	 */
 	if (fs->st_size > 0) {
+		struct finfo fi;
+
+		fi.from = entp->fts_path;
+		fi.to = to.p_path;
+		fi.size = (size_t)fs->st_size;
+
 		/*
 		 * Mmap and write if less than 8M (the limit is so
 		 * we don't totally trash memory on big files).
@@ -203,6 +226,9 @@ copy_file(FTSENT *entp, int dne)
 						break;
 					}
 					remainder -= chunk;
+					ptotal += chunk;
+					if (pinfo)
+						progress(&fi, ptotal);
 				} while (remainder > 0);
 
 				if (munmap(p, fsize) < 0) {
@@ -220,6 +246,9 @@ copy_file(FTSENT *entp, int dne)
 					rval = 1;
 					break;
 				}
+				ptotal += wcount;
+				if (pinfo)
+					progress(&fi, ptotal);
 			}
 			if (rcount < 0) {
 				warn("%s", entp->fts_path);

Reply via email to