wip builtins: env, exit

This commit is contained in:
LuckyLaszlo
2021-10-10 08:57:33 +02:00
parent a7e0066458
commit c0068a5ec7
9 changed files with 113 additions and 21 deletions

21
srcs/builtins/env.c Normal file
View File

@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 05:01:26 by lperrey #+# #+# */
/* Updated: 2021/10/10 07:37:29 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_env(int argc, char *argv[], t_all *c) // WIP
{
(void)argc;
(void)argv;
ft_putendl_arr_fd(c->envp, 1);
return (0);
}

46
srcs/builtins/exit.c Normal file
View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 05:01:22 by lperrey #+# #+# */
/* Updated: 2021/10/10 08:50:37 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_exit(int argc, char *argv[], t_all *c) // WIP
{
unsigned char status;
int i;
status = 0;
if (argc > 2)
return (ft_reti_print(1, "exit: too many arguments\n", 2));
if (argc == 2)
{
i = 0;
while (argv[1][i])
{
if ((argv[1][0] == '-' || argv[1][0] == '+') && argv[1][1] != '\0')
i++;
while (ft_isdigit(argv[1][i]))
i++;
if (argv[1][i] != '\0')
{
ft_putstr_fd("exit: ", 2);
ft_putstr_fd(argv[1], 2);
return (ft_reti_print(2, " numeric argument required\n", 2));
}
}
status = ft_atoi(argv[1]);
}
// TODO : remplacer exit(status) par
(void)c;
exit(status);
return (0);
// return (free_exit(&c, status));
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/10/08 19:09:12 by lperrey ### ########.fr */
/* Updated: 2021/10/10 08:54:17 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -15,9 +15,10 @@
static char *init_prompt_base(void);
static char *init_prompt(char *prompt_base);
int init(t_all *c)
int init(t_all *c, char *envp[])
{
ft_bzero(c, sizeof *c);
c->envp = envp;
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base() fail"));
@@ -36,12 +37,12 @@ static char *init_prompt_base(void)
if (!tmp)
tmp = getenv("LOGNAME");
if (!tmp)
tmp = "NoName";
tmp = "NoUser";
prompt_base = ft_strjoin(TERM_LIGHT_GREEN, tmp);
prompt_base = ft_strjoinfree_s1(prompt_base, "@");
tmp = getenv("NAME");
if (!tmp)
tmp = "NoHost";
tmp = "NoName";
prompt_base = ft_strjoinfree_s1(prompt_base, tmp);
prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
return (prompt_base);
@@ -52,6 +53,6 @@ static char *init_prompt(char *prompt_base)
char *prompt;
prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
prompt = ft_strjoinfree_s1(prompt, TERM_RESET PROMPT_EURO);
prompt = ft_strjoinfree_s1(prompt, TERM_RESET USER_PROMPT);
return (prompt);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/08 19:11:03 by lperrey ### ########.fr */
/* Updated: 2021/10/10 08:50:20 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -24,13 +24,13 @@ void shell_loop(t_all *c)
line_input = readline(c->prompt);
if (line_input && *line_input)
{
if (!ft_strncmp(line_input, "exit", 5))
exit(EXIT_SUCCESS);
if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
builtin_env(0, NULL, c);
else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder
builtin_exit(0, NULL, c);
else
printf("echo: %s\n", line_input);
}
//rl_redisplay();
//rl_on_new_line();
}
}
@@ -63,13 +63,9 @@ int main(int argc, char *argv[], char *envp[])
{
t_all c;
ft_putendl_arr_fd(envp, 1);
//char *path = getenv("PATH");
(void)argc;
(void)argv;
(void)envp;
if (!init(&c))
if (!init(&c, envp))
exit(EXIT_FAILURE);
shell_loop(&c);
return (0);