(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
@ -5,69 +5,89 @@
|
||||
#include <stdio.h>
|
||||
#include "../cmd/cmd.h"
|
||||
#include <unistd.h>
|
||||
#include "../env/env.h"
|
||||
|
||||
|
||||
int execute(cmd** input, char** env)
|
||||
int execute(cmd* input, char** env)
|
||||
{
|
||||
int exitcode;
|
||||
for(int i=0; input[i]!=NULL; i++)
|
||||
{
|
||||
// création d'un processus enfant du programme
|
||||
const int pid = fork();
|
||||
// pid de l'enfant est 0
|
||||
if (pid == 0)
|
||||
{
|
||||
// redirige la sortie standard vers un file descriptor
|
||||
dup2(1, input[i]->fd_out);
|
||||
// on tue l'enfant (l'enfant devient la commande entrée par l'utilisateur)
|
||||
exitcode = execve(input[i]->executable, input[i]->args, env);
|
||||
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(char** list)
|
||||
int len(void** list)
|
||||
{
|
||||
int index;
|
||||
for (index = 0; list[index]!=NULL; 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;
|
||||
}
|
||||
if(chdir(args[1])!=0)
|
||||
if (i < len((void**)cmds)-1)
|
||||
{
|
||||
dprintf(2, "Mauvais chemin : %s\n", args[1]);
|
||||
return 1;
|
||||
cmds[i]->fd_out = fds[1];
|
||||
cmds[i+1]->fd_in = fds[0];
|
||||
}
|
||||
char cwd[PATH_MAX];
|
||||
if (getcwd(cwd, sizeof(cwd)) != NULL)
|
||||
{
|
||||
printf("cd %s \nCurrent working dir: %s\n", args[1], cwd);
|
||||
execute(cmds[i], env);
|
||||
|
||||
}
|
||||
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;
|
||||
(void) env;
|
||||
for(int i=0; input[i]!=NULL; i++)
|
||||
if (strcmp(input->executable, "cd") == 0)
|
||||
{
|
||||
if (strcmp(input[i]->executable, "cd") == 0)
|
||||
{
|
||||
exitcode = change_directory(input[i]->args);
|
||||
|
||||
}
|
||||
exitcode = change_directory(input->args, env);
|
||||
}
|
||||
return exitcode;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
int execute(cmd** input, char** env);
|
||||
int builtin_execute(cmd** input, char** env);
|
||||
int 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