cp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini cp implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  7. *
  8. * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  9. */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
  11. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  12. *
  13. * Size reduction.
  14. */
  15. #include "libbb.h"
  16. #include "libcoreutils/coreutils.h"
  17. /* This is a NOEXEC applet. Be very careful! */
  18. int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  19. int cp_main(int argc, char **argv)
  20. {
  21. struct stat source_stat;
  22. struct stat dest_stat;
  23. const char *last;
  24. const char *dest;
  25. int s_flags;
  26. int d_flags;
  27. int flags;
  28. int status = 0;
  29. enum {
  30. OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
  31. OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
  32. OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
  33. OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
  34. };
  35. // Need at least two arguments
  36. // Soft- and hardlinking doesn't mix
  37. // -P and -d are the same (-P is POSIX, -d is GNU)
  38. // -r and -R are the same
  39. // -R (and therefore -r) turns on -d (coreutils does this)
  40. // -a = -pdR
  41. opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL";
  42. flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPH");
  43. argc -= optind;
  44. argv += optind;
  45. flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */
  46. /* coreutils 6.9 compat:
  47. * by default, "cp" derefs symlinks (creates regular dest files),
  48. * but "cp -R" does not. We switch off deref if -r or -R (see above).
  49. * However, "cp -RL" must still deref symlinks: */
  50. if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
  51. flags |= FILEUTILS_DEREFERENCE;
  52. /* The behavior of -H is *almost* like -L, but not quite, so let's
  53. * just ignore it too for fun. TODO.
  54. if (flags & OPT_H) ... // deref command-line params only
  55. */
  56. #if ENABLE_SELINUX
  57. if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
  58. selinux_or_die();
  59. }
  60. #endif
  61. last = argv[argc - 1];
  62. /* If there are only two arguments and... */
  63. if (argc == 2) {
  64. s_flags = cp_mv_stat2(*argv, &source_stat,
  65. (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
  66. if (s_flags < 0)
  67. return EXIT_FAILURE;
  68. d_flags = cp_mv_stat(last, &dest_stat);
  69. if (d_flags < 0)
  70. return EXIT_FAILURE;
  71. /* ...if neither is a directory or... */
  72. if ( !((s_flags | d_flags) & 2) ||
  73. /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
  74. ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
  75. ) {
  76. /* ...do a simple copy. */
  77. dest = last;
  78. goto DO_COPY; /* NB: argc==2 -> *++argv==last */
  79. }
  80. }
  81. while (1) {
  82. dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
  83. DO_COPY:
  84. if (copy_file(*argv, dest, flags) < 0) {
  85. status = 1;
  86. }
  87. if (*++argv == last) {
  88. /* possibly leaking dest... */
  89. break;
  90. }
  91. free((void*)dest);
  92. }
  93. /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
  94. return status;
  95. }