Skip to content
Snippets Groups Projects
Commit 1ff119e8 authored by a2-stratford's avatar a2-stratford
Browse files

Alex - Added Argument Passing

parent 061e88ba
No related branches found
No related tags found
No related merge requests found
......@@ -2,3 +2,4 @@ cscope.files
cscope.out
TAGS
tags
/.vs
......@@ -28,6 +28,9 @@ static bool load (const char *cmdline, void (**eip) (void), void **esp);
tid_t
process_execute (const char *file_name)
{
char *save_ptr, *real_name;
real_name = strtok_r(file_name, " ", &save_ptr);
puts(real_name)
char *fn_copy;
tid_t tid;
......@@ -207,7 +210,7 @@ struct Elf32_Phdr
#define PF_W 2 /* Writable. */
#define PF_R 4 /* Readable. */
static bool setup_stack (void **esp);
static bool setup_stack(void **esp, char **argv, int argc);
static bool validate_segment (const struct Elf32_Phdr *, struct file *);
static bool load_segment (struct file *file, off_t ofs, uint8_t *upage,
uint32_t read_bytes, uint32_t zero_bytes,
......@@ -227,6 +230,21 @@ load (const char *file_name, void (**eip) (void), void **esp)
bool success = false;
int i;
/* extract arguments */
// create a copy of file_name and operate on it (modifying it)
char file_name_copy[100];
strlcpy(file_name_copy, file_name, 100);
char *argv[255];
int argc;
char *save_ptr;
argv[0] = strtok_r(file_name_copy, " ", &save_ptr);
char *token;
argc = 1;
while ((token = strtok_r(NULL, " ", &save_ptr)) != NULL)
{
argv[argc++] = token;
}
/* Allocate and activate page directory. */
t->pagedir = pagedir_create ();
if (t->pagedir == NULL)
......@@ -315,7 +333,7 @@ load (const char *file_name, void (**eip) (void), void **esp)
}
/* Set up stack. */
if (!setup_stack (esp))
if (!setup_stack(esp, argv, argc))
goto done;
/* Start address. */
......@@ -440,7 +458,7 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage,
/* Create a minimal stack by mapping a zeroed page at the top of
user virtual memory. */
static bool
setup_stack (void **esp)
setup_stack (void **esp, char **argv, int argc)
{
uint8_t *kpage;
bool success = false;
......@@ -450,7 +468,30 @@ setup_stack (void **esp)
{
success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
if (success) {
*esp = PHYS_BASE - 12;
*esp = PHYS_BASE;
int i = argc;
// this array holds reference to differences arguments in the stack
uint32_t * arr[argc];
while (--i >= 0)
{
*esp = *esp - (strlen(argv[i]) + 1) * sizeof(char);
arr[i] = (uint32_t *)*esp;
memcpy(*esp, argv[i], strlen(argv[i]) + 1);
}
*esp = *esp - 4;
(*(int *)(*esp)) = 0;//sentinel
i = argc;
while (--i >= 0)
{
*esp = *esp - 4;//32bit
(*(uint32_t **)(*esp)) = arr[i];
}
*esp = *esp - 4;
(*(uintptr_t **)(*esp)) = (*esp + 4);
*esp = *esp - 4;
*(int *)(*esp) = argc;
*esp = *esp - 4;
(*(int *)(*esp)) = 0;
} else
palloc_free_page (kpage);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment