summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-21 23:40:20 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-21 23:40:20 +0000
commitf24e1f40e032e99cf57a05730e7632b825d3016d (patch)
treea6d0fa74d59e67d1570c54157f5ebcc47b4522d0 /coreutils
parent8d73c35916cbae67f5d0269b128de40f9992ddc6 (diff)
downloadbusybox-w32-f24e1f40e032e99cf57a05730e7632b825d3016d.tar.gz
busybox-w32-f24e1f40e032e99cf57a05730e7632b825d3016d.tar.bz2
busybox-w32-f24e1f40e032e99cf57a05730e7632b825d3016d.zip
cp: add support for -s, -l. Fix free(nonmalloc) bug.
Add doc on POSIX's rules on -i and -f (insane!). ln: make "ln dangling_symlink new_link" work.
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/cp.c72
-rw-r--r--coreutils/libcoreutils/cp_mv_stat.c4
-rw-r--r--coreutils/ln.c43
-rw-r--r--coreutils/mv.c13
4 files changed, 58 insertions, 74 deletions
diff --git a/coreutils/cp.c b/coreutils/cp.c
index fabfe58e0..47ad85ecf 100644
--- a/coreutils/cp.c
+++ b/coreutils/cp.c
@@ -7,8 +7,6 @@
7 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. 7 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
8 */ 8 */
9 9
10/* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
11/* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
12/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */ 10/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
13 11
14/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) 12/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
@@ -16,15 +14,6 @@
16 * Size reduction. 14 * Size reduction.
17 */ 15 */
18 16
19#include <sys/types.h>
20#include <sys/stat.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <utime.h>
24#include <errno.h>
25#include <dirent.h>
26#include <stdlib.h>
27#include <assert.h>
28#include "busybox.h" 17#include "busybox.h"
29#include "libcoreutils/coreutils.h" 18#include "libcoreutils/coreutils.h"
30 19
@@ -38,31 +27,26 @@ int cp_main(int argc, char **argv)
38 int d_flags; 27 int d_flags;
39 int flags; 28 int flags;
40 int status = 0; 29 int status = 0;
30 enum {
31 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
32 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
33 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
34 OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
35 OPT_L = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
36 };
41 37
42 flags = getopt32(argc, argv, "pdRfiarPHL"); 38 // Soft- and hardlinking don't mix
43 39 // -P and -d are the same (-P is POSIX, -d is GNU)
44 if (flags & 32) { 40 // -r and -R are the same
45 flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE); 41 // -a = -pdR
46 } 42 opt_complementary = "?:l--s:s--l:Pd:rR:apdR";
47 if (flags & 64) { 43 flags = getopt32(argc, argv, FILEUTILS_CP_OPTSTR "arPHL");
48 /* Make -r a synonym for -R,
49 * -r was marked as obsolete in SUSv3, but is included for compatibility
50 */
51 flags |= FILEUTILS_RECUR;
52 }
53 if (flags & 128) {
54 /* Make -P a synonym for -d,
55 * -d is the GNU option while -P is the POSIX 2003 option
56 */
57 flags |= FILEUTILS_DEREFERENCE;
58 }
59 /* Default behavior of cp is to dereference, so we don't have to do 44 /* Default behavior of cp is to dereference, so we don't have to do
60 * anything special when we are given -L. 45 * anything special when we are given -L.
61 * The behavior of -H is *almost* like -L, but not quite, so let's 46 * The behavior of -H is *almost* like -L, but not quite, so let's
62 * just ignore it too for fun. 47 * just ignore it too for fun.
63 if (flags & 256 || flags & 512) { 48 if (flags & OPT_L) ...
64 ; 49 if (flags & OPT_H) ... // deref command-line params only
65 }
66 */ 50 */
67 51
68 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */ 52 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
@@ -78,33 +62,31 @@ int cp_main(int argc, char **argv)
78 if (optind + 2 == argc) { 62 if (optind + 2 == argc) {
79 s_flags = cp_mv_stat2(*argv, &source_stat, 63 s_flags = cp_mv_stat2(*argv, &source_stat,
80 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); 64 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
81 if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) { 65 if (s_flags < 0)
82 exit(EXIT_FAILURE); 66 return EXIT_FAILURE;
83 } 67 d_flags = cp_mv_stat(last, &dest_stat);
68 if (d_flags < 0)
69 return EXIT_FAILURE;
70
84 /* ...if neither is a directory or... */ 71 /* ...if neither is a directory or... */
85 if ( !((s_flags | d_flags) & 2) || 72 if ( !((s_flags | d_flags) & 2) ||
86 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ 73 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
87 /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */ 74 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
88 /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
89 ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
90 ) { 75 ) {
91 /* ...do a simple copy. */ 76 /* ...do a simple copy. */
92 dest = last; 77 dest = xstrdup(last);
93 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */ 78 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
94 } 79 }
95 } 80 }
96 81
97 do { 82 do {
98 dest = concat_path_file(last, bb_get_last_path_component(*argv)); 83 dest = concat_path_file(last, bb_get_last_path_component(*argv));
99 DO_COPY: 84 DO_COPY:
100 if (copy_file(*argv, dest, flags) < 0) { 85 if (copy_file(*argv, dest, flags) < 0) {
101 status = 1; 86 status = 1;
102 } 87 }
103 if (*++argv == last) { 88 free((void*)dest);
104 break; 89 } while (*++argv != last);
105 }
106 free((void *) dest);
107 } while (1);
108 90
109 exit(status); 91 return status;
110} 92}
diff --git a/coreutils/libcoreutils/cp_mv_stat.c b/coreutils/libcoreutils/cp_mv_stat.c
index d401bcc75..0849ebc6c 100644
--- a/coreutils/libcoreutils/cp_mv_stat.c
+++ b/coreutils/libcoreutils/cp_mv_stat.c
@@ -20,8 +20,6 @@
20 * 20 *
21 */ 21 */
22 22
23#include <errno.h>
24#include <sys/stat.h>
25#include "libbb.h" 23#include "libbb.h"
26#include "coreutils.h" 24#include "coreutils.h"
27 25
@@ -29,7 +27,7 @@ int cp_mv_stat2(const char *fn, struct stat *fn_stat, stat_func sf)
29{ 27{
30 if (sf(fn, fn_stat) < 0) { 28 if (sf(fn, fn_stat) < 0) {
31 if (errno != ENOENT) { 29 if (errno != ENOENT) {
32 bb_perror_msg("unable to stat `%s'", fn); 30 bb_perror_msg("unable to stat '%s'", fn);
33 return -1; 31 return -1;
34 } 32 }
35 return 0; 33 return 0;
diff --git a/coreutils/ln.c b/coreutils/ln.c
index cd6e470be..231a3bf03 100644
--- a/coreutils/ln.c
+++ b/coreutils/ln.c
@@ -49,36 +49,39 @@ int ln_main(int argc, char **argv)
49 src = last; 49 src = last;
50 50
51 if (is_directory(src, 51 if (is_directory(src,
52 (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, 52 (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
53 NULL)) { 53 NULL)) {
54 src_name = xstrdup(*argv); 54 src_name = xstrdup(*argv);
55 src = concat_path_file(src, bb_get_last_path_component(src_name)); 55 src = concat_path_file(src, bb_get_last_path_component(src_name));
56 free(src_name); 56 free(src_name);
57 src_name = src; 57 src_name = src;
58 } 58 }
59 if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) { 59 if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) {
60 bb_perror_msg("%s", *argv); 60 // coreutils: "ln dangling_symlink new_hardlink" works
61 status = EXIT_FAILURE; 61 if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
62 free(src_name); 62 bb_perror_msg("%s", *argv);
63 continue; 63 status = EXIT_FAILURE;
64 free(src_name);
65 continue;
66 }
64 } 67 }
65 68
66 if (flag & LN_BACKUP) { 69 if (flag & LN_BACKUP) {
67 char *backup; 70 char *backup;
68 backup = xasprintf("%s%s", src, suffix); 71 backup = xasprintf("%s%s", src, suffix);
69 if (rename(src, backup) < 0 && errno != ENOENT) { 72 if (rename(src, backup) < 0 && errno != ENOENT) {
70 bb_perror_msg("%s", src); 73 bb_perror_msg("%s", src);
71 status = EXIT_FAILURE; 74 status = EXIT_FAILURE;
72 free(backup);
73 continue;
74 }
75 free(backup); 75 free(backup);
76 /* 76 continue;
77 * When the source and dest are both hard links to the same 77 }
78 * inode, a rename may succeed even though nothing happened. 78 free(backup);
79 * Therefore, always unlink(). 79 /*
80 */ 80 * When the source and dest are both hard links to the same
81 unlink(src); 81 * inode, a rename may succeed even though nothing happened.
82 * Therefore, always unlink().
83 */
84 unlink(src);
82 } else if (flag & LN_FORCE) { 85 } else if (flag & LN_FORCE) {
83 unlink(src); 86 unlink(src);
84 } 87 }
diff --git a/coreutils/mv.c b/coreutils/mv.c
index 770b42417..52ddd9fb7 100644
--- a/coreutils/mv.c
+++ b/coreutils/mv.c
@@ -57,7 +57,8 @@ int mv_main(int argc, char **argv)
57 argv += optind; 57 argv += optind;
58 58
59 if (optind + 2 == argc) { 59 if (optind + 2 == argc) {
60 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) { 60 dest_exists = cp_mv_stat(last, &dest_stat);
61 if (dest_exists < 0) {
61 return 1; 62 return 1;
62 } 63 }
63 64
@@ -69,8 +70,8 @@ int mv_main(int argc, char **argv)
69 70
70 do { 71 do {
71 dest = concat_path_file(last, bb_get_last_path_component(*argv)); 72 dest = concat_path_file(last, bb_get_last_path_component(*argv));
72 73 dest_exists = cp_mv_stat(dest, &dest_stat);
73 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) { 74 if (dest_exists < 0) {
74 goto RET_1; 75 goto RET_1;
75 } 76 }
76 77
@@ -79,7 +80,7 @@ DO_MOVE:
79 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) && 80 if (dest_exists && !(flags & OPT_FILEUTILS_FORCE) &&
80 ((access(dest, W_OK) < 0 && isatty(0)) || 81 ((access(dest, W_OK) < 0 && isatty(0)) ||
81 (flags & OPT_FILEUTILS_INTERACTIVE))) { 82 (flags & OPT_FILEUTILS_INTERACTIVE))) {
82 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) { 83 if (fprintf(stderr, "mv: overwrite '%s'? ", dest) < 0) {
83 goto RET_1; /* Ouch! fprintf failed! */ 84 goto RET_1; /* Ouch! fprintf failed! */
84 } 85 }
85 if (!bb_ask_confirmation()) { 86 if (!bb_ask_confirmation()) {
@@ -92,7 +93,7 @@ DO_MOVE:
92 93
93 if (errno != EXDEV || 94 if (errno != EXDEV ||
94 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) { 95 (source_exists = cp_mv_stat(*argv, &source_stat)) < 1) {
95 bb_perror_msg("unable to rename `%s'", *argv); 96 bb_perror_msg("cannot rename '%s'", *argv);
96 } else { 97 } else {
97 if (dest_exists) { 98 if (dest_exists) {
98 if (dest_exists == 3) { 99 if (dest_exists == 3) {
@@ -107,7 +108,7 @@ DO_MOVE:
107 } 108 }
108 } 109 }
109 if (unlink(dest) < 0) { 110 if (unlink(dest) < 0) {
110 bb_perror_msg("cannot remove `%s'", dest); 111 bb_perror_msg("cannot remove '%s'", dest);
111 goto RET_1; 112 goto RET_1;
112 } 113 }
113 } 114 }