lab 2 cleanup

This commit is contained in:
2023-02-23 22:48:09 -06:00
parent d963d07d65
commit 5db840787d
3 changed files with 27 additions and 28 deletions

Binary file not shown.

View File

@@ -61,7 +61,7 @@ int parse_commands(char input[INPUT_MAX], char cmd_strs[CMD_MAX][INPUT_MAX])
{ {
if (cmd_count >= CMD_MAX) if (cmd_count >= CMD_MAX)
{ {
fprintf(stderr, "Too many commands\n"); fprintf(stderr, "\nToo many commands\n");
return -1; return -1;
} }
strncpy(cmd_strs[cmd_count], cmd_ptr, INPUT_MAX); strncpy(cmd_strs[cmd_count], cmd_ptr, INPUT_MAX);
@@ -79,8 +79,7 @@ int main()
int quit_flag = 0; int quit_flag = 0;
// TODO need to be able to get input from // Get user input in a loop
// the user in a loop
while(quit_flag == 0) { while(quit_flag == 0) {
// Print the input prompt // Print the input prompt
@@ -92,7 +91,7 @@ int main()
return 1; return 1;
} }
// TODO: Figure out how to handle the 'quit' command // Set quit flag
if (strcmp(user_input, "quit") == 0){ if (strcmp(user_input, "quit") == 0){
printf("Quitting...\n"); printf("Quitting...\n");
quit_flag = 1; quit_flag = 1;
@@ -107,8 +106,8 @@ int main()
} }
// Chop the commands into arguments and execute one at a time // Chop the commands into arguments and execute one at a time
if(!quit_flag) {
for (int i = 0; i < cmd_count; i++) { for (int i = 0; i < cmd_count; i++) {
char **args = malloc(ARG_MAX*sizeof(char *)); char **args = malloc(ARG_MAX*sizeof(char *));
char* str = malloc(10*sizeof(char)); char* str = malloc(10*sizeof(char));
strncpy(str, strtok(cmd_strs[i], " "), 10); strncpy(str, strtok(cmd_strs[i], " "), 10);
@@ -120,7 +119,6 @@ int main()
j++; j++;
} }
pid_t pid = fork(); pid_t pid = fork();
if(pid == 0) { if(pid == 0) {
execvp(args[0], args); execvp(args[0], args);
printf("Command not found: %s\n", args[0]); printf("Command not found: %s\n", args[0]);
@@ -134,5 +132,6 @@ int main()
} }
} }
} }
}
return 0; return 0;
} }

Binary file not shown.