From d01338eb3d30e5634f1b4d4179c229f54eea0b44 Mon Sep 17 00:00:00 2001 From: Kamil Dudka Date: Tue, 13 Jan 2009 18:35:00 +0100 Subject: cp/mv: add --no-clobber (-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. --- src/cp.c | 24 ++++++++++++++++++++---- src/mv.c | 17 +++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/cp.c b/src/cp.c index 8e349658a..191f73e7b 100644 --- a/src/cp.c +++ b/src/cp.c @@ -1,5 +1,5 @@ /* cp.c -- file copying (main routines) - Copyright (C) 89, 90, 91, 1995-2008 Free Software Foundation, Inc. + Copyright (C) 89, 90, 91, 1995-2009 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 @@ -116,6 +116,7 @@ static struct option const long_opts[] = {"force", no_argument, NULL, 'f'}, {"interactive", no_argument, NULL, 'i'}, {"link", no_argument, NULL, 'l'}, + {"no-clobber", 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'}, @@ -167,8 +168,10 @@ Mandatory arguments to long options are mandatory for short options too.\n\ "), stdout); fputs (_("\ -f, --force if an existing destination file cannot be\n\ - opened, remove it and try again\n\ - -i, --interactive prompt before overwrite\n\ + opened, remove it and try again (redundant if\n\ + the -n option is used)\n\ + -i, --interactive prompt before overwrite (overrides a previous -n\n\ + option)\n\ -H follow command-line symbolic links in SOURCE\n\ "), stdout); fputs (_("\ @@ -176,6 +179,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-clobber do not overwrite an existing file (overrides\n\ + a previous -i option)\n\ -P, --no-dereference never follow symbolic links in SOURCE\n\ "), stdout); fputs (_("\ @@ -894,7 +899,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) { @@ -950,6 +955,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; @@ -1050,6 +1059,13 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } + if (make_backups && x.interactive == I_ALWAYS_NO) + { + error (0, 0, + _("options --backup and --no-clobber are mutually exclusive")); + usage (EXIT_FAILURE); + } + if (backup_suffix_string) simple_backup_suffix = xstrdup (backup_suffix_string); diff --git a/src/mv.c b/src/mv.c index 98309a412..a5ab95dcf 100644 --- a/src/mv.c +++ b/src/mv.c @@ -1,5 +1,5 @@ /* mv -- move or rename files - Copyright (C) 86, 89, 90, 91, 1995-2008 Free Software Foundation, Inc. + Copyright (C) 86, 89, 90, 91, 1995-2009 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 @@ -62,6 +62,7 @@ static struct option const long_options[] = {"backup", optional_argument, NULL, 'b'}, {"force", no_argument, NULL, 'f'}, {"interactive", no_argument, NULL, 'i'}, + {"no-clobber", no_argument, NULL, 'n'}, {"no-target-directory", no_argument, NULL, 'T'}, {"strip-trailing-slashes", no_argument, NULL, STRIP_TRAILING_SLASHES_OPTION}, {"suffix", required_argument, NULL, 'S'}, @@ -296,6 +297,8 @@ Mandatory arguments to long options are mandatory for short options too.\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\ + -n, --no-clobber do not overwrite an existing file\n\ +If you specify more than one of -i, -f, -n, only the final one takes effect.\n\ "), stdout); fputs (_("\ --strip-trailing-slashes remove any trailing slashes from each SOURCE\n\ @@ -358,7 +361,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) @@ -374,6 +377,9 @@ main (int argc, char **argv) case 'i': x.interactive = I_ASK_USER; break; + case 'n': + x.interactive = I_ALWAYS_NO; + break; case STRIP_TRAILING_SLASHES_OPTION: remove_trailing_slashes = true; break; @@ -446,6 +452,13 @@ main (int argc, char **argv) quote (file[n_files - 1])); } + if (make_backups && x.interactive == I_ALWAYS_NO) + { + error (0, 0, + _("options --backup and --no-clobber are mutually exclusive")); + usage (EXIT_FAILURE); + } + if (backup_suffix_string) simple_backup_suffix = xstrdup (backup_suffix_string); -- cgit v1.2.3-70-g09d2