Refactor process_init_stack to reduce code duplication

This commit is contained in:
Themis Demetriades
2024-11-10 14:34:38 +00:00
parent 324301e7b3
commit 8b2fc86b51

View File

@@ -90,6 +90,7 @@ process_execute (const char *cmd)
static bool process_init_stack (char *cmd_saveptr, void **esp, char *file_name);
static void *push_to_stack (void **esp, void *data, size_t data_size);
#define push_var_to_stack(esp, var) (push_to_stack (esp, &var, sizeof (var)))
/* Make the current thread execute 'cmd', passing in a copy of the
command string used for processing, the saveptr used by strtok_r
@@ -196,11 +197,8 @@ process_init_stack (char *cmd_saveptr, void **esp, char *file_name)
*esp -= align_size;
/* Push a null pointer sentinel inside argv. */
*esp -= sizeof (char *);
*(char *) *esp = 0;
/* Push pointer to the process file name to the stack. */
char **argv;
char *null_sentinel = NULL;
push_var_to_stack (esp, null_sentinel);
/* Push pointers to process arguments from argument linked list */
struct list_elem *e = list_begin (&arg_list);
@@ -209,21 +207,21 @@ process_init_stack (char *cmd_saveptr, void **esp, char *file_name)
{
struct arg_elem *arg_elem = list_entry (e, struct arg_elem, elem);
argv = push_to_stack (esp, &arg_elem->arg, sizeof (arg_elem->arg));
push_var_to_stack(esp, arg_elem->arg);
e = list_next (e);
free (arg_elem);
}
/* Push pointer to the start of argv array. */
push_to_stack (esp, &argv, sizeof(argv));
char **argv = *esp;
push_var_to_stack(esp, argv);
/* Push the number of arguments to the stack. */
push_to_stack (esp, &arg_count, sizeof (arg_count));
push_var_to_stack(esp, arg_count);
/* Push fake return address (null pointer). */
*esp -= sizeof (char *);
*(char *) *esp = 0;
push_var_to_stack (esp, null_sentinel);
return true;
}