(copy): Detect (and fail) when creating backup file would destroy source.
authorJim Meyering <jim@meyering.net>
Fri, 16 Feb 1996 05:08:04 +0000 (05:08 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 16 Feb 1996 05:08:04 +0000 (05:08 +0000)
Reported by Martin C. Martin <mcm@cs.cmu.edu>.

src/cp.c

index 84385b6..af32243 100644 (file)
--- a/src/cp.c
+++ b/src/cp.c
@@ -620,6 +620,14 @@ copy (char *src_path, char *dst_path, int new_dst, dev_t device,
              char *tmp_backup = find_backup_file_name (dst_path);
              if (tmp_backup == NULL)
                error (1, 0, _("virtual memory exhausted"));
+             if (strcmp (tmp_backup, src_path) == 0)
+               {
+                 error (0, 0,
+                  _("backing up `%s' would destroy source;  `%s' not copied"),
+                        dst_path, src_path);
+                 return 1;
+
+               }
              dst_backup = (char *) alloca (strlen (tmp_backup) + 1);
              strcpy (dst_backup, tmp_backup);
              free (tmp_backup);