fixed argv open().

previously wrongly dup2() to STDIN.
This commit is contained in:
lperrey
2021-12-20 22:52:41 +01:00
parent 6a9b7bae1b
commit 8ed97346f7
11 changed files with 78 additions and 87 deletions

View File

@@ -1,62 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:46:05 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int remap_stdin_to_script_file(char *script_file);
char *init_prompt_base(void);
int handle_argv(t_all *c, char *argv[])
{
int ret;
if (argv[1])
{
ret = remap_stdin_to_script_file(argv[1]);
if (ret)
exit_free(c, ret);
}
else if (isatty(STDIN_FILENO))
{
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base()"));
c->prompt = init_prompt(c->prompt_base);
if (!c->prompt)
return (ft_reti_perror(0, "init_prompt()"));
}
return (1);
}
int remap_stdin_to_script_file(char *script_file)
{
int fd;
int ret;
int tmp;
fd = open(script_file, O_RDONLY);
if (fd == -1)
{
shell_perror(script_file, ": ", "", 0);
return (EXIT_CMD_NOT_FOUND);
}
ret = dup2(fd, STDIN_FILENO);
tmp = errno;
if (close(fd) == -1)
perror("close()");
if (ret == -1)
{
errno = tmp;
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
}
return (0);
}

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 14:22:20 by lperrey ### ########.fr */
/* Updated: 2021/12/20 22:26:05 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,7 +14,8 @@
void init_readline(void);
int init_shlvl(void);
int handle_argv(t_all *c, char *argv[]);
void open_script_file(t_all *c, char *argv[]);
int init_prompt(t_all *c, int script_fd);
int init(t_all *c, char *argv[])
{
@@ -27,7 +28,8 @@ int init(t_all *c, char *argv[])
c->path = retrieve_path();
if (!init_shlvl())
return (ft_reti_perror(0, "init_shlvl()"));
if (!handle_argv(c, argv))
return (0);
open_script_file(c, argv);
if (!init_prompt(c, c->script_fd))
return (ft_reti_perror(0, "init_prompt()"));
return (1);
}

View File

@@ -6,13 +6,29 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:28:46 by lperrey ### ########.fr */
/* Updated: 2021/12/20 22:25:46 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *init_prompt_base(void)
static char *init_prompt_base(void);
int init_prompt(t_all *c, int script_fd)
{
if (!script_fd && isatty(STDIN_FILENO))
{
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (0);
c->prompt = update_prompt(c->prompt_base);
if (!c->prompt)
return (0);
}
return (1);
}
static char *init_prompt_base(void)
{
char *prompt_base;
char *tmp;
@@ -40,7 +56,7 @@ char *init_prompt_base(void)
return (prompt_base);
}
char *init_prompt(char *prompt_base)
char *update_prompt(char *prompt_base)
{
char *prompt;

View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* open_script_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/20 22:22:29 by lperrey #+# #+# */
/* Updated: 2021/12/20 22:27:14 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void open_script_file(t_all *c, char *argv[])
{
if (argv[1])
{
c->script_fd = open(argv[1], O_RDONLY);
if (c->script_fd == -1)
{
shell_perror(argv[1], ": ", "", 0);
exit_free(c, EXIT_CMD_NOT_FOUND);
}
}
}