summaryrefslogtreecommitdiffstats
path: root/src/b_type.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-04 15:52:47 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-04 15:52:47 +0200
commit5cb3373a2e5a5109a5d3b72ef45978b98f885706 (patch)
tree11a0a6f467da3ddf227d2eaf6824c4509242025e /src/b_type.c
parentok nice (diff)
parent$? fix (diff)
download42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.gz
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.bz2
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.xz
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.zst
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.zip
Merge branch 'master' into fix-pwd
Diffstat (limited to 'src/b_type.c')
-rw-r--r--src/b_type.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/b_type.c b/src/b_type.c
new file mode 100644
index 0000000..00bad1a
--- /dev/null
+++ b/src/b_type.c
@@ -0,0 +1,120 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* b_type.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <stdint.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "f_fail.h"
+#include "s_lcom.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]))
+ {
+ if (absolute_path_exists(com))
+ {
+ if (!(fullpath = ft_strdup(com)))
+ {
+ lcom_clear(&msh->curr);
+ f_fail_alloc(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
+ chk_nonbuilt(char **ptr,
+ t_msh *msh)
+{
+ char *fullpath;
+ int32_t ret;
+
+ ret = 0;
+ fullpath = 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
+ b_type(char *args[],
+ t_msh *msh)
+{
+ char **ptr;
+ char **p_bu;
+ int32_t ret;
+
+ ptr = args;
+ if (!*ptr)
+ return (0);
+ ret = 0;
+ while (*ptr)
+ {
+ p_bu = msh->bu_ref;
+ while (*p_bu && ft_strncmp(*ptr, *p_bu, ft_strlen(*p_bu) + 1))
+ p_bu++;
+ if (*p_bu != NULL)
+ ft_printf("%s is a shell builtin\n", *ptr);
+ else
+ {
+ ret = chk_nonbuilt(ptr, msh);
+ }
+ ptr++;
+ }
+ return (ret);
+}