From b701e39fbbbf571671b0fa3b184d4e040f646f7c Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 18 Oct 2023 21:44:56 +0300 Subject: [PATCH] . --- coreutils/chroot.c | 6 ++++-- coreutils/cp.c | 13 +++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/coreutils/chroot.c b/coreutils/chroot.c index 9d25aae..7f3268e 100644 --- a/coreutils/chroot.c +++ b/coreutils/chroot.c @@ -5,7 +5,7 @@ #include int main(const int argc, const char **argv) { - if (argc <= 2) { + if (argc < 3) { printf("chroot [dir] [\"command arg\"]\n"); return 0; } @@ -16,6 +16,8 @@ int main(const int argc, const char **argv) { } chdir("/"); - for (int i = 2; i < argc; i++) + for (int i = 1; i < argc; i++) system(argv[i]); + + return 0; } diff --git a/coreutils/cp.c b/coreutils/cp.c index c863630..dafca31 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c @@ -21,11 +21,20 @@ char *make_path(const char *src, const char *dst) { return full_path; } -int write_buffer(int ifd, int ofd) { +int write_buffer(int ifd, int ofd, const char *dst) { off_t len = lseek(ifd, 0, SEEK_END); if (len < 0) return 1; + else if (len == 0) { + int fd = open(dst, O_CREAT | O_RDONLY, 0666); + if (fd < 0) + return 1; + + close(fd); + return 0; + } + if (ftruncate(ofd, len) < 0) return 1; @@ -57,7 +66,7 @@ int copy(const char *src, const char *dst) { return 1; } - if (write_buffer(ifd, ofd)) + if (write_buffer(ifd, ofd, dst)) fprintf(stderr, "cp: (%s %s) %s\n", src, dst, strerror(errno)); close(ifd);