commit 02918a46e88a521847664bfa91dc09cb91bc6f19
Author: sin <[email protected]>
Date:   Mon May 5 14:58:14 2014 +0100

    Implement cp -f

diff --git a/cp.1 b/cp.1
index 0f092c4..9940046 100644
--- a/cp.1
+++ b/cp.1
@@ -17,6 +17,9 @@ copies a given file, naming it the given name.  If multiple 
files are listed
 they will be copied into the given directory.
 .SH OPTIONS
 .TP
+.B \-f
+if an existing destination file cannot be opened, remove it and try again.
+.TP
 .B \-R
 equivalent to -r.
 .TP
diff --git a/cp.c b/cp.c
index fec7aa7..7ae6fac 100644
--- a/cp.c
+++ b/cp.c
@@ -8,7 +8,7 @@
 static void
 usage(void)
 {
-       eprintf("usage: %s [-Rr] source... dest
", argv0);
+       eprintf("usage: %s [-fRr] source... dest
", argv0);
 }
 
 int
@@ -17,6 +17,9 @@ main(int argc, char *argv[])
        struct stat st;
 
        ARGBEGIN {
+       case 'f':
+               cp_fflag = true;
+               break;
        case 'R':
        case 'r':
                cp_rflag = true;
diff --git a/fs.h b/fs.h
index c034616..6b46434 100644
--- a/fs.h
+++ b/fs.h
@@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <stdbool.h>
 
+extern bool cp_fflag;
 extern bool cp_rflag;
 extern bool rm_fflag;
 extern bool rm_rflag;
diff --git a/util/cp.c b/util/cp.c
index 9aa637d..471a89b 100644
--- a/util/cp.c
+++ b/util/cp.c
@@ -12,6 +12,7 @@
 #include "../text.h"
 #include "../util.h"
 
+bool cp_fflag = false;
 bool cp_rflag = false;
 
 int
@@ -62,8 +63,12 @@ cp(const char *s1, const char *s2)
        if(!(f1 = fopen(s1, "r")))
                eprintf("fopen %s:", s1);
 
-       if(!(f2 = fopen(s2, "w")))
-               eprintf("fopen %s:", s2);
+       if(!(f2 = fopen(s2, "w"))) {
+               if (cp_fflag == true)
+                       unlink(s2);
+               if (!(f2 = fopen(s2, "w")))
+                       eprintf("fopen %s:", s2);
+       }
 
        concat(f1, s1, f2, s2);
        fchmod(fileno(f2), st.st_mode);
diff --git a/util/fnck.c b/util/fnck.c
index 37da996..84123bc 100644
--- a/util/fnck.c
+++ b/util/fnck.c
@@ -18,4 +18,3 @@ fnck(const char *a, const char *b, int (*fn)(const char *, 
const char *))
        if(fn(a, b) == -1)
                eprintf("%s -> %s:", a, b);
 }
-


Reply via email to