(feat/fix) : ajout de | et changement des variable env OLDPWD et PWD quand un cd est effectué
This commit is contained in:
parent
ea313d6252
commit
92dc04d96e
100
src/exec/exec.c
100
src/exec/exec.c
@ -5,69 +5,89 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "../cmd/cmd.h"
|
#include "../cmd/cmd.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "../env/env.h"
|
||||||
|
|
||||||
|
int execute(cmd* input, char** env)
|
||||||
int execute(cmd** input, char** env)
|
|
||||||
{
|
{
|
||||||
int exitcode;
|
int exitcode;
|
||||||
for(int i=0; input[i]!=NULL; i++)
|
const int pid = fork();
|
||||||
|
if (pid == 0)
|
||||||
{
|
{
|
||||||
// création d'un processus enfant du programme
|
dup2(input->fd_out, 1);
|
||||||
const int pid = fork();
|
dup2(input->fd_in, 0);
|
||||||
// pid de l'enfant est 0
|
exitcode = execve(input->executable, input->args, env);
|
||||||
if (pid == 0)
|
}
|
||||||
{
|
else
|
||||||
// redirige la sortie standard vers un file descriptor
|
{
|
||||||
dup2(1, input[i]->fd_out);
|
waitpid(pid, &exitcode, 0);
|
||||||
// on tue l'enfant (l'enfant devient la commande entrée par l'utilisateur)
|
|
||||||
exitcode = execve(input[i]->executable, input[i]->args, env);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
waitpid(pid, &exitcode, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return exitcode;
|
return exitcode;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len(char** list)
|
int len(void** list)
|
||||||
{
|
{
|
||||||
int index;
|
int index;
|
||||||
for (index = 0; list[index]!=NULL; index++);
|
for (index = 0; list[index]!=NULL; index++);
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int change_directory(char** args)
|
int piper(cmd** cmds, char** env)
|
||||||
{
|
{
|
||||||
if(len(args)!=2)
|
int fds[2];
|
||||||
|
for (int i=0; cmds[i] != NULL; i++)
|
||||||
{
|
{
|
||||||
dprintf(2, "Mauvais arguments\n");
|
if (pipe(fds) < 0)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
if (i < len((void**)cmds)-1)
|
||||||
if(chdir(args[1])!=0)
|
{
|
||||||
{
|
cmds[i]->fd_out = fds[1];
|
||||||
dprintf(2, "Mauvais chemin : %s\n", args[1]);
|
cmds[i+1]->fd_in = fds[0];
|
||||||
return 1;
|
}
|
||||||
}
|
execute(cmds[i], env);
|
||||||
char cwd[PATH_MAX];
|
|
||||||
if (getcwd(cwd, sizeof(cwd)) != NULL)
|
|
||||||
{
|
|
||||||
printf("cd %s \nCurrent working dir: %s\n", args[1], cwd);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int builtin_execute(cmd** input, char** env)
|
int change_directory(char** args, lst** env)
|
||||||
|
{
|
||||||
|
char cwd[PATH_MAX];
|
||||||
|
char* oldpwd = get_env_variable(env, "PWD");
|
||||||
|
if(len((void**)args)==1)
|
||||||
|
{
|
||||||
|
char* path = get_env_variable(env, "HOME");
|
||||||
|
chdir(path);
|
||||||
|
edit_env_variable(env, "OLDPWD", oldpwd);
|
||||||
|
if (getcwd(cwd, sizeof(cwd)) != NULL)
|
||||||
|
{
|
||||||
|
edit_env_variable(env, "PWD", cwd);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (chdir(args[1]) == 0)
|
||||||
|
{
|
||||||
|
edit_env_variable(env, "OLDPWD", oldpwd);
|
||||||
|
if (getcwd(cwd, sizeof(cwd)) != NULL)
|
||||||
|
{
|
||||||
|
edit_env_variable(env, "PWD", cwd);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dprintf(2, "Mauvais chemin : %s\n", args[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int builtin_execute(cmd* input, lst** env)
|
||||||
{
|
{
|
||||||
int exitcode;
|
int exitcode;
|
||||||
(void) env;
|
if (strcmp(input->executable, "cd") == 0)
|
||||||
for(int i=0; input[i]!=NULL; i++)
|
|
||||||
{
|
{
|
||||||
if (strcmp(input[i]->executable, "cd") == 0)
|
exitcode = change_directory(input->args, env);
|
||||||
{
|
|
||||||
exitcode = change_directory(input[i]->args);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return exitcode;
|
return exitcode;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
int execute(cmd** input, char** env);
|
int execute(cmd* input, char** env);
|
||||||
int builtin_execute(cmd** input, char** env);
|
int builtin_execute(cmd* input, lst** env);
|
||||||
|
int piper(cmd** cmds, char** env);
|
||||||
|
Loading…
Reference in New Issue
Block a user