mv.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/String.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <stdio.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. int main(int argc, char** argv)
  33. {
  34. if (pledge("stdio rpath wpath cpath fattr", nullptr) < 0) {
  35. perror("pledge");
  36. return 1;
  37. }
  38. // NOTE: The "force" option is a dummy for now, it's just here to silence scripts that use "mv -f"
  39. // In the future, it might be used to cancel out an "-i" interactive option.
  40. bool force = false;
  41. const char* old_path = nullptr;
  42. const char* new_path = nullptr;
  43. Core::ArgsParser args_parser;
  44. args_parser.add_option(force, "Force", "force", 'f');
  45. args_parser.add_positional_argument(old_path, "The file or directory being moved", "source");
  46. args_parser.add_positional_argument(new_path, "destination of the move operation", "destination");
  47. args_parser.parse(argc, argv);
  48. struct stat st;
  49. int rc = lstat(new_path, &st);
  50. if (rc != 0 && errno != ENOENT) {
  51. perror("lstat");
  52. return 1;
  53. }
  54. String combined_new_path;
  55. if (rc == 0 && S_ISDIR(st.st_mode)) {
  56. auto old_basename = LexicalPath(old_path).basename();
  57. combined_new_path = String::format("%s/%s", new_path, old_basename.characters());
  58. new_path = combined_new_path.characters();
  59. }
  60. rc = rename(old_path, new_path);
  61. if (rc < 0) {
  62. perror("rename");
  63. return 1;
  64. }
  65. return 0;
  66. }