From 15fad649ea51d25e168e0456b5621beb820e254d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 27 Jan 2019 07:18:26 +0100 Subject: Userland: Make a simple /bin/cp for copying files. --- Kernel/sync.sh | 1 + Userland/.gitignore | 1 + Userland/Makefile | 5 +++++ Userland/cp.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 Userland/cp.cpp diff --git a/Kernel/sync.sh b/Kernel/sync.sh index 6b83e1549a..0a7524f6a7 100755 --- a/Kernel/sync.sh +++ b/Kernel/sync.sh @@ -43,6 +43,7 @@ cp -v ../Userland/touch mnt/bin/touch cp -v ../Userland/sync mnt/bin/sync cp -v ../Userland/more mnt/bin/more cp -v ../Userland/rm mnt/bin/rm +cp -v ../Userland/cp mnt/bin/cp cp -v ../Userland/guitest mnt/bin/guitest cp -v ../Userland/guitest2 mnt/bin/guitest2 cp -v ../Userland/sysctl mnt/bin/sysctl diff --git a/Userland/.gitignore b/Userland/.gitignore index 425fa3f830..45e4d0fcc6 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -25,3 +25,4 @@ guitest guitest2 sysctl rm +cp diff --git a/Userland/Makefile b/Userland/Makefile index a774dd3e40..4cca09a50d 100644 --- a/Userland/Makefile +++ b/Userland/Makefile @@ -22,6 +22,7 @@ OBJS = \ guitest.o \ guitest2.o \ sysctl.o \ + cp.o \ rm.o APPS = \ @@ -49,6 +50,7 @@ APPS = \ guitest \ guitest2 \ sysctl \ + cp \ rm ARCH_FLAGS = @@ -141,6 +143,9 @@ guitest2: guitest2.o sysctl: sysctl.o $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a +cp: cp.o + $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a + rm: rm.o $(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a diff --git a/Userland/cp.cpp b/Userland/cp.cpp new file mode 100644 index 0000000000..db5c08b2d9 --- /dev/null +++ b/Userland/cp.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + if (argc != 3) { + printf("usage: cp \n"); + return 0; + } + int src_fd = open(argv[1], O_RDONLY); + if (src_fd < 0) { + perror("open src"); + return 1; + } + int dst_fd = open(argv[2], O_WRONLY | O_CREAT); + if (dst_fd < 0) { + perror("open dst"); + return 1; + } + + for (;;) { + char buffer[BUFSIZ]; + ssize_t nread = read(src_fd, buffer, sizeof(buffer)); + if (nread < 0) { + perror("read src"); + return 1; + } + if (nread == 0) + break; + ssize_t nwritten = write(dst_fd, buffer, nread); + if (nwritten < 0) { + perror("write dst"); + return 1; + } + assert(nwritten != 0); + } + close(src_fd); + close(dst_fd); + return 0; +} -- cgit v1.2.3