aboutsummaryrefslogtreecommitdiff
path: root/src/execute_cmd.c
diff options
context:
space:
mode:
authorChristopher Uhlig2025-01-13 13:30:32 +0100
committerChristopher Uhlig2025-01-13 13:30:32 +0100
commit8c37f835ba29bcecc1d779edb396d97b18667026 (patch)
tree43e8d2cee19cbd95997a75990f5ff5a839145005 /src/execute_cmd.c
parent78dc50a2bce3c6e31405437189e2990d8fc720ac (diff)
downloadminishell-8c37f835ba29bcecc1d779edb396d97b18667026.tar.gz
minishell-8c37f835ba29bcecc1d779edb396d97b18667026.zip
running version but with comments
Diffstat (limited to 'src/execute_cmd.c')
-rw-r--r--src/execute_cmd.c123
1 files changed, 66 insertions, 57 deletions
diff --git a/src/execute_cmd.c b/src/execute_cmd.c
index 803d88f..7f93c5a 100644
--- a/src/execute_cmd.c
+++ b/src/execute_cmd.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */
-/* Updated: 2025/01/13 09:50:56 by chuhlig ### ########.fr */
+/* Updated: 2025/01/09 16:07:01 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,60 +18,69 @@
#include <sys/fcntl.h>
#include <unistd.h>
-int execute_cmd(t_cmd *cmd, t_env **env)
-{
- char *cmd_path;
- pid_t pid;
- int status;
- int result;
- int original_stdout;
- int original_stdin;
+// static void format_args(char **args, t_env *env);
- original_stdout = dup(STDOUT_FILENO);
- original_stdin = dup(STDIN_FILENO);
- if (handle_redirections(cmd->redirs) == -1)
- {
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
- return (EXIT_FAILURE);
- }
- if (is_builtin(cmd->args[0]))
- {
- result = execute_builtin(cmd->args, env);
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
- return (result);
- }
- pid = fork();
- if (pid == -1)
- {
- perror("fork");
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
- return (EXIT_FAILURE);
- }
- if (pid == 0)
- {
- cmd_path = get_cmd_path(cmd->args[0], *env);
- if (!cmd_path)
- {
- printf("%s: command not found\n", cmd->args[0]);
- exit(EXIT_FAILURE);
- }
- execve(cmd_path, cmd->args, env_to_strlst(*env));
- perror("execve");
- exit(EXIT_FAILURE);
- }
- waitpid(pid, &status, 0);
- dup2(original_stdout, STDOUT_FILENO);
- dup2(original_stdin, STDIN_FILENO);
- close(original_stdout);
- close(original_stdin);
- return (WEXITSTATUS(status));
-} \ No newline at end of file
+// int execute_cmd(t_cmd *cmd, t_env **env)
+// {
+// int result;
+// char *cmd_path;
+// int fd;
+
+// if (ft_strncmp(cmd->args[0], "pwd", 4) == 0)
+// return (pwd(*env));
+// else if (ft_strncmp(cmd->args[0], "echo", 5) == 0)
+// return (echo(cmd->args));
+// else if (ft_strncmp(cmd->args[0], "env", 4) == 0)
+// return (ft_env(*env));
+// else if (ft_strncmp(cmd->args[0], "cd", 3) == 0)
+// return (cd(env, cmd->args));
+// else if (ft_strncmp(cmd->args[0], "unset", 6) == 0)
+// return (unset(cmd->args, env));
+// else if (ft_strncmp(cmd->args[0], "export", 7) == 0)
+// return (export(cmd->args, env));
+// format_args(cmd->args, *env);
+// cmd_path = get_cmd_path(cmd->args[0], *env);
+// cmd->args[0] = cmd_path;
+// if (cmd->redirs[0].type == INPUT_FILE)
+// {
+// fd = open(cmd->redirs[0].specifier, O_RDONLY);
+// if (fd < 0)
+// return (EXIT_FAILURE);
+// dup2(fd, STDIN_FILENO);
+// }
+// else if (cmd->redirs[0].type == INPUT_LIMITER)
+// {
+// dbg("INPUT_LIMITER");
+// }
+// if (cmd->redirs[1].type == OUTPUT_APPEND)
+// {
+// dbg("OUTPUT_APPEND");
+// fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 644);
+// if (fd < 0)
+// return (EXIT_FAILURE);
+// dup2(fd, STDOUT_FILENO);
+// }
+// else if (cmd->redirs[1].type == OUTPUT_OVERRIDE)
+// {
+// fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 644);
+// if (fd < 0)
+// return (EXIT_FAILURE);
+// dup2(fd, STDOUT_FILENO);
+// dbg("OUTPUT_OVERRIDE");
+// }
+// result = execve(cmd->args[0], cmd->args, env_to_strlst(*env));
+// return (result);
+// }
+
+// static void format_args(char **args, t_env *env)
+// {
+// char *formatted;
+
+// while (*args != NULL)
+// {
+// formatted = format_string(*args, env);
+// /* free(*args); */
+// *args = formatted;
+// args++;
+// }
+// } \ No newline at end of file