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..cf54bd02b6 --- /dev/null +++ b/cp.c @@ -0,0 +1,52 @@ +#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]; + + 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 < 0) { + printf(1, "Cannot open destination file\n"); + 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..86eb033782 --- /dev/null +++ b/mv.c @@ -0,0 +1,21 @@ +#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, "Cannot link first path to second\n"); + exit(); + } + + if(unlink(argv[1]) < 0) { + printf(1, "Cannot unlink file from source path\n"); + 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