diff options
author | Erik Andersen <andersen@codepoet.org> | 1999-12-29 02:10:35 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 1999-12-29 02:10:35 +0000 |
commit | 2fe08c7afb3ddef42f304e78cb6edfa28e0741ef (patch) | |
tree | 2c44125f9324373494668c513c31be2a172a54d4 | |
parent | 00266d3df6ba8dcc6247f112372a0ce5a8ab2c32 (diff) | |
download | busybox-w32-2fe08c7afb3ddef42f304e78cb6edfa28e0741ef.tar.gz busybox-w32-2fe08c7afb3ddef42f304e78cb6edfa28e0741ef.tar.bz2 busybox-w32-2fe08c7afb3ddef42f304e78cb6edfa28e0741ef.zip |
Fixed cp so it works as God intended it to.
-Erik
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | coreutils/cp.c | 40 | ||||
-rw-r--r-- | coreutils/mv.c | 42 | ||||
-rw-r--r-- | cp.c | 40 | ||||
-rw-r--r-- | findutils/grep.c | 2 | ||||
-rw-r--r-- | grep.c | 2 | ||||
-rw-r--r-- | mv.c | 42 | ||||
-rw-r--r-- | utility.c | 5 |
8 files changed, 122 insertions, 53 deletions
@@ -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. |
25 | DODEBUG=false | 25 | DODEBUG=true |
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 ce632016e..1e10f2868 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c | |||
@@ -42,26 +42,36 @@ static int followLinks = FALSE; | |||
42 | static int preserveFlag = FALSE; | 42 | static int preserveFlag = FALSE; |
43 | static const char *srcName; | 43 | static const char *srcName; |
44 | static const char *destName; | 44 | static const char *destName; |
45 | static const char *skipName; | 45 | static int destDirFlag = FALSE; |
46 | static int dirFlag = FALSE; | 46 | static int destExistsFlag = FALSE; |
47 | 47 | static int srcDirFlag = FALSE; | |
48 | 48 | ||
49 | static int fileAction(const char *fileName, struct stat* statbuf) | 49 | static 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 | ||
63 | extern int cp_main(int argc, char **argv) | 72 | extern 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 | ||
36 | static const char *srcName; | 36 | static const char *srcName; |
37 | static const char *destName; | 37 | static const char *destName; |
38 | static const char *skipName; | ||
39 | static int dirFlag = FALSE; | 38 | static int dirFlag = FALSE; |
40 | 39 | ||
40 | static 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 | ||
42 | extern int mv_main(int argc, char **argv) | 53 | extern 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); |
@@ -42,26 +42,36 @@ static int followLinks = FALSE; | |||
42 | static int preserveFlag = FALSE; | 42 | static int preserveFlag = FALSE; |
43 | static const char *srcName; | 43 | static const char *srcName; |
44 | static const char *destName; | 44 | static const char *destName; |
45 | static const char *skipName; | 45 | static int destDirFlag = FALSE; |
46 | static int dirFlag = FALSE; | 46 | static int destExistsFlag = FALSE; |
47 | 47 | static int srcDirFlag = FALSE; | |
48 | 48 | ||
49 | static int fileAction(const char *fileName, struct stat* statbuf) | 49 | static 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 | ||
63 | extern int cp_main(int argc, char **argv) | 72 | extern 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/findutils/grep.c b/findutils/grep.c index a0457df91..fdfc959af 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -46,7 +46,7 @@ static const char grep_usage[] = | |||
46 | "\t-h\tsuppress the prefixing filename on output\n" | 46 | "\t-h\tsuppress the prefixing filename on output\n" |
47 | "\t-i\tignore case distinctions\n" | 47 | "\t-i\tignore case distinctions\n" |
48 | "\t-n\tprint line number with output lines\n" | 48 | "\t-n\tprint line number with output lines\n" |
49 | "\t-q\tbe quiet\n\n" | 49 | "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n" |
50 | #if defined BB_REGEXP | 50 | #if defined BB_REGEXP |
51 | "This version of grep matches full regular expresions.\n"; | 51 | "This version of grep matches full regular expresions.\n"; |
52 | #else | 52 | #else |
@@ -46,7 +46,7 @@ static const char grep_usage[] = | |||
46 | "\t-h\tsuppress the prefixing filename on output\n" | 46 | "\t-h\tsuppress the prefixing filename on output\n" |
47 | "\t-i\tignore case distinctions\n" | 47 | "\t-i\tignore case distinctions\n" |
48 | "\t-n\tprint line number with output lines\n" | 48 | "\t-n\tprint line number with output lines\n" |
49 | "\t-q\tbe quiet\n\n" | 49 | "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n" |
50 | #if defined BB_REGEXP | 50 | #if defined BB_REGEXP |
51 | "This version of grep matches full regular expresions.\n"; | 51 | "This version of grep matches full regular expresions.\n"; |
52 | #else | 52 | #else |
@@ -35,13 +35,25 @@ static const char mv_usage[] = "mv SOURCE DEST\n" | |||
35 | 35 | ||
36 | static const char *srcName; | 36 | static const char *srcName; |
37 | static const char *destName; | 37 | static const char *destName; |
38 | static const char *skipName; | ||
39 | static int dirFlag = FALSE; | 38 | static int dirFlag = FALSE; |
40 | 39 | ||
40 | static 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 | ||
42 | extern int mv_main(int argc, char **argv) | 53 | extern 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); |
@@ -151,7 +151,8 @@ copyFile( const char *srcName, const char *destName, | |||
151 | if (S_ISDIR(srcStatBuf.st_mode)) { | 151 | if (S_ISDIR(srcStatBuf.st_mode)) { |
152 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); | 152 | //fprintf(stderr, "copying directory %s to %s\n", srcName, destName); |
153 | /* Make sure the directory is writable */ | 153 | /* Make sure the directory is writable */ |
154 | if (mkdir(destName, 0777777 ^ umask(0))) { | 154 | result = mkdir(destName, 0777777 ^ umask(0)); |
155 | if (result < 0 && errno != EEXIST) { | ||
155 | perror(destName); | 156 | perror(destName); |
156 | return (FALSE); | 157 | return (FALSE); |
157 | } | 158 | } |
@@ -478,7 +479,7 @@ recursiveAction(const char *fileName, int recurse, int followLinks, int depthFir | |||
478 | 479 | ||
479 | 480 | ||
480 | 481 | ||
481 | #if defined (BB_TAR) || defined (BB_MKDIR) | 482 | #if defined (BB_TAR) || defined (BB_MKDIR) || defined (BB_CP) |
482 | /* | 483 | /* |
483 | * Attempt to create the directories along the specified path, except for | 484 | * Attempt to create the directories along the specified path, except for |
484 | * the final component. The mode is given for the final directory only, | 485 | * the final component. The mode is given for the final directory only, |