-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexect.c
More file actions
111 lines (102 loc) · 3.34 KB
/
exect.c
File metadata and controls
111 lines (102 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elavrich <elavrich@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/30 15:17:31 by elavrich #+# #+# */
/* Updated: 2025/07/27 22:33:12 by elavrich ### ########.fr */
/* */
/* ************************************************************************** */
#include "Minishell.h"
static void child_exec(char *path, char **cmd, t_shell *shell, t_token **tokens)
{
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
if (execve(path, cmd, shell->env_var) == -1)
{
close_free(tokens, shell);
free(path);
free_array(cmd);
perror("execve failed");
exit(EXIT_FAILURE);
}
}
static void exec_fork_and_wait(char *path, char **cmd, t_shell *shell,
t_token **tokens)
{
int status;
shell->pid1 = fork();
if (shell->pid1 == -1)
{
perror("fork");
shell->exit_stat = 1;
return ;
}
else if (shell->pid1 == 0)
child_exec(path, cmd, shell, tokens);
signal(SIGINT, SIG_IGN);
waitpid(shell->pid1, &status, 0);
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGINT)
{
write(1, "\n", 1);
shell->exit_stat = 128 + WTERMSIG(status);
}
else
shell->exit_stat = WEXITSTATUS(status);
setup_shell_signals();
}
void execute_single_cmd(char **cmd, t_shell *shell, t_token **tokens)
{
char *path;
if (!cmd[0] || !cmd)
return ;
path = get_right_path(cmd[0], shell, 0);
if (!path)
{
printf("%s: command not found\n", cmd[0]);
shell->exit_stat = 127;
return ;
}
exec_fork_and_wait(path, cmd, shell, tokens);
if (path != cmd[0])
free(path);
}
//printf("builtin used\n"); // for checking if builtin is used or not
bool handle_builtin(char **cmd, t_shell *shell)
{
if (!cmd || !cmd[0] || ft_strchr(*cmd, '/'))
return (false);
if (ft_strcmp(cmd[0], "cd") == 0)
return (shell->exit_stat = 0, builtin_cd(cmd, shell), true);
else if (ft_strcmp(cmd[0], "pwd") == 0)
return (shell->exit_stat = 0, builtin_pwd(shell), true);
else if (ft_strcmp(cmd[0], "export") == 0)
return (shell->exit_stat = 0, ft_export(cmd, shell), true);
else if (ft_strcmp(cmd[0], "env") == 0)
return (shell->exit_stat = 0, print_env(*shell), true);
else if (ft_strcmp(cmd[0], "unset") == 0)
return (shell->exit_stat = 0, builtin_unset(cmd, shell), true);
else if (ft_strcmp(cmd[0], "echo") == 0)
return (shell->exit_stat = 0, ft_echo(cmd), true);
else if (ft_strcmp(cmd[0], "exit") == 0)
return (shell->exit_stat = 0, ft_exit(cmd, shell), true);
return (false);
}
int is_valid_directory(char *path)
{
struct stat sb;
if (access(path, F_OK) == -1)
return (perror("cd: No such file or directory"), 0);
else if (stat(path, &sb) == -1)
return (perror("cd: stat error"), 0);
else if (!S_ISDIR(sb.st_mode))
{
perror("cd: Not a directory");
return (0);
}
else if (access(path, X_OK) == -1)
return (perror("cd: Permission denied"), 0);
return (1);
}