Skip to content
Snippets Groups Projects
Commit 920b9aa5 authored by Joey Le's avatar Joey Le
Browse files

Fixed a few bugs on powershell hopefully it works on tux?!?!?!

parent 65c97d44
No related branches found
No related tags found
No related merge requests found
...@@ -53,9 +53,6 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) { ...@@ -53,9 +53,6 @@ int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) {
return OK; return OK;
} }
int execute_pipeline(command_list_t *cmd_list) { int execute_pipeline(command_list_t *cmd_list) {
int num_commands = cmd_list->num; int num_commands = cmd_list->num;
int pipefd[2]; int pipefd[2];
...@@ -76,7 +73,7 @@ int execute_pipeline(command_list_t *cmd_list) { ...@@ -76,7 +73,7 @@ int execute_pipeline(command_list_t *cmd_list) {
return ERR_MEMORY; return ERR_MEMORY;
} }
if (pids[i] == 0) { if (pids[i] == 0) { // Child process
if (prev_pipe_read != -1) { if (prev_pipe_read != -1) {
dup2(prev_pipe_read, STDIN_FILENO); dup2(prev_pipe_read, STDIN_FILENO);
close(prev_pipe_read); close(prev_pipe_read);
...@@ -89,7 +86,7 @@ int execute_pipeline(command_list_t *cmd_list) { ...@@ -89,7 +86,7 @@ int execute_pipeline(command_list_t *cmd_list) {
} }
cmd_buff_t *cmd = &cmd_list->commands[i]; cmd_buff_t *cmd = &cmd_list->commands[i];
execvp(cmd->exe, cmd->argv); execvp(cmd->argv[0], cmd->argv);
perror("execvp"); perror("execvp");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} else { } else {
...@@ -135,30 +132,17 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) { ...@@ -135,30 +132,17 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
char *command = strtok_r(trimmed_line, PIPE_STRING, &saveptr1); char *command = strtok_r(trimmed_line, PIPE_STRING, &saveptr1);
while (command != NULL && command_count < CMD_MAX) { while (command != NULL && command_count < CMD_MAX) {
memset(&clist->commands[command_count], 0, sizeof(command_t)); memset(&clist->commands[command_count], 0, sizeof(cmd_buff_t));
char *token = strtok_r(command, " ", &saveptr2); char *token = strtok_r(command, " ", &saveptr2);
if (token != NULL) { if (token != NULL) {
if (strlen(token) >= EXE_MAX) {
free(original_line);
return ERR_CMD_OR_ARGS_TOO_BIG;
}
strcpy(clist->commands[command_count].exe, token);
int arg_count = 0; int arg_count = 0;
while ((token = strtok_r(NULL, " ", &saveptr2)) != NULL) { while (token != NULL && arg_count < CMD_ARGV_MAX - 1) {
if (arg_count >= ARG_MAX - 1) { clist->commands[command_count].argv[arg_count] = token;
free(original_line);
return ERR_CMD_OR_ARGS_TOO_BIG;
}
if (strlen(token) >= ARG_MAX) {
free(original_line);
return ERR_CMD_OR_ARGS_TOO_BIG;
}
strcpy(clist->commands[command_count].args[arg_count], token);
arg_count++; arg_count++;
token = strtok_r(NULL, " ", &saveptr2);
} }
clist->commands[command_count].args[arg_count] = NULL; clist->commands[command_count].argv[arg_count] = NULL;
} }
command_count++; command_count++;
...@@ -166,7 +150,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) { ...@@ -166,7 +150,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
} }
if (command != NULL) { if (command != NULL) {
// Too many commands
free(original_line); free(original_line);
return ERR_TOO_MANY_COMMANDS; return ERR_TOO_MANY_COMMANDS;
} }
...@@ -176,51 +159,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) { ...@@ -176,51 +159,6 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
return OK; return OK;
} }
/*
* Implement your exec_local_cmd_loop function by building a loop that prompts the
* user for input. Use the SH_PROMPT constant from dshlib.h and then
* use fgets to accept user input.
*
* while(1){
* printf("%s", SH_PROMPT);
* if (fgets(cmd_buff, ARG_MAX, stdin) == NULL){
* printf("\n");
* break;
* }
* //remove the trailing \n from cmd_buff
* cmd_buff[strcspn(cmd_buff,"\n")] = '\0';
*
* //IMPLEMENT THE REST OF THE REQUIREMENTS
* }
*
* Also, use the constants in the dshlib.h in this code.
* SH_CMD_MAX maximum buffer size for user input
* EXIT_CMD constant that terminates the dsh program
* SH_PROMPT the shell prompt
* OK the command was parsed properly
* WARN_NO_CMDS the user command was empty
* ERR_TOO_MANY_COMMANDS too many pipes used
* ERR_MEMORY dynamic memory management failure
*
* errors returned
* OK No error
* ERR_MEMORY Dynamic memory management failure
* WARN_NO_CMDS No commands parsed
* ERR_TOO_MANY_COMMANDS too many pipes used
*
* console messages
* CMD_WARN_NO_CMD print on WARN_NO_CMDS
* CMD_ERR_PIPE_LIMIT print on ERR_TOO_MANY_COMMANDS
* CMD_ERR_EXECUTE print on execution failure of external command
*
* Standard Library Functions You Might Want To Consider Using (assignment 1+)
* malloc(), free(), strlen(), fgets(), strcspn(), printf()
*
* Standard Library Functions You Might Want To Consider Using (assignment 2+)
* fork(), execvp(), exit(), chdir()
*/
int exec_local_cmd_loop() { int exec_local_cmd_loop() {
char cmd_buff[SH_CMD_MAX]; char cmd_buff[SH_CMD_MAX];
int rc = OK; int rc = OK;
...@@ -254,7 +192,9 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) { ...@@ -254,7 +192,9 @@ int parse_pipeline(char *cmd_line, command_list_t *clist) {
rc = ERR_MEMORY; rc = ERR_MEMORY;
} }
free_command_list(&cmd_list); for (int i = 0; i < cmd_list.num; i++) {
free(cmd_list.commands[i]._cmd_buffer);
}
} else { } else {
cmd_buff_t cmd; cmd_buff_t cmd;
if (build_cmd_buff(cmd_buff, &cmd) != OK) { if (build_cmd_buff(cmd_buff, &cmd) != OK) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment