Refactor process_init_stack to reduce code duplication
This commit is contained in:
@@ -90,6 +90,7 @@ process_execute (const char *cmd)
|
|||||||
|
|
||||||
static bool process_init_stack (char *cmd_saveptr, void **esp, char *file_name);
|
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);
|
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
|
/* Make the current thread execute 'cmd', passing in a copy of the
|
||||||
command string used for processing, the saveptr used by strtok_r
|
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;
|
*esp -= align_size;
|
||||||
|
|
||||||
/* Push a null pointer sentinel inside argv. */
|
/* Push a null pointer sentinel inside argv. */
|
||||||
*esp -= sizeof (char *);
|
char *null_sentinel = NULL;
|
||||||
*(char *) *esp = 0;
|
push_var_to_stack (esp, null_sentinel);
|
||||||
|
|
||||||
/* Push pointer to the process file name to the stack. */
|
|
||||||
char **argv;
|
|
||||||
|
|
||||||
/* Push pointers to process arguments from argument linked list */
|
/* Push pointers to process arguments from argument linked list */
|
||||||
struct list_elem *e = list_begin (&arg_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);
|
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);
|
e = list_next (e);
|
||||||
free (arg_elem);
|
free (arg_elem);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Push pointer to the start of argv array. */
|
/* 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 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). */
|
/* Push fake return address (null pointer). */
|
||||||
*esp -= sizeof (char *);
|
push_var_to_stack (esp, null_sentinel);
|
||||||
*(char *) *esp = 0;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user