/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* 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 #ifdef __linux__ # include #else # include #endif #include "d_define.h" #include "f_fail.h" #include "s_line.h" #include "s_struct.h" #include "u_path.h" #include "u_utils.h" static char b_absolute_path_exists(char com[]) { int 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 void b_type_get_path(char fullpath[], char com[], t_msh *msh) { if (ft_strchr(com, '/') != NULL) { if (b_absolute_path_exists(com)) { ft_strlcpy(fullpath, com, PATH_MAX); return ; } return ; } u_search_in_path(fullpath, com, PATH_MAX, msh); } static unsigned char b_check_nonbuilt(char *ptr, unsigned char ret, t_msh *msh) { char fullpath[PATH_MAX]; fullpath[0] = C_NUL; b_type_get_path(fullpath, ptr, msh); if (fullpath[0] != C_NUL) ft_printf("%s is %s\n", ptr, fullpath); else { ft_printf("minishell: type: %s: not found\n", ptr); ret = 1; } return (ret); } static unsigned char b_check_builtins(char *ptr) { char tmp[M_BUILTINS_REF_LEN]; char *tok_bu; ft_strlcpy(tmp, M_BUILTINS_REF, M_BUILTINS_REF_LEN); tok_bu = ft_strtok(tmp, ":"); while (tok_bu != NULL && ft_strncmp(ptr, tok_bu, ft_strlen(tok_bu) + 1) != 0) { tok_bu = ft_strtok(NULL, ":"); } if (tok_bu != NULL) { ft_printf("%s is a shell builtin\n", ptr); return (0); } return (1); } unsigned char b_type(char *args[], t_msh *msh) { t_lalias *p_alias; char **ptr; int 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) == 1) ret = b_check_nonbuilt(*ptr, ret, msh); ptr++; } return (ret); }