From 8f4c4dd8a0f4ef88a15bfe1e441476017b1f1d86 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 24 Oct 2023 21:25:29 +0300 Subject: [PATCH] cp fix --- LICENSE | 14 ------------ coreutils/cp.c | 53 +++++++++++++++++++++++++++++++++++++--------- coreutils/mktemp.c | 4 ++-- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/LICENSE b/LICENSE index 4eeca54..e69de29 100644 --- a/LICENSE +++ b/LICENSE @@ -1,14 +0,0 @@ - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - Version 2, December 2004 - - Copyright (C) 2004 Sam Hocevar - - Everyone is permitted to copy and distribute verbatim or modified - copies of this license document, and changing it is allowed as long - as the name is changed. - - DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. You just DO WHAT THE FUCK YOU WANT TO. - diff --git a/coreutils/cp.c b/coreutils/cp.c index 1957fc4..db3b79f 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c @@ -23,10 +23,10 @@ char *make_path(const char *src, const char *dst) { int write_buffer(int mode, int ifd, int ofd, const char *dst) { off_t len = lseek(ifd, 0, SEEK_END); - if (len < 0) - return 1; + if (len <= 0) { + if (len < 0) + return 1; - else if (len == 0) { int fd = open(dst, O_CREAT | O_RDONLY, mode); if (fd < 0) return 1; @@ -60,18 +60,51 @@ int copy(int mode, const char *src, const char *dst) { if (ifd < 0) return 1; + /* test ./testdir/ -> test ./testdir/test */ + char *dup = NULL; + char *new_path = (char *)dst; + int flag = 0; + int ret = 0; + int ofd = open(dst, O_CREAT | O_TRUNC | O_RDWR, mode); if (ofd < 0) { - close(ifd); - return 1; + ret = 1; + + dup = strdup(src); + if (dup == NULL) + goto CLOSE; + + flag = 1; + new_path = NULL; + + new_path = make_path(dst, basename(dup)); + if (new_path == NULL) + goto CLOSE; + + ofd = open(new_path, O_CREAT | O_TRUNC | O_RDWR, mode); + if (ofd < 0) + goto CLOSE; + + ret = 0; } - if (write_buffer(mode, ifd, ofd, dst)) + if (write_buffer(mode, ifd, ofd, new_path)) fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno)); - close(ifd); + close(ofd); - return 0; + +CLOSE: + close(ifd); + if (flag) { + if (dup != NULL) + free(dup); + + if (new_path != NULL) + free(new_path); + } + + return ret; } int get_stat(const char *path, struct stat *stat_path) { @@ -88,9 +121,9 @@ int cptree(const char *src, const char *dst) { if (get_stat(src, &stat_path)) return 1; - if (S_ISDIR(stat_path.st_mode) == 0) { + if (!S_ISDIR(stat_path.st_mode)) { if (copy(stat_path.st_mode, src, dst)) { - fprintf(stderr, "cp: %s: copy() failed (%s)\n", src, strerror(errno)); + fprintf(stderr, "cp: (%s %s): copy() failed (%s)\n", src, dst, strerror(errno)); return 1; } diff --git a/coreutils/mktemp.c b/coreutils/mktemp.c index 89626a1..76795af 100644 --- a/coreutils/mktemp.c +++ b/coreutils/mktemp.c @@ -20,11 +20,11 @@ int make_temp_dir(char *tmp) { int get_suffix(const char *str) { size_t len = strlen(str); - for (size_t i = len - 1; i >= 0; i--) + for (size_t i = len - 1; i > 0; i--) if (str[i] == 'X') return len - i - 1; - exit(1); + return 0; } int make_temp_file(char *tmp) {