Hello,
proposed patch is attached.
Kamil
On Wednesday 15 October 2008 15:10:58 Jim Meyering wrote:
> Eric Blake <[EMAIL PROTECTED]> wrote:
> > According to Kamil Dudka on 10/15/2008 3:50 AM:
> >> Hello,
> >>
> >> at https://bugs.launchpad.net/ubuntu/+source/coreutils/+bug/229182 is
> >> proposed cp/mv enhancement: -n option to not overwrite target files.
> >> This option is available on FreeBSD cp/mv as complement to options -i,
> >> -f:
> >> http://www.ipnom.com/FreeBSD-Man-Pages/cp.1.html
> >> http://www.ipnom.com/FreeBSD-Man-Pages/mv.1.html
> >>
> >> Do you think it would be useful to implement such enhancement in GNU
> >> coreutils?
> >
> > The mere fact that another implementation provides this extension to
> > Posix is a relatively strong argument for adding it to coreutils. What
> > long option name would you give it? Care to write a patch for review?
>
> The timing is good, too, now that --reply=... is nearly gone.
> BTW, I've just noticed that while I removed --reply= documentation
> from --help, I didn't remove it from coreutils.texi.
> This completes the job:
>
> From 555c219d9eff5b0b15be7c479a8efc81f59b0ffb Mon Sep 17 00:00:00 2001
> From: Jim Meyering <[EMAIL PROTECTED]>
> Date: Wed, 15 Oct 2008 15:07:14 +0200
> Subject: [PATCH] doc: cp, mv: remove mention of deprecated --reply= option
>
> * doc/coreutils.texi (cp invocation, mv invocation): Don't mention it.
> ---
> doc/coreutils.texi | 12 ------------
> 1 files changed, 0 insertions(+), 12 deletions(-)
>
> diff --git a/doc/coreutils.texi b/doc/coreutils.texi
> index b7e044d..183e070 100644
> --- a/doc/coreutils.texi
> +++ b/doc/coreutils.texi
> @@ -7404,13 +7404,6 @@ cp invocation
> copies the file @file{a/b/c} to @file{existing_dir/a/b/c}, creating
> any missing intermediate directories.
>
> [EMAIL PROTECTED] The --reply option was deprecated in 2005, and is scheduled
> to
> [EMAIL PROTECTED] be removed in 2008. It is already missing from the --help
> output.
> [EMAIL PROTECTED] @[EMAIL PROTECTED]@var{how}}
> [EMAIL PROTECTED] --reply
> [EMAIL PROTECTED] interactivity
> -This option is deprecated.
> -
> @item -R
> @itemx -r
> @itemx --recursive
> @@ -8095,11 +8088,6 @@ mv invocation
> of its permissions.
> If the response is not affirmative, the file is skipped.
>
> [EMAIL PROTECTED] @[EMAIL PROTECTED]@var{how}}
> [EMAIL PROTECTED] --reply
> [EMAIL PROTECTED] interactivity
> -This option is deprecated.
> -
> @item -u
> @itemx --update
> @opindex -u
> --
> 1.6.0.2.526.g5c283
>
>
> _______________________________________________
> Bug-coreutils mailing list
> [email protected]
> http://lists.gnu.org/mailman/listinfo/bug-coreutils
From fd6f9c3485d847776cd23e1a289a12d57aa1316f Mon Sep 17 00:00:00 2001
From: Kamil Dudka <[EMAIL PROTECTED]>
Date: Mon, 27 Oct 2008 15:08:14 +0100
Subject: [PATCH] cp/mv: add -n option to not overwrite target
* src/cp.c (usage): Show new option -n in --help.
(main): Handle new option -n.
* src/mv.c (usage): Show new option -n in --help.
(main): Handle new option -n.
* doc/coreutils.texi: Document new cp/mv option -n.
* tests/cp/cp-i: Add tests for -f, -i and -n options.
* tests/mv/mv-n: New test for mv -n.
* tests/Makefile.am: Add test mv/mv-n to the list.
* NEWS: Mention the change.
---
NEWS | 2 +
doc/coreutils.texi | 23 ++++++++++++++++++-
src/cp.c | 12 +++++++++-
src/mv.c | 15 ++++++++++--
tests/Makefile.am | 1 +
tests/cp/cp-i | 33 +++++++++++++++++++++++++++++
tests/mv/mv-n | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 137 insertions(+), 7 deletions(-)
create mode 100755 tests/mv/mv-n
diff --git a/NEWS b/NEWS
index 79d5148..b02af7b 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,8 @@ GNU coreutils NEWS -*- outline -*-
stat -f recognizes the Lustre file system type
+ cp/mv new option -n to not overwrite target
+
** Bug fixes
seq 9223372036854775807 9223372036854775808 now prints only two numbers
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 2d11388..6037ad4 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -7318,7 +7318,8 @@ via recursive traversal.
@opindex -i
@opindex --interactive
When copying a file other than a directory, prompt whether to
-overwrite an existing destination file.
+overwrite an existing destination file. The @option{-i} option overrides
+previous @option{-n} option.
@item -l
@itemx --link
@@ -7332,6 +7333,13 @@ Make hard links instead of copies of non-directories.
@opindex --dereference
Follow symbolic links when copying from them.
[EMAIL PROTECTED] -n
[EMAIL PROTECTED] --no-replace
[EMAIL PROTECTED] -n
[EMAIL PROTECTED] --no-replace
+Do not overwrite an existing file. The @option{-n} option overrides previous
[EMAIL PROTECTED] option.
+
@item -P
@itemx --no-dereference
@opindex -P
@@ -8079,7 +8087,8 @@ The program accepts the following options. Also see @ref{Common options}.
@opindex -f
@opindex --force
@cindex prompts, omitting
-Do not prompt the user before removing a destination file.
+Do not prompt the user before removing a destination file. The @option{-f}
+option overrides any previous @option{-i} or @option{-n} options.
@item -i
@itemx --interactive
@@ -8089,6 +8098,16 @@ Do not prompt the user before removing a destination file.
Prompt whether to overwrite each existing destination file, regardless
of its permissions.
If the response is not affirmative, the file is skipped.
+The @option{-i} option overrides any previous @option{-f} or @option{-n}
+options.
+
[EMAIL PROTECTED] -n
[EMAIL PROTECTED] --no-replace
[EMAIL PROTECTED] -n
[EMAIL PROTECTED] --no-replace
[EMAIL PROTECTED] prompts, omitting
+Do not overwrite an existing file. The @option{-n} option overrides any
+previous @option{-f} or @option{-i} options.
@item -u
@itemx --update
diff --git a/src/cp.c b/src/cp.c
index 95eba0c..3b93dcf 100644
--- a/src/cp.c
+++ b/src/cp.c
@@ -129,6 +129,7 @@ static struct option const long_opts[] =
{"force", no_argument, NULL, 'f'},
{"interactive", no_argument, NULL, 'i'},
{"link", no_argument, NULL, 'l'},
+ {"no-replace", no_argument, NULL, 'n'},
{"no-dereference", no_argument, NULL, 'P'},
{"no-preserve", required_argument, NULL, NO_PRESERVE_ATTRIBUTES_OPTION},
{"no-target-directory", no_argument, NULL, 'T'},
@@ -183,7 +184,8 @@ Mandatory arguments to long options are mandatory for short options too.\n\
fputs (_("\
-f, --force if an existing destination file cannot be\n\
opened, remove it and try again\n\
- -i, --interactive prompt before overwrite\n\
+ -i, --interactive prompt before overwrite (The -i option\n\
+ overrides previous -n option.)\n\
-H follow command-line symbolic links in SOURCE\n\
"), stdout);
fputs (_("\
@@ -191,6 +193,8 @@ Mandatory arguments to long options are mandatory for short options too.\n\
-L, --dereference always follow symbolic links in SOURCE\n\
"), stdout);
fputs (_("\
+ -n, --no-replace do not overwrite an existing file (The -n\n\
+ option overrides previous -i option.)\n\
-P, --no-dereference never follow symbolic links in SOURCE\n\
"), stdout);
fputs (_("\
@@ -909,7 +913,7 @@ main (int argc, char **argv)
we'll actually use backup_suffix_string. */
backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
- while ((c = getopt_long (argc, argv, "abdfHilLprst:uvxPRS:T",
+ while ((c = getopt_long (argc, argv, "abdfHilLnprst:uvxPRS:T",
long_opts, NULL))
!= -1)
{
@@ -965,6 +969,10 @@ main (int argc, char **argv)
x.dereference = DEREF_ALWAYS;
break;
+ case 'n':
+ x.interactive = I_ALWAYS_NO;
+ break;
+
case 'P':
x.dereference = DEREF_NEVER;
break;
diff --git a/src/mv.c b/src/mv.c
index fc255f3..bfdf286 100644
--- a/src/mv.c
+++ b/src/mv.c
@@ -76,6 +76,7 @@ static struct option const long_options[] =
{"backup", optional_argument, NULL, 'b'},
{"force", no_argument, NULL, 'f'},
{"interactive", no_argument, NULL, 'i'},
+ {"no-replace", no_argument, NULL, 'n'},
{"no-target-directory", no_argument, NULL, 'T'},
{"reply", required_argument, NULL, REPLY_OPTION}, /* Deprecated 2005-07-03,
remove in 2008. */
@@ -310,8 +311,13 @@ Mandatory arguments to long options are mandatory for short options too.\n\
fputs (_("\
--backup[=CONTROL] make a backup of each existing destination file\n\
-b like --backup but does not accept an argument\n\
- -f, --force do not prompt before overwriting\n\
- -i, --interactive prompt before overwrite\n\
+ -f, --force do not prompt before overwriting (The -f option\n\
+ overrides any previous -i or -n options.)\n\
+ -i, --interactive prompt before overwrite (The -i option\n\
+ overrides any previous -f or -n options.)\n\
+ -n, --no-replace do not overwrite an existing file (The -n\n\
+ option overrides any previous -f or -i\n\
+ options.)\n\
"), stdout);
fputs (_("\
--strip-trailing-slashes remove any trailing slashes from each SOURCE\n\
@@ -374,7 +380,7 @@ main (int argc, char **argv)
we'll actually use backup_suffix_string. */
backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
- while ((c = getopt_long (argc, argv, "bfit:uvS:T", long_options, NULL))
+ while ((c = getopt_long (argc, argv, "bfint:uvS:T", long_options, NULL))
!= -1)
{
switch (c)
@@ -390,6 +396,9 @@ main (int argc, char **argv)
case 'i':
x.interactive = I_ASK_USER;
break;
+ case 'n':
+ x.interactive = I_ALWAYS_NO;
+ break;
case REPLY_OPTION: /* Deprecated */
x.interactive = XARGMATCH ("--reply", optarg,
reply_args, reply_vals);
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 808c61e..a15c2b0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -376,6 +376,7 @@ TESTS = \
mv/into-self-3 \
mv/into-self-4 \
mv/leak-fd \
+ mv/mv-n \
mv/mv-special-1 \
mv/no-target-dir \
mv/part-fail \
diff --git a/tests/cp/cp-i b/tests/cp/cp-i
index 94284be..406d10a 100755
--- a/tests/cp/cp-i
+++ b/tests/cp/cp-i
@@ -31,4 +31,37 @@ fail=0
# coreutils 6.2 cp would neglect to prompt in this case.
echo n | cp -iR a b 2>/dev/null || fail=1
+# test miscellaneous combinations of -f -i -n parameters
+touch c d || framework_failure
+echo "\`c' -> \`d'" > out_copy
+> out_empty
+
+# ask for overwrite, answer no
+echo n | LC_ALL=C cp -vi c d 2>/dev/null > out1 || fail=1
+compare out1 out_empty || fail=1
+
+# ask for overwrite, answer yes
+echo y | LC_ALL=C cp -vi c d 2>/dev/null > out2 || fail=1
+compare out2 out_copy || fail=1
+
+# -i wins over -n
+echo y | LC_ALL=C cp -vni c d 2>/dev/null > out3 || fail=1
+compare out3 out_copy || fail=1
+
+# -n wins over -i
+echo y | LC_ALL=C cp -vin c d 2>/dev/null > out4 || fail=1
+compare out4 out_empty || fail=1
+
+# ask for overwrite, answer yes
+echo y | LC_ALL=C cp -vfi c d 2>/dev/null > out5 || fail=1
+compare out5 out_copy || fail=1
+
+# do not ask, prevent from overwrite
+echo n | LC_ALL=C cp -vfn c d 2>/dev/null > out6 || fail=1
+compare out6 out_empty || fail=1
+
+# do not ask, prevent from overwrite
+echo n | LC_ALL=C cp -vnf c d 2>/dev/null > out7 || fail=1
+compare out7 out_empty || fail=1
+
Exit $fail
diff --git a/tests/mv/mv-n b/tests/mv/mv-n
new file mode 100755
index 0000000..52b781d
--- /dev/null
+++ b/tests/mv/mv-n
@@ -0,0 +1,58 @@
+#!/bin/sh
+# Test whether mv -n works as documented (not overwrite target).
+
+# Copyright (C) 2006-2008 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+if test "$VERBOSE" = yes; then
+ set -x
+ mv --version
+fi
+
+. $srcdir/test-lib.sh
+
+fail=0
+
+# test miscellaneous combinations of -f -i -n parameters
+touch a b || framework_failure
+echo "\`a' -> \`b'" > out_move
+> out_empty
+
+# ask for overwrite, answer no
+touch a b || framework_failure
+echo n | LC_ALL=C mv -vi a b 2>/dev/null > out1 || fail=1
+compare out1 out_empty || fail=1
+
+# ask for overwrite, answer yes
+touch a b || framework_failure
+echo y | LC_ALL=C mv -vi a b 2>/dev/null > out2 || fail=1
+compare out2 out_move || fail=1
+
+# -n wins (as the last option)
+touch a b || framework_failure
+echo y | LC_ALL=C mv -vin a b 2>/dev/null > out3 || fail=1
+compare out3 out_empty || fail=1
+
+# -n wins (as the last option)
+touch a b || framework_failure
+echo y | LC_ALL=C mv -vfn a b 2>/dev/null > out4 || fail=1
+compare out4 out_empty || fail=1
+
+# -n wins (as the last option)
+touch a b || framework_failure
+echo y | LC_ALL=C mv -vifn a b 2>/dev/null > out5 || fail=1
+compare out5 out_empty || fail=1
+
+Exit $fail
--
1.5.4.1
_______________________________________________
Bug-coreutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-coreutils