merge with 3.8.4e
[coreutils.git] / src / mv.c
blob1e858856b9d7e0e6a12050e492baebf2b5cfbb66
1 /* mv -- move or rename files
2 Copyright (C) 1986, 1989, 1990, 1991 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)
7 any later version.
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. */
18 /* Options:
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
24 existing file.
26 -u, --update Do not move a nondirectory that has an
27 existing destination with the same or newer
28 modification time.
30 -v, --verbose List the name of each file as it is moved, and
31 the name it is moved to.
33 -b, --backup
34 -S, --suffix
35 -V, --version-control
36 Backup file creation.
38 Written by Mike Parker and David MacKenzie */
40 #ifdef _AIX
41 #pragma alloca
42 #endif
44 #ifdef HAVE_CONFIG_H
45 #if defined (CONFIG_BROKETS)
46 /* We use <config.h> instead of "config.h" so that a compilation
47 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
48 (which it would do because it found this file in $srcdir). */
49 #include <config.h>
50 #else
51 #include "config.h"
52 #endif
53 #endif
55 #include <stdio.h>
56 #include <getopt.h>
57 #include <sys/types.h>
58 #include "system.h"
59 #include "backupfile.h"
60 #include "version.h"
62 #ifndef _POSIX_VERSION
63 uid_t geteuid ();
64 #endif
66 char *basename ();
67 enum backup_type get_version ();
68 int isdir ();
69 int yesno ();
70 void error ();
71 void strip_trailing_slashes ();
72 int eaccess_stat ();
73 char *stpcpy ();
75 static int copy_reg ();
76 static int do_move ();
77 static int movefile ();
78 static void usage ();
80 /* The name this program was run with. */
81 char *program_name;
83 /* If nonzero, query the user before overwriting files. */
84 static int interactive;
86 /* If nonzero, do not query the user before overwriting unwritable
87 files. */
88 static int override_mode;
90 /* If nonzero, do not move a nondirectory that has an existing destination
91 with the same or newer modification time. */
92 static int update = 0;
94 /* If nonzero, list each file as it is moved. */
95 static int verbose;
97 /* If nonzero, stdin is a tty. */
98 static int stdin_tty;
100 /* This process's effective user ID. */
101 static uid_t myeuid;
103 /* If non-zero, display usage information and exit. */
104 static int show_help;
106 /* If non-zero, print the version on standard output and exit. */
107 static int show_version;
109 static struct option const long_options[] =
111 {"backup", no_argument, NULL, 'b'},
112 {"force", no_argument, NULL, 'f'},
113 {"interactive", no_argument, NULL, 'i'},
114 {"suffix", required_argument, NULL, 'S'},
115 {"update", no_argument, &update, 1},
116 {"verbose", no_argument, &verbose, 1},
117 {"version-control", required_argument, NULL, 'V'},
118 {"help", no_argument, &show_help, 1},
119 {"version", no_argument, &show_version, 1},
120 {NULL, 0, NULL, 0}
123 void
124 main (argc, argv)
125 int argc;
126 char **argv;
128 int c;
129 int errors;
130 int make_backups = 0;
131 char *version;
133 version = getenv ("SIMPLE_BACKUP_SUFFIX");
134 if (version)
135 simple_backup_suffix = version;
136 version = getenv ("VERSION_CONTROL");
137 program_name = argv[0];
138 myeuid = geteuid ();
139 interactive = override_mode = verbose = update = 0;
140 errors = 0;
142 while ((c = getopt_long (argc, argv, "bfiuvS:V:", long_options, (int *) 0))
143 != EOF)
145 switch (c)
147 case 0:
148 break;
149 case 'b':
150 make_backups = 1;
151 break;
152 case 'f':
153 interactive = 0;
154 override_mode = 1;
155 break;
156 case 'i':
157 interactive = 1;
158 override_mode = 0;
159 break;
160 case 'u':
161 update = 1;
162 break;
163 case 'v':
164 verbose = 1;
165 break;
166 case 'S':
167 simple_backup_suffix = optarg;
168 break;
169 case 'V':
170 version = optarg;
171 break;
172 default:
173 usage (1);
177 if (show_version)
179 printf ("%s\n", version_string);
180 exit (0);
183 if (show_help)
184 usage (0);
186 if (argc < optind + 2)
187 usage (1);
189 if (make_backups)
190 backup_type = get_version (version);
192 stdin_tty = isatty (0);
194 if (argc > optind + 2 && !isdir (argv[argc - 1]))
195 error (1, 0, "when moving multiple files, last argument must be a directory");
197 /* Move each arg but the last onto the last. */
198 for (; optind < argc - 1; ++optind)
199 errors |= movefile (argv[optind], argv[argc - 1]);
201 exit (errors);
204 /* If PATH is an existing directory, return nonzero, else 0. */
206 static int
207 is_real_dir (path)
208 char *path;
210 struct stat stats;
212 return lstat (path, &stats) == 0 && S_ISDIR (stats.st_mode);
215 /* Move file SOURCE onto DEST. Handles the case when DEST is a directory.
216 Return 0 if successful, 1 if an error occurred. */
218 static int
219 movefile (source, dest)
220 char *source;
221 char *dest;
223 strip_trailing_slashes (source);
225 if ((dest[strlen (dest) - 1] == '/' && !is_real_dir (source))
226 || isdir (dest))
228 /* Target is a directory; build full target filename. */
229 char *base;
230 char *new_dest;
232 base = basename (source);
233 new_dest = (char *) alloca (strlen (dest) + 1 + strlen (base) + 1);
234 stpcpy (stpcpy (stpcpy (new_dest, dest), "/"), base);
235 return do_move (source, new_dest);
237 else
238 return do_move (source, dest);
241 static struct stat dest_stats, source_stats;
243 /* Move SOURCE onto DEST. Handles cross-filesystem moves.
244 If DEST is a directory, SOURCE must be also.
245 Return 0 if successful, 1 if an error occurred. */
247 static int
248 do_move (source, dest)
249 char *source;
250 char *dest;
252 char *dest_backup = NULL;
254 if (lstat (source, &source_stats) != 0)
256 error (0, errno, "%s", source);
257 return 1;
260 if (lstat (dest, &dest_stats) == 0)
262 if (source_stats.st_dev == dest_stats.st_dev
263 && source_stats.st_ino == dest_stats.st_ino)
265 error (0, 0, "`%s' and `%s' are the same file", source, dest);
266 return 1;
269 if (S_ISDIR (dest_stats.st_mode))
271 error (0, 0, "%s: cannot overwrite directory", dest);
272 return 1;
275 if (!S_ISDIR (source_stats.st_mode) && update
276 && source_stats.st_mtime <= dest_stats.st_mtime)
277 return 0;
279 if (!override_mode && (interactive || stdin_tty)
280 && eaccess_stat (&dest_stats, W_OK))
282 fprintf (stderr, "%s: replace `%s', overriding mode %04o? ",
283 program_name, dest,
284 (unsigned int) (dest_stats.st_mode & 07777));
285 if (!yesno ())
286 return 0;
288 else if (interactive)
290 fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
291 if (!yesno ())
292 return 0;
295 if (backup_type != none)
297 char *tmp_backup = find_backup_file_name (dest);
298 if (tmp_backup == NULL)
299 error (1, 0, "virtual memory exhausted");
300 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
301 strcpy (dest_backup, tmp_backup);
302 free (tmp_backup);
303 if (rename (dest, dest_backup))
305 if (errno != ENOENT)
307 error (0, errno, "cannot backup `%s'", dest);
308 return 1;
310 else
311 dest_backup = NULL;
315 else if (errno != ENOENT)
317 error (0, errno, "%s", dest);
318 return 1;
321 if (verbose)
322 printf ("%s -> %s\n", source, dest);
324 if (rename (source, dest) == 0)
326 return 0;
329 if (errno != EXDEV)
331 error (0, errno, "cannot move `%s' to `%s'", source, dest);
332 goto un_backup;
335 /* rename failed on cross-filesystem link. Copy the file instead. */
337 if (copy_reg (source, dest))
338 goto un_backup;
340 if (unlink (source))
342 error (0, errno, "cannot remove `%s'", source);
343 return 1;
346 return 0;
348 un_backup:
349 if (dest_backup)
351 if (rename (dest_backup, dest))
352 error (0, errno, "cannot un-backup `%s'", dest);
354 return 1;
357 /* Copy regular file SOURCE onto file DEST.
358 Return 1 if an error occurred, 0 if successful. */
360 static int
361 copy_reg (source, dest)
362 char *source, *dest;
364 int ifd;
365 int ofd;
366 char buf[1024 * 8];
367 int len; /* Number of bytes read into `buf'. */
369 if (!S_ISREG (source_stats.st_mode))
371 error (0, 0, "cannot move `%s' across filesystems: Not a regular file",
372 source);
373 return 1;
376 if (unlink (dest) && errno != ENOENT)
378 error (0, errno, "cannot remove `%s'", dest);
379 return 1;
382 ifd = open (source, O_RDONLY, 0);
383 if (ifd < 0)
385 error (0, errno, "%s", source);
386 return 1;
388 ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC, 0600);
389 if (ofd < 0)
391 error (0, errno, "%s", dest);
392 close (ifd);
393 return 1;
396 while ((len = read (ifd, buf, sizeof (buf))) > 0)
398 int wrote = 0;
399 char *bp = buf;
403 wrote = write (ofd, bp, len);
404 if (wrote < 0)
406 error (0, errno, "%s", dest);
407 close (ifd);
408 close (ofd);
409 unlink (dest);
410 return 1;
412 bp += wrote;
413 len -= wrote;
414 } while (len > 0);
416 if (len < 0)
418 error (0, errno, "%s", source);
419 close (ifd);
420 close (ofd);
421 unlink (dest);
422 return 1;
425 if (close (ifd) < 0)
427 error (0, errno, "%s", source);
428 close (ofd);
429 return 1;
431 if (close (ofd) < 0)
433 error (0, errno, "%s", dest);
434 return 1;
437 /* chown turns off set[ug]id bits for non-root,
438 so do the chmod last. */
440 /* Try to copy the old file's modtime and access time. */
442 struct utimbuf tv;
444 tv.actime = source_stats.st_atime;
445 tv.modtime = source_stats.st_mtime;
446 if (utime (dest, &tv))
448 error (0, errno, "%s", dest);
449 return 1;
453 /* Try to preserve ownership. For non-root it might fail, but that's ok.
454 But root probably wants to know, e.g. if NFS disallows it. */
455 if (chown (dest, source_stats.st_uid, source_stats.st_gid)
456 && (errno != EPERM || myeuid == 0))
458 error (0, errno, "%s", dest);
459 return 1;
462 if (chmod (dest, source_stats.st_mode & 07777))
464 error (0, errno, "%s", dest);
465 return 1;
468 return 0;
471 static void
472 usage (status)
473 int status;
475 fprintf (status == 0 ? stdout : stderr, "\
476 Usage: %s [OPTION]... SOURCE DEST\n\
477 or: %s [OPTION]... SOURCE... DIRECTORY\n\
478 \n",
479 program_name, program_name);
481 if (status == 0)
482 fprintf (stdout, "\
483 -b, --backup make backup before removal\n\
484 -f, --force remove existing destinations, never prompt\n\
485 -i, --interactive prompt before overwrite\n\
486 -u, --update move only older or brand new files\n\
487 -v, --verbose explain what is being done\n\
488 -S, --suffix SUFFIX override the usual backup suffix\n\
489 -V, --version-control WORD override the usual version control\n\
490 --help display this help and exit\n\
491 --version output version information and exit\n\
493 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
494 version control may be set with VERSION_CONTROL, values are:\n\
496 t, numbered make numbered backups\n\
497 nil, existing numbered if numbered backups exist, simple otherwise\n\
498 never, simple always make simple backups \n");
500 else
501 fprintf (stderr, "Try `%s --help' for more information.\n",
502 program_name);
504 exit (status);