Quellcode durchsuchen

Ajout commande myjobs

Loquicom vor 6 Jahren
Ursprung
Commit
bafa9fdb75
5 geänderte Dateien mit 82 neuen und 6 gelöschten Zeilen
  1. BIN
      a
  2. 32 2
      command.c
  3. 47 1
      command.h
  4. 2 2
      makefile
  5. 1 1
      mysh.c

+ 32 - 2
command.c

@@ -12,11 +12,11 @@
 #include "error.h"
 #include "str.h"
 #include "parser.h"
-#include "mysh.h"
 #include "execute.h"
 #include "ipc.h"
 #include "variable.h"
 #include "command.h"
+#include "mysh.h"
 
 /* --- Extern --- */
 extern Error error;
@@ -24,6 +24,7 @@ extern pid_t last;
 extern int status_cmd;
 extern int result_cmd;
 extern char base_path[];
+extern pid_list pidlist;
 char* cmdlist[] = {
     "cd",
     "exit",
@@ -34,6 +35,7 @@ char* cmdlist[] = {
     "unset",
     "myls",
     "myps",
+    "myjobs",
     NULL
 };
 boolean exitsh = false;
@@ -44,10 +46,18 @@ void ini_pid_list(pid_list* pl) {
     pl->last = NULL;
 }
 
-pid_node* add_pid(pid_list* pl, pid_t pid, int job) {
+pid_node* add_pid(pid_list* pl, pid_t pid, int job, char* cmd) {
+    int length;
+    //Creation noeud
     pid_node* pn = malloc(sizeof (pid_node));
     pn->pid = pid;
     pn->job = job;
+    //Copie nom commande
+    length = strlen(cmd) + 1;
+    pn->cmd = malloc(sizeof(char) * length);
+    memset(pn->cmd, 0, length);
+    strncpy(pn->cmd, cmd, length - 1);
+    //Setup chainement
     pn->next = NULL;
     if (pl->first == NULL) {
         pn->prev = NULL;
@@ -73,6 +83,7 @@ pid_node* search_pid(pid_list* pl, pid_t pid) {
 }
 
 void remove_pid(pid_list* pl, pid_node* pn) {
+    free(pn->cmd);
     //Si 1er et seul
     if (pn->prev == NULL && pn->next == NULL) {
         pl->first = NULL;
@@ -101,6 +112,7 @@ void clean_pid(pid_list* pl) {
     pid_node* tmp, * pn = pl->first;
     while (pn != NULL) {
         tmp = pn->next;
+        free(pn->cmd);
         free(pn);
         pn = tmp;
     }
@@ -165,6 +177,11 @@ int launch_internal_command(Command* cmd) {
         result = myps(cmd->argc, cmd->argv);
         return result;
     }   
+    //myjobs
+    else if (strncmp(cmd->name, cmdlist[9], length) == 0) {
+        result = myjobs(cmd->argc, cmd->argv);
+        return result;
+    }   
     //Aucune commande
     else {
         return SHELL_FAIL;
@@ -368,4 +385,17 @@ int myps(int argc, char** argv){
     //Execution
     //return exec_file(path, argv);
     return 0;
+}
+
+int myjobs(int argc, char** argv){
+    pid_node* pn = pidlist.first;
+    if(pn == NULL){
+        printf("Aucun job\n");
+        return EXIT_SUCCESS;
+    }
+    while(pn != NULL){
+        printf("[%d] %d En cours %s\n", pn->job, pn->pid, pn->cmd);
+        pn = pn->next;
+    }
+    return EXIT_SUCCESS;
 }

+ 47 - 1
command.h

@@ -17,6 +17,7 @@ typedef struct pid_node pid_node;
 struct pid_node{
     pid_t pid;
     int job;
+    char* cmd;
     pid_node* prev;
     pid_node* next;
 };
@@ -41,9 +42,10 @@ void ini_pid_list(pid_list*);
  * @param pid_list* La liste de PID
  * @param pid_t Le pid
  * @param int Le numero de job
+ * @param char* Le nom de la commande
  * @return Le noeud crée
  */
-pid_node* add_pid(pid_list*, pid_t, int);
+pid_node* add_pid(pid_list*, pid_t, int, char*);
 
 /**
  * Cherche un PID dans la liste
@@ -105,17 +107,61 @@ int exit_sh(int, char**);
  */
 int status(int, char**);
 
+/**
+ * AJoute une variable d'environnement
+ * @param int argc
+ * @param char** argv
+ * @return Statut
+ */
 int set_env(int, char**);
 
+/**
+ * Supprime une variable d'environnement
+ * @param int argc
+ * @param char** argv
+ * @return Statut
+ */
 int unset_env(int, char**);
 
+/**
+ * Ajoute une variable locale
+ * @param int argc
+ * @param char** argv
+ * @return Statut
+ */
 int set_local(int, char**);
 
+/**
+ * Supprime une variable locale
+ * @param int argc
+ * @param char** argv
+ * @return Statut
+ */
 int unset_local(int, char**);
 
+/**
+ * Execute le programme myls
+ * @param int argc
+ * @param char** argv
+ * @return Statut
+ */
 int myls(int, char**);
 
+/**
+ * Execute le programma myps
+ * @param int argc
+ * @param char** argv
+ * @return Statut
+ */
 int myps(int, char**);
 
+/**
+ * Affiche tous les jobs
+ * @param int argc
+ * @param char** argv
+ * @return Statut
+ */
+int myjobs(int, char**);
+
 #endif /* COMMAND_H */
 

+ 2 - 2
makefile

@@ -103,8 +103,8 @@ str.o: str.c str.h
 parser.o: parser.c error.h str.h wildcard.h constante.h ipc.h sem.h shm.h \
  subdiv.h variable.h parser.h
 wildcard.o: wildcard.c error.h wildcard.h constante.h
-command.o: command.c error.h str.h parser.h constante.h mysh.h execute.h \
- ipc.h sem.h shm.h subdiv.h variable.h command.h
+command.o: command.c error.h str.h parser.h constante.h execute.h ipc.h \
+ sem.h shm.h subdiv.h variable.h command.h mysh.h
 execute.o: execute.c error.h execute.h constante.h
 sem.o: sem.c error.h sem.h constante.h
 shm.o: shm.c error.h shm.h constante.h

+ 1 - 1
mysh.c

@@ -268,7 +268,7 @@ int run(CommandTab ct, int* status){
         }
         printf("[%d] %d\n", job, pid);
         //Ajout du fils dans la liste des pid
-        add_pid(&pidlist, pid, job++);
+        add_pid(&pidlist, pid, job++, ct.line);
         //Pour le pere c'est fini
         return SHELL_OK;
     }