cp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
  23. /* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
  24. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
  25. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  26. *
  27. * Size reduction.
  28. */
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <unistd.h>
  32. #include <fcntl.h>
  33. #include <utime.h>
  34. #include <errno.h>
  35. #include <dirent.h>
  36. #include <stdlib.h>
  37. #include <assert.h>
  38. #include "busybox.h"
  39. #include "libcoreutils/coreutils.h"
  40. /* WARNING!! ORDER IS IMPORTANT!! */
  41. static const char cp_opts[] = "pdRfiarPHL";
  42. extern int cp_main(int argc, char **argv)
  43. {
  44. struct stat source_stat;
  45. struct stat dest_stat;
  46. const char *last;
  47. const char *dest;
  48. int s_flags;
  49. int d_flags;
  50. int flags;
  51. int status = 0;
  52. /* Since these are enums, #if tests will not work. So use assert()s. */
  53. assert(FILEUTILS_PRESERVE_STATUS == 1);
  54. assert(FILEUTILS_DEREFERENCE == 2);
  55. assert(FILEUTILS_RECUR == 4);
  56. assert(FILEUTILS_FORCE == 8);
  57. assert(FILEUTILS_INTERACTIVE == 16);
  58. flags = bb_getopt_ulflags(argc, argv, cp_opts);
  59. if (flags & 32) {
  60. flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
  61. }
  62. if (flags & 64) {
  63. /* Make -r a synonym for -R,
  64. * -r was marked as obsolete in SUSv3, but is included for compatability
  65. */
  66. flags |= FILEUTILS_RECUR;
  67. }
  68. if (flags & 128) {
  69. /* Make -P a synonym for -d,
  70. * -d is the GNU option while -P is the POSIX 2003 option
  71. */
  72. flags |= FILEUTILS_DEREFERENCE;
  73. }
  74. /* Default behavior of cp is to dereference, so we don't have to do
  75. * anything special when we are given -L.
  76. * The behavior of -H is *almost* like -L, but not quite, so let's
  77. * just ignore it too for fun.
  78. if (flags & 256 || flags & 512) {
  79. ;
  80. }
  81. */
  82. flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
  83. if (optind + 2 > argc) {
  84. bb_show_usage();
  85. }
  86. last = argv[argc - 1];
  87. argv += optind;
  88. /* If there are only two arguments and... */
  89. if (optind + 2 == argc) {
  90. s_flags = cp_mv_stat2(*argv, &source_stat,
  91. (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
  92. if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
  93. exit(EXIT_FAILURE);
  94. }
  95. /* ...if neither is a directory or... */
  96. if ( !((s_flags | d_flags) & 2) ||
  97. /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
  98. /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
  99. /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
  100. ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
  101. ) {
  102. /* ...do a simple copy. */
  103. dest = last;
  104. goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
  105. }
  106. }
  107. do {
  108. dest = concat_path_file(last, bb_get_last_path_component(*argv));
  109. DO_COPY:
  110. if (copy_file(*argv, dest, flags) < 0) {
  111. status = 1;
  112. }
  113. if (*++argv == last) {
  114. break;
  115. }
  116. free((void *) dest);
  117. } while (1);
  118. exit(status);
  119. }