/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* b_type.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #include #include #include "f_fail.h" #include "s_line.h" #include "s_struct.h" #include "e_externs_next.h" #include "u_utils.h" static int8_t 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 *type_get_path(char com[], t_msh *msh) { char **envpath; char *fullpath; envpath = NULL; fullpath = NULL; if (ft_ischarset("/.", com[0]) == TRUE) { if (absolute_path_exists(com)) { if (!(fullpath = ft_strdup(com))) { f_alloc_and_destroy_msh(msh); } return (fullpath); } return (NULL); } else if ((envpath = get_env_path(msh)) != NULL) { fullpath = search_in_path(com, envpath, msh); ft_delwords(envpath); } return (fullpath); } static uint8_t b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh) { char *fullpath; fullpath = type_get_path(ptr, msh); if (fullpath != NULL) 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); } static uint8_t b_check_builtins(char *ptr, t_msh *msh) { char **p_bu; p_bu = msh->bu_ref; while (*p_bu != NULL && ft_strncmp(ptr, *p_bu, ft_strlen(*p_bu) + 1) != 0) { p_bu++; } if (*p_bu != NULL) { ft_printf("%s is a shell builtin\n", ptr); return (0); } return (1); } uint8_t b_type(char *args[], t_msh *msh) { t_lalias *p_alias; char **ptr; int32_t ret; ptr = args; if (*ptr == NULL) return (0); ret = 0; while (*ptr != NULL) { p_alias = msh->alias; while (p_alias != NULL && ft_strncmp(*ptr, p_alias->name, ft_strlen(p_alias->name) + 1) != 0) p_alias = p_alias->next; if (p_alias != NULL) { ft_printf("%s is aliased to `%s'\n", *ptr, p_alias->val); } else if (b_check_builtins(*ptr, msh) == 1) ret = b_check_nonbuilt(*ptr, ret, msh); ptr++; } return (ret); }