-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
67 lines (62 loc) · 1.11 KB
/
execute.c
File metadata and controls
67 lines (62 loc) · 1.11 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
#include "shell.h"
/**
* perrs - prints error message
* @shellf: ..
* @loop: ..
*/
void perrs(shell_info shellf, char *loop)
{
write(STDERR_FILENO, shellf.name, _strlen(shellf.name));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, loop, _strlen(loop));
write(STDERR_FILENO, ": ", 2);
perror(shellf.args[0]);
}
/**
* execute - executes the command
* @shellf: shell data
*
* Return: 0 on success
*/
int execute(shell_info shellf)
{
pid_t pid;
int exec_stat;
char *loop;
if (shellf.args == NULL)
return (-1);
if (shellf.args[0] == NULL)
{
free(shellf.args);
return (-1);
}
loop = _itoa(shellf.loop_count);
pid = fork();
if (pid < 0)
{
perror("Error:");
return (-1);
}
if (pid == 0)
{
execve((shellf.args)[0], shellf.args, environ);
if (errno == EACCES)
{
exec_stat = 126;
perrs(shellf, loop);
} else
{
exec_stat = errno;
perror(shellf.args[0]);
}
free(loop), freedom(shellf);
exit(exec_stat);
} else
{
wait(&(shellf.stat));
if (WIFEXITED(shellf.stat))
shellf.stat = WEXITSTATUS(shellf.stat);
}
free(loop), freedom(shellf);
return (shellf.stat);
}