builtins
This commit is contained in:
@@ -1,18 +1,5 @@
|
||||
#include "../../includes/builtins.h"
|
||||
|
||||
void check_format(char **arg)
|
||||
{
|
||||
(void)arg;
|
||||
}
|
||||
|
||||
void print_error(char *str, int *code)
|
||||
{
|
||||
ft_put_s_fd("minishell: export: ", 2);
|
||||
ft_put_s_fd(str, 2);
|
||||
ft_putendl_fd(": not a valid identifier", 2);
|
||||
*code = 1;
|
||||
}
|
||||
|
||||
void print_arr(char **envp)
|
||||
{
|
||||
int i;
|
||||
@@ -42,7 +29,7 @@ void print_export(char **envp)
|
||||
j = 0;
|
||||
while (j < len - i - 1)
|
||||
{
|
||||
if (ft_strncmp(envp[j], envp[j + 1], ft_strlen(envp[j])) > 0)
|
||||
if (ft_strncmp(envp[j], envp[j + 1], ft_strchr(envp[j], '=') - envp[j]) > 0)
|
||||
{
|
||||
tmp = envp[j];
|
||||
envp[j] = envp[j + 1];
|
||||
@@ -55,35 +42,51 @@ void print_export(char **envp)
|
||||
print_arr(envp);
|
||||
}
|
||||
|
||||
char **builtin_export(char **arg, char **envp)
|
||||
static void free_tmp(char **tab)
|
||||
{
|
||||
char **new_envp;
|
||||
int i;
|
||||
// int code;
|
||||
int i;
|
||||
|
||||
i =0;
|
||||
while (envp[i])
|
||||
i++;
|
||||
new_envp = malloc(sizeof(char *) * i + 1);
|
||||
new_envp[i] = NULL;
|
||||
i = -1;
|
||||
while (envp[++i])
|
||||
new_envp[i] = ft_strdup(envp[i]);
|
||||
// code = 0;
|
||||
i = 0;
|
||||
while (arg[i])
|
||||
i++;
|
||||
if (i == 1)
|
||||
return(print_export(new_envp), envp);
|
||||
return (envp);
|
||||
// i = -1;
|
||||
// while (arg[++i])
|
||||
// {
|
||||
// if (check_format(arg[i]))
|
||||
// {
|
||||
//
|
||||
// }
|
||||
// else
|
||||
// print_error(arg[i], &code);
|
||||
// }
|
||||
while (tab[i])
|
||||
free(tab[i++]);
|
||||
free(tab);
|
||||
}
|
||||
|
||||
char **key_value(char *str)
|
||||
{
|
||||
char **tmp;
|
||||
char *equal;
|
||||
|
||||
tmp = malloc(sizeof(char *) * (2 + 1));
|
||||
if (!tmp)
|
||||
return (NULL);
|
||||
equal = ft_strchr(str, '=');
|
||||
tmp[0] = ft_substr(str, 0, equal - str);
|
||||
if (equal - str == 0)
|
||||
tmp[2] = ft_strdup("");
|
||||
else
|
||||
tmp[1] = ft_substr(equal, 1, ft_strlen(equal));
|
||||
tmp[2] = NULL;
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
void builtin_export(char **arg, t_data *data)
|
||||
{
|
||||
int i;
|
||||
char **tmp;
|
||||
|
||||
i = 0;
|
||||
if (count_var(arg) == 1)
|
||||
return (print_export(data->env));
|
||||
while (++i < count_var(arg))
|
||||
{
|
||||
if (ft_strchr(arg[i], '=') != NULL)
|
||||
{
|
||||
tmp = key_value(arg[i]);
|
||||
set_var_env(tmp[0], tmp[1], data);
|
||||
free_tmp(tmp);
|
||||
}
|
||||
else
|
||||
set_var_env(arg[i], NULL, data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user