You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/05/03 18:17:46 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_unset(t_command *cmd)
3 years ago
{
if (!cmd->argv[1])
3 years ago
{
//read(input_fd, PIPE_MAX_SIZE);
return (1);
}
return (0);
}
int ft_env(t_command *cmd)
3 years ago
{
int i;
i = 0;
while ((*cmd->envp)[i])
3 years ago
{
printf("%s\n", (*cmd->envp)[i]);
3 years ago
i++;
}
return (0);
}
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_export(t_command *cmd)
3 years ago
{
char **new;
char **env;
int i;
i = 0;
new = ft_split(cmd->argv[1], '=');
3 years ago
if (!is_valid_identifier(new[0]))
{
printf ("export: not an identifier: %s\n", new[0]);
return (0);
}
while ((*cmd->envp)[i])
3 years ago
{
env = ft_split((*cmd->envp)[i], '=');
3 years ago
if (!ft_strncmp(new[0], env[0], ft_strlen(new[0]) + 1))
{
(*cmd->envp)[i] = cmd->argv[1];
3 years ago
return (1);
}
i++;
}
(*cmd->envp)[i] = cmd->argv[1];
(*cmd->envp)[i + 1] = NULL;
3 years ago
return (1);
}