aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>1999-12-29 02:36:29 +0000
committerErik Andersen <andersen@codepoet.org>1999-12-29 02:36:29 +0000
commitf664c004db12cdb3e379c1745351e225c7b92de7 (patch)
tree0eb9f4d1d08bde592fc79ef034990fd5c0788e25
parent2fe08c7afb3ddef42f304e78cb6edfa28e0741ef (diff)
downloadbusybox-w32-f664c004db12cdb3e379c1745351e225c7b92de7.tar.gz
busybox-w32-f664c004db12cdb3e379c1745351e225c7b92de7.tar.bz2
busybox-w32-f664c004db12cdb3e379c1745351e225c7b92de7.zip
Fixed mv so it now does the right thing (same method used in cp). Removed
some cruft from cp. -Erik
-rw-r--r--Changelog3
-rw-r--r--Makefile2
-rw-r--r--coreutils/cp.c9
-rw-r--r--coreutils/mv.c82
-rw-r--r--cp.c9
-rw-r--r--mv.c82
6 files changed, 88 insertions, 99 deletions
diff --git a/Changelog b/Changelog
index 8edf3a62a..8d0b78eeb 100644
--- a/Changelog
+++ b/Changelog
@@ -13,6 +13,9 @@
13 * Fixed a bug where tar would set, and then clear SGID and SUID bits. 13 * Fixed a bug where tar would set, and then clear SGID and SUID bits.
14 * Fixed a bug where tar would not set the user and group on device 14 * Fixed a bug where tar would not set the user and group on device
15 special files. 15 special files.
16 * cp and mv were quite broken when moving directories. I have rewritten
17 them so they should now work as expected.
18
16 19
17 -Erik Andersen 20 -Erik Andersen
18 21
diff --git a/Makefile b/Makefile
index 4215b291b..73aa2fc90 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ BUILDTIME=$(shell date "+%Y%m%d-%H%M")
22 22
23# Comment out the following to make a debuggable build 23# Comment out the following to make a debuggable build
24# Leave this off for production use. 24# Leave this off for production use.
25DODEBUG=true 25DODEBUG=false
26# If you want a static binary, turn this on. I can't think 26# If you want a static binary, turn this on. I can't think
27# of many situations where anybody would ever want it static, 27# of many situations where anybody would ever want it static,
28# but... 28# but...
diff --git a/coreutils/cp.c b/coreutils/cp.c
index 1e10f2868..83460190a 100644
--- a/coreutils/cp.c
+++ b/coreutils/cp.c
@@ -43,7 +43,6 @@ static int preserveFlag = FALSE;
43static const char *srcName; 43static const char *srcName;
44static const char *destName; 44static const char *destName;
45static int destDirFlag = FALSE; 45static int destDirFlag = FALSE;
46static int destExistsFlag = FALSE;
47static int srcDirFlag = FALSE; 46static int srcDirFlag = FALSE;
48 47
49static int fileAction(const char *fileName, struct stat* statbuf) 48static int fileAction(const char *fileName, struct stat* statbuf)
@@ -71,8 +70,6 @@ static int fileAction(const char *fileName, struct stat* statbuf)
71 70
72extern int cp_main(int argc, char **argv) 71extern int cp_main(int argc, char **argv)
73{ 72{
74 struct stat statBuf;
75
76 if (argc < 3) { 73 if (argc < 3) {
77 usage (cp_usage); 74 usage (cp_usage);
78 } 75 }
@@ -106,11 +103,7 @@ extern int cp_main(int argc, char **argv)
106 103
107 104
108 destName = argv[argc - 1]; 105 destName = argv[argc - 1];
109 if (stat(destName, &statBuf) >= 0) { 106 destDirFlag = isDirectory(destName);
110 destExistsFlag = TRUE;
111 if (S_ISDIR(statBuf.st_mode))
112 destDirFlag = TRUE;
113 }
114 107
115 if ((argc > 3) && destDirFlag==FALSE) { 108 if ((argc > 3) && destDirFlag==FALSE) {
116 fprintf(stderr, "%s: not a directory\n", destName); 109 fprintf(stderr, "%s: not a directory\n", destName);
diff --git a/coreutils/mv.c b/coreutils/mv.c
index d0f346196..92c40c9b7 100644
--- a/coreutils/mv.c
+++ b/coreutils/mv.c
@@ -27,7 +27,6 @@
27#include <utime.h> 27#include <utime.h>
28#include <dirent.h> 28#include <dirent.h>
29 29
30
31static const char mv_usage[] = "mv SOURCE DEST\n" 30static const char mv_usage[] = "mv SOURCE DEST\n"
32" or: mv SOURCE... DIRECTORY\n\n" 31" or: mv SOURCE... DIRECTORY\n\n"
33"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"; 32"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n";
@@ -35,26 +34,49 @@ static const char mv_usage[] = "mv SOURCE DEST\n"
35 34
36static const char *srcName; 35static const char *srcName;
37static const char *destName; 36static const char *destName;
38static int dirFlag = FALSE; 37static int destDirFlag = FALSE;
38static int srcDirFlag = FALSE;
39 39
40static int fileAction(const char *fileName, struct stat* statbuf) 40static int fileAction(const char *fileName, struct stat* statbuf)
41{ 41{
42 char newdestName[NAME_MAX]; 42 char newdestName[NAME_MAX];
43 43
44 fprintf(stderr, "srcName='%s' destName='%s'\n", srcName, destName);
45 strcpy(newdestName, destName); 44 strcpy(newdestName, destName);
46 strcat(newdestName, "/"); 45 if ( srcDirFlag == TRUE ) {
47 strcat(newdestName, strstr(fileName, fileName)); 46 strcat(newdestName, strstr(fileName, srcName) + strlen(srcName));
48 fprintf(stderr, "newdestName='%s'\n", newdestName); 47 }
48
49 if (destDirFlag==TRUE && srcDirFlag == FALSE) {
50 if (newdestName[strlen(newdestName)-1] != '/' ) {
51 strcat(newdestName, "/");
52 }
53 strcat(newdestName, srcName);
54 }
55
49 return (copyFile(fileName, newdestName, TRUE, TRUE)); 56 return (copyFile(fileName, newdestName, TRUE, TRUE));
50} 57}
51 58
59static int rmfileAction(const char *fileName, struct stat* statbuf)
60{
61 if (unlink( fileName) < 0 ) {
62 perror( fileName);
63 return ( FALSE);
64 }
65 return ( TRUE);
66}
52 67
53extern int mv_main(int argc, char **argv) 68static int rmdirAction(const char *fileName, struct stat* statbuf)
54{ 69{
55 char newdestName[NAME_MAX]; 70 if (rmdir( fileName) < 0 ) {
56 char *skipName; 71 perror( fileName);
72 return ( FALSE);
73 }
74 return ( TRUE);
75}
57 76
77
78extern int mv_main(int argc, char **argv)
79{
58 if (argc < 3) { 80 if (argc < 3) {
59 usage (mv_usage); 81 usage (mv_usage);
60 } 82 }
@@ -62,45 +84,23 @@ extern int mv_main(int argc, char **argv)
62 argv++; 84 argv++;
63 85
64 destName = argv[argc - 1]; 86 destName = argv[argc - 1];
65 dirFlag = isDirectory(destName); 87 destDirFlag = isDirectory(destName);
66 88
67 if ((argc > 3) && dirFlag==FALSE) { 89 if ((argc > 3) && destDirFlag==FALSE) {
68 fprintf(stderr, "%s: not a directory\n", destName); 90 fprintf(stderr, "%s: not a directory\n", destName);
69 exit (FALSE); 91 exit (FALSE);
70 } 92 }
71 93
72 while (argc-- > 1) { 94 while (argc-- > 1) {
73 srcName = *(argv++); 95 srcName = *(argv++);
74 skipName = strrchr(srcName, '/'); 96 srcDirFlag = isDirectory(srcName);
75 if (skipName) 97 if (recursiveAction(srcName, TRUE, TRUE, FALSE,
76 skipName++; 98 fileAction, fileAction) == FALSE) {
77 strcpy(newdestName, destName); 99 exit( FALSE);
78 if (dirFlag==TRUE) {
79 strcat(newdestName, "/");
80 if ( skipName != NULL)
81 strcat(newdestName, strstr(srcName, skipName));
82 else
83 strcat(newdestName, srcName);
84 } 100 }
85 if (isDirectory(srcName)==TRUE && newdestName[strlen(newdestName)] != '/') { 101 if (recursiveAction(srcName, TRUE, TRUE, TRUE,
86 strcat(newdestName, "/"); 102 rmfileAction, rmdirAction) == FALSE) {
87 createPath(newdestName, 0777); 103 exit( FALSE);
88 fprintf(stderr, "srcName = '%s'\n", srcName);
89 fprintf(stderr, "newdestName = '%s'\n", newdestName);
90 if (recursiveAction(srcName, TRUE, TRUE, FALSE,
91 fileAction, fileAction) == FALSE)
92 {
93 exit( FALSE);
94 }
95 exit( TRUE);
96 } else {
97 if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) {
98 exit( FALSE);
99 }
100 if (unlink (srcName) < 0) {
101 perror (srcName);
102 exit( FALSE);
103 }
104 } 104 }
105 } 105 }
106 exit( TRUE); 106 exit( TRUE);
diff --git a/cp.c b/cp.c
index 1e10f2868..83460190a 100644
--- a/cp.c
+++ b/cp.c
@@ -43,7 +43,6 @@ static int preserveFlag = FALSE;
43static const char *srcName; 43static const char *srcName;
44static const char *destName; 44static const char *destName;
45static int destDirFlag = FALSE; 45static int destDirFlag = FALSE;
46static int destExistsFlag = FALSE;
47static int srcDirFlag = FALSE; 46static int srcDirFlag = FALSE;
48 47
49static int fileAction(const char *fileName, struct stat* statbuf) 48static int fileAction(const char *fileName, struct stat* statbuf)
@@ -71,8 +70,6 @@ static int fileAction(const char *fileName, struct stat* statbuf)
71 70
72extern int cp_main(int argc, char **argv) 71extern int cp_main(int argc, char **argv)
73{ 72{
74 struct stat statBuf;
75
76 if (argc < 3) { 73 if (argc < 3) {
77 usage (cp_usage); 74 usage (cp_usage);
78 } 75 }
@@ -106,11 +103,7 @@ extern int cp_main(int argc, char **argv)
106 103
107 104
108 destName = argv[argc - 1]; 105 destName = argv[argc - 1];
109 if (stat(destName, &statBuf) >= 0) { 106 destDirFlag = isDirectory(destName);
110 destExistsFlag = TRUE;
111 if (S_ISDIR(statBuf.st_mode))
112 destDirFlag = TRUE;
113 }
114 107
115 if ((argc > 3) && destDirFlag==FALSE) { 108 if ((argc > 3) && destDirFlag==FALSE) {
116 fprintf(stderr, "%s: not a directory\n", destName); 109 fprintf(stderr, "%s: not a directory\n", destName);
diff --git a/mv.c b/mv.c
index d0f346196..92c40c9b7 100644
--- a/mv.c
+++ b/mv.c
@@ -27,7 +27,6 @@
27#include <utime.h> 27#include <utime.h>
28#include <dirent.h> 28#include <dirent.h>
29 29
30
31static const char mv_usage[] = "mv SOURCE DEST\n" 30static const char mv_usage[] = "mv SOURCE DEST\n"
32" or: mv SOURCE... DIRECTORY\n\n" 31" or: mv SOURCE... DIRECTORY\n\n"
33"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"; 32"Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n";
@@ -35,26 +34,49 @@ static const char mv_usage[] = "mv SOURCE DEST\n"
35 34
36static const char *srcName; 35static const char *srcName;
37static const char *destName; 36static const char *destName;
38static int dirFlag = FALSE; 37static int destDirFlag = FALSE;
38static int srcDirFlag = FALSE;
39 39
40static int fileAction(const char *fileName, struct stat* statbuf) 40static int fileAction(const char *fileName, struct stat* statbuf)
41{ 41{
42 char newdestName[NAME_MAX]; 42 char newdestName[NAME_MAX];
43 43
44 fprintf(stderr, "srcName='%s' destName='%s'\n", srcName, destName);
45 strcpy(newdestName, destName); 44 strcpy(newdestName, destName);
46 strcat(newdestName, "/"); 45 if ( srcDirFlag == TRUE ) {
47 strcat(newdestName, strstr(fileName, fileName)); 46 strcat(newdestName, strstr(fileName, srcName) + strlen(srcName));
48 fprintf(stderr, "newdestName='%s'\n", newdestName); 47 }
48
49 if (destDirFlag==TRUE && srcDirFlag == FALSE) {
50 if (newdestName[strlen(newdestName)-1] != '/' ) {
51 strcat(newdestName, "/");
52 }
53 strcat(newdestName, srcName);
54 }
55
49 return (copyFile(fileName, newdestName, TRUE, TRUE)); 56 return (copyFile(fileName, newdestName, TRUE, TRUE));
50} 57}
51 58
59static int rmfileAction(const char *fileName, struct stat* statbuf)
60{
61 if (unlink( fileName) < 0 ) {
62 perror( fileName);
63 return ( FALSE);
64 }
65 return ( TRUE);
66}
52 67
53extern int mv_main(int argc, char **argv) 68static int rmdirAction(const char *fileName, struct stat* statbuf)
54{ 69{
55 char newdestName[NAME_MAX]; 70 if (rmdir( fileName) < 0 ) {
56 char *skipName; 71 perror( fileName);
72 return ( FALSE);
73 }
74 return ( TRUE);
75}
57 76
77
78extern int mv_main(int argc, char **argv)
79{
58 if (argc < 3) { 80 if (argc < 3) {
59 usage (mv_usage); 81 usage (mv_usage);
60 } 82 }
@@ -62,45 +84,23 @@ extern int mv_main(int argc, char **argv)
62 argv++; 84 argv++;
63 85
64 destName = argv[argc - 1]; 86 destName = argv[argc - 1];
65 dirFlag = isDirectory(destName); 87 destDirFlag = isDirectory(destName);
66 88
67 if ((argc > 3) && dirFlag==FALSE) { 89 if ((argc > 3) && destDirFlag==FALSE) {
68 fprintf(stderr, "%s: not a directory\n", destName); 90 fprintf(stderr, "%s: not a directory\n", destName);
69 exit (FALSE); 91 exit (FALSE);
70 } 92 }
71 93
72 while (argc-- > 1) { 94 while (argc-- > 1) {
73 srcName = *(argv++); 95 srcName = *(argv++);
74 skipName = strrchr(srcName, '/'); 96 srcDirFlag = isDirectory(srcName);
75 if (skipName) 97 if (recursiveAction(srcName, TRUE, TRUE, FALSE,
76 skipName++; 98 fileAction, fileAction) == FALSE) {
77 strcpy(newdestName, destName); 99 exit( FALSE);
78 if (dirFlag==TRUE) {
79 strcat(newdestName, "/");
80 if ( skipName != NULL)
81 strcat(newdestName, strstr(srcName, skipName));
82 else
83 strcat(newdestName, srcName);
84 } 100 }
85 if (isDirectory(srcName)==TRUE && newdestName[strlen(newdestName)] != '/') { 101 if (recursiveAction(srcName, TRUE, TRUE, TRUE,
86 strcat(newdestName, "/"); 102 rmfileAction, rmdirAction) == FALSE) {
87 createPath(newdestName, 0777); 103 exit( FALSE);
88 fprintf(stderr, "srcName = '%s'\n", srcName);
89 fprintf(stderr, "newdestName = '%s'\n", newdestName);
90 if (recursiveAction(srcName, TRUE, TRUE, FALSE,
91 fileAction, fileAction) == FALSE)
92 {
93 exit( FALSE);
94 }
95 exit( TRUE);
96 } else {
97 if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) {
98 exit( FALSE);
99 }
100 if (unlink (srcName) < 0) {
101 perror (srcName);
102 exit( FALSE);
103 }
104 } 104 }
105 } 105 }
106 exit( TRUE); 106 exit( TRUE);