summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-15 18:51:24 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-15 18:51:24 +0200
commitd6cee7ca8a59f8e5348a1d5dbbab723a6db35f93 (patch)
tree4d46c1428026a9e8554c5968998fce1db4f5be26
parentAliases done (diff)
download42-minishell-d6cee7ca8a59f8e5348a1d5dbbab723a6db35f93.tar.gz
42-minishell-d6cee7ca8a59f8e5348a1d5dbbab723a6db35f93.tar.bz2
42-minishell-d6cee7ca8a59f8e5348a1d5dbbab723a6db35f93.tar.xz
42-minishell-d6cee7ca8a59f8e5348a1d5dbbab723a6db35f93.tar.zst
42-minishell-d6cee7ca8a59f8e5348a1d5dbbab723a6db35f93.zip
type builtin fix
-rw-r--r--TODO.org4
-rw-r--r--src/b_type.c68
2 files changed, 42 insertions, 30 deletions
diff --git a/TODO.org b/TODO.org
index e3374ab..9cc733d 100644
--- a/TODO.org
+++ b/TODO.org
@@ -26,7 +26,7 @@
** DONE [#C] Handle comments better #
** TODO [#C] msh ~> ./qwe.sh <---- without shebang (maybe works fine already)
** TODO [#C] Fix ~ only first pos of arg
-** TODO [#C] Fix $?
+** DONE [#C] Fix $?
* Stuff to add
** DONE [#A] builtins to pipes
@@ -43,5 +43,7 @@
** DONE [#C] alias
** DONE [#C] Read scripts
** DONE [#C] PSX rice
+** TODO [#C] if else
+** TODO [#C] <<
** TODO [#C] Termcaps
** TODO [#C] Simple arithmetics $((a + 1))
diff --git a/src/b_type.c b/src/b_type.c
index e55a87c..c09f441 100644
--- a/src/b_type.c
+++ b/src/b_type.c
@@ -22,8 +22,7 @@
#include "e_externs_next.h"
#include "u_utils.h"
-static int8_t
- absolute_path_exists(char com[])
+static int8_t absolute_path_exists(char com[])
{
int32_t fd;
DIR *dir;
@@ -41,9 +40,7 @@ static int8_t
return (0);
}
-static char
- *type_get_path(char com[],
- t_msh *msh)
+static char *type_get_path(char com[], t_msh *msh)
{
char **envpath;
char *fullpath;
@@ -70,49 +67,62 @@ static char
return (fullpath);
}
-static uint8_t
- chk_nonbuilt(char **ptr,
- t_msh *msh)
+static uint8_t b_check_nonbuilt(char *ptr, uint8_t ret, 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);
+ 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);
+ 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)
+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)
{
- char **ptr;
- char **p_bu;
- int32_t ret;
+ t_lalias *p_alias;
+ char **ptr;
+ int32_t ret;
ptr = args;
- if (!*ptr)
+ if (*ptr == NULL)
return (0);
ret = 0;
- while (*ptr)
+ while (*ptr != NULL)
{
- 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
+ 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)
{
- ret = chk_nonbuilt(ptr, msh);
+ 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);