|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* env.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */
|
|
|
|
/* Updated: 2022/05/06 13:39:03 by narnaud ### ########.fr */
|
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "minishell.h"
|
|
|
|
|
|
|
|
int is_valid_identifier(char *ident)
|
|
|
|
{
|
|
|
|
if (ft_isalpha(ident[0]) || ident[0] == '_')
|
|
|
|
{
|
|
|
|
while (*ident)
|
|
|
|
{
|
|
|
|
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_')
|
|
|
|
return (0);
|
|
|
|
ident++;
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ft_unset(t_datas *datas, t_command *cmd)
|
|
|
|
{
|
|
|
|
(void)datas;
|
|
|
|
if (!cmd->argv[1])
|
|
|
|
{
|
|
|
|
//read(input_fd, PIPE_MAX_SIZE);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ft_env(t_datas *datas, t_command *cmd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int fd_out;
|
|
|
|
|
|
|
|
if (cmd->fd[1])
|
|
|
|
fd_out = cmd->fd[1];
|
|
|
|
else
|
|
|
|
fd_out = 1;
|
|
|
|
i = 0;
|
|
|
|
while (datas->envp[i])
|
|
|
|
{
|
|
|
|
ft_putstr_fd(datas->envp[i], fd_out);
|
|
|
|
ft_putchar_fd('\n', fd_out);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ft_export(t_datas *datas, t_command *cmd)
|
|
|
|
{
|
|
|
|
char **new;
|
|
|
|
char **env;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
new = ft_split(cmd->argv[1], '=');
|
|
|
|
if (!is_valid_identifier(new[0]))
|
|
|
|
{
|
|
|
|
ft_putstr_fd("export: not an identifier: ", 2);
|
|
|
|
ft_putstr_fd(new[0], 2);
|
|
|
|
ft_putchar_fd('\n', 2);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
while (datas->envp[i])
|
|
|
|
{
|
|
|
|
env = ft_split(datas->envp[i], '=');
|
|
|
|
if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1))
|
|
|
|
{
|
|
|
|
datas->envp[i] = cmd->argv[1];
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
datas->envp[i] = cmd->argv[1];
|
|
|
|
datas->envp[i + 1] = NULL;
|
|
|
|
return (1);
|
|
|
|
}
|