diff --git a/src/builtin-exec/builtin.c b/src/builtin-exec/builtin.c new file mode 100644 index 0000000..48914ae --- /dev/null +++ b/src/builtin-exec/builtin.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include "../cmd/cmd.h" +#include +#include "../env/env.h" +#include "../utils/utils.h" +#include "../../lib/bozolib/bozolib.h" +#include "../exec/exec.h" + +char* builtin_path(const char* executable) +{ + if (strcmp(executable, "cd") == 0 || strcmp(executable, "where") == 0); + else + return NULL; + return strdup(executable); +} + +int change_directory(char** args, lst** env) +{ + char cwd[PATH_MAX]; + char* oldpwd = get_env_variable(env, "PWD"); + if(len((void**)args)==1) + { + char* path = get_env_variable(env, "HOME"); + chdir(path); + } + else if (len((void**)args)>2) + { + dprintf(2, "cd : Trop d'arguments!\n"); + return 1; + } + else if (chdir(args[1]) != 0) + { + dprintf(2, "Mauvais chemin : %s\n", args[1]); + return 1; + } + edit_env_variable(env, "OLDPWD", oldpwd); + if (getcwd(cwd, sizeof(cwd)) != NULL) + edit_env_variable(env, "PWD", cwd); + return 0; +} + +int where(char** args, lst** env) +{ + if (len((void**)args)==1) + { + dprintf(2, "where : Il faut au moins 1 argument!\n"); + return 1; + } + char* exec; + for (int i=1; args[i]!=NULL; i++) + { + exec = get_executable_path(args[i], env); + if (exec == NULL) + printf("commande inconnue : %s\n", args[i]); + else if (builtin_path(exec) != NULL) + printf("%s : shell built-in command\n", exec); + else + printf("%s\n", exec); + free(exec); + } + return 0; +} + +int builtin_execute(cmd* input, lst** env) +{ + if (strcmp(input->executable, "cd") == 0) + { + change_directory(input->args, env); + return 0; + } + else if(strcmp(input->executable, "where") == 0) + { + where(input->args, env); + return 0; + } + else + { + return 1; + } +} diff --git a/src/builtin-exec/builtin.h b/src/builtin-exec/builtin.h new file mode 100644 index 0000000..e5f15c6 --- /dev/null +++ b/src/builtin-exec/builtin.h @@ -0,0 +1,9 @@ +#pragma once + +#include "../cmd/cmd.h" +#include "../../lib/bozolib/bozolib.h" + +char* builtin_path(const char* executable); +int change_directory(char** args, lst** env); +int where(char** args); +int builtin_execute(cmd* input, lst** env); diff --git a/src/exec/exec.c b/src/exec/exec.c index 3c6fc8a..a2209f9 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -8,6 +8,7 @@ #include "../env/env.h" #include "../utils/utils.h" #include "../../lib/bozolib/bozolib.h" +#include "../builtin-exec/builtin.h" int len(void** list) { @@ -16,52 +17,6 @@ int len(void** list) return index; } -char* builtin_path(const char* executable) -{ - if (strcmp(executable, "cd") == 0); - else - return NULL; - return strdup(executable); -} - -int change_directory(char** args, lst** env) -{ - char cwd[PATH_MAX]; - char* oldpwd = get_env_variable(env, "PWD"); - if(len((void**)args)==1) - { - char* path = get_env_variable(env, "HOME"); - chdir(path); - } - else if (len((void**)args)>2) - { - dprintf(2, "cd : Trop d'arguments!\n"); - return 1; - } - else if (chdir(args[1]) != 0) - { - dprintf(2, "Mauvais chemin : %s\n", args[1]); - return 1; - } - edit_env_variable(env, "OLDPWD", oldpwd); - if (getcwd(cwd, sizeof(cwd)) != NULL) - edit_env_variable(env, "PWD", cwd); - return 0; -} - -int builtin_execute(cmd* input, lst** env) -{ - if (strcmp(input->executable, "cd") == 0) - { - change_directory(input->args, env); - return 0; - } - else - { - return 1; - } -} - int execute(cmd* input, lst** env) { int pid; @@ -94,6 +49,35 @@ int execute(cmd* input, lst** env) return exitcode; } +char* get_executable_path(const char* executable, lst** env) +{ + char* builtin = builtin_path(executable); + if (builtin != NULL) + return builtin; + int size_path_str; + char * path_file; + char* path_env = get_env_variable(env, "PATH"); + char** path_env_splited =split_quoted_charset(path_env, ":"); + + for (int i=0; path_env_splited[i] != NULL; i++) + { + size_path_str = strlen(path_env_splited[i])+strlen(executable)+1; + path_file = malloc((size_path_str+1)*sizeof(char)); + strcpy(path_file, path_env_splited[i]); + strcat(path_file, "/"); + strcat(path_file, executable); + if (access(path_file, X_OK) == 0) + { + tab_free((void**)path_env_splited); + return path_file; + } + + free(path_file); + } + tab_free((void**)path_env_splited); + return NULL; +} + int cmds_list_exec(lst** cmds, lst** env) { lst* current = *cmds; @@ -117,32 +101,3 @@ int cmds_list_exec(lst** cmds, lst** env) } return 0; } - -char* get_executable_path(const char* executable, lst** env) -{ - char* builtin = builtin_path(executable); - if (builtin != NULL) - return builtin; - int size_path_str; - char * path_file; - char* path_env = get_env_variable(env, "PATH"); - char** path_env_splited =split_quoted_charset(path_env, ":"); - - for (int i=0; path_env_splited[i] != NULL; i++) - { - size_path_str = strlen(path_env_splited[i])+strlen(executable); - path_file = malloc((size_path_str+1)*sizeof(char)); - strcpy(path_file, path_env_splited[i]); - strcat(path_file, "/"); - strcat(path_file, executable); - if (access(path_file, X_OK) == 0) - { - tab_free((void**)path_env_splited); - return path_file; - } - - free(path_file); - } - tab_free((void**)path_env_splited); - return NULL; -} diff --git a/src/exec/exec.h b/src/exec/exec.h index 2773607..6df14b1 100644 --- a/src/exec/exec.h +++ b/src/exec/exec.h @@ -6,4 +6,5 @@ int cmds_list_exec(lst** cmds, lst** env); int execute(cmd* input, lst** env); int builtin_execute(cmd* input, lst** env); +int len(void** list); char* get_executable_path(const char* executable, lst** env);