diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 79 |
1 files changed, 55 insertions, 24 deletions
@@ -8,13 +8,14 @@ #include <dirent.h> #include <unistd.h> #include <stdbool.h> +#include <linux/limits.h> #define MAX_HISTORY 100 #define MAX_COMMAND_LENGTH 256 -static int CURRENT_COMMAND_INDEX = 0; -static int CURRENT_HISTORY_INDEX = 0; -static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL}; +// static int CURRENT_COMMAND_INDEX = 0; +// static int CURRENT_HISTORY_INDEX = 0; +// static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL}; void test_print() @@ -29,48 +30,76 @@ void exit_program() exit(0); } -// pwd command -void list_directories() +// ls command +void list_current_directories() { - struct dirent **namelist; - int n; - n = scandir(".", &namelist, NULL, alphasort); - if (n == -1) - { - perror("scandir"); - exit(EXIT_FAILURE); - } + fork(); + + DIR *DIRECTORY_STREAM; + DIRECTORY_STREAM = opendir("."); - while (n--) + if (DIRECTORY_STREAM == NULL) { - printf("%s\n", namelist[n]->d_name); - free(namelist[n]); + perror("ERROR_DIRECTORY_STREAM_EMPTY"); } - free(namelist[n]); - exit(EXIT_SUCCESS); + else + printf("%p", (&DIRECTORY_STREAM)); + _exit(0); + } -void print_working_directory() +char *print_working_directory() { - char current_working_directory[100]; + char *current_working_directory = malloc(sizeof(char) * 100); if(getcwd(current_working_directory, sizeof(current_working_directory) != 0)) { - printf("%s", current_working_directory); + return current_working_directory; } else { perror("getcwd() error"); + return "ERROR"; } } // echo emulation command, without variables -void echo(char *command, char *argv) +char *echo(char *command) { - if (strcmp(command, "echo") == 0) + 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++) { - printf("argv"); + if (strcmp(&command[i], " ") == 0) + { + separation_index = i; + + 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]); + } + } } + + return command_argument; + } @@ -107,6 +136,8 @@ int main(void) print_working_directory(); else if(strcmp(input, "test") == 0) test_print(); + else if(strcmp(input,"ls") == 0) + list_current_directories(); } return 0; |
