minishell/src/builtin/builtin_export.c

53 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* :::::::: */
/* builtin_export.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/20 11:32:53 by whaffman #+# #+# */
/* Updated: 2025/03/04 16:41:06 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int ft_isvalid_identifier(char *str)
{
if (ft_isalpha(*str) == FALSE && *str != '_')
return (FALSE);
while (*str != '\0')
{
if (ft_isalnum(*str) == FALSE && *str != '_')
return (FALSE);
str++;
}
return (TRUE);
}
int builtin_export(t_minishell *msh, t_command *cmd)
{
char *err;
char **arr;
int i;
i = 0;
arr = NULL;
if (cmd->args[1] == NULL)
environment_print(msh, TRUE);
while (cmd->args[++i] != NULL)
{
arr = ft_split_safe(msh, cmd->args[i], '=');
if (arr != NULL && arr[0] != NULL && ft_isvalid_identifier(arr[0]) == TRUE)
environment_update(msh, arr[0], arr[1]);
else
{
err = ft_strjoin_safe(msh, cmd->args[1], ": not a valid identifier");
error_msg("export", err);
return (ft_free_arr_safe(msh, arr), EXIT_FAILURE);
}
ft_free_arr_safe(msh, arr);
}
return (EXIT_SUCCESS);
}