diff options
-rw-r--r-- | LibC/stdio.cpp | 5 | ||||
-rw-r--r-- | LibC/stdio.h | 1 | ||||
-rw-r--r-- | LibC/stdlib.cpp | 5 | ||||
-rw-r--r-- | LibC/stdlib.h | 1 | ||||
-rw-r--r-- | LibC/string.cpp | 11 | ||||
-rw-r--r-- | LibC/string.h | 1 | ||||
-rw-r--r-- | LibC/unistd.cpp | 20 | ||||
-rw-r--r-- | LibC/unistd.h | 1 | ||||
-rw-r--r-- | Userland/sh.cpp | 8 |
9 files changed, 51 insertions, 2 deletions
diff --git a/LibC/stdio.cpp b/LibC/stdio.cpp index 5153983047..ca9cd549b3 100644 --- a/LibC/stdio.cpp +++ b/LibC/stdio.cpp @@ -133,6 +133,11 @@ int getchar() return getc(stdin); } +int ungetc(int, FILE*) +{ + ASSERT_NOT_REACHED(); +} + int fputc(int ch, FILE* stream) { assert(stream); diff --git a/LibC/stdio.h b/LibC/stdio.h index b52b0f5680..11816f6182 100644 --- a/LibC/stdio.h +++ b/LibC/stdio.h @@ -40,6 +40,7 @@ int fileno(FILE*); int fgetc(FILE*); int getc(FILE*); int getchar(); +int ungetc(int c, FILE*); FILE* fdopen(int fd, const char* mode); FILE* fopen(const char* pathname, const char* mode); int fclose(FILE*); diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp index b5a179ac98..74fcfd9436 100644 --- a/LibC/stdlib.cpp +++ b/LibC/stdlib.cpp @@ -262,4 +262,9 @@ void srandom(unsigned seed) srand(seed); } +int system(const char* command) +{ + return execl("/bin/sh", "sh", "-c", command, nullptr); +} + } diff --git a/LibC/stdlib.h b/LibC/stdlib.h index 028e9d4a91..26b3a8739d 100644 --- a/LibC/stdlib.h +++ b/LibC/stdlib.h @@ -21,6 +21,7 @@ void abort() __NORETURN; char* ptsname(int fd); int ptsname_r(int fd, char* buffer, size_t); int abs(int); +int system(const char* command); #define RAND_MAX 32767 int rand(); diff --git a/LibC/string.cpp b/LibC/string.cpp index c5284fa936..79d425dc4f 100644 --- a/LibC/string.cpp +++ b/LibC/string.cpp @@ -186,6 +186,17 @@ char* strchr(const char* str, int c) } } +void* memchr(const void* ptr, int c, size_t size) +{ + char ch = c; + char* cptr = (char*)ptr; + for (size_t i = 0; i < size; ++i) { + if (cptr[i] == ch) + return cptr + i; + } + return nullptr; +} + char* strrchr(const char* str, int ch) { char *last = nullptr; diff --git a/LibC/string.h b/LibC/string.h index 316cd944b0..c2d501a8a7 100644 --- a/LibC/string.h +++ b/LibC/string.h @@ -11,6 +11,7 @@ int strncmp(const char*, const char*, size_t); int memcmp(const void*, const void*, size_t); void* memcpy(void*, const void*, size_t); void* memmove(void*, const void*, size_t); +void* memchr(const void*, int c, size_t); void bzero(void*, size_t); void bcopy(const void*, void*, size_t); void* memset(void*, int, size_t); diff --git a/LibC/unistd.cpp b/LibC/unistd.cpp index 6f3b16598c..3064e438c9 100644 --- a/LibC/unistd.cpp +++ b/LibC/unistd.cpp @@ -9,6 +9,7 @@ #include <sys/ioctl.h> #include <sys/types.h> #include <Kernel/Syscall.h> +#include <AK/Vector.h> extern "C" { @@ -26,9 +27,28 @@ int execve(const char* filename, char* const argv[], char* const envp[]) int execvp(const char* filename, char* const argv[]) { + // FIXME: This should do some sort of shell-like path resolution! return execve(filename, argv, nullptr); } +int execl(const char* filename, const char* arg0, ...) +{ + Vector<const char*> args; + args.append(arg0); + + va_list ap; + va_start(ap, arg0); + for (;;) { + const char* arg = va_arg(ap, const char*); + if (!arg) + break; + args.append(arg); + } + va_end(ap); + + return execve(filename, (char* const *)args.data(), nullptr); +} + uid_t getuid() { return syscall(SC_getuid); diff --git a/LibC/unistd.h b/LibC/unistd.h index f93fc35f37..a88a11b7c6 100644 --- a/LibC/unistd.h +++ b/LibC/unistd.h @@ -13,6 +13,7 @@ inline int getpagesize() { return 4096; } pid_t fork(); int execve(const char* filename, char* const argv[], char* const envp[]); int execvp(const char* filename, char* const argv[]); +int execl(const char* filename, const char* arg, ...); void sync(); void _exit(int status); pid_t getsid(pid_t); diff --git a/Userland/sh.cpp b/Userland/sh.cpp index 0c1391f5b2..0fc4ee4642 100644 --- a/Userland/sh.cpp +++ b/Userland/sh.cpp @@ -358,13 +358,12 @@ static void greeting() printf("\n%s/%s on %s\n\n", uts.sysname, uts.machine, g->ttyname); } -int main(int, char**) +int main(int argc, char** argv) { g = new GlobalState; g->uid = getuid(); g->sid = setsid(); tcsetpgrp(0, getpgrp()); - tcgetattr(0, &g->termios); { @@ -391,6 +390,11 @@ int main(int, char**) endpwent(); } + if (argc > 1 && !strcmp(argv[1], "-c")) { + fprintf(stderr, "FIXME: Implement /bin/sh -c\n"); + return 1; + } + greeting(); char linebuf[128]; |