1 /* mv -- move or rename files
2 Copyright (C) 1986, 1989, 1990, 1991, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 -f, --force Assume a 'y' answer to all questions it would
20 normally ask, and not ask the questions.
22 -i, --interactive Require confirmation from the user before
23 performing any move that would destroy an
26 -u, --update Do not move a nondirectory that has an
27 existing destination with the same or newer
30 -v, --verbose List the name of each file as it is moved, and
31 the name it is moved to.
38 Written by Mike Parker and David MacKenzie */
47 #include <sys/types.h>
50 #include "backupfile.h"
54 #ifndef _POSIX_VERSION
59 enum backup_type get_version ();
64 void strip_trailing_slashes ();
68 static int copy_reg ();
69 static int do_move ();
70 static int movefile ();
73 /* The name this program was run with. */
76 /* If nonzero, query the user before overwriting files. */
77 static int interactive;
79 /* If nonzero, do not query the user before overwriting unwritable
81 static int override_mode;
83 /* If nonzero, do not move a nondirectory that has an existing destination
84 with the same or newer modification time. */
85 static int update = 0;
87 /* If nonzero, list each file as it is moved. */
90 /* If nonzero, stdin is a tty. */
93 /* This process's effective user ID. */
96 /* If non-zero, display usage information and exit. */
99 /* If non-zero, print the version on standard output and exit. */
100 static int show_version;
102 static struct option const long_options[] =
104 {"backup", no_argument, NULL, 'b'},
105 {"force", no_argument, NULL, 'f'},
106 {"interactive", no_argument, NULL, 'i'},
107 {"suffix", required_argument, NULL, 'S'},
108 {"update", no_argument, &update, 1},
109 {"verbose", no_argument, &verbose, 1},
110 {"version-control", required_argument, NULL, 'V'},
111 {"help", no_argument, &show_help, 1},
112 {"version", no_argument, &show_version, 1},
123 int make_backups = 0;
126 version = getenv ("SIMPLE_BACKUP_SUFFIX");
128 simple_backup_suffix = version;
129 version = getenv ("VERSION_CONTROL");
130 program_name = argv[0];
132 interactive = override_mode = verbose = update = 0;
135 while ((c = getopt_long (argc, argv, "bfiuvS:V:", long_options, (int *) 0))
160 simple_backup_suffix = optarg;
172 printf ("%s\n", version_string);
179 if (argc < optind + 2)
181 error (0, 0, "missing file argument%s", argc == optind ? "s" : "");
186 backup_type = get_version (version);
188 stdin_tty = isatty (STDIN_FILENO);
190 if (argc > optind + 2 && !isdir (argv[argc - 1]))
191 error (1, 0, "when moving multiple files, last argument must be a directory");
193 /* Move each arg but the last onto the last. */
194 for (; optind < argc - 1; ++optind)
195 errors |= movefile (argv[optind], argv[argc - 1]);
200 /* If PATH is an existing directory, return nonzero, else 0. */
208 return lstat (path, &stats) == 0 && S_ISDIR (stats.st_mode);
211 /* Move file SOURCE onto DEST. Handles the case when DEST is a directory.
212 Return 0 if successful, 1 if an error occurred. */
215 movefile (source, dest)
219 strip_trailing_slashes (source);
221 if ((dest[strlen (dest) - 1] == '/' && !is_real_dir (source))
224 /* Target is a directory; build full target filename. */
228 base = basename (source);
229 /* Remove a (single) trailing slash if there is at least one. */
230 if (dest[strlen (dest) - 1] == '/')
231 dest[strlen (dest) - 1] = '\0';
232 new_dest = (char *) alloca (strlen (dest) + 1 + strlen (base) + 1);
233 stpcpy (stpcpy (stpcpy (new_dest, dest), "/"), base);
234 return do_move (source, new_dest);
237 return do_move (source, dest);
240 static struct stat dest_stats, source_stats;
242 /* Move SOURCE onto DEST. Handles cross-filesystem moves.
243 If DEST is a directory, SOURCE must be also.
244 Return 0 if successful, 1 if an error occurred. */
247 do_move (source, dest)
251 char *dest_backup = NULL;
253 if (lstat (source, &source_stats) != 0)
255 error (0, errno, "%s", source);
259 if (lstat (dest, &dest_stats) == 0)
261 if (source_stats.st_dev == dest_stats.st_dev
262 && source_stats.st_ino == dest_stats.st_ino)
264 error (0, 0, "`%s' and `%s' are the same file", source, dest);
268 if (S_ISDIR (dest_stats.st_mode))
270 error (0, 0, "%s: cannot overwrite directory", dest);
274 if (!S_ISDIR (source_stats.st_mode) && update
275 && source_stats.st_mtime <= dest_stats.st_mtime)
278 if (!override_mode && (interactive || stdin_tty)
279 && eaccess_stat (&dest_stats, W_OK, dest))
281 fprintf (stderr, "%s: replace `%s', overriding mode %04o? ",
283 (unsigned int) (dest_stats.st_mode & 07777));
287 else if (interactive)
289 fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
294 if (backup_type != none)
296 char *tmp_backup = find_backup_file_name (dest);
297 if (tmp_backup == NULL)
298 error (1, 0, "virtual memory exhausted");
299 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
300 strcpy (dest_backup, tmp_backup);
302 if (rename (dest, dest_backup))
306 error (0, errno, "cannot backup `%s'", dest);
314 else if (errno != ENOENT)
316 error (0, errno, "%s", dest);
321 printf ("%s -> %s\n", source, dest);
323 if (rename (source, dest) == 0)
330 error (0, errno, "cannot move `%s' to `%s'", source, dest);
334 /* rename failed on cross-filesystem link. Copy the file instead. */
336 if (copy_reg (source, dest))
341 error (0, errno, "cannot remove `%s'", source);
350 if (rename (dest_backup, dest))
351 error (0, errno, "cannot un-backup `%s'", dest);
356 /* Copy regular file SOURCE onto file DEST.
357 Return 1 if an error occurred, 0 if successful. */
360 copy_reg (source, dest)
366 int len; /* Number of bytes read into `buf'. */
368 if (!S_ISREG (source_stats.st_mode))
370 error (0, 0, "cannot move `%s' across filesystems: Not a regular file",
375 if (unlink (dest) && errno != ENOENT)
377 error (0, errno, "cannot remove `%s'", dest);
381 ifd = open (source, O_RDONLY, 0);
384 error (0, errno, "%s", source);
387 ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC, 0600);
390 error (0, errno, "%s", dest);
395 while ((len = safe_read (ifd, buf, sizeof (buf))) > 0)
397 if (full_write (ofd, buf, len) < 0)
399 error (0, errno, "%s", dest);
408 error (0, errno, "%s", source);
417 error (0, errno, "%s", source);
423 error (0, errno, "%s", dest);
427 /* chown turns off set[ug]id bits for non-root,
428 so do the chmod last. */
430 /* Try to copy the old file's modtime and access time. */
434 tv.actime = source_stats.st_atime;
435 tv.modtime = source_stats.st_mtime;
436 if (utime (dest, &tv))
438 error (0, errno, "%s", dest);
443 /* Try to preserve ownership. For non-root it might fail, but that's ok.
444 But root probably wants to know, e.g. if NFS disallows it. */
445 if (chown (dest, source_stats.st_uid, source_stats.st_gid)
446 && (errno != EPERM || myeuid == 0))
448 error (0, errno, "%s", dest);
452 if (chmod (dest, source_stats.st_mode & 07777))
454 error (0, errno, "%s", dest);
466 fprintf (stderr, "Try `%s --help' for more information.\n",
471 Usage: %s [OPTION]... SOURCE DEST\n\
472 or: %s [OPTION]... SOURCE... DIRECTORY\n\
474 program_name, program_name);
477 -b, --backup make backup before removal\n\
478 -f, --force remove existing destinations, never prompt\n\
479 -i, --interactive prompt before overwrite\n\
480 -u, --update move only older or brand new files\n\
481 -v, --verbose explain what is being done\n\
482 -S, --suffix=SUFFIX override the usual backup suffix\n\
483 -V, --version-control=WORD override the usual version control\n\
484 --help display this help and exit\n\
485 --version output version information and exit\n\
487 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
488 version control may be set with VERSION_CONTROL, values are:\n\
490 t, numbered make numbered backups\n\
491 nil, existing numbered if numbered backups exist, simple otherwise\n\
492 never, simple always make simple backups \n");