81 lines
2.2 KiB
C
81 lines
2.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* msh_struct.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/02/06 15:32:03 by lderidde #+# #+# */
|
|
/* Updated: 2025/02/11 16:34:46 by nalebrun ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../includes/minishell.h"
|
|
#include <fcntl.h>
|
|
|
|
static char **ft_setnewenv(void)
|
|
{
|
|
char **envp;
|
|
|
|
envp = malloc(sizeof(char *) * 2);
|
|
if (!envp)
|
|
return (NULL);
|
|
envp[0] = ft_strjoin("PWD=", getcwd(NULL, 0));
|
|
envp[1] = ft_strdup("SHLVL=1");
|
|
if (!envp[0] || !envp[1])
|
|
return (ft_free(&envp[0]), ft_free(&envp[1]), NULL);
|
|
return (envp);
|
|
}
|
|
|
|
void set_shellenv(t_msh *msh)
|
|
{
|
|
char *src;
|
|
char *tmp;
|
|
int sh;
|
|
|
|
src = getcwd(NULL, 0);
|
|
tmp = ft_strjoin(src, "/minishell");
|
|
set_var_env("SHELL", tmp, msh);
|
|
ft_free(&src);
|
|
ft_free(&tmp);
|
|
sh = ft_atoi(get_var_value("SHLVL", msh->env));
|
|
tmp = ft_itoa(sh + 1);
|
|
set_var_env("SHLVL", tmp, msh);
|
|
ft_free(&tmp);
|
|
}
|
|
|
|
t_msh *init_msh(char **envp)
|
|
{
|
|
t_msh *msh;
|
|
int fd;
|
|
|
|
msh = malloc(sizeof(t_msh) * 1);
|
|
fd = open(".heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
close(fd);
|
|
msh->hist = open(".mmoat_history", O_RDWR | O_CREAT | O_APPEND, 0666);
|
|
msh->ex_code = 0;
|
|
msh->here_fd = -1;
|
|
msh->input = NULL;
|
|
msh->prev_input = NULL;
|
|
if (!msh)
|
|
return (NULL);
|
|
if (!envp[0])
|
|
msh->env = ft_setnewenv();
|
|
else
|
|
msh->env = init_env(envp);
|
|
set_shellenv(msh);
|
|
return (msh);
|
|
}
|
|
|
|
void free_msh(t_msh *msh)
|
|
{
|
|
free_tab(msh->env);
|
|
if (msh->hist != -1)
|
|
close(msh->hist);
|
|
if (msh->here_fd != -1)
|
|
close(msh->here_fd);
|
|
ft_free(&msh->input);
|
|
ft_free(&msh->prev_input);
|
|
free(msh);
|
|
}
|