summaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>1999-12-29 02:10:35 +0000
committerErik Andersen <andersen@codepoet.org>1999-12-29 02:10:35 +0000
commit2fe08c7afb3ddef42f304e78cb6edfa28e0741ef (patch)
tree2c44125f9324373494668c513c31be2a172a54d4 /coreutils
parent00266d3df6ba8dcc6247f112372a0ce5a8ab2c32 (diff)
downloadbusybox-w32-2fe08c7afb3ddef42f304e78cb6edfa28e0741ef.tar.gz
busybox-w32-2fe08c7afb3ddef42f304e78cb6edfa28e0741ef.tar.bz2
busybox-w32-2fe08c7afb3ddef42f304e78cb6edfa28e0741ef.zip
Fixed cp so it works as God intended it to.
-Erik
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/cp.c40
-rw-r--r--coreutils/mv.c42
2 files changed, 58 insertions, 24 deletions
diff --git a/coreutils/cp.c b/coreutils/cp.c
index ce632016e..1e10f2868 100644
--- a/coreutils/cp.c
+++ b/coreutils/cp.c
@@ -42,26 +42,36 @@ static int followLinks = FALSE;
42static int preserveFlag = FALSE; 42static int preserveFlag = FALSE;
43static const char *srcName; 43static const char *srcName;
44static const char *destName; 44static const char *destName;
45static const char *skipName; 45static int destDirFlag = FALSE;
46static int dirFlag = FALSE; 46static int destExistsFlag = FALSE;
47 47static int srcDirFlag = FALSE;
48 48
49static int fileAction(const char *fileName, struct stat* statbuf) 49static int fileAction(const char *fileName, struct stat* statbuf)
50{ 50{
51 char newdestName[NAME_MAX]; 51 char newdestName[NAME_MAX];
52
52 strcpy(newdestName, destName); 53 strcpy(newdestName, destName);
53 if (dirFlag==TRUE) { 54 if ( srcDirFlag == TRUE ) {
54 strcat(newdestName, "/"); 55 if (recursiveFlag!=TRUE ) {
55 if ( skipName != NULL) 56 fprintf(stderr, "cp: %s: omitting directory\n", srcName);
56 strcat(newdestName, strstr(fileName, skipName)); 57 return( TRUE);
57 else 58 }
58 strcat(newdestName, srcName); 59 strcat(newdestName, strstr(fileName, srcName) + strlen(srcName));
60 }
61
62 if (destDirFlag==TRUE && srcDirFlag == FALSE) {
63 if (newdestName[strlen(newdestName)-1] != '/' ) {
64 strcat(newdestName, "/");
65 }
66 strcat(newdestName, srcName);
59 } 67 }
68
60 return (copyFile(fileName, newdestName, preserveFlag, followLinks)); 69 return (copyFile(fileName, newdestName, preserveFlag, followLinks));
61} 70}
62 71
63extern int cp_main(int argc, char **argv) 72extern int cp_main(int argc, char **argv)
64{ 73{
74 struct stat statBuf;
65 75
66 if (argc < 3) { 76 if (argc < 3) {
67 usage (cp_usage); 77 usage (cp_usage);
@@ -96,18 +106,20 @@ extern int cp_main(int argc, char **argv)
96 106
97 107
98 destName = argv[argc - 1]; 108 destName = argv[argc - 1];
99 dirFlag = isDirectory(destName); 109 if (stat(destName, &statBuf) >= 0) {
110 destExistsFlag = TRUE;
111 if (S_ISDIR(statBuf.st_mode))
112 destDirFlag = TRUE;
113 }
100 114
101 if ((argc > 3) && dirFlag==FALSE) { 115 if ((argc > 3) && destDirFlag==FALSE) {
102 fprintf(stderr, "%s: not a directory\n", destName); 116 fprintf(stderr, "%s: not a directory\n", destName);
103 exit (FALSE); 117 exit (FALSE);
104 } 118 }
105 119
106 while (argc-- > 1) { 120 while (argc-- > 1) {
107 srcName = *(argv++); 121 srcName = *(argv++);
108 skipName = strrchr(srcName, '/'); 122 srcDirFlag = isDirectory(srcName);
109 if (skipName)
110 skipName++;
111 if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE, 123 if (recursiveAction(srcName, recursiveFlag, followLinks, FALSE,
112 fileAction, fileAction) == FALSE) { 124 fileAction, fileAction) == FALSE) {
113 exit( FALSE); 125 exit( FALSE);
diff --git a/coreutils/mv.c b/coreutils/mv.c
index 2be3961ca..d0f346196 100644
--- a/coreutils/mv.c
+++ b/coreutils/mv.c
@@ -35,13 +35,25 @@ static const char mv_usage[] = "mv SOURCE DEST\n"
35 35
36static const char *srcName; 36static const char *srcName;
37static const char *destName; 37static const char *destName;
38static const char *skipName;
39static int dirFlag = FALSE; 38static int dirFlag = FALSE;
40 39
40static int fileAction(const char *fileName, struct stat* statbuf)
41{
42 char newdestName[NAME_MAX];
43
44 fprintf(stderr, "srcName='%s' destName='%s'\n", srcName, destName);
45 strcpy(newdestName, destName);
46 strcat(newdestName, "/");
47 strcat(newdestName, strstr(fileName, fileName));
48 fprintf(stderr, "newdestName='%s'\n", newdestName);
49 return (copyFile(fileName, newdestName, TRUE, TRUE));
50}
51
41 52
42extern int mv_main(int argc, char **argv) 53extern int mv_main(int argc, char **argv)
43{ 54{
44 char newdestName[NAME_MAX]; 55 char newdestName[NAME_MAX];
56 char *skipName;
45 57
46 if (argc < 3) { 58 if (argc < 3) {
47 usage (mv_usage); 59 usage (mv_usage);
@@ -69,16 +81,26 @@ extern int mv_main(int argc, char **argv)
69 strcat(newdestName, strstr(srcName, skipName)); 81 strcat(newdestName, strstr(srcName, skipName));
70 else 82 else
71 strcat(newdestName, srcName); 83 strcat(newdestName, srcName);
72 fprintf(stderr, "srcName='%s'\n", srcName);
73 fprintf(stderr, "skipName='%s'\n", skipName);
74 fprintf(stderr, "newdestName='%s'\n", newdestName);
75 }
76 if (copyFile(srcName, newdestName, FALSE, FALSE) == FALSE) {
77 exit( FALSE);
78 } 84 }
79 if (unlink (srcName) < 0) { 85 if (isDirectory(srcName)==TRUE && newdestName[strlen(newdestName)] != '/') {
80 perror (srcName); 86 strcat(newdestName, "/");
81 exit( FALSE); 87 createPath(newdestName, 0777);
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 }
82 } 104 }
83 } 105 }
84 exit( TRUE); 106 exit( TRUE);