cp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
  10. /* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
  12. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  13. *
  14. * Size reduction.
  15. */
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <utime.h>
  21. #include <errno.h>
  22. #include <dirent.h>
  23. #include <stdlib.h>
  24. #include <assert.h>
  25. #include "busybox.h"
  26. #include "libcoreutils/coreutils.h"
  27. int cp_main(int argc, char **argv)
  28. {
  29. struct stat source_stat;
  30. struct stat dest_stat;
  31. const char *last;
  32. const char *dest;
  33. int s_flags;
  34. int d_flags;
  35. int flags;
  36. int status = 0;
  37. flags = bb_getopt_ulflags(argc, argv, "pdRfiarPHL");
  38. if (flags & 32) {
  39. flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
  40. }
  41. if (flags & 64) {
  42. /* Make -r a synonym for -R,
  43. * -r was marked as obsolete in SUSv3, but is included for compatability
  44. */
  45. flags |= FILEUTILS_RECUR;
  46. }
  47. if (flags & 128) {
  48. /* Make -P a synonym for -d,
  49. * -d is the GNU option while -P is the POSIX 2003 option
  50. */
  51. flags |= FILEUTILS_DEREFERENCE;
  52. }
  53. /* Default behavior of cp is to dereference, so we don't have to do
  54. * anything special when we are given -L.
  55. * The behavior of -H is *almost* like -L, but not quite, so let's
  56. * just ignore it too for fun.
  57. if (flags & 256 || flags & 512) {
  58. ;
  59. }
  60. */
  61. flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
  62. if (optind + 2 > argc) {
  63. bb_show_usage();
  64. }
  65. last = argv[argc - 1];
  66. argv += optind;
  67. /* If there are only two arguments and... */
  68. if (optind + 2 == argc) {
  69. s_flags = cp_mv_stat2(*argv, &source_stat,
  70. (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
  71. if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
  72. exit(EXIT_FAILURE);
  73. }
  74. /* ...if neither is a directory or... */
  75. if ( !((s_flags | d_flags) & 2) ||
  76. /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
  77. /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
  78. /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
  79. ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
  80. ) {
  81. /* ...do a simple copy. */
  82. dest = last;
  83. goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
  84. }
  85. }
  86. do {
  87. dest = concat_path_file(last, bb_get_last_path_component(*argv));
  88. DO_COPY:
  89. if (copy_file(*argv, dest, flags) < 0) {
  90. status = 1;
  91. }
  92. if (*++argv == last) {
  93. break;
  94. }
  95. free((void *) dest);
  96. } while (1);
  97. exit(status);
  98. }