From bd005d4e69a42d954e6c9b0c7c3d88726609d665 Mon Sep 17 00:00:00 2001 From: Gandalf <> Date: Thu, 10 Apr 2025 17:39:46 +0000 Subject: [PATCH] cp: add -i flag --- README | 2 +- cp.1 | 6 ++++-- cp.c | 6 +++++- fs.h | 1 + libutil/cp.c | 12 +++++++++++- 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README b/README index 698eae3..fd66095 100644 --- a/README +++ b/README @@ -56,7 +56,7 @@ The following tools are implemented: 0=*|o cmp . 0#*|x cols . 0=*|o comm . -0=*|o cp (-i) +0=*|o cp . 0=*|x cron . 0#*|o cut . 0=*|o date . diff --git a/cp.1 b/cp.1 index 93f2bc0..eabc02e 100644 --- a/cp.1 +++ b/cp.1 @@ -1,4 +1,4 @@ -.Dd October 8, 2015 +.Dd April 15, 2025 .Dt CP 1 .Os sbase .Sh NAME @@ -6,7 +6,7 @@ .Nd copy files and directories .Sh SYNOPSIS .Nm -.Op Fl afpv +.Op Fl afipv .Oo .Fl R .Op Fl H | L | P @@ -37,6 +37,8 @@ and If an existing .Ar dest cannot be opened, remove it and try again. +.It Fl i +Interactive prompt before overwrite. .It Fl p Preserve mode, timestamp and permissions. .It Fl v diff --git a/cp.c b/cp.c index 6abe02c..c6b32f5 100644 --- a/cp.c +++ b/cp.c @@ -7,7 +7,7 @@ static void usage(void) { - eprintf("usage: %s [-afpv] [-R [-H | -L | -P]] source ... dest\n", argv0); + eprintf("usage: %s [-afipv] [-R [-H | -L | -P]] source ... dest\n", argv0); } int @@ -16,6 +16,9 @@ main(int argc, char *argv[]) struct stat st; ARGBEGIN { + case 'i': + cp_iflag = 1; + break; case 'a': cp_follow = 'P'; cp_aflag = cp_pflag = cp_rflag = 1; @@ -58,3 +61,4 @@ main(int argc, char *argv[]) return fshut(stdout, "") || cp_status; } + diff --git a/fs.h b/fs.h index 00ecd3b..f8a9322 100644 --- a/fs.h +++ b/fs.h @@ -28,6 +28,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 23275ac..ae8de22 100644 --- a/libutil/cp.c +++ b/libutil/cp.c @@ -16,6 +16,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; @@ -42,8 +43,17 @@ cp(const char *s1, const char *s2, int depth) return 0; } + if (cp_iflag && access(s2, F_OK) == 0) { + weprintf("overwrite '%s'? ", s2); + int c = getchar(); + if (c != 'y' && c != 'Y') + return 0; + + while (getchar() != '\n'); + } + if (cp_vflag) - printf("%s -> %s\n", s1, s2); + printf("'%s' -> '%s'\n", s1, s2); if (S_ISLNK(st.st_mode)) { if ((r = readlink(s1, target, sizeof(target) - 1)) >= 0) { -- 2.48.1