Skip to content
Snippets Groups Projects
Commit 569b022a authored by Sébastien Gendre's avatar Sébastien Gendre
Browse files

Modify the command struct to have an argv close to what execve ask

parent 0022fd02
No related branches found
No related tags found
No related merge requests found
......@@ -9,33 +9,34 @@ int command_from_str(char *str, cmd_t *cmd) {
char* next_arg;
//Initialize a simple command (empty, simple, foreground)
cmd->command = NULL;
cmd->argv = NULL;
cmd->argc = 0;
cmd->foreground = true;
// Extract the commmand name
cmd->command = strtok(str, DELIMITERS);
// Extract the command arguments
do {
// Get the next argument
next_arg = strtok(NULL, DELIMITERS);
if (cmd->argc == 0) {
// Extract the commmand name
next_arg = strtok(str, DELIMITERS);
} else {
// Get the next argument
next_arg = strtok(NULL, DELIMITERS);
}
// Extend the cmd->argv array witha new char pointer
cmd->argv = realloc(cmd->argv, (cmd->argc+1)*sizeof(char*));
if( cmd->argv == NULL) {
// In case the reallocation didn't work
die_errno("parse_command::realloc");
}
// Store the argument inside cmd->argv
cmd->argv[cmd->argc] = next_arg;
if (next_arg != NULL) {
// A new element will be added to cmd->argv
// A new non null element will be added to cmd->argv
cmd->argc += 1;
// Extend the cmd->argv array witha new char pointer
cmd->argv = realloc(cmd->argv, (cmd->argc)*sizeof(char*));
if( cmd->argv == NULL) {
// In case the reallocation didn't work
die_errno("parse_command::realloc");
}
// Store the argument inside cmd->argv
cmd->argv[cmd->argc-1] = next_arg;
}
} while (next_arg != NULL);
......@@ -44,8 +45,6 @@ int command_from_str(char *str, cmd_t *cmd) {
// Dispose the commande
void command_dispose(cmd_t *cmd) {
// Free the command name str
free(cmd->command);
// Free all the command arguments str inside argv
for (int i = 0; i < cmd->argc; ++i) {
free(cmd->argv[i]);
......
......@@ -16,7 +16,6 @@
// A full user command
typedef struct cmd cmd_t;
struct cmd {
char* command; // The command name
char** argv; // The command arguments
int argc; // Number of command arguments
bool foreground; // Should the command be run in foreground or background (might not be relevant for your TP depending on TP version)
......
......@@ -25,13 +25,13 @@ int interface_ask_user_input(char* user_input) {
//Print command for debugging
void interface_print_command(cmd_t* cmd) {
printf("Command:\n");
printf("\t- Name: %s\n", cmd->command);
printf("\t- Arguments count: %d\n", cmd->argc);
if(cmd->argc == 0)
printf("\t- Name: %s\n", cmd->argv[0]);
printf("\t- Arguments count: %d\n", cmd->argc - 1);
if(cmd->argc == 1)
printf("\t- No arguments\n");
else {
printf("\t- Arguments:\n");
for (int i = 0; i < cmd->argc; ++i) {
for (int i = 1; i < cmd->argc; ++i) {
printf("\t\t- %s\n", cmd->argv[i]);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment