85 lines
2.1 KiB
C
85 lines
2.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* export.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */
|
|
/* Updated: 2023/02/24 12:32:22 by erey-bet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../minishell.h"
|
|
|
|
static int error(char *str, int fd)
|
|
{
|
|
write(fd, "bash: export: `", 15);
|
|
write(fd, str, ft_strlen(str));
|
|
write(fd, "': not a valid identifier\n", 26);
|
|
return (1);
|
|
}
|
|
|
|
void print_export(t_list **env, int fd)
|
|
{
|
|
t_list *current;
|
|
|
|
current = *env;
|
|
while (current->next != NULL)
|
|
{
|
|
write(fd, "declare -x ", 11);
|
|
ft_putstr_fd(((t_env *)(current->content))->key, fd);
|
|
ft_putstr_fd("=", fd);
|
|
write(fd, "\"", 1);
|
|
ft_putstr_fd(((t_env *)(current->content))->value, fd);
|
|
write(fd, "\"\n", 2);
|
|
current = current->next;
|
|
}
|
|
}
|
|
|
|
void add_export(t_list **env, char *args, int fd, int *err)
|
|
{
|
|
char *key;
|
|
char *value;
|
|
|
|
key = args;
|
|
value = "";
|
|
if (ft_strchr(args, '=') != NULL)
|
|
{
|
|
key = ft_strndup(args, ft_strnchr(args, '='));
|
|
value = ft_strchr(args, '=') + 1;
|
|
}
|
|
if (!possible_key(key))
|
|
{
|
|
*err = error(key, fd);
|
|
return ;
|
|
}
|
|
create_value_by_key_dup(key, value, env);
|
|
}
|
|
|
|
int export(t_list **env, char **args, int fd)
|
|
{
|
|
int err;
|
|
int i;
|
|
|
|
err = 0;
|
|
if (args[0] == NULL)
|
|
print_export(env, fd);
|
|
else
|
|
{
|
|
i = -1;
|
|
while (args[++i])
|
|
add_export(env, args[i], fd, &err);
|
|
}
|
|
return (err);
|
|
}
|
|
|
|
/*int main(int argc, char *argv[], char **env)
|
|
{
|
|
t_list **n_env;
|
|
|
|
n_env = init_env(env);
|
|
export(n_env, argv + 1, 1);
|
|
return (0);
|
|
}*/
|