diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-01-04 18:06:31 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-01-04 18:06:31 +0800 |
| commit | 3886749b1c8d79d4701bf6b8e66249c4e1491648 (patch) | |
| tree | 142e6d9ada3d511663c6c0540196d270c69c0227 /src/user/cp.cc | |
| parent | a64d25c07b31d57f8706073e11cd774d0880d6dd (diff) | |
| download | lume-3886749b1c8d79d4701bf6b8e66249c4e1491648.tar.gz lume-3886749b1c8d79d4701bf6b8e66249c4e1491648.tar.xz | |
dev(shell): implement basic shell tools
fixed(file-system): fixed read and write bugs
Diffstat (limited to 'src/user/cp.cc')
| -rw-r--r-- | src/user/cp.cc | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/user/cp.cc b/src/user/cp.cc new file mode 100644 index 0000000..9f13bef --- /dev/null +++ b/src/user/cp.cc @@ -0,0 +1,48 @@ +#include "ulib/stdio.h" +#include "ulib/stdlib.h" +#include "user/user.h" +#include "common/fcntl.h" + +char buf[512]; + +int main(int argc, char *argv[]) +{ + int fd_src, fd_dst, n; + + if (argc != 3) + { + printf("Usage: cp src dst\n"); + exit(1); + } + + if ((fd_src = open(argv[1], O_RDONLY)) < 0) + { + printf("cp: cannot open source %s\n", argv[1]); + exit(1); + } + + if ((fd_dst = open(argv[2], O_WRONLY | O_CREATE | O_TRUNC)) < 0) + { + printf("cp: cannot create destination %s\n", argv[2]); + close(fd_src); + exit(1); + } + + while ((n = read(fd_src, buf, sizeof(buf))) > 0) + { + if (write(fd_dst, buf, n) != n) + { + printf("cp: write error\n"); + exit(1); + } + } + + if (n < 0) + { + printf("cp: read error\n"); + } + + close(fd_src); + close(fd_dst); + exit(0); +}
\ No newline at end of file |
