MiniDevil As beautiful as a shell
exec_utils.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* exec_utils.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/14 01:16:34 by baelgadi #+# #+# */
9 /* Updated: 2026/03/04 04:49:22 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include <sys/wait.h>
14 #include <stdio.h>
15 #include "executor.h"
16 #include "builtins.h"
17 #include "libft.h"
18 
25 int exec_cmd_not_found(char *cmd)
26 {
27  ft_putstr_fd("minishell: ", STDERR_FILENO);
28  ft_putstr_fd(cmd, STDERR_FILENO);
29  ft_putstr_fd(": command not found\n", STDERR_FILENO);
30  return (127);
31 }
32 
39 int is_builtin(char *cmd)
40 {
41  if (!cmd)
42  return (0);
43  if (ft_strncmp(cmd, "echo", 5) == 0)
44  return (1);
45  if (ft_strncmp(cmd, "cd", 3) == 0)
46  return (1);
47  if (ft_strncmp(cmd, "pwd", 4) == 0)
48  return (1);
49  if (ft_strncmp(cmd, "export", 7) == 0)
50  return (1);
51  if (ft_strncmp(cmd, "unset", 6) == 0)
52  return (1);
53  if (ft_strncmp(cmd, "env", 4) == 0)
54  return (1);
55  if (ft_strncmp(cmd, "exit", 5) == 0)
56  return (1);
57  return (0);
58 }
59 
67 int exec_builtin(char **args, t_shell *shell)
68 {
69  if (!args || !args[0])
70  return (0);
71  if (ft_strncmp(args[0], "echo", 5) == 0)
72  return (builtin_echo(args));
73  if (ft_strncmp(args[0], "cd", 3) == 0)
74  return (builtin_cd(args, &shell->env));
75  if (ft_strncmp(args[0], "pwd", 4) == 0)
76  return (builtin_pwd());
77  if (ft_strncmp(args[0], "export", 7) == 0)
78  return (builtin_export(args, &shell->env));
79  if (ft_strncmp(args[0], "unset", 6) == 0)
80  return (builtin_unset(args, &shell->env));
81  if (ft_strncmp(args[0], "env", 4) == 0)
82  return (builtin_env(args, shell->env));
83  if (ft_strncmp(args[0], "exit", 5) == 0)
84  return (builtin_exit(args, shell));
85  return (0);
86 }
87 
95 int exec_simple_command(char **args, t_shell *shell)
96 {
97  if (!args || !args[0])
98  return (0);
99  if (is_builtin(args[0]))
100  return (exec_builtin(args, shell));
101  return (exec_external(args, shell));
102 }
103 
111 int pipe_fork_error(int pipe_fd[2], pid_t left_pid)
112 {
113  close(pipe_fd[0]);
114  close(pipe_fd[1]);
115  if (left_pid > 0)
116  waitpid(left_pid, NULL, 0);
117  perror("minishell: fork");
118  return (1);
119 }
int builtin_cd(char **args, t_env **env)
Implement the cd command.
Definition: builtin_cd.c:101
int builtin_echo(char **args)
Implement the echo command.
Definition: builtin_echo.c:52
int builtin_env(char **args, t_env *env)
Implement the env command.
Definition: builtin_env.c:26
int builtin_exit(char **args, t_shell *shell)
Implement the exit command.
Definition: builtin_exit.c:29
int builtin_export(char **args, t_env **env)
Implement the export command.
int builtin_pwd(void)
Implement the pwd command.
Definition: builtin_pwd.c:24
int builtin_unset(char **args, t_env **env)
Implement the unset command.
Definition: builtin_unset.c:86
Shell builtin commands prototypes.
int exec_external(char **args, t_shell *shell)
Fork and execute an external command.
Definition: exec_cmd.c:150
int exec_cmd_not_found(char *cmd)
Print "command not found" to STDERR.
Definition: exec_utils.c:25
int exec_simple_command(char **args, t_shell *shell)
Execute a simple command (builtin or external)
Definition: exec_utils.c:95
int exec_builtin(char **args, t_shell *shell)
Dispatch a builtin command to its function.
Definition: exec_utils.c:67
int pipe_fork_error(int pipe_fd[2], pid_t left_pid)
Clean up pipe fds and wait for left child on fork failure.
Definition: exec_utils.c:111
int is_builtin(char *cmd)
Check if a command is a shell builtin.
Definition: exec_utils.c:39
Executor prototypes for commands, pipes, redirections & heredocs.
Shell state.
Definition: structs.h:164
t_env * env
Environment linked list.
Definition: structs.h:165