fix(here_doc): do not try to tcset/get attr for non-tty devices

This commit is contained in:
Khaïs COLIN 2025-05-05 11:55:23 +02:00
parent 7cd8d981e2
commit 4e7c9f4da9
5 changed files with 59 additions and 7 deletions

View file

@ -39,6 +39,7 @@ srcs = \
src/executing/here_doc/here_doc_utils.c \
src/executing/here_doc/random_filename.c \
src/executing/here_doc/strip_newline.c \
src/executing/here_doc/tcattr.c \
src/executing/simple_cmd/builtin_cd.c \
src/executing/simple_cmd/builtin_echo.c \
src/executing/simple_cmd/builtin_env.c \

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/05 11:03:08 by kcolin #+# #+# */
/* Updated: 2025/05/05 11:40:04 by kcolin ### ########.fr */
/* Updated: 2025/05/05 12:03:42 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,13 +18,14 @@
#include "../../ft_errno.h"
#include "../../sig/sig.h"
#include "here_doc_utils.h"
#include "tcattr.h"
#include <termios.h>
#include <stdio.h>
char *expand_line(char *line, t_minishell *app);
char *failed_to_open_tmp_file(void);
static char *tcgetatr_fail(void)
static char *tcgetattr_fail(void)
{
return (ft_errno(FT_EHEREDOC_FAILED),
perror("minishell: tcgetattr"), NULL);
@ -44,7 +45,7 @@ static char *setup_term_and_signal(int outfd, char *filename, int infd,
set_here_doc_mode_sig_handling();
term_new = term_save;
term_new.c_lflag = term_new.c_lflag & ~ECHOCTL;
if (tcsetattr(infd, TCSANOW, &term_new) < 0)
if (tcset(infd, TCSANOW, &term_new) < 0)
return (perror("minishell: tcsetattr"),
interupted(outfd, filename, infd, &term_save));
return (filename);
@ -57,8 +58,8 @@ char *here_doc(t_worddesc *marker, int infd, t_minishell *app)
char *line;
struct termios term_save;
if (tcgetattr(infd, &term_save) < 0)
return (tcgetatr_fail());
if (tcget(infd, &term_save) < 0)
return (tcgetattr_fail());
outfd = setup_here_doc(marker, infd, &filename);
if (outfd < 0)
return (ft_errno(FT_EHEREDOC_FAILED), failed_to_open_tmp_file());

View file

@ -16,6 +16,7 @@
#include "../../ft_errno.h"
#include "here_doc.h"
#include "../../sig/sig.h"
#include "tcattr.h"
int setup_here_doc(t_worddesc *marker, int infd, char **filename)
{
@ -44,7 +45,7 @@ char *finalize(int outfd, char *filename, int infd,
struct termios *term_save)
{
close(outfd);
if (tcsetattr(infd, TCSADRAIN, term_save) < 0)
if (tcset(infd, TCSADRAIN, term_save) < 0)
{
perror("minishell: tcsetattr");
ft_errno(FT_EHEREDOC_FAILED);
@ -62,7 +63,7 @@ char *interupted(int outfd, char *filename, int infd,
unlink(filename);
free(filename);
g_signum = 0;
if (tcsetattr(infd, TCSADRAIN, term_save) < 0)
if (tcset(infd, TCSADRAIN, term_save) < 0)
{
perror("minishell: tcsetattr");
free(filename);

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tcattr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/05 11:58:14 by kcolin #+# #+# */
/* Updated: 2025/05/05 12:08:40 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "tcattr.h"
#include <unistd.h>
int tcset(int infd, int optional_actions, const struct termios *termios_p)
{
if (isatty(infd))
return (tcsetattr(infd, optional_actions, termios_p));
return (0);
}
int tcget(int infd, struct termios *termios_p)
{
if (isatty(infd))
return (tcgetattr(infd, termios_p));
return (1);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tcattr.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/05 11:57:56 by kcolin #+# #+# */
/* Updated: 2025/05/05 12:08:35 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef TCATTR_H
# define TCATTR_H
# include <termios.h>
int tcset(int infd, int optional_actions, const struct termios *termios_p);
int tcget(int infd, struct termios *termios_p);
#endif // TCATTR_H