-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.c
41 lines (40 loc) · 847 Bytes
/
worker.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
#include "holberton.h"
/**
* worker - funtion for execute a command
* @stringconcat: command directory
* @tokenscommand: arguments array command
* @commandstring: strings of commands
* @env: environ double pointer variable
* Return: always 1
*/
int worker(char *stringconcat, char **tokenscommand, char *commandstring,
char **env)
{
pid_t pid;
size_t cont = 0;
if ((tokenscommand == NULL) || (stringconcat == NULL) || (env == NULL))
return (-1);
pid = fork();
if (pid < 0)
perror("fork failure\n");
if (pid == 0)
{
if (execve(stringconcat, tokenscommand, env) == -1)
{
if (commandstring)
free(commandstring);
return (0);
}
else
{
cont++;
write(STDIN_FILENO, "command not found", 17);
if (commandstring)
free(commandstring);
return (cont);
}
}
if (pid > 0)
wait(NULL);
return (0);
}