diff options
author | Andreas Kling <awesomekling@gmail.com> | 2018-10-26 11:16:56 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2018-10-26 11:16:56 +0200 |
commit | df87dda63ca3e363913341ac8e96f9a2523b9196 (patch) | |
tree | c5dae84ec1e489b0b8b8a802eab86cf769371774 /Userland | |
parent | 53abfa7ea1ef7ea1b9e47ca9818e48ab0fa32b8b (diff) | |
download | serenity-df87dda63ca3e363913341ac8e96f9a2523b9196.zip |
Implement argc/argv support for spawned tasks.
Celebrate the new functionality with a simple /bin/cat implementation. :^)
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/.gitignore | 3 | ||||
-rw-r--r-- | Userland/Makefile | 9 | ||||
-rw-r--r-- | Userland/cat.cpp | 30 | ||||
-rw-r--r-- | Userland/sh.cpp | 15 |
4 files changed, 52 insertions, 5 deletions
diff --git a/Userland/.gitignore b/Userland/.gitignore index ffdbff1187..2c52d710f8 100644 --- a/Userland/.gitignore +++ b/Userland/.gitignore @@ -1,3 +1,4 @@ +*.o id sh ps @@ -8,4 +9,4 @@ date false true hostname -*.o +cat diff --git a/Userland/Makefile b/Userland/Makefile index c5a8b4e209..d433c7422d 100644 --- a/Userland/Makefile +++ b/Userland/Makefile @@ -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 $< diff --git a/Userland/cat.cpp b/Userland/cat.cpp new file mode 100644 index 0000000000..ce99c6499b --- /dev/null +++ b/Userland/cat.cpp @@ -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; +} diff --git a/Userland/sh.cpp b/Userland/sh.cpp index 35905dc861..b4adda1731 100644 --- a/Userland/sh.cpp +++ b/Userland/sh.cpp @@ -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; |