aboutsummaryrefslogtreecommitdiff
path: root/toys/cp.c
diff options
context:
space:
mode:
authorBryce Fricke <devnull@localhost>2012-02-23 22:08:27 -0500
committerBryce Fricke <devnull@localhost>2012-02-23 22:08:27 -0500
commitbef57ed266cfad82114b5bb16052571035e9fff1 (patch)
tree08e1e32ccb59d9f8c38dd3e63de1ace1487d5386 /toys/cp.c
parent1b7ad01f5e5ead83fbc06ff20248b085a15dc279 (diff)
downloadtoybox-bef57ed266cfad82114b5bb16052571035e9fff1.tar.gz
Implemented -i for cp
Diffstat (limited to 'toys/cp.c')
-rw-r--r--toys/cp.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/toys/cp.c b/toys/cp.c
index fadab319..e9d9d503 100644
--- a/toys/cp.c
+++ b/toys/cp.c
@@ -31,7 +31,7 @@ config CP
#include "toys.h"
#define FLAG_f 1
-#define FLAG_i 2 // todo
+#define FLAG_i 2
#define FLAG_P 4 // todo
#define FLAG_L 8 // todo
#define FLAG_H 16 // todo
@@ -58,6 +58,18 @@ DEFINE_GLOBALS(
void cp_file(char *src, char *dst, struct stat *srcst)
{
int fdout = -1;
+ char overwrite;
+
+ if ((toys.optflags & FLAG_i) && access(dst, R_OK) == 0) {
+ // -i flag is specified and dst file exists.
+ // If user does not confirm, don't copy the file
+ // Ideally I'd use perror here, but it always appends a newline
+ // to the string, resulting in the input prompt being displayed
+ // on the next line.
+ fprintf(stderr, "cp: overwrite '%s'? ", dst);
+ (void)scanf("%c", &overwrite);
+ if (!(overwrite == 'y' || overwrite == 'Y')) return;
+ }
if (toys.optflags & FLAG_v)
printf("'%s' -> '%s'\n", src, dst);