mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:17:35 +00:00
Userland: Make a simple /bin/cp for copying files.
This commit is contained in:
parent
a34bb07abb
commit
15fad649ea
4 changed files with 49 additions and 0 deletions
|
@ -43,6 +43,7 @@ cp -v ../Userland/touch mnt/bin/touch
|
||||||
cp -v ../Userland/sync mnt/bin/sync
|
cp -v ../Userland/sync mnt/bin/sync
|
||||||
cp -v ../Userland/more mnt/bin/more
|
cp -v ../Userland/more mnt/bin/more
|
||||||
cp -v ../Userland/rm mnt/bin/rm
|
cp -v ../Userland/rm mnt/bin/rm
|
||||||
|
cp -v ../Userland/cp mnt/bin/cp
|
||||||
cp -v ../Userland/guitest mnt/bin/guitest
|
cp -v ../Userland/guitest mnt/bin/guitest
|
||||||
cp -v ../Userland/guitest2 mnt/bin/guitest2
|
cp -v ../Userland/guitest2 mnt/bin/guitest2
|
||||||
cp -v ../Userland/sysctl mnt/bin/sysctl
|
cp -v ../Userland/sysctl mnt/bin/sysctl
|
||||||
|
|
1
Userland/.gitignore
vendored
1
Userland/.gitignore
vendored
|
@ -25,3 +25,4 @@ guitest
|
||||||
guitest2
|
guitest2
|
||||||
sysctl
|
sysctl
|
||||||
rm
|
rm
|
||||||
|
cp
|
||||||
|
|
|
@ -22,6 +22,7 @@ OBJS = \
|
||||||
guitest.o \
|
guitest.o \
|
||||||
guitest2.o \
|
guitest2.o \
|
||||||
sysctl.o \
|
sysctl.o \
|
||||||
|
cp.o \
|
||||||
rm.o
|
rm.o
|
||||||
|
|
||||||
APPS = \
|
APPS = \
|
||||||
|
@ -49,6 +50,7 @@ APPS = \
|
||||||
guitest \
|
guitest \
|
||||||
guitest2 \
|
guitest2 \
|
||||||
sysctl \
|
sysctl \
|
||||||
|
cp \
|
||||||
rm
|
rm
|
||||||
|
|
||||||
ARCH_FLAGS =
|
ARCH_FLAGS =
|
||||||
|
@ -141,6 +143,9 @@ guitest2: guitest2.o
|
||||||
sysctl: sysctl.o
|
sysctl: sysctl.o
|
||||||
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
||||||
|
|
||||||
|
cp: cp.o
|
||||||
|
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
||||||
|
|
||||||
rm: rm.o
|
rm: rm.o
|
||||||
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
|
||||||
|
|
||||||
|
|
42
Userland/cp.cpp
Normal file
42
Userland/cp.cpp
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
|
if (argc != 3) {
|
||||||
|
printf("usage: cp <source> <destination>\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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue