From bd26aff1fdd18b16b4b08ea624409dd3e97e4d3c Mon Sep 17 00:00:00 2001 From: Tyler Bindon Date: Wed, 10 Apr 2013 04:25:04 +0000 Subject: [PATCH 1/2] Let's not overwrite directories --- userspace/core/cp.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/userspace/core/cp.c b/userspace/core/cp.c index 8eb56973..287ce418 100644 --- a/userspace/core/cp.c +++ b/userspace/core/cp.c @@ -1,6 +1,7 @@ /* * cp */ +#include #include #define CHUNK_SIZE 4096 @@ -18,6 +19,14 @@ int main(int argc, char ** argv) { fprintf(stderr, "%s: %s: no such file or directory\n", argv[0], argv[1]); return 1; } + + struct stat statbuf; + stat(argv[2], &statbuf); + if (S_ISDIR(statbuf.st_mode)) { + fprintf(stderr, "%s: let's not overwrite a directory: %s\n", argv[0], argv[2]); + return 1; + } + fout = fopen(argv[2], "w"); size_t length; From ccfcd53943be85f8f590b101cbbd3e24692ff35c Mon Sep 17 00:00:00 2001 From: Tyler Bindon Date: Wed, 10 Apr 2013 05:50:06 +0000 Subject: [PATCH 2/2] Use source filename when copying to directory --- userspace/core/cp.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/userspace/core/cp.c b/userspace/core/cp.c index 287ce418..35f46929 100644 --- a/userspace/core/cp.c +++ b/userspace/core/cp.c @@ -1,6 +1,8 @@ /* * cp */ +#include +#include #include #include @@ -23,11 +25,19 @@ int main(int argc, char ** argv) { struct stat statbuf; stat(argv[2], &statbuf); if (S_ISDIR(statbuf.st_mode)) { - fprintf(stderr, "%s: let's not overwrite a directory: %s\n", argv[0], argv[2]); - return 1; - } + char *filename = strrchr(argv[1], '/'); + if (!filename) { + filename = argv[1]; + } - fout = fopen(argv[2], "w"); + char *target_path = malloc((strlen(argv[2]) + strlen(filename) + 2) * sizeof(char)); + sprintf(target_path, "%s/%s", argv[2], filename ); + fout = fopen( target_path, "w" ); + + free(target_path); + } else { + fout = fopen( argv[2], "w" ); + } size_t length;