-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_commands.c
90 lines (84 loc) · 1.52 KB
/
execute_commands.c
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
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include "main.h"
/**
* handle_builtin - Handle Builtin Commands
* @cmd: Parsed Command
* @ex_status: status of last Execute
* Return: 0 on success, -1 on failure.
*/
int handle_builtin(char **cmd, int ex_status)
{
bul_t bil[] = {
{"cd", change_dir},
{"env", print_env},
{"help", print_help},
{"echo", echo_call},
{"history", print_hist},
{NULL, NULL}
};
int i = 0;
while ((bil + i)->command)
{
if (_strcmp(cmd[0], (bil + i)->command) == 0)
{
return ((bil + i)->fun(cmd, ex_status));
}
i++;
}
return (-1);
}
/**
* check_cmd - execute Simple Shell Command (Fork,Wait,execute)
*
* @cmd:Parsed Command
* @input: User Input
* @c:Shell Execution Time Case of Command Not Found
* @argv:Program Name
* Return: 1 Case Command Null -1 Wrong Command 0 Command executed
*/
int check_cmd(char **cmd, char *input, int c, char **argv)
{
int status;
pid_t pid;
if (*cmd == NULL)
{
return (-1);
}
pid = fork();
if (pid == -1)
{
perror("Error");
return (-1);
}
if (pid == 0)
{
if (_strncmp(*cmd, "./", 2) != 0 && _strncmp(*cmd, "/", 1) != 0)
{
path_cmd(cmd);
}
if (execve(*cmd, cmd, environ) == -1)
{
print_error(cmd[0], c, argv);
free(input);
free(cmd);
exit(EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
wait(&status);
return (0);
}
/**
* signal_to_handle - Handle ctrl-C
* @sig:Captured Signal
* Return: Void
*/
void signal_to_handle(int sig)
{
if (sig == SIGINT)
{
PRINT("\n$ ");
}
}