merging
This commit is contained in:
4
srcs/env/var.c
vendored
4
srcs/env/var.c
vendored
@@ -6,7 +6,7 @@
|
|||||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/28 10:08:49 by lderidde #+# #+# */
|
/* Created: 2025/01/28 10:08:49 by lderidde #+# #+# */
|
||||||
/* Updated: 2025/02/03 13:10:32 by lderidde ### ########.fr */
|
/* Updated: 2025/02/04 10:37:20 by lderidde ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -154,6 +154,7 @@ void set_var_env(char *key, char *value, t_msh *msh)
|
|||||||
int i;
|
int i;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
|
|
||||||
|
tmp = "";
|
||||||
i = get_var_index(key, msh);
|
i = get_var_index(key, msh);
|
||||||
if (value)
|
if (value)
|
||||||
tmp = ft_strjoin(key, "=");
|
tmp = ft_strjoin(key, "=");
|
||||||
@@ -172,6 +173,7 @@ void set_var_env(char *key, char *value, t_msh *msh)
|
|||||||
return ;
|
return ;
|
||||||
msh->env[i] = ft_strjoin(tmp, value);
|
msh->env[i] = ft_strjoin(tmp, value);
|
||||||
}
|
}
|
||||||
|
if (tmp && tmp[0])
|
||||||
free_null_ptr(tmp);
|
free_null_ptr(tmp);
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
|
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
|
||||||
/* Updated: 2025/02/03 15:44:14 by lderidde ### ########.fr */
|
/* Updated: 2025/02/04 10:50:43 by lderidde ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -14,16 +14,16 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
void handle_file(t_ast_n *node, int check)
|
void handle_file(t_ast_n *node, int check, int i)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if (check == 1)
|
if (check == 1)
|
||||||
fd = open(node->infile, O_RDONLY);
|
fd = open(node->files[i], O_RDONLY);
|
||||||
else if (check == 2)
|
else if (check == 2)
|
||||||
fd = open(node->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
fd = open(node->files[i], O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||||
else if (check == 3)
|
else if (check == 3)
|
||||||
fd = open(node->outfile, O_WRONLY | O_CREAT | O_APPEND, 0666);
|
fd = open(node->files[i], O_WRONLY | O_CREAT | O_APPEND, 0666);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
{
|
{
|
||||||
perror("open");
|
perror("open");
|
||||||
@@ -36,26 +36,32 @@ void handle_file(t_ast_n *node, int check)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void handle_redir(t_ast_n *node)
|
void handle_redir(t_ast_n *node)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
while (node->redir[++i])
|
||||||
{
|
{
|
||||||
if (node->redir == _NR)
|
if (node->redir == _NR)
|
||||||
return ;
|
return ;
|
||||||
else if (node->redir == _RED_L)
|
else if (node->redir[i] == _RED_L)
|
||||||
handle_file(node, 1);
|
handle_file(node, 1, i);
|
||||||
else if (node->redir == _RED_R)
|
else if (node->redir[i] == _RED_R)
|
||||||
handle_file(node, 2);
|
handle_file(node, 2, i);
|
||||||
else if (node->redir == _RED_DR)
|
else if (node->redir[i] == _RED_DR)
|
||||||
handle_file(node, 3);
|
handle_file(node, 3, i);
|
||||||
if (node->redir == _RED_L)
|
if (node->redir[i] == _RED_L)
|
||||||
{
|
{
|
||||||
dup2(node->_stdin, STDIN_FILENO);
|
dup2(node->_stdin, STDIN_FILENO);
|
||||||
close(node->_stdin);
|
close(node->_stdin);
|
||||||
}
|
}
|
||||||
else if (node->redir == _RED_R || node->redir == _RED_DR)
|
else if (node->redir[i] == _RED_R || node->redir[i] == _RED_DR)
|
||||||
{
|
{
|
||||||
dup2(node->_stdout, STDOUT_FILENO);
|
dup2(node->_stdout, STDOUT_FILENO);
|
||||||
close(node->_stdout);
|
close(node->_stdout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int is_builtin(char *str)
|
int is_builtin(char *str)
|
||||||
@@ -78,16 +84,21 @@ int is_builtin(char *str)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset_redir(t_ast_n *node)
|
void reset_redir(void)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
char *tty;
|
||||||
|
|
||||||
fd = open("/dev/pts/1", O_WRONLY);
|
tty = ttyname(STDERR_FILENO);
|
||||||
(void)node;
|
fd = open(tty, O_WRONLY);
|
||||||
if (dup2(fd, 1) == -1)
|
if (dup2(fd, STDOUT_FILENO) == -1)
|
||||||
|
printf("error\n");
|
||||||
|
close(fd);
|
||||||
|
tty = ttyname(STDERR_FILENO);
|
||||||
|
fd = open(tty, O_RDONLY);
|
||||||
|
if (dup2(fd, STDIN_FILENO) == -1)
|
||||||
printf("error\n");
|
printf("error\n");
|
||||||
close(fd);
|
close(fd);
|
||||||
// close(node->save_std);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int exec_builtin(t_ast_n *node)
|
int exec_builtin(t_ast_n *node)
|
||||||
@@ -109,7 +120,7 @@ int exec_builtin(t_ast_n *node)
|
|||||||
ret = builtin_cd(node->args, node);
|
ret = builtin_cd(node->args, node);
|
||||||
else
|
else
|
||||||
ret = builtin_export(node->args, node);
|
ret = builtin_export(node->args, node);
|
||||||
reset_redir(node);
|
reset_redir();
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,38 +214,6 @@ int exec_scmd(t_ast_n *node)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// int *create_pipes(t_ast_n **pline)
|
|
||||||
// {
|
|
||||||
// int i;
|
|
||||||
// int arg;
|
|
||||||
// int *pipes;
|
|
||||||
//
|
|
||||||
// arg = count_cmds(pline);
|
|
||||||
// pipes = malloc(2 * (arg - 1) * sizeof(int));
|
|
||||||
// if (!pipes)
|
|
||||||
// return (NULL);
|
|
||||||
// i = -1;
|
|
||||||
// while (++i < arg - 1)
|
|
||||||
// {
|
|
||||||
// pipe(&pipes[2 * i]);
|
|
||||||
// }
|
|
||||||
// return (pipes);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// void close_pipes(int *pipes, int count, bool flag)
|
|
||||||
// {
|
|
||||||
// int i;
|
|
||||||
//
|
|
||||||
// i = -1;
|
|
||||||
// while (++i < count)
|
|
||||||
// {
|
|
||||||
// close(pipes[2 * i]);
|
|
||||||
// close(pipes[(2 * i) + 1]);
|
|
||||||
// }
|
|
||||||
// if (flag)
|
|
||||||
// free(pipes);
|
|
||||||
// }
|
|
||||||
|
|
||||||
int err_fork_pline(int *pipes)
|
int err_fork_pline(int *pipes)
|
||||||
{
|
{
|
||||||
close(pipes[0]);
|
close(pipes[0]);
|
||||||
@@ -277,6 +256,7 @@ int end_pline(pid_t last_pid, t_ast_n **pline)
|
|||||||
waitpid(last_pid, &status, 0);
|
waitpid(last_pid, &status, 0);
|
||||||
while (++i < count_cmds(pline) - 1)
|
while (++i < count_cmds(pline) - 1)
|
||||||
wait(NULL);
|
wait(NULL);
|
||||||
|
reset_redir();
|
||||||
if (WIFEXITED(status))
|
if (WIFEXITED(status))
|
||||||
return (WEXITSTATUS(status));
|
return (WEXITSTATUS(status));
|
||||||
else
|
else
|
||||||
@@ -311,31 +291,86 @@ int exec_pline(t_ast_n **pline)
|
|||||||
return (end_pline(last_pid, pline));
|
return (end_pline(last_pid, pline));
|
||||||
}
|
}
|
||||||
|
|
||||||
// int exec_subsh(t_ast_n *node)
|
int exec_shcmd(t_ast_n *node)
|
||||||
// {
|
{
|
||||||
// int status;
|
pid_t pid;
|
||||||
// pid_t pid;
|
int status;
|
||||||
//
|
|
||||||
// pid = fork();
|
if (is_builtin(node->cmd))
|
||||||
// if (pid == 0)
|
exit (exec_builtin(node));
|
||||||
// {
|
else
|
||||||
// handle_redir(node);
|
{
|
||||||
// return (execute_command(t_node));
|
pid = fork();
|
||||||
// }
|
if (pid == 0)
|
||||||
// else if (pid > 0)
|
exec(node);
|
||||||
// {
|
else if (pid > 0)
|
||||||
// waitpid(pid, &status, 0);
|
{
|
||||||
// if (WIFEXITED(status))
|
waitpid(pid, &status, 0);
|
||||||
// return (WEXITSTATUS(status));
|
if (WIFEXITED(status))
|
||||||
// else
|
exit (WEXITSTATUS(status));
|
||||||
// return (1);
|
else
|
||||||
// }
|
exit (1);
|
||||||
// else
|
}
|
||||||
// {
|
else
|
||||||
// perror("fork");
|
perror("fork");
|
||||||
// return (1);
|
exit (1);
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
int exec_subsh(t_ast_n *node);
|
||||||
|
|
||||||
|
int execute_shcommand(t_ast_n *node)
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
|
||||||
|
if (node->state == _CMD)
|
||||||
|
return (exec_shcmd(node));
|
||||||
|
else if (node->state == _AND)
|
||||||
|
{
|
||||||
|
status = execute_command(node->left);
|
||||||
|
if (status == 0)
|
||||||
|
return (execute_command(node->right));
|
||||||
|
return (status);
|
||||||
|
}
|
||||||
|
else if (node->state == _OR)
|
||||||
|
{
|
||||||
|
status = execute_command(node->left);
|
||||||
|
if (status != 0)
|
||||||
|
return (execute_command(node->right));
|
||||||
|
return (status);
|
||||||
|
}
|
||||||
|
else if (node->state == _PLINE)
|
||||||
|
return (exec_pline(node->pline));
|
||||||
|
else if (node->state == _SUBSH)
|
||||||
|
return (exec_subsh(node->left));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int exec_subsh(t_ast_n *node)
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
|
pid = fork();
|
||||||
|
if (pid == 0)
|
||||||
|
{
|
||||||
|
handle_redir(node);
|
||||||
|
return (execute_shcommand(node));
|
||||||
|
}
|
||||||
|
else if (pid > 0)
|
||||||
|
{
|
||||||
|
waitpid(pid, &status, 0);
|
||||||
|
if (WIFEXITED(status))
|
||||||
|
return (WEXITSTATUS(status));
|
||||||
|
else
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
perror("fork");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int execute_command(t_ast_n *node)
|
int execute_command(t_ast_n *node)
|
||||||
{
|
{
|
||||||
@@ -359,7 +394,7 @@ int execute_command(t_ast_n *node)
|
|||||||
}
|
}
|
||||||
else if (node->state == _PLINE)
|
else if (node->state == _PLINE)
|
||||||
return (exec_pline(node->pline));
|
return (exec_pline(node->pline));
|
||||||
// else if (node->state == _SUBSH)
|
else if (node->state == _SUBSH)
|
||||||
// return (exec_subsh(node->subsh));
|
return (exec_subsh(node->left));
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user