forked from kei-en/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_functions.c
125 lines (112 loc) · 2.28 KB
/
exec_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
#include "shell.h"
/**
* _exec - executes a command
* @av: arguments passed to the function
* @full_path: full path of the command
* @buffer: buffer containing the command
* @st: status of the command
*
* Return: int (status)
*/
int _exec(char **buffer, char ***av, char *full_path, int *st)
{
int child_pid, ex, status;
if (*av == NULL)
return (1);
if (**buffer == '\n' || full_path == NULL)
{
free(*av);
return (1);
}
child_pid = fork();
if (child_pid == -1)
{
perror("Error:");
return (-1);
}
if (child_pid == 0)
{
ex = execve(full_path, *av, environ);
if (ex == -1)
{
free(*av);
free(*buffer);
exit(99);
}
}
else
{
wait(&status);
if (WIFEXITED(status))
*st = WEXITSTATUS(status);
}
free(*av);
free(full_path);
return (1);
}
/**
* check_path - checks if the command is in the PATH
* @av: arguments passed to the function
* @path: path of the command
* @tokens: tokens of the path
* @count: number of times the shell has been executed
* @error: error
*
* Return: void
*/
void check_path(char ***tokens, char **path, char **av, int *cnt, int *err)
{
char *first = NULL, buffer[33], *msg = NULL;
if (*path != NULL || *tokens == NULL)
return;
first = *(tokens)[0];
if (access(first, F_OK | X_OK) == 0)
*path = _strdup(first);
else
{
if (access(first, F_OK) != 0)
{
msg = "not found\n";
pfError(av[0], itoa(*cnt, buffer, 10), first, msg);
*err = 127;
}
else if (access(first, X_OK) != 0)
{
msg = "Permission denied\n";
pfError(av[0], itoa(*cnt, buffer, 10), first, msg);
*err = 126;
}
}
}
/**
* check_dir - checks if the command is in the current directory
* @av: arguments passed to the function
* @path: path of the command
* @tokens: tokens of the path
* @count: number of times the shell has been executed
* @error: error
*
* Return: void
*/
void check_dir(char ***tokens, char **path, char **av, int *count, int *error)
{
struct stat st;
char *first = NULL, buffer[33], *msg = NULL;
if (*tokens == NULL)
return;
first = *(tokens)[0];
if (stat(first, &st) == 0)
{
if ((st.st_mode & S_IFMT) == S_IFDIR)
{
msg = "Permission denied\n";
pfError(av[0], itoa(*count, buffer, 10), first, msg);
*error = 126;
if (*path != NULL)
{
free(*path);
*path = NULL;
}
}
}
}