-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_utils.c
More file actions
147 lines (132 loc) · 3.2 KB
/
exec_utils.c
File metadata and controls
147 lines (132 loc) · 3.2 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
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
145
146
147
#include "main.h"
/**
* commandExists - checks if the command exists in the PATH or not.
*
* @cmd: command.
*
* Return: if command exists, returns the Path to the command, otherwise NULL.
*/
char *commandExists(char *cmd)
{
char **path = _getenv("PATH");
int index;
char *command, *commandPath, *commandFound = NULL;
if (path == NULL)
return (NULL);
command = _strdup("/");
command = _realloc(command, _strlen(command) + 1,
_strlen(command) + _strlen(cmd) + 1);
command = _strcat(command, cmd);
for (index = 0; path[index] != NULL; index++)
{
commandPath = _strdup(path[index]);
commandPath = _realloc(commandPath, _strlen(commandPath) + 1,
_strlen(command) + _strlen(path[index]) + 1);
commandPath = _strcat(commandPath, command);
if (access(commandPath, F_OK) == 0)
{
commandFound = commandPath;
break;
}
free(commandPath);
}
free(command);
for (index = 0; path[index] != NULL;)
index++;
free_2D(path, index);
return (commandFound);
}
/**
* doExec - checks if the command should be executed or not.
*
* @lastSignal: last status code from executed functions.
* @operator: logical operator in for the previous command.
*
* Return: (0) don't execute, (1) otherwise.
*/
int doExec(int *lastSignal, char *operator)
{
int exec = 0;
if (operator == NULL || _strcmp(operator, ";") == 0)
exec = 1;
else if (_strcmp(operator, "&&") == 0 && *lastSignal == 0)
exec = 1;
else if (_strcmp(operator, "||") == 0 && *lastSignal != 0)
exec = 1;
return (exec);
}
/**
* execAbsolutePath - executes a command which the user has provided its path.
*
* @cmdExec: commands as Commands type single list.
* @lastSignal: last status code from functions.
* @programName: the name of the running executable.
* @buffer: the original buffer from getline function.
*
* Return: Nothing.
*/
void execAbsolutePath(Commands *cmdExec, int *lastSignal,
char *programName, char *buffer)
{
pid_t pid;
int status;
pid = fork();
if (pid == -1)
perror("fork error - absolute path");
else if (pid == 0)
{
status = execve(cmdExec->cmd[0], cmdExec->cmd, environ);
if (status == -1)
perror(programName);
free(buffer);
free_commands(cmdExec);
_exit(EXIT_FAILURE);
}
else
{
wait(lastSignal);
*lastSignal = WEXITSTATUS(*lastSignal);
}
}
/**
* execCommandPath - executes a command from PATH.
*
* @cmdExec: commands as Commands type single list.
* @lastSignal: last status code from functions.
* @command_path: the command path.
* @programName: the name of the running executable.
* @buffer: the original buffer from getline function.
*
* Return: Nothing.
*/
void execCommandPath(Commands *cmdExec, int *lastSignal,
char *command_path, char *programName, char *buffer)
{
pid_t pid;
int execStatus;
pid = fork();
if (pid == -1)
perror("fork error - command path");
else if (pid == 0)
{
if (access(command_path, X_OK) == 0)
{
execStatus = execve(command_path, cmdExec->cmd, environ);
if (execStatus == -1)
perror(programName);
}
else
perror(programName);
free_commands(cmdExec);
fflush(NULL);
free(buffer);
free(command_path);
_exit(EXIT_FAILURE);
}
else
{
free(command_path);
wait(lastSignal);
*lastSignal = WEXITSTATUS(*lastSignal);
}
}