From 07f9affe5287556d3342695d7951582153c698b2 Mon Sep 17 00:00:00 2001 From: Denis Humeniuk Date: Tue, 17 Sep 2024 20:47:21 +0300 Subject: [PATCH 1/2] Done lab --- Makefile | 6 ++++++ cp.c | 35 +++++++++++++++++++++++++++++++++++ mv.c | 19 +++++++++++++++++++ myprogram.c | 9 +++++++++ test_file1.txt | 0 5 files changed, 69 insertions(+) create mode 100644 cp.c create mode 100644 mv.c create mode 100644 myprogram.c create mode 100644 test_file1.txt diff --git a/Makefile b/Makefile index a3b840dd8e..ad28f3194b 100644 --- a/Makefile +++ b/Makefile @@ -184,6 +184,9 @@ UPROGS=\ _usertests\ _wc\ _zombie\ + _myprogram\ + _cp\ + _mv\ fs.img: mkfs README $(UPROGS) ./mkfs fs.img README $(UPROGS) @@ -253,6 +256,9 @@ qemu-nox-gdb: fs.img xv6.img .gdbinit EXTRA=\ mkfs.c ulib.c user.h cat.c echo.c forktest.c grep.c kill.c\ ln.c ls.c mkdir.c rm.c stressfs.c usertests.c wc.c zombie.c\ + myprogram.c\ + cp.c\ + mv.c\ printf.c umalloc.c\ README dot-bochsrc *.pl toc.* runoff runoff1 runoff.list\ .gdbinit.tmpl gdbutil\ diff --git a/cp.c b/cp.c new file mode 100644 index 0000000000..a283bc47f3 --- /dev/null +++ b/cp.c @@ -0,0 +1,35 @@ +#include "types.h" +#include "user.h" +#include "fcntl.h" + +void cp(char *from, char *to) { + int fd1, fd2, n; + char buf[512]; + + if((fd1 = open(from, O_RDONLY)) < 0) { + printf(1, "cp: cannot open %s\n", from); + exit(); + } + + if((fd2 = open(to, O_CREATE | O_WRONLY)) < 0) { + printf(1, "cp: cannot open %s\n", to); + exit(); + } + + while((n = read(fd1, buf, sizeof(buf))) > 0) { + write(fd2, buf, n); + } + + close(fd1); + close(fd2); +} + +int main(int argc, char *argv[]) { + if(argc != 3) { + printf(1, "Wrong number of arguments\n"); + exit(); + } + cp(argv[1], argv[2]); + exit(); +} + diff --git a/mv.c b/mv.c new file mode 100644 index 0000000000..195e5249a6 --- /dev/null +++ b/mv.c @@ -0,0 +1,19 @@ +#include "types.h" +#include "user.h" + +int main(int argc, char *argv[]) { + if(argc != 3) { + printf(1, "Usage: mv \n"); + exit(); + } + if(link(argv[1], argv[2]) < 0) { + printf(1, "mv: cannot link %s to %s\n", argv[1], argv[2]); + exit(); + } + if(unlink(argv[1]) < 0) { + printf(1, "mv: cannot unlink %s\n", argv[1]); + exit(); + } + exit(); +} + diff --git a/myprogram.c b/myprogram.c new file mode 100644 index 0000000000..8cff10f451 --- /dev/null +++ b/myprogram.c @@ -0,0 +1,9 @@ +#include "types.h" +#include "stat.h" +#include "user.h" + +int main(void) +{ + printf(1, "Hello world!\n"); + exit(); +} diff --git a/test_file1.txt b/test_file1.txt new file mode 100644 index 0000000000..e69de29bb2 From 985f1d95719fb0fbc0e9c653895566e90801cea3 Mon Sep 17 00:00:00 2001 From: Denis Humeniuk Date: Tue, 17 Sep 2024 22:47:29 +0300 Subject: [PATCH 2/2] Finally done lab --- cp.c | 27 ++++++++++++++++++++++----- mv.c | 8 +++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cp.c b/cp.c index a283bc47f3..cf54bd02b6 100644 --- a/cp.c +++ b/cp.c @@ -1,18 +1,36 @@ #include "types.h" #include "user.h" #include "fcntl.h" +#include "stat.h" + +int is_directory(struct stat *st) { + return st->type == T_DIR; +} void cp(char *from, char *to) { int fd1, fd2, n; char buf[512]; - if((fd1 = open(from, O_RDONLY)) < 0) { - printf(1, "cp: cannot open %s\n", from); + struct stat st_1; + + if (stat(from, &st_1) < 0){ + printf(1, "Error in source file\n"); + } + + if (is_directory(&st_1)) { + printf(1, "Can not copy from directory\n"); + } + + fd1 = open(from, O_RDONLY); + fd2 = open(to, O_CREATE | O_WRONLY); + + if(fd1 < 0) { + printf(1, "Cannot open source file\n"); exit(); } - if((fd2 = open(to, O_CREATE | O_WRONLY)) < 0) { - printf(1, "cp: cannot open %s\n", to); + if(fd2 < 0) { + printf(1, "Cannot open destination file\n"); exit(); } @@ -32,4 +50,3 @@ int main(int argc, char *argv[]) { cp(argv[1], argv[2]); exit(); } - diff --git a/mv.c b/mv.c index 195e5249a6..86eb033782 100644 --- a/mv.c +++ b/mv.c @@ -6,14 +6,16 @@ int main(int argc, char *argv[]) { printf(1, "Usage: mv \n"); exit(); } + if(link(argv[1], argv[2]) < 0) { - printf(1, "mv: cannot link %s to %s\n", argv[1], argv[2]); + printf(1, "Cannot link first path to second\n"); exit(); } + if(unlink(argv[1]) < 0) { - printf(1, "mv: cannot unlink %s\n", argv[1]); + printf(1, "Cannot unlink file from source path\n"); exit(); } + exit(); } -