aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2022-12-17 17:27:57 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2022-12-17 17:27:57 +0100
commit0e0f22cde8fa9a8c9e0ff509a90727092e0af58b (patch)
treece32216649fa8651d3ae29a4429427ed033f9c37
parent48ab221a6d6e4d71e00e0258dd26d63f35bb6f38 (diff)
add errno and file support
-rw-r--r--lib/sys/errno.h77
-rw-r--r--lib/sys/io.h6
-rw-r--r--lib/sys/open.h11
-rw-r--r--smash/main.c28
4 files changed, 117 insertions, 5 deletions
diff --git a/lib/sys/errno.h b/lib/sys/errno.h
new file mode 100644
index 0000000..86ae4bf
--- /dev/null
+++ b/lib/sys/errno.h
@@ -0,0 +1,77 @@
+#ifndef ERRNO_H
+#define ERRNO_H
+
+#define ERROR_PERM 1 /* Operation not permitted */
+#define ERROR_NOENT 2 /* No such file or directory */
+#define ERROR_SRCH 3 /* No such process */
+#define ERROR_INTR 4 /* Interrupted system call */
+#define ERROR_IO 5 /* I/O error */
+#define ERROR_NXIO 6 /* No such device or address */
+#define ERROR_2BIG 7 /* Argument list too long */
+#define ERROR_NOEXEC 8 /* Exec format error */
+#define ERROR_BADF 9 /* Bad file number */
+#define ERROR_CHILD 10 /* No child processes */
+#define ERROR_AGAIN 11 /* Try again */
+#define ERROR_NOMEM 12 /* Out of memory */
+#define ERROR_ACCES 13 /* Permission denied */
+#define ERROR_FAULT 14 /* Bad address */
+#define ERROR_NOTBLK 15 /* Block device required */
+#define ERROR_BUSY 16 /* Device or resource busy */
+#define ERROR_EXIST 17 /* File exists */
+#define ERROR_XDEV 18 /* Cross-device link */
+#define ERROR_NODEV 19 /* No such device */
+#define ERROR_NOTDIR 20 /* Not a directory */
+#define ERROR_ISDIR 21 /* Is a directory */
+#define ERROR_INVAL 22 /* Invalid argument */
+#define ERROR_NFILE 23 /* File table overflow */
+#define ERROR_MFILE 24 /* Too many open files */
+#define ERROR_NOTTY 25 /* Not a typewriter */
+#define ERROR_TXTBSY 26 /* Text file busy */
+#define ERROR_FBIG 27 /* File too large */
+#define ERROR_NOSPC 28 /* No space left on device */
+#define ERROR_SPIPE 29 /* Illegal seek */
+#define ERROR_ROFS 30 /* Read-only file system */
+#define ERROR_MLINK 31 /* Too many links */
+#define ERROR_PIPE 32 /* Broken pipe */
+#define ERROR_DOM 33 /* Math argument out of domain of func */
+#define ERROR_RANGE 34 /* Math result not representable */
+
+static const char *errstr[] = {
+ "",
+ "Operation not permitted",
+ "No such file or directory",
+ "No such process",
+ "Interrupted system call",
+ "I/O error",
+ "No such device or address",
+ "Argument list too long",
+ "Exec format error",
+ "Bad file number",
+ "No child processes",
+ "Try again",
+ "Out of memory",
+ "Permission denied",
+ "Bad address",
+ "Block device required",
+ "Device or resource busy",
+ "File exists",
+ "Cross-device link",
+ "No such device",
+ "Not a directory",
+ "Is a directory",
+ "Invalid argument",
+ "File table overflow",
+ "Too many open files",
+ "Not a typewriter",
+ "Text file busy",
+ "File too large",
+ "No space left on device",
+ "Illegal seek",
+ "Read-only file system",
+ "Too many links",
+ "Broken pipe",
+ "Math argument out of domain of func",
+ "Math result not representable",
+};
+
+#endif
diff --git a/lib/sys/io.h b/lib/sys/io.h
index 8086869..4222c4d 100644
--- a/lib/sys/io.h
+++ b/lib/sys/io.h
@@ -3,9 +3,15 @@
#include "write.h"
#include "read.h"
+#include "open.h"
+#include "close.h"
#define STDIN_FD 0
#define STDOUT_FD 1
#define STDERR_FD 2
+#define FILE_READ_ONLY 0
+#define FILE_WRITE_ONLY 1
+#define FILE_READ_WRITE 2
+
#endif
diff --git a/lib/sys/open.h b/lib/sys/open.h
new file mode 100644
index 0000000..5aeab6d
--- /dev/null
+++ b/lib/sys/open.h
@@ -0,0 +1,11 @@
+#ifndef OPEN_H
+#define OPEN_H
+
+#include "syscalls.h"
+
+static int open(const char *filename, int flags, int mode)
+{
+ return syscall(OPEN, filename, flags, mode);
+}
+
+#endif
diff --git a/smash/main.c b/smash/main.c
index 656eeea..57de496 100644
--- a/smash/main.c
+++ b/smash/main.c
@@ -4,6 +4,9 @@
#include "../lib/cstr/cstr.h"
#include "../lib/sys/dup2.h"
+#include "../lib/io/io.h"
+#include "../lib/sys/errno.h"
+
#define BUFSIZ 1024
void clear_buf(char *buf)
@@ -17,20 +20,35 @@ int main(int argc, char *argv[], char *envp[])
char buf[BUFSIZ] = {0};
char prompt[] = "$ ";
u64 line_length;
+ int fd = STDIN_FD;
+
+ if (argc == 2) {
+ fd = open(argv[1], FILE_READ_ONLY, 0);
+
+ if (fd < 0)
+ wstdf("%s\n", errstr[-fd]);
+ }
while (1) {
- write(STDOUT_FD, prompt, cstr_length(prompt));
- read(STDIN_FD, buf, 1024);
+ if (fd == STDIN_FD)
+ write(STDOUT_FD, prompt, cstr_length(prompt));
+ int p = read(fd, buf, BUFSIZ);
line_length = cstr_length(buf);
+
if (line_length == 0)
- return 0;
+ break;
+
+ if (buf[line_length - 1] == '\n')
+ buf[--line_length] = 0;
- if (line_length > 1) {
- buf[line_length - 1] = 0;
+ if (line_length > 0) {
exec(buf);
}
clear_buf(buf);
}
+
+ if (fd != STDOUT_FD)
+ close(fd);
}