/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* handle_argv.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); }