aboutsummaryrefslogtreecommitdiff
path: root/src/builtins_part_one.c
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-20 18:46:39 +0100
committerGitHub2025-01-20 18:46:39 +0100
commit9fa887da20e409c2f25fac44b57f999e508a30ea (patch)
tree7e3fee3edea18d0d81abddb89104858cfc0ec5e3 /src/builtins_part_one.c
parentaf9d1a9b39daaf1b86cf94ee629e06503d8ab6d4 (diff)
parent62845ce01f222fad126372c574c7ab084478adf0 (diff)
downloadminishell-9fa887da20e409c2f25fac44b57f999e508a30ea.tar.gz
minishell-9fa887da20e409c2f25fac44b57f999e508a30ea.zip
Merge pull request #30 from dpu-kaiser/merge_format_redir
Merge format redir
Diffstat (limited to 'src/builtins_part_one.c')
-rw-r--r--src/builtins_part_one.c144
1 files changed, 60 insertions, 84 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c
index f3bcfc0..629cc87 100644
--- a/src/builtins_part_one.c
+++ b/src/builtins_part_one.c
@@ -6,60 +6,13 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */
-/* Updated: 2025/01/14 19:51:59 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 17:05:19 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "env.h"
#include <stdio.h>
-int echo(char **av)
-{
- int i;
- int f;
-
- i = 1;
- f = 1;
- if (av[1] == NULL || av[1][0] == '\0')
- {
- write(1, "\n", 1);
- return (0);
- }
- if (ft_strncmp(av[1], "-n", 3) == 0)
- {
- i++;
- f = 0;
- }
- while (av[i])
- {
- write(1, av[i], ft_strlen(av[i]));
- i++;
- if (av[i])
- write(1, " ", 1);
- }
- if (f)
- write(1, "\n", 1);
- return (0);
-}
-
-void exit_shell(t_env **env, int exit_status)
-{
- free_envlst(env);
- exit(exit_status);
-}
-
-int builtin_exit(char **args, t_env **env)
-{
- int exit_status;
-
- if (args[1])
- exit_status = ft_atoi(args[1]);
- else
- exit_status = 0;
- exit_shell(env, exit_status);
- return (exit_status);
-}
-
int unset(char **av, t_env **env)
{
t_env *current;
@@ -69,13 +22,19 @@ int unset(char **av, t_env **env)
i = 0;
while (av[++i])
{
- if (ft_strchr(av[i], '?'))
- return (1);
current = *env;
prev = NULL;
while (current)
{
- if (ft_strcmp(current->name, av[i]) == 0)
+ if ((!ft_strcmp(current->name, av[i])) && (!ft_strcmp("?", av[1])))
+ {
+ if (prev)
+ prev->next = current->next;
+ else
+ *env = current->next;
+ free_env_node(current);
+ break ;
+ }
{
if (prev)
prev->next = current->next;
@@ -95,6 +54,8 @@ t_env *check_existing(t_env *env, char *av)
{
while (env)
{
+ if (ft_strcmp("$", av) == 0)
+ return (NULL);
if (ft_strcmp(env->name, av) == 0)
return (env);
env = env->next;
@@ -102,51 +63,66 @@ t_env *check_existing(t_env *env, char *av)
return (NULL);
}
-int export(char **av, t_env **env)
+void export_export(char *av, t_env **env)
{
char *tmp;
t_env *current;
- int i;
current = NULL;
+ tmp = ft_strchr(av, '=');
+ *tmp = '\0';
+ current = check_existing(*env, av);
+ if (current)
+ free(current->value);
+ else
+ {
+ current = env_new(ft_strdup(av));
+ current->next = *env;
+ *env = current;
+ }
+ current->value = ft_strdup(tmp + 1);
+}
+
+int is_valid_identifier(char *str)
+{
+ int i;
+
i = 0;
- while (av[++i])
+ if (!ft_isalpha(str[0]) && str[0] != '_')
+ return (0);
+ while (str[i] && str[i] != '=')
{
- if ((ft_strchr(av[i], '=')))
- {
- tmp = ft_strchr(av[i], '=');
- *tmp = '\0';
- if (ft_strchr(av[i], '?'))
- return (1);
- current = check_existing(*env, av[i]);
- if (current)
- free(current->value);
- else
- {
- current = env_new(ft_strdup(av[i]));
- current->next = *env;
- *env = current;
- }
- current->value = ft_strdup(tmp + 1);
- }
- else
- return (1);
+ if (!ft_isalnum(str[i]) && str[i] != '_')
+ return (0);
+ i++;
}
- return (0);
+ return (1);
}
-void set_return_code(int return_code, t_env **env)
+int export(char **av, t_env **env, int f)
{
- t_env *cur;
+ char *equal_sign;
+ int i;
- cur = check_existing(*env, "?");
- if (cur)
- free(cur->value);
- else
+ i = 0;
+ while (av[++i])
{
- cur = env_new(ft_strdup("?"));
- cur->next = *env;
- *env = cur;
+ equal_sign = ft_strchr(av[i], '=');
+ if (equal_sign)
+ *equal_sign = '\0';
+ if (!is_valid_identifier(av[i]))
+ {
+ write(1, "Minishell $ export: not a valid identifier\n", 43);
+ if (equal_sign)
+ *equal_sign = '=';
+ f++;
+ continue ;
+ }
+ if (equal_sign)
+ {
+ *equal_sign = '=';
+ export_export(av[i], env);
+ }
}
- cur->value = ft_itoa(return_code);
+ return (check_flag(f));
}