[dev][sbase][PATCH] Add -i option to cp command

From: <antenore_AT_simbiosi.org>
Date: Tue, 17 Mar 2015 17:54:42 +0100

Hi All,

As promised find here after a patch that add the -i option to the cp command.
As most of the code comes from OpenBSD, I'm not sure is suitable for suckless (
License/Copyright issues? Style? ).
If you have better ideas I'll try to improve it, otherwise I was thinkoing to
go ahead with mv as well.

Let me know!!!
Thanks in advance
A.G.

---
 cp.c         |  8 +++++++-
 fs.h         |  1 +
 libutil/cp.c | 16 +++++++++++++++-
 3 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/cp.c b/cp.c
index 4bba1b5..4e79d8b 100644
--- a/cp.c
+++ b/cp.c
_AT_@ -1,5 +1,6 @@
 /* See LICENSE file for copyright and license details. */
 #include <sys/stat.h>
+#include <unistd.h>
 #include "fs.h"
 #include "util.h"
_AT_@ -7,7 +8,7 @@
 static void
 usage(void)
 {
-	eprintf("usage: %s [-afpv] [-R [-H | -L | -P]] source ... dest\n", argv0);
+	eprintf("usage: %s [-aifpv] [-R [-H | -L | -P]] source ... dest\n", argv0);
 }
 int
_AT_@ -22,6 +23,11 @@ main(int argc, char *argv[])
 		break;
 	case 'f':
 		cp_fflag = 1;
+		cp_iflag = 0;
+		break;
+	case 'i':
+		cp_iflag = isatty(STDIN_FILENO);
+		cp_fflag = 0;
 		break;
 	case 'p':
 		cp_pflag = 1;
diff --git a/fs.h b/fs.h
index 853996d..6f08c5f 100644
--- a/fs.h
+++ b/fs.h
_AT_@ -21,6 +21,7 @@ enum {
 extern int cp_aflag;
 extern int cp_fflag;
+extern int cp_iflag;
 extern int cp_pflag;
 extern int cp_rflag;
 extern int cp_vflag;
diff --git a/libutil/cp.c b/libutil/cp.c
index 135a23f..621a523 100644
--- a/libutil/cp.c
+++ b/libutil/cp.c
_AT_@ -17,6 +17,7 @@
 int cp_aflag = 0;
 int cp_fflag = 0;
+int cp_iflag = 0;
 int cp_pflag = 0;
 int cp_rflag = 0;
 int cp_vflag = 0;
_AT_@ -35,10 +36,23 @@ cp(const char *s1, const char *s2, int depth)
 	char buf[PATH_MAX];
 	DIR *dp;
 	int r;
+	int c, ch;
 	if (cp_vflag)
 		printf("'%s' -> '%s'\n", s1, s2);
-
+	if ((f2 = fopen(s2, "r"))) {
+		if (cp_iflag) {
+			weprintf("overwrite %s (y/n [n])? ", s2);
+			ch = c = getchar();
+			while (c != '\n' && c != EOF)
+				c = getchar();
+			if (ch != 'Y' && ch != 'y') {
+				cp_status = 1;
+				return 0;
+			}
+		}
+	}
+	fclose(f2);
 	r = (cp_HLPflag == 'P' || (cp_HLPflag == 'H' && depth > 0)) ?
 	    lstat(s1, &st) : stat(s1, &st);
 	if (r < 0) {
--
2.3.2
Received on Tue Mar 17 2015 - 17:54:42 CET

This archive was generated by hypermail 2.3.0 : Tue Mar 17 2015 - 18:00:20 CET