diff --git a/apps/src/init.c b/apps/src/init.c index 1d7c62b2..e8850e0a 100644 --- a/apps/src/init.c +++ b/apps/src/init.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -31,17 +32,16 @@ int main() printf("Opening %s for reading...\n", filename); - int fd = syscall(SYS_open, filename, 1); + int fd = open(filename, O_RDONLY); if (fd < 0) { perror("open"); // return 1; } - else { printf("Got fd %d\n", fd); } char buf[4096]; - ssize_t nread = syscall(SYS_read, fd, sizeof(buf), buf); + ssize_t nread = read(fd, buf, sizeof(buf)); if (nread < 0) { perror("read"); @@ -56,7 +56,7 @@ int main() printf("%s", buf); } - if (syscall(SYS_close, fd) < 0) + if (close(fd) < 0) { perror("close"); // return 1; diff --git a/libs/libc/include/fcntl.h b/libs/libc/include/fcntl.h new file mode 100644 index 00000000..df2d77a7 --- /dev/null +++ b/libs/libc/include/fcntl.h @@ -0,0 +1,17 @@ +#ifndef _FCNTL_H +#define _FCNTL_H + +#define O_RDONLY 1 + +#ifdef __cplusplus +extern "C" +{ +#endif + + int open(const char*, int); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/libs/libc/include/sys/types.h b/libs/libc/include/sys/types.h index 6147106a..bd838261 100644 --- a/libs/libc/include/sys/types.h +++ b/libs/libc/include/sys/types.h @@ -1,4 +1,8 @@ #ifndef _SYS_TYPES_H #define _SYS_TYPES_H + typedef long int pid_t; +typedef unsigned long int size_t; +typedef long int ssize_t; + #endif \ No newline at end of file diff --git a/libs/libc/include/unistd.h b/libs/libc/include/unistd.h index 0e2fe102..5ab7f8f6 100644 --- a/libs/libc/include/unistd.h +++ b/libs/libc/include/unistd.h @@ -1,10 +1,13 @@ #ifndef _UNISTD_H #define _UNISTD_H + #include + #ifdef __cplusplus extern "C" { #endif + int execv(const char*, char* const[]); int execve(const char*, char* const[], char* const[]); int execvp(const char*, char* const[]); @@ -12,7 +15,11 @@ extern "C" long syscall(long, ...); unsigned int sleep(unsigned int); + ssize_t read(int, void*, size_t); + int close(int); + #ifdef __cplusplus } #endif + #endif \ No newline at end of file diff --git a/libs/libc/src/fcntl.cpp b/libs/libc/src/fcntl.cpp new file mode 100644 index 00000000..58839006 --- /dev/null +++ b/libs/libc/src/fcntl.cpp @@ -0,0 +1,11 @@ +#include +#include +#include + +extern "C" +{ + int open(const char* pathname, int flags) + { + return (int)syscall(SYS_open, pathname, flags); + } +} \ No newline at end of file diff --git a/libs/libc/src/unistd.cpp b/libs/libc/src/unistd.cpp index bf1ca1e9..41ff2be7 100644 --- a/libs/libc/src/unistd.cpp +++ b/libs/libc/src/unistd.cpp @@ -74,4 +74,14 @@ extern "C" { return msleep(seconds * 1000); } + + ssize_t read(int fd, void* buf, size_t count) + { + return syscall(SYS_read, fd, count, buf); // yes, our read() syscall is in the wrong order. + } + + int close(int fd) + { + return (int)syscall(SYS_close, fd); + } } \ No newline at end of file