execve and forks ok

This commit is contained in:
Hugo LAMY
2022-06-26 18:03:13 +02:00
parent a035862584
commit 14130caef7
4 changed files with 99 additions and 7 deletions

View File

@@ -1,14 +1,20 @@
#include <unistd.h> // write
#include <stdio.h> // printf
#include <string.h> // strcmp
#include <unistd.h> // write, fork, execve, pipe
#include <stdio.h> // printf
#include <string.h> // strcmp
#include <sys/types.h> // pid_t
#include <sys/wait.h> // waitpid
void print_cmd(char **av, int end)
#define STDIN 0
#define STDOUT 1
#define STDERR 2
void print_cmd(char **av)
{
int i;
int size;
i = 0;
while (i < end)
while (av[i])
{
size = 0;
while (av[i][size] != '\0')
@@ -20,7 +26,23 @@ void print_cmd(char **av, int end)
write(1, "\n", 1);
}
int main(int ac, char **av)
//void exec_cmd(char **av, int end, char **en)
void exec_cmd(char **av, char **en)
{
pid_t pid = fork();
// av[end] = NULL;
if (pid == 0) // child process
{
execve(av[0], av, en);
}
else // parent process
{
waitpid(pid, NULL, 0);
}
}
int main(int ac, char **av, char **en)
{
int i;
int start;
@@ -33,9 +55,13 @@ int main(int ac, char **av)
start = i;
while (i < ac && strcmp(av[i], "|") && strcmp(av[i], ";"))
i++;
print_cmd(&av[start], i - start);
av[i] = NULL;
// print_cmd(&av[start]);
exec_cmd(&av[start], en);
if (i < ac)
i++;
}
return (0);
}
// https://www.rozmichelle.com/pipes-forks-dups/