(feat): possibilité d'éxecuter les commandes built-in avec un pipe
This commit is contained in:
parent
284fc07f9b
commit
69d4d2613b
@ -7,23 +7,6 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "../env/env.h"
|
#include "../env/env.h"
|
||||||
|
|
||||||
int execute(cmd* input, char** env)
|
|
||||||
{
|
|
||||||
int exitcode;
|
|
||||||
const int pid = fork();
|
|
||||||
if (pid == 0)
|
|
||||||
{
|
|
||||||
dup2(input->fd_out, 1);
|
|
||||||
dup2(input->fd_in, 0);
|
|
||||||
exitcode = execve(input->executable, input->args, env);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
waitpid(pid, &exitcode, 0);
|
|
||||||
}
|
|
||||||
return exitcode;
|
|
||||||
}
|
|
||||||
|
|
||||||
int len(void** list)
|
int len(void** list)
|
||||||
{
|
{
|
||||||
int index;
|
int index;
|
||||||
@ -31,24 +14,6 @@ int len(void** list)
|
|||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int piper(cmd** cmds, char** env)
|
|
||||||
{
|
|
||||||
int fds[2];
|
|
||||||
for (int i=0; cmds[i] != NULL; i++)
|
|
||||||
{
|
|
||||||
if (pipe(fds) < 0)
|
|
||||||
return 1;
|
|
||||||
if (i < len((void**)cmds)-1)
|
|
||||||
{
|
|
||||||
cmds[i]->fd_out = fds[1];
|
|
||||||
cmds[i+1]->fd_in = fds[0];
|
|
||||||
}
|
|
||||||
execute(cmds[i], env);
|
|
||||||
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int change_directory(char** args, lst** env)
|
int change_directory(char** args, lst** env)
|
||||||
{
|
{
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
@ -84,10 +49,48 @@ int change_directory(char** args, lst** env)
|
|||||||
|
|
||||||
int builtin_execute(cmd* input, lst** env)
|
int builtin_execute(cmd* input, lst** env)
|
||||||
{
|
{
|
||||||
int exitcode;
|
|
||||||
if (strcmp(input->executable, "cd") == 0)
|
if (strcmp(input->executable, "cd") == 0)
|
||||||
{
|
{
|
||||||
exitcode = change_directory(input->args, env);
|
change_directory(input->args, env);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int execute(cmd* input, lst** env)
|
||||||
|
{
|
||||||
|
int exitcode;
|
||||||
|
const int pid = fork();
|
||||||
|
if (pid == 0)
|
||||||
|
{
|
||||||
|
dup2(input->fd_out, 1);
|
||||||
|
dup2(input->fd_in, 0);
|
||||||
|
exitcode = execve(input->executable, input->args, get_env_str(env));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
waitpid(pid, &exitcode, 0);
|
||||||
}
|
}
|
||||||
return exitcode;
|
return exitcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int command_list_exec(cmd** cmds, lst** env)
|
||||||
|
{
|
||||||
|
int fds[2];
|
||||||
|
for (int i=0; cmds[i] != NULL; i++)
|
||||||
|
{
|
||||||
|
if (pipe(fds) < 0)
|
||||||
|
return 1;
|
||||||
|
if (i < len((void**)cmds)-1)
|
||||||
|
{
|
||||||
|
cmds[i]->fd_out = fds[1];
|
||||||
|
cmds[i+1]->fd_in = fds[0];
|
||||||
|
}
|
||||||
|
if (builtin_execute(cmds[i], env) == 1)
|
||||||
|
execute(cmds[i], env);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int execute(cmd* input, char** env);
|
int execute(cmd* input, lst** env);
|
||||||
int builtin_execute(cmd* input, lst** env);
|
int builtin_execute(cmd* input, lst** env);
|
||||||
int piper(cmd** cmds, char** env);
|
int piper(cmd** cmds, lst** env);
|
||||||
|
Loading…
Reference in New Issue
Block a user