-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecution_functions.c
145 lines (134 loc) · 2.44 KB
/
execution_functions.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "main.h"
#include <stdint.h>
/**
* spawn_and_exec_cmd - Executes the command
* @av: list of args
* @pid: proccess id
* @cmdstatus: exit status pf prev cmd
* Return: -1 (failed execution), 0 (success)
*/
int spawn_and_exec_cmd(char **av, pid_t pid, int *cmdstatus)
{
int i = 0, status;
break_loop = 1;
pid = fork();
if (pid == 0)
{
while (break_loop)
{
signal(SIGINT, my_sigint);
if (av[i])
{
if (execve(av[0], av, NULL) == -1)
{
perror("./hsh");
return (-1);
}
}
}
for (i = 0; av[i]; i++)
free(av[i]);
free(av);
}
else if (pid < 0)
{
perror("./hsh");
exit(1);
}
else
{
wait(&status);
if (WIFEXITED(status))
{
*cmdstatus = WEXITSTATUS(status);
}
}
return (0);
}
/**
* exec_from_path - creates full path of command and executes
* @envp: env variable path
* @pid: process id
* @cmdstatus: exit status pf prev cmd
* @av: user input tokens
* Return: 1 if error in execution or malloc error,
* -1 if command doesn't exist
*/
int exec_from_path(char **envp, pid_t pid, char **av, int *cmdstatus)
{
char *shortcmd = av[0], *fullpath = NULL;
int status;
break_loop = 1;
fullpath = _getfullpath(envp, shortcmd);
if (fullpath == NULL)
{
clean_cmd(1, av);
return (127);
}
pid = fork();
if (pid == 0)
{
while (break_loop)
{
signal(SIGINT, my_sigint);
if (execve(fullpath, av, NULL) == -1)
{
free(fullpath);
perror("./hsh");
return (-1);
}
}
free(fullpath);
}
else if (pid < 0)
{
perror("./hsh");
exit(1);
}
else
{
wait(&status);
if (WIFEXITED(status))
{
*cmdstatus = WEXITSTATUS(status);
}
free(fullpath);
}
return (0);
}
/**
* is_bultin_cmd - checks whether command is a built in
* @av: tokens from terminal
* @ac: number of tokens
* @cmdline: shell cmdline
* @status: status of exit of prev cmd
* @newenviron: ptr to env copy
* @newentry: ptr to new entry in environ
* Return: cmd handler
*/
int is_builtin_cmd(char **av, char *cmdline, int status, char ***newenviron,
char **newentry, int ac)
{
char *cmd = av[0];
if (_strcmp("exit", cmd) == 0)
{
terminate_shell(status, av, cmdline, (*newenviron), (*newentry));
return (0);
}
if (_strcmp("env", cmd) == 0)
{
_printenv(av);
return (0);
}
if (_strcmp("setenv", cmd) == 0 || _strcmp("unsetenv", cmd) == 0)
{
_handle_set_unset(av, newenviron, newentry);
return (0);
}
if (_strcmp("cd", cmd) == 0)
{
_cd(av, ac);
return (0);
}
return (-1);
}