From d32895b78e609bc5f4cc39c656d74af8d3f4d6dd Mon Sep 17 00:00:00 2001
From: Joshua Saxby <joshua.a.saxby@gmail.com>
Date: Wed, 4 Dec 2019 16:48:27 +0000
Subject: [PATCH] Implement tell()

---
 Makefile.build          |  1 +
 userprog/syscall_tell.c | 19 +++++++++++++++++++
 userprog/system_calls.h |  6 ++++++
 3 files changed, 26 insertions(+)
 create mode 100644 userprog/syscall_tell.c

diff --git a/Makefile.build b/Makefile.build
index 39f59e1..06c2627 100644
--- a/Makefile.build
+++ b/Makefile.build
@@ -73,6 +73,7 @@ userprog_SRC += userprog/syscall_write.c
 userprog_SRC += userprog/file_descriptors_map.c
 userprog_SRC += userprog/syscall_read.c
 userprog_SRC += userprog/syscall_filesize.c
+userprog_SRC += userprog/syscall_tell.c
 
 # No virtual memory code yet.
 #vm_SRC = vm/file.c			# Some file.
diff --git a/userprog/syscall_tell.c b/userprog/syscall_tell.c
new file mode 100644
index 0000000..4fa049a
--- /dev/null
+++ b/userprog/syscall_tell.c
@@ -0,0 +1,19 @@
+/*
+ * Returns the current offset into the file that the given file descriptor is at
+ *
+ * Authored by Joshua Saxby
+ */
+#include <stddef.h>
+#include "system_calls.h"
+#include "filesys/file.h"
+
+void syscall_tell(struct intr_frame *f) {
+	// pop off first int argument from interrupt frame
+	int file_descriptor = *((int*)f->esp + 1);
+	struct file *file = get_associated_file_pointer(file_descriptor);
+    /*
+     * tell() return type is unsigned so we can't return a special error code if
+     * file descriptor passed is invalid. Instead, we return 0 silently.
+     */
+	f->eax = (file != NULL) ? file_tell(file) : 0;
+}
diff --git a/userprog/system_calls.h b/userprog/system_calls.h
index b228670..ebeed4c 100644
--- a/userprog/system_calls.h
+++ b/userprog/system_calls.h
@@ -69,6 +69,12 @@ void syscall_filesize(struct intr_frame *f);
  */
 void syscall_remove(struct intr_frame *f);
 
+/*
+ * Returns the position of the next byte to be read or written in open file fd,
+ * expressed in bytes from the beginning of the file. 
+ */
+void syscall_tell(struct intr_frame *f);
+
 /*
  * special additional stuff for handling file descriptors because they're annoying
  */
-- 
GitLab