Making note of my changes
[oweals/busybox.git] / cp.c
diff --git a/cp.c b/cp.c
index 1e10f286839035b0ced254c0df02895d47b7b802..e96012d500fea7916d4b84ae2ad6545ef521a26d 100644 (file)
--- a/cp.c
+++ b/cp.c
@@ -33,7 +33,7 @@ static const char cp_usage[] = "cp [OPTION]... SOURCE DEST\n"
     "\n"
     "\t-a\tsame as -dpR\n"
     "\t-d\tpreserve links\n"
-    "\t-p\tpreserve file attributes if possable\n"
+    "\t-p\tpreserve file attributes if possible\n"
     "\t-R\tcopy directories recursively\n";
 
 
@@ -43,12 +43,12 @@ static int preserveFlag = FALSE;
 static const char *srcName;
 static const char *destName;
 static int destDirFlag = FALSE;
-static int destExistsFlag = FALSE;
 static int srcDirFlag = FALSE;
 
 static int fileAction(const char *fileName, struct stat* statbuf)
 {
     char newdestName[NAME_MAX];
+    char* newsrcName = NULL;
 
     strcpy(newdestName, destName);
     if ( srcDirFlag == TRUE ) {
@@ -63,7 +63,11 @@ static int fileAction(const char *fileName, struct stat* statbuf)
        if (newdestName[strlen(newdestName)-1] != '/' ) {
            strcat(newdestName, "/");
        }
-       strcat(newdestName, srcName);
+       newsrcName = strrchr(srcName, '/');
+       if (newsrcName && *newsrcName != '\0')
+           strcat(newdestName, newsrcName);
+       else
+           strcat(newdestName, srcName);
     }
     
     return (copyFile(fileName, newdestName, preserveFlag, followLinks));
@@ -71,8 +75,6 @@ static int fileAction(const char *fileName, struct stat* statbuf)
 
 extern int cp_main(int argc, char **argv)
 {
-    struct stat statBuf;
-
     if (argc < 3) {
        usage (cp_usage);
     }
@@ -106,11 +108,7 @@ extern int cp_main(int argc, char **argv)
 
 
     destName = argv[argc - 1];
-    if (stat(destName, &statBuf) >= 0) {
-       destExistsFlag = TRUE;
-       if (S_ISDIR(statBuf.st_mode))
-           destDirFlag = TRUE;
-    }
+    destDirFlag = isDirectory(destName);
 
     if ((argc > 3) && destDirFlag==FALSE) {
        fprintf(stderr, "%s: not a directory\n", destName);