diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 143 |
1 files changed, 56 insertions, 87 deletions
@@ -15,141 +15,104 @@ // static int CURRENT_HISTORY_INDEX = 0; // static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL}; -struct com_struct -{ +struct com_struct { char *com; char *arg; }; -void test() -{ +void test() { printf("print to output"); } // exit() command -void exit_() -{ - exit(0); +void exit_() { + exit(0); } // ls command // get the requested path and put it into a const later on when getting the user input -void ls(const char *path) -{ - fork(); - +void ls(const char *path) { struct dirent *entry; DIR *dP = opendir(path); - // check if the directory got opened succefully - if (dP == NULL) - { + // check if the directory got opened successfully + if (dP == NULL) { perror("opendir"); return; } // print the folder|directory name - while((entry = readdir(dP)) != NULL) - { + while ((entry = readdir(dP)) != NULL) { printf("s\n", entry->d_name); } closedir(dP); - } // pwd command -char *pwd() -{ +char *pwd() { char *current_working_directory = malloc(sizeof(char) * 100); - - if(getcwd(current_working_directory, sizeof(current_working_directory) != 0)) - { + + if (getcwd(current_working_directory, sizeof(current_working_directory) != 0)) { return current_working_directory; - } - else - { + } else { perror("getcwd() error"); return "ERROR"; } } // echo command -char *echo(char *command) -{ - char *argv = (char *)malloc(sizeof(char) * 4); - char *command_argument = (char *)malloc(sizeof(char) * 100); +char *echo(char *command) { + char *argv = (char *) malloc(sizeof(char) * 4); + char *command_argument = (char *) malloc(sizeof(char) * 100); int separation_index = 0; - for (int i = 0; i < sizeof(command); i++) - { - if (strcmp(&command[i], " ") == 0) - { + for (int i = 0; i < sizeof(command); i++) { + if (strcmp(&command[i], " ") == 0) { separation_index = i; - for ( int j = 0; j < separation_index; j++) - { + for (int j = 0; j < separation_index; j++) { argv = &command[j]; } } } - if (strcmp(argv, "echo") == 0) - { - - for (int i = separation_index + 1; i < (sizeof(command) - separation_index); i++) - { - if(strcmp(&command[i], "\0") != 0) - { - break; - } - else - { - strcpy(&command_argument[i - (separation_index - 1)], &command[i]); - } + if (strcmp(argv, "echo") == 0) { + + for (int i = separation_index + 1; i < (sizeof(command) - separation_index); i++) { + if (strcmp(&command[i], "\0") != 0) { + break; + } else { + strcpy(&command_argument[i - (separation_index - 1)], &command[i]); + } } } - return command_argument; - } -struct com_struct split_command(char *input) -{ - - - // command bestaat uit 2 delen - // het commando zelf - // en argumenten voor het commado - // we moeten het commando opsplitsen in 2 delen - // we moeten een struct gebruiken +struct com_struct split_command(char *input) { struct com_struct NEW_COMMAND; - char *command = (char *)malloc(sizeof(char) * 4); - char *argument = (char *)malloc(sizeof(char) * 100); + char *command = (char *) malloc(sizeof(char) * 4); + char *argument = (char *) malloc(sizeof(char) * 100); - if (command == NULL || argument == NULL) - { + if (command == NULL || argument == NULL) { printf("found a null pointer when dynamically assigning memory to the command or variable argument"); exit(1); } int space_position = 0; - for (int i = 0; i < sizeof(command); i++) - { - if (strcmp(&input[i], " ") == 0) - { + for (int i = 0; i < sizeof(command); i++) { + if (strcmp(&input[i], " ") == 0) { space_position = i; - for ( int j = 0; j < space_position; j++) - { + for (int j = 0; j < space_position; j++) { command = &input[j]; } - for (int x = space_position; x < sizeof(input) - space_position; x++) - { + for (int x = space_position; x < sizeof(input) - space_position; x++) { argument = &input[x]; } } - + } NEW_COMMAND.com = command; NEW_COMMAND.arg = argument; @@ -158,22 +121,28 @@ struct com_struct split_command(char *input) } -int main(int argc, char *argv[]) -{ - char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); - while (true) - { - printf("\nSHELL> "); - scanf("%s", input); +int main(void) { + char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); + + do { + if (getchar()) { + printf("\nSHELL> "); + scanf("%s", input); + + struct com_struct new_input = split_command(input); - // input converted to struct with members: command and argument - struct com_struct new_input = split_command(input); + if (strcmp(new_input.com, "ls") == 0) { + char *path; + path = (char *) malloc(sizeof(char) * 100); + + strcpy(path, new_input.arg); + ls(path); + } else if (strcmp(new_input.com, "test") == 0){ + test(); + } + } + } + while (true); - if (strcmp(new_input.com, "ls")) - { - // failed logic, argv is for when executing the program, not for calling functions - } - } - return 0; } |
