cp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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[] = "pdRfiarP";
  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. flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
  75. if (optind + 2 > argc) {
  76. bb_show_usage();
  77. }
  78. last = argv[argc - 1];
  79. argv += optind;
  80. /* If there are only two arguments and... */
  81. if (optind + 2 == argc) {
  82. s_flags = cp_mv_stat2(*argv, &source_stat,
  83. (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
  84. if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
  85. exit(EXIT_FAILURE);
  86. }
  87. /* ...if neither is a directory or... */
  88. if ( !((s_flags | d_flags) & 2) ||
  89. /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
  90. /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
  91. /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
  92. ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
  93. ) {
  94. /* ...do a simple copy. */
  95. dest = last;
  96. goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
  97. }
  98. }
  99. do {
  100. dest = concat_path_file(last, bb_get_last_path_component(*argv));
  101. DO_COPY:
  102. if (copy_file(*argv, dest, flags) < 0) {
  103. status = 1;
  104. }
  105. if (*++argv == last) {
  106. break;
  107. }
  108. free((void *) dest);
  109. } while (1);
  110. exit(status);
  111. }