2023-02-14 11:11:39 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* export.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/14 14:27:08 by cchauvet #+# #+# */
|
2023-02-28 08:25:20 -05:00
|
|
|
/* Updated: 2023/02/28 13:30:32 by erey-bet ### ########.fr */
|
2023-02-14 11:11:39 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "../minishell.h"
|
|
|
|
|
2023-02-24 06:46:02 -05:00
|
|
|
static int error(char *str, int fd)
|
2023-02-23 11:19:25 -05:00
|
|
|
{
|
|
|
|
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)
|
2023-02-14 11:11:39 -05:00
|
|
|
{
|
|
|
|
t_list *current;
|
|
|
|
|
2023-02-23 11:19:25 -05:00
|
|
|
current = *env;
|
2023-02-17 11:46:12 -05:00
|
|
|
while (current->next != NULL)
|
2023-02-14 11:11:39 -05:00
|
|
|
{
|
2023-02-17 11:46:12 -05:00
|
|
|
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);
|
2023-02-14 11:11:39 -05:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 11:19:25 -05:00
|
|
|
|
2023-02-28 08:25:20 -05:00
|
|
|
int add_export(t_list **env, char *args, int fd, int *err)
|
2023-02-23 11:19:25 -05:00
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
key = args;
|
|
|
|
value = "";
|
|
|
|
if (ft_strchr(args, '=') != NULL)
|
|
|
|
{
|
|
|
|
key = ft_strndup(args, ft_strnchr(args, '='));
|
2023-02-28 08:25:20 -05:00
|
|
|
if (key == NULL)
|
|
|
|
return (1);
|
|
|
|
if (ft_strlen(key) == 0)
|
|
|
|
{
|
|
|
|
error(args, fd);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-02-23 11:19:25 -05:00
|
|
|
value = ft_strchr(args, '=') + 1;
|
|
|
|
}
|
|
|
|
if (!possible_key(key))
|
|
|
|
{
|
|
|
|
*err = error(key, fd);
|
2023-02-28 08:25:20 -05:00
|
|
|
return (1);
|
2023-02-23 11:19:25 -05:00
|
|
|
}
|
|
|
|
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])
|
2023-02-28 08:25:20 -05:00
|
|
|
if (add_export(env, args[i], fd, &err) == 1)
|
|
|
|
err = 1;
|
2023-02-23 11:19:25 -05:00
|
|
|
}
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
2023-02-28 08:25:20 -05:00
|
|
|
int main(int argc, char *argv[], char **env)
|
2023-02-23 11:19:25 -05:00
|
|
|
{
|
|
|
|
t_list **n_env;
|
|
|
|
|
2023-02-28 08:25:20 -05:00
|
|
|
(void)argc;
|
2023-02-23 11:19:25 -05:00
|
|
|
n_env = init_env(env);
|
|
|
|
export(n_env, argv + 1, 1);
|
|
|
|
return (0);
|
2023-02-28 08:25:20 -05:00
|
|
|
}
|