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-03-31 14:45:06 -04:00
|
|
|
/* Updated: 2023/03/31 20:42:17 by erey-bet ### ########.fr */
|
2023-02-14 11:11:39 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "./builtins_private.h"
|
2023-02-14 11:11:39 -05:00
|
|
|
|
2023-03-31 14:45:06 -04:00
|
|
|
static int error(char *str, char *to_free1, char *to_free2)
|
2023-02-23 11:19:25 -05:00
|
|
|
{
|
2023-03-30 09:16:21 -04:00
|
|
|
write(2, "bozoshell: export: `", 20);
|
2023-03-13 08:09:41 -04:00
|
|
|
write(2, str, ft_strlen(str));
|
|
|
|
write(2, "': not a valid identifier\n", 26);
|
2023-03-31 14:45:06 -04:00
|
|
|
if (to_free1 != NULL)
|
|
|
|
free(to_free1);
|
|
|
|
if (to_free2 != NULL)
|
|
|
|
free(to_free2);
|
2023-02-23 11:19:25 -05:00
|
|
|
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);
|
2023-03-09 09:07:36 -05:00
|
|
|
if (((t_env *)(current->content))->value != NULL)
|
|
|
|
{
|
|
|
|
ft_putstr_fd("=", fd);
|
|
|
|
write(fd, "\"", 1);
|
|
|
|
ft_putstr_fd(((t_env *)(current->content))->value, fd);
|
|
|
|
write(fd, "\"\n", 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
write(fd, "\n", 2);
|
2023-02-14 11:11:39 -05:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 11:19:25 -05:00
|
|
|
|
2023-03-09 14:13:38 -05:00
|
|
|
int set_key_value_export(t_list **env, char *args, char **key, char **value)
|
|
|
|
{
|
|
|
|
*key = ft_strndup(args, ft_strnchr(args, '='));
|
|
|
|
if (*key == NULL)
|
|
|
|
return (1);
|
|
|
|
if (ft_strlen(*key) == 0)
|
|
|
|
return (1);
|
|
|
|
if (possible_key(*key) == 2)
|
|
|
|
{
|
|
|
|
(*key)[ft_strlen(*key) - 1] = '\0';
|
|
|
|
if (get_value_by_key(*key, env) == NULL)
|
|
|
|
*value = ft_strdup(ft_strchr(args, '=') + 1);
|
|
|
|
else
|
2023-03-31 14:45:06 -04:00
|
|
|
*value = ft_strjoin(get_value_by_key(*key, env), ft_strchr(args, '=') + 1);
|
2023-03-09 14:13:38 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
*value = ft_strdup(ft_strchr(args, '=') + 1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-03-13 08:26:36 -04:00
|
|
|
int add_export(t_list **env, char *args)
|
2023-02-23 11:19:25 -05:00
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
if (ft_strchr(args, '=') != NULL)
|
|
|
|
{
|
2023-03-09 14:13:38 -05:00
|
|
|
if (set_key_value_export(env, args, &key, &value))
|
2023-03-31 14:45:06 -04:00
|
|
|
return (error(args, key, NULL));
|
2023-02-23 11:19:25 -05:00
|
|
|
}
|
2023-03-09 14:13:38 -05:00
|
|
|
else
|
2023-02-23 11:19:25 -05:00
|
|
|
{
|
2023-03-09 14:13:38 -05:00
|
|
|
key = ft_strdup(args);
|
2023-03-09 09:07:36 -05:00
|
|
|
value = get_value_by_key(key, env);
|
2023-03-09 14:13:38 -05:00
|
|
|
if (value != NULL)
|
|
|
|
value = ft_strdup(value);
|
|
|
|
if (possible_key(key) == 2)
|
2023-03-31 14:45:06 -04:00
|
|
|
return (error(key, key, value));
|
2023-02-23 11:19:25 -05:00
|
|
|
}
|
2023-03-09 09:07:36 -05:00
|
|
|
if (!possible_key(key))
|
2023-03-31 14:45:06 -04:00
|
|
|
return (error(args, key, value));
|
2023-03-09 14:13:38 -05:00
|
|
|
create_value_by_key(key, value, env);
|
2023-02-28 08:28:54 -05:00
|
|
|
return (0);
|
2023-02-23 11:19:25 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
int ft_export(t_list **env, char **args, int fd)
|
2023-02-23 11:19:25 -05:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
err = 0;
|
|
|
|
if (args[0] == NULL)
|
|
|
|
print_export(env, fd);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = -1;
|
|
|
|
while (args[++i])
|
2023-03-13 08:26:36 -04:00
|
|
|
if (add_export(env, args[i]) == 1)
|
2023-02-28 08:25:20 -05:00
|
|
|
err = 1;
|
2023-02-23 11:19:25 -05:00
|
|
|
}
|
|
|
|
return (err);
|
|
|
|
}
|