Browse Source

:sparkles: Ajout commande status

Loquicom 6 years ago
parent
commit
a3577e2715
6 changed files with 44 additions and 5 deletions
  1. 27 2
      command.c
  2. 5 1
      command.h
  3. 3 0
      execute.c
  4. 1 0
      execute.h
  5. 4 2
      mysh.c
  6. 4 0
      mysh.h

+ 27 - 2
command.c

@@ -11,13 +11,19 @@
 #include "error.h"
 #include "str.h"
 #include "parser.h"
+#include "mysh.h"
+#include "execute.h"
 #include "command.h"
 
 /* --- Extern --- */
 extern Error error;
-char* cmdlist[3] = {
+extern pid_t last;
+extern int status_cmd;
+extern int result_cmd;
+char* cmdlist[NB_CMD] = {
     "cd",
     "exit",
+    "status",
     NULL
 };
 boolean exitsh = false;
@@ -113,7 +119,12 @@ int launch_internal_command(Command* cmd) {
     else if (strncmp(cmd->name, cmdlist[1], length) == 0) {
         result = exit_sh(cmd->argc, cmd->argv);
         return result;
-    } 
+    }
+    //status
+    else if (strncmp(cmd->name, cmdlist[2], length) == 0) {
+        result = status(cmd->argc, cmd->argv);
+        return result;
+    }
     //Aucune commande
     else {
         return SHELL_FAIL;
@@ -149,4 +160,18 @@ int cd(int argc, char** argv) {
 int exit_sh(int argc, char** argv){
     exitsh = true;
     return EXIT_SUCCESS;
+}
+
+int status(int argc, char** argv){
+    if(!(last != -1 && status_cmd != -1)){
+        error.print("Aucune commande executée\n");
+        return EXIT_FAILURE;
+    }
+    //En fonction du status
+    if(result_cmd == SHELL_FAIL){
+        printf("%d terminé anormalement\n", last);
+    } else {
+        printf("%d terminé avec comme code de retour %d\n", last, status_cmd);
+    }
+    return EXIT_SUCCESS;
 }

+ 5 - 1
command.h

@@ -12,6 +12,9 @@
 #include <sys/types.h>
 #include "constante.h"
 
+/* --- Constante --- */
+#define NB_CMD 4
+
 /* --- Structure --- */
 typedef struct pid_node pid_node;
 struct pid_node{
@@ -26,7 +29,7 @@ typedef struct{
 }pid_list;
 
 /* --- Extern --- */
-extern char* cmdlist[3];
+extern char* cmdlist[NB_CMD];
 extern boolean exitsh;
 
 /* --- Fonctions --- */
@@ -46,6 +49,7 @@ int launch_internal_command(Command*);
  */
 int cd(int, char**);
 int exit_sh(int, char**);
+int status(int, char**);
 
 #endif /* COMMAND_H */
 

+ 3 - 0
execute.c

@@ -16,6 +16,7 @@
 /* --- Extern --- */
 extern Error error;
 pid_t active = -1;
+pid_t last = -1;
 
 /* --- Fonctions publiques --- */
 boolean is_executable_file(const char * cmd) {
@@ -53,6 +54,7 @@ int exec_shell(char* name, char** argv){
     //Pere
     active = pid;
     wait(&result);
+    last = pid;
     active = -1;
     //Retourne retour fils
     if(WIFEXITED(result)){
@@ -87,6 +89,7 @@ int exec_file(char* name, char** argv){
     //Pere
     active = pid;
     wait(&result);
+    last = pid;
     active = -1;
     //Retourne retour fils
     if(WIFEXITED(result)){

+ 1 - 0
execute.h

@@ -14,6 +14,7 @@
 
 /* --- Extern --- */
 extern pid_t active;
+extern pid_t last;
 
 /* --- Fonctions publiques --- */
 boolean is_executable_file(const char *);

+ 4 - 2
mysh.c

@@ -25,6 +25,8 @@
 extern Error error;
 extern boolean exitsh;
 extern pid_t active;
+int status_cmd = -1;
+int result_cmd = -1;
 
 /* --- Global --- */
 pid_list pidlist;
@@ -210,8 +212,8 @@ int main(int argc, char* argv[]) {
             continue;
         }
         //Execute
-        result = run(ct, NULL);
-        printf("Result : %d\n", result);
+        result_cmd = run(ct, &status_cmd);
+        printf("Result : %d\n", result_cmd);
         //Vide le resultat du parse de la ligne de commande
         clean_command(&ct);
     }

+ 4 - 0
mysh.h

@@ -8,6 +8,10 @@
 #ifndef MYSH_H
 #define MYSH_H
 
+/* --- Extern --- */
+extern int status_cmd;
+extern int result_cmd;
+
 /* --- Fonctions --- */
 /**
  * Affiche le dossier de travail actuel