Changement noms fonctions

This commit is contained in:
rick 2021-02-17 10:51:41 +01:00
parent 56fdea8ed2
commit fc85ceef5d
Signed by: Rick
GPG key ID: 2B593F087240EE99
3 changed files with 49 additions and 35 deletions

View file

@ -31,22 +31,22 @@ char* get_input()
} }
/** /**
* parse_char(): Récupère lentrée de lutilisateur avant de la parser * get_command(): Récupère lentrée de lutilisateur avant de la parser
* @args: Tableau de string * @args: Tableau de string
* @find: caractère utilisé pour parser * @find: caractère utilisé pour parser
* *
* Récupère lentrée de lutilisateur avant de la parser avec le caractère * Récupère lentrée de lutilisateur avant de la parser avec le caractère
* find. Chaque string sera mise dans une case de args. * find. Chaque string sera mise dans une case de args.
*/ */
void parse_char(char *args[], char find) void get_command(char *args[], char find)
{ {
char *user_input = get_input(); char *user_input = get_input();
tok_space(user_input, args, find); parse_string(user_input, args, find);
free(user_input); free(user_input);
} }
/** /**
* tok_space(): Découpe la chaine orig et met chaque mot dans le tableau dest * parse_string(): Découpe la chaine orig et met chaque mot dans le tableau dest
* @orig: chaine à découper * @orig: chaine à découper
* @dest: tableau de string sera stocker les mots * @dest: tableau de string sera stocker les mots
* @find: caractère à utilise pour parser la chaine orig * @find: caractère à utilise pour parser la chaine orig
@ -54,7 +54,7 @@ void parse_char(char *args[], char find)
* La chaine de caractères orig est découpée selon le caractère find * La chaine de caractères orig est découpée selon le caractère find
* et chaques mots sont mis dans le tableau dest. * et chaques mots sont mis dans le tableau dest.
*/ */
void tok_space(char *orig, char *dest[], char find) void parse_string(char *orig, char *dest[], char find)
{ {
char *token = strtok(orig, &find); char *token = strtok(orig, &find);

View file

@ -9,9 +9,10 @@
# define _PARSER_H_ # define _PARSER_H_
#define MAX_LENGTH 200 /* taille maximum des tableaux utilisés */ #define MAX_LENGTH 200 /* taille maximum des tableaux utilisés */
#define SHELL rishtik
char* get_input(); char* get_input();
void parse_char(char *args[], char find); void get_command(char *args[], char find);
void tok_space(char *args,char *commands[], char find); void parse_string(char *args,char *commands[], char find);
#endif #endif

View file

@ -29,44 +29,57 @@ int main()
command[i] = (char *) calloc(MAX_LENGTH, sizeof(char)); command[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
} }
parse_char(args, '|'); get_command(args, '|');
int index = 0; int index = 0;
while (strcmp(args[0], "exit")) while (strcmp(args[0], "exit"))
{ {
while (args[index] != NULL) while (args[index] != NULL)
{ {
pid = fork(); if (strncmp(args[index], "cd", 2))
if (!pid)
{ {
tok_space(args[index], command, ' '); pid = fork();
if (args[1] != NULL) if (!pid)
{ {
if (args[index+1] == NULL) parse_string(args[index], command, ' ');
dup2(my_pipe[0], STDIN_FILENO); if (args[1] != NULL)
else if (index == 0) {
dup2(my_pipe[1], STDOUT_FILENO); if (args[index+1] == NULL)
/*else { dup2(my_pipe[0], STDIN_FILENO);
dup2(my_pipe[0], STDIN_FILENO); else if (index == 0)
dup2(my_pipe[1], STDOUT_FILENO); dup2(my_pipe[1], STDOUT_FILENO);
}*/ /*else {
close(my_pipe[0]); dup2(my_pipe[0], STDIN_FILENO);
close(my_pipe[1]); dup2(my_pipe[1], STDOUT_FILENO);
} }*/
close(my_pipe[0]);
close(my_pipe[1]);
}
execvp(command[0], command); execvp(command[0], command);
return 0; return 0;
} }
else else
{ {
if (args[index + 1] == NULL) if (args[index + 1] == NULL)
{ {
close(my_pipe[1]); close(my_pipe[1]);
close(my_pipe[0]); close(my_pipe[0]);
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
}
} }
index++;
} }
else
{
parse_string(args[index], command, ' ');
execvp(command[0], command);
for (int i = 0; i < MAX_LENGTH; i++)
{
free(command[i]);
command[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
}
}
index++;
} }
for (int i = 0; i < MAX_LENGTH; i++) for (int i = 0; i < MAX_LENGTH; i++)
@ -76,7 +89,7 @@ int main()
args[i] = (char *) calloc(MAX_LENGTH, sizeof(char)); args[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
command[i] = (char *) calloc(MAX_LENGTH, sizeof(char)); command[i] = (char *) calloc(MAX_LENGTH, sizeof(char));
} }
parse_char(args, '|'); get_command(args, '|');
if (pipe(my_pipe) == -1) if (pipe(my_pipe) == -1)
{ {
printf("erreur sur le pipe"); printf("erreur sur le pipe");