diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-27 16:41:06 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-04-27 16:41:06 +0200 |
commit | aa8af198bbe1ea7ba0db60f07b1880d181447336 (patch) | |
tree | 50ff8aa1114aaeb175a4f412710c3ba2760def01 /src | |
parent | Fixed relative pathn exec (diff) | |
download | 42-minishell-aa8af198bbe1ea7ba0db60f07b1880d181447336.tar.gz 42-minishell-aa8af198bbe1ea7ba0db60f07b1880d181447336.tar.bz2 42-minishell-aa8af198bbe1ea7ba0db60f07b1880d181447336.tar.xz 42-minishell-aa8af198bbe1ea7ba0db60f07b1880d181447336.tar.zst 42-minishell-aa8af198bbe1ea7ba0db60f07b1880d181447336.zip |
Finished and normed type
Diffstat (limited to 'src')
-rw-r--r-- | src/ft_b_type.c | 72 | ||||
-rw-r--r-- | src/ft_e_externs_next.c | 1 |
2 files changed, 57 insertions, 16 deletions
diff --git a/src/ft_b_type.c b/src/ft_b_type.c index f6601d8..4338b32 100644 --- a/src/ft_b_type.c +++ b/src/ft_b_type.c @@ -12,13 +12,37 @@ #include <libft.h> #include <stdint.h> +#include <dirent.h> +#include <fcntl.h> +#include <unistd.h> +#include "ft_f_fail.h" +#include "ft_s_lcom.h" #include "ft_s_struct.h" #include "ft_e_externs_next.h" #include "ft_u_utils.h" +static int8_t + ft_absolute_path_exists(char com[]) +{ + int32_t fd; + DIR *dir; + + if ((dir = opendir(com)) != NULL) + { + closedir(dir); + return (0); + } + if ((fd = open(com, O_RDONLY)) != -1) + { + close(fd); + return (1); + } + return (0); +} + static char - *ft_type_get_path(char *com, + *ft_type_get_path(char com[], t_msh *msh) { char **envpath; @@ -28,7 +52,16 @@ static char fullpath = NULL; if (ft_ischarset("/.", com[0])) { - /* TODO: ft_get_absolute_path(ptr->com); */ + if (ft_absolute_path_exists(com)) + { + if (!(fullpath = ft_strdup(com))) + { + ft_lcom_clear(&msh->curr); + ft_fail_alloc(msh); + } + return (fullpath); + } + return (NULL); } else if ((envpath = ft_get_env_path(msh)) != NULL) { @@ -38,19 +71,37 @@ static char return (fullpath); } +static uint8_t + ft_chk_nonbuilt(char **ptr, + t_msh *msh) +{ + char *fullpath; + int32_t ret; + + ret = 0; + fullpath = ft_type_get_path(*ptr, msh); + if (fullpath) + ft_printf("%s is %s\n", *ptr, fullpath); + else + { + ft_printf("minishell: type: %s: not found\n", *ptr); + ret = 1; + } + ft_memdel((void*)&fullpath); + return (ret); +} + uint8_t ft_b_type(char *args[], t_msh *msh) { - /* TODO: norme */ char **ptr; char **p_bu; - char *fullpath; int32_t ret; ptr = args; if (!*ptr) - return (1); + return (0); ret = 0; while (*ptr) { @@ -61,18 +112,9 @@ uint8_t ft_printf("%s is a shell builtin\n", *ptr); else { - fullpath = ft_type_get_path(*ptr, msh); - if (fullpath) - ft_printf("%s is %s\n", *ptr, fullpath); - else - { - ft_printf("minishell: type: %s: not found\n", *ptr); - ret = 1; - } - ft_memdel((void*)&fullpath); + ret = ft_chk_nonbuilt(ptr, msh); } ptr++; } - /* TODO: finish type */ return (ret); } diff --git a/src/ft_e_externs_next.c b/src/ft_e_externs_next.c index ab5f150..36bf5e6 100644 --- a/src/ft_e_externs_next.c +++ b/src/ft_e_externs_next.c @@ -53,7 +53,6 @@ char char *fullpath; DIR *dir; - (void)com; p_path = envpath; while (*p_path) { |