#include "user.h"
#include "fcntl.h"
+/* defines the temporary file hello world is written to */
+#define TEMPFILE "/tmpfile"
+
+/* define standard file descriptors - no magic numbers in code */
+#define STDIN_FD 0
+#define STDOUT_FD 1
+#define STDERR_FD 2
+
+#define LEN_HELLOWORLD 13
+#define LEN_DISCLAIMER 26
+
int
main(int argc, char *argv[])
{
- // Student code goes here
+ /* Student code goes here */
+
+ /* part (a): write "Hello, World" */
+
+ /* note file is created only when O_CREATE is specified! */
+ /* first try opening the file without O_CREATE */
+ int fd = open(TEMPFILE, O_RDONLY);
+
+ /* if no issue, read its contents and close that same file */
+ if (fd >= 0) {
+ /* since no libc no need to worry about \0, treat like binary data,
+ * we anyway supply size */
+ char hellostr[LEN_HELLOWORLD];
+ read(fd, hellostr, LEN_HELLOWORLD);
+ write(STDOUT_FD, hellostr, LEN_HELLOWORLD);
+ close(fd);
+ } else {
+ /* if there is an issue, create the file! */
+ write(STDOUT_FD, "First run - creating file\n", LEN_DISCLAIMER);
+ int wfd = open(TEMPFILE, O_CREATE | O_WRONLY);
+ write(wfd, "Hello World!\n", LEN_HELLOWORLD);
+ close(wfd);
+ }
exit();
-}
\ No newline at end of file
+}