Kaynağa Gözat

:sparkles: AJout structure de stockage pid

Loquicom 6 yıl önce
ebeveyn
işleme
76dee88c60
4 değiştirilmiş dosya ile 148 ekleme ve 16 silme
  1. 76 10
      command.c
  2. 18 0
      command.h
  3. 46 5
      mysh.c
  4. 8 1
      mysh.h

+ 76 - 10
command.c

@@ -15,31 +15,97 @@
 
 /* --- Extern --- */
 extern Error error;
-char* cmdlist[2]  = {
+char* cmdlist[2] = {
     "cd",
     NULL
 };
 
 /* --- Fonctions publiques --- */
-boolean is_internal_cmd(const char* cmd){
+void ini_pid_list(pid_list* pl) {
+    pl->first = NULL;
+    pl->last = NULL;
+}
+
+pid_node* add_pid(pid_list* pl, pid_t pid) {
+    pid_node* pn = malloc(sizeof (pid_node));
+    pn->pid = pid;
+    pn->next = NULL;
+    if (pl->first = NULL) {
+        pn->prev = NULL;
+        pl->first = pn;
+        pl->last = pn;
+    } else {
+        pn->prev = pl->last;
+        pl->last->next = pn;
+        pl->last = pn;
+    }
+    return pn;
+}
+
+pid_node* search_pid(pid_list* pl, pid_t pid) {
+    pid_node pn = pl->first;
+    while (pn != NULL) {
+        if (pn->pid == pid) {
+            return pn;
+        }
+        pn = pn->next;
+    }
+    return NULL;
+}
+
+void remove_pid(pid_list* pl, pid_node* pn) {
+    //Si 1er et seul
+    if (pn->prev == NULL && pn->next == NULL) {
+        pl->first = NULL;
+        pl->last = NULL;
+    }        
+    //Si 1er et non seul
+    else if (pn->prev == NULL && pn->next != NULL) {
+        pn->next->prev = NULL;
+        pl->first = pn->next;
+    }        
+    //Si dernier
+    else if (pn->next == NULL) {
+        pn->prev->next = NULL;
+        pl->last = pn->prev;
+    }        
+    //Sinon si il est au milieu
+    else {
+        pn->prev->next = pn->next;
+        pn->next->prev = pn->prev;
+    }
+    //Free
+    free(pn);
+}
+
+void clean_pid(pid_list* pl) {
+    pid_node* tmp, * pn = pl->first;
+    while (pn != NULL) {
+        tmp = pn->next;
+        free(pn);
+        pn = tmp;
+    }
+    free(pl);
+}
+
+boolean is_internal_cmd(const char* cmd) {
     //Parcours tableau pour trouver correspondance
-    for(int i = 0; cmdlist[i] != NULL; i++){
+    for (int i = 0; cmdlist[i] != NULL; i++) {
         printf("%s - %s\n", cmd, cmdlist[i]);
-        if(strncmp(cmd, cmdlist[i], strlen(cmdlist[i]) + 1) == 0){
+        if (strncmp(cmd, cmdlist[i], strlen(cmdlist[i]) + 1) == 0) {
             return true;
         }
     }
     return false;
 }
 
-int launch_internal_command(Command* cmd){
+int launch_internal_command(Command* cmd) {
     int result, length = strlen(cmd->name) + 1;
     //cd
-    if(strncmp(cmd->name, cmdlist[0], length) == 0){
+    if (strncmp(cmd->name, cmdlist[0], length) == 0) {
         result = cd(cmd->argc, cmd->argv);
         return result;
-    }
-    //Aucune commande
+    }        //Aucune commande
     else {
         return SHELL_FAIL;
     }
@@ -58,8 +124,8 @@ int cd(int argc, char** argv) {
                 addperror("Erreur chdir()");
                 return SHELL_ERR;
             }
-        }            
-        //Sinon on va dans le dossier indiqué par l'utilisateur
+        }
+            //Sinon on va dans le dossier indiqué par l'utilisateur
         else {
             if (chdir(argv[1]) == ERR) {
                 error.print("path does not exist\n");

+ 18 - 0
command.h

@@ -9,12 +9,30 @@
 #define COMMAND_H
 
 /* --- Include --- */
+#include <sys/types.h>
 #include "constante.h"
 
+/* --- Structure --- */
+typedef struct pid_node pid_node;
+struct pid_node{
+    pid_t pid;
+    pid_node prev;
+    pid_node next;
+};
+typedef struct{
+    pid_node first;
+    pid_node last;
+}pid_list;
+
 /* --- Extern --- */
 extern char* cmdlist[2];
 
 /* --- Fonctions --- */
+void ini_pid_list(pid_list*);
+pid_node* add_pid(pid_list*, pid_t);
+pid_node* search_pid(pid_list*, pid_t);
+void remove_pid(pid_list*, pid_node*);
+void clean_pid(pid_list*);
 boolean is_internal_cmd(const char*);
 int launch_internal_command(Command*);
 

+ 46 - 5
mysh.c

@@ -8,6 +8,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <wait.h>
 #include "error.h"
 #include "str.h"
 #include "parser.h"
@@ -15,12 +17,13 @@
 #include "execute.h"
 #include "mysh.h"
 
-#include <sys/types.h>
-#include <sys/stat.h>
-
 /* --- Extern --- */
 extern Error error;
 
+/* --- Global --- */
+pid_list pidlist;
+pid_node* active = NULL;
+
 /* --- Fonctions privées --- */
 void test_write() {
     char* a = "azerty\n";
@@ -51,8 +54,9 @@ int main(int argc, char* argv[]) {
     CommandTab ct;
     char str[BUFFER_SIZE];
     int a;
-    //Initialisation erreur
+    //Initialisation structures
     error_finit("mysh.log");
+    ini_pid_list(&pidlist);
     
     printf("%d\n", is_executable_file("../TP4/exo1"));
     
@@ -90,7 +94,44 @@ int main(int argc, char* argv[]) {
             show_current_dir(NULL, "\n");
         }
     }
-    //Supprime
+    //Nettoyage structures
     clean_command(&ct);
+    clean_pid(&pidlist);
     error.exit();
+}
+
+int run(CommandTab ct){
+    pid_t pid;
+    int result;
+    pid_node* pnode;
+    //Fork pour executer la commande
+    pid = fork();
+    if(pid == ERR){
+        addperror("Erreur lors du fork pour l'execution des commandes");
+        error.print("Erreur systeme, impossible de continuer\n");
+        return SHELL_ERR;
+    } 
+    //Pere
+    else if(pid != 0){
+        //Ajoute du pid dans la liste des pid actifs
+        pnode = add_pid(&pidlist, pid);
+        //Si on attend le retour de la commande
+        if(!ct.bck){
+            //Ajout du pid comme étant le pid actif et attente
+            active = pnode;
+            wait(&result);
+            //Le processus n'est plus actif et analyse retour
+            active = NULL;
+            remove_pid(&pidlist, pnode);
+            if(WIFEXITED(result)){
+                return WEXITSTATUS(result);
+            } else {
+                return SHELL_FAIL;
+            }
+        }
+        //Sinon ok
+        return SHELL_OK;
+    }
+    //Fils
+    return SHELL_OK;
 }

+ 8 - 1
mysh.h

@@ -8,7 +8,7 @@
 #ifndef MYSH_H
 #define MYSH_H
 
-/* --- Fonctions utilitaires --- */
+/* --- Fonctions --- */
 /**
  * Affiche le dossier de travail actuel
  * @param char* Chaine à afficher avant le dossier
@@ -16,5 +16,12 @@
  */
 void show_current_dir(const char*, const char*);
 
+/**
+ * Execute l'ensemble des commandes d'une ligne
+ * @param CommandTab La ligne de commande parser
+ * @return 
+ */
+int run(CommandTab);
+
 #endif /* MYSH_H */