From c9ce75a5de32b4fc76ed4c260ecde10014609c9e Mon Sep 17 00:00:00 2001
From: Joshua Saxby <joshua.a.saxby@gmail.com>
Date: Sat, 30 Nov 2019 10:32:33 +0000
Subject: [PATCH] Implement loading of first argument using strtok_r()

---
 userprog/parse_arguments.c | 20 ++++++++++++++------
 userprog/process.c         |  9 ++++++++-
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/userprog/parse_arguments.c b/userprog/parse_arguments.c
index c86364f..71abd85 100644
--- a/userprog/parse_arguments.c
+++ b/userprog/parse_arguments.c
@@ -1,11 +1,19 @@
+#include <stdio.h>
+#include <string.h>
+
 #include "userprog/argument_parsing.h"
 
 
 int parse_arguments(const char* command_line, char**argv) {
-    /*
-     * XXX: Dummy implementation which sets argv[0] to "echo" so at least there's
-     * a filename to load.
-     */
-    argv[0] = "echo";
-    return 1;
+    printf("parse_arguments(command_line) = '%s'\n", command_line);
+    int argc = 0;
+    // the state pointer required by strtok_r() for use between calls
+    char* state_pointer;
+    // first call to strtok_r() is the only one where string to parse is passed
+    argv[0] = strtok_r(command_line, " ", &state_pointer);
+    printf("parse_arguments(): argv[0] = '%s'\n", argv[0]);
+    argc++;
+    // now, parse token-by-token for any additional arguments
+    // TODO: implementation!
+    return argc;
 }
diff --git a/userprog/process.c b/userprog/process.c
index 87fd42a..ce72cf2 100644
--- a/userprog/process.c
+++ b/userprog/process.c
@@ -234,7 +234,14 @@ load (const char *command, void (**eip) (void), void **esp)
   // extract arguments (including file name)
   // NOTE: decide how many arguments we want --is 255 the standard number?
   char* argv[255];
-  int argc = parse_arguments(command, argv);
+  printf("load(command) = '%s'\n", command);
+  // copy the command string so we have a separate copy to manipulate
+  // generous length limit based on 255 arguments that are 15 chars long each
+  char command_copy[255];
+  strlcpy(command_copy, command, 255);
+  printf("load(): command_copy = '%s'\n", command_copy);
+  int argc = parse_arguments(command_copy, argv);
+  printf("load(): argv[0] = '%s'\n", argv[0]);
   char* file_name = argv[0]; // file name is always argument 0
 
   /* Allocate and activate page directory. */
-- 
GitLab