mv.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini mv implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  10. *
  11. * Size reduction and improved error checking.
  12. */
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. #include <dirent.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <getopt.h> /* struct option */
  20. #include "busybox.h"
  21. #include "libcoreutils/coreutils.h"
  22. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  23. static const struct option mv_long_options[] = {
  24. { "interactive", 0, NULL, 'i' },
  25. { "force", 0, NULL, 'f' },
  26. { 0, 0, 0, 0 }
  27. };
  28. #endif
  29. #define OPT_FILEUTILS_FORCE 1
  30. #define OPT_FILEUTILS_INTERACTIVE 2
  31. static const char fmt[] = "cannot overwrite %sdirectory with %sdirectory";
  32. int mv_main(int argc, char **argv)
  33. {
  34. struct stat dest_stat;
  35. const char *last;
  36. const char *dest;
  37. unsigned long flags;
  38. int dest_exists;
  39. int status = 0;
  40. #if ENABLE_FEATURE_MV_LONG_OPTIONS
  41. applet_long_options = mv_long_options;
  42. #endif
  43. opt_complementary = "f-i:i-f";
  44. flags = getopt32(argc, argv, "fi");
  45. if (optind + 2 > argc) {
  46. bb_show_usage();
  47. }
  48. last = argv[argc - 1];
  49. argv += optind;
  50. if (optind + 2 == argc) {
  51. dest_exists = cp_mv_stat(last, &dest_stat);
  52. if (dest_exists < 0) {
  53. return 1;
  54. }
  55. if (!(dest_exists & 2)) {
  56. dest = last;
  57. goto DO_MOVE;
  58. }
  59. }
  60. do {
  61. dest = concat_path_file(last, bb_get_last_path_component(*argv));
  62. dest_exists = cp_mv_stat(dest, &dest_stat);
  63. if (dest_exists < 0) {
  64. goto RET_1;
  65. }
  66. DO_MOVE:
  67. if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
  68. ((access(dest, W_OK) < 0 && isatty(0)) ||
  69. (flags & OPT_FILEUTILS_INTERACTIVE))) {
  70. if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
  71. goto RET_1; /* Ouch! fprintf failed! */
  72. }
  73. if (!bb_ask_confirmation()) {
  74. goto RET_0;
  75. }
  76. }
  77. if (rename(*argv, dest) < 0) {
  78. struct stat source_stat;
  79. int source_exists;
  80. if (errno != EXDEV ||
  81. (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
  82. bb_perror_msg("cannot rename '%s'", *argv);
  83. } else {
  84. if (dest_exists) {
  85. if (dest_exists == 3) {
  86. if (source_exists != 3) {
  87. bb_error_msg(fmt, "", "non-");
  88. goto RET_1;
  89. }
  90. } else {
  91. if (source_exists == 3) {
  92. bb_error_msg(fmt, "non-", "");
  93. goto RET_1;
  94. }
  95. }
  96. if (unlink(dest) < 0) {
  97. bb_perror_msg("cannot remove '%s'", dest);
  98. goto RET_1;
  99. }
  100. }
  101. if ((copy_file(*argv, dest,
  102. FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0) &&
  103. (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)) {
  104. goto RET_0;
  105. }
  106. }
  107. RET_1:
  108. status = 1;
  109. }
  110. RET_0:
  111. if (dest != last) {
  112. free((void *) dest);
  113. }
  114. } while (*++argv != last);
  115. return status;
  116. }