1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 04:27:45 +00:00

Implement argc/argv support for spawned tasks.

Celebrate the new functionality with a simple /bin/cat implementation. :^)
This commit is contained in:
Andreas Kling 2018-10-26 11:16:56 +02:00
parent 53abfa7ea1
commit df87dda63c
13 changed files with 105 additions and 17 deletions

3
Userland/.gitignore vendored
View file

@ -1,3 +1,4 @@
*.o
id
sh
ps
@ -8,4 +9,4 @@ date
false
true
hostname
*.o
cat

View file

@ -8,7 +8,8 @@ OBJS = \
date.o \
true.o \
false.o \
hostname.o
hostname.o \
cat.o
APPS = \
id \
@ -20,7 +21,8 @@ APPS = \
date \
true \
false \
hostname
hostname \
cat
ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib
@ -70,6 +72,9 @@ false: false.o
hostname: hostname.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
cat: cat.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

30
Userland/cat.cpp Normal file
View file

@ -0,0 +1,30 @@
#include <LibC/stdio.h>
#include <LibC/unistd.h>
#include <LibC/errno.h>
#include <LibC/string.h>
int main(int argc, char** argv)
{
if (argc != 2) {
printf("usage: cat <file>\n");
return 1;
}
int fd = open(argv[1]);
if (fd == -1) {
printf("failed to open %s: %s\n", argv[1], strerror(errno));
return 1;
}
for (;;) {
char buf[128];
ssize_t nread = read(fd, buf, sizeof(buf));
if (nread == 0)
break;
if (nread < 0) {
printf("read() error: %s\n", strerror(errno));
return 2;
}
for (ssize_t i = 0; i < nread; ++i)
putchar(buf[i]);
}
return 0;
}

View file

@ -16,10 +16,21 @@ static int runcmd(char* cmd)
{
if (cmd[0] == 0)
return 0;
//printf("command: '%s'\n", cmd);
char buf[128];
sprintf(buf, "/bin/%s", cmd);
int ret = spawn(buf);
const char* argv[32];
size_t argi = 1;
argv[0] = &buf[0];
size_t buflen = strlen(buf);
for (size_t i = 0; i < buflen; ++i) {
if (buf[i] == ' ') {
buf[i] = '\0';
argv[argi++] = &buf[i + 1];
}
}
argv[argi + 1] = nullptr;
int ret = spawn(argv[0], argv);
if (ret == -1) {
printf("spawn failed: %s (%s)\n", cmd, strerror(errno));
return 1;