This commit is contained in:
Loic Deridder
2025-02-12 10:10:29 +01:00
parent ced584b17a
commit adc59eb33b
20 changed files with 822 additions and 662 deletions

View File

@@ -37,7 +37,8 @@ int builtin_cd(char **arg, t_ast_n *head)
{
char *path;
if (count_var(arg) == 1 || (count_var(arg) == 2 && ft_strncmp(arg[1], "~", 1) == 0))
if (count_var(arg) == 1 || (count_var(arg) == 2
&& ft_strncmp(arg[1], "~", 1) == 0))
{
path = get_var_value("HOME", head->msh->env);
if (!path)

View File

@@ -46,27 +46,9 @@ char *ft_getenv(char *str, char **envp)
return (&envp[j][len]);
}
int extractenv(char *str, char **envp)
int print_exit(char **arg, t_ast_n *node)
{
int i;
char *var;
char *tmp;
i = 0;
while (str[i] && str[i] != ' ')
i++;
if (i >= 1)
tmp = ft_substr(str, 1, i - 1);
var = get_var_value(tmp, envp);
free(tmp);
if (var)
ft_printf("%s", var);
return (i);
}
int print_exit(char *arg, t_ast_n *node)
{
if (arg && ft_strncmp(arg, "$?", 2) == 0)
if (*arg && ft_strncmp(*arg, "$?", 2) == 0)
{
ft_fprintf(1, "%d", node->msh->ex_code);
return (1);
@@ -84,15 +66,8 @@ static void echo_print(t_ast_n *node, int j, char **envp)
i = 0;
while (node->args[j][i])
{
if (print_exit(node->args[j], node))
break ;
// if (node->args[j][i] == '$')
// {
// if (!node->args[j][i + 1] || node->args[j][i + 1] == ' ')
// ft_put_c(node->args[j][i++]);
// else
// i += extractenv(&node->args[j][i], envp);
// }
if (print_exit(&node->args[j], node))
i += 2;
else
ft_put_c(node->args[j][i++]);
}

View File

@@ -19,7 +19,7 @@ int is_export_valid(char *str)
if (!ft_isalpha(*str) && str[0] != '_')
return (0);
equal = ft_strchr(str, '=');
if(!equal)
if (!equal)
{
while (*(++str))
{
@@ -43,7 +43,7 @@ char **key_value(char *str)
{
char **tmp;
char *save;
char *equal;
char *equal;
tmp = malloc(sizeof(char *) * (2 + 1));
if (!tmp)
@@ -85,7 +85,6 @@ int print_export(char **envp)
{
int i;
int j;
char *tmp;
int len;
i = -1;
@@ -97,12 +96,9 @@ int print_export(char **envp)
j = 0;
while (j < len - i - 1)
{
if (ft_strncmp(envp[j], envp[j + 1], ft_strchr(envp[j], '=') - envp[j]) > 0)
{
tmp = envp[j];
envp[j] = envp[j + 1];
envp[j + 1] = tmp;
}
if (ft_strncmp(envp[j], envp[j + 1],
ft_strchr(envp[j], '=') - envp[j]) > 0)
char_swap(&envp[j], &(envp[j + 1]));
j++;
}
}
@@ -112,7 +108,7 @@ int print_export(char **envp)
int builtin_export(char **arg, t_ast_n *head)
{
int i;
int i;
char **tmp;
i = 0;

View File

@@ -24,7 +24,7 @@ int builtin_pwd(char **arg)
free(cwd);
return (0);
}
else
else
{
perror("pwd");
return (1);

View File

@@ -12,27 +12,6 @@
#include "../../includes/minishell.h"
// void builtin_unset(char *str, char **envp)
// {
// int i;
// char *var;
//
// if (count_char(str) == 5)
// {
// ft_putendl_fd("unset: not enough arguments", 2);
// // exit(1);
// }
// i = 0;
// while(str[5 + i] && str[5 + i] == ' ')
// i++;
// var = &str[5 + i];
// i = 0;
// while (ft_strnstr(envp[i], var, ft_strlen(var)) == NULL)
// i++;
// if (envp[i])
// envp[i][0] = '\0';
// }
//
int builtin_unset(char **arg, t_ast_n *head)
{
int i;
@@ -40,8 +19,8 @@ int builtin_unset(char **arg, t_ast_n *head)
i = 0;
if (count_args(arg) == 1)
return (err_msg_cmd("unset", NULL, "not enough arguments", EXIT_FAILURE));
return (err_msg_cmd("unset", NULL, UNSET_ARG, EXIT_FAILURE));
while (++i < count_args(arg))
ret = remove_env_var(arg[i], head->msh);
ret = remove_env_var(arg[i], head->msh);
return (!ret);
}

View File

@@ -32,3 +32,12 @@ int count_args(char **tab)
i++;
return (i);
}
void char_swap(char **s1, char **s2)
{
char *tmp;
tmp = *s1;
*s1 = *s2;
*s2 = tmp;
}