Kaynağa Gözat

Merge pull request #6 from Loquicom/master

MaJ debug
Loquicom 6 yıl önce
ebeveyn
işleme
ad1e91fad5
4 değiştirilmiş dosya ile 59 ekleme ve 7 silme
  1. 1 1
      makefile
  2. 37 1
      mysh.c
  3. 18 0
      mysh.h
  4. 3 5
      parser.c

+ 1 - 1
makefile

@@ -90,4 +90,4 @@ archive: clean
 # DEPENDANCES
 str.o: str.c str.h
 parser.o: parser.c parser.h constante.h str.h
-mysh.o: mysh.c parser.h constante.h str.h
+mysh.o: mysh.c parser.h constante.h str.h mysh.h

+ 37 - 1
mysh.c

@@ -8,14 +8,16 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include "parser.h"
+#include "mysh.h"
 
-
+/* --- Fonctions privées --- */
 void test_write(){
     char* a = "azerty\n";
     int tmp = write(1, a, strlen(a));
     printf("%d\n", tmp);
 }
 
+/* --- Main --- */
 int main(int argc, char* argv[]) {
 
     CommandTab ct;
@@ -44,5 +46,39 @@ int main(int argc, char* argv[]) {
     //Supprime
     clean_command(&ct);
     return (EXIT_SUCCESS);
+
+}
+
+/* --- Commandes internes --- */
+void cd(int argc, char** argv){
+    //Si trop d'arguments
+    if(argc > 2) {
+        printf("too many arguments : 1 required, %d given\n", argc-1);
+    }
+    else {
+        //Si aucun argument on vas à la racine
+        if(argc == 1) {
+            if(chdir("/") == ERR){
+                perror("Erreur chdir() : ");
+            }
+        }
+        //Sinon on va dans le dossier indiqué par l'utilisateur
+        else {
+            if(chdir(argv[1]) == ERR){
+                printf("path does not exist\n");
+            }
+        }
+    }
+    //show_current_dir("current working directory is: ", "\n");
 }
 
+/* --- Fonctions utilitaires --- */
+void show_current_dir(const char* before, const char* after){
+    char buffer[512];
+    if (getcwd(buffer, sizeof(buffer)) == NULL){
+        perror("Erreur getcwd() : ");
+    }
+    else {
+        printf("%s%s%s", before, buffer, after);
+    }
+}

+ 18 - 0
mysh.h

@@ -0,0 +1,18 @@
+/* 
+ * File:   mysh.h
+ * Author: Arthur Brandao
+ *
+ * Created on 6 novembre 2018
+ */
+
+#ifndef MYSH_H
+#define MYSH_H
+
+/* --- Fonctions utilitaires --- */
+void show_current_dir(const char*, const char*);
+
+/* --- Commandes internes --- */
+void cd(int, char**);
+
+#endif /* MYSH_H */
+

+ 3 - 5
parser.c

@@ -509,25 +509,23 @@ void clean_command(CommandTab* ct){
         if(ct->cmd[i]->argc > 0){
             ct->cmd[i]->name = NULL;
             for(int j = 0; j < ct->cmd[i]->argc; j++){
-                printf("free argv %d : %s\n", j, ct->cmd[i]->argv[j]);
-                fflush(stdout);
                 free(ct->cmd[i]->argv[j]);
             }
         }
         //Ferme les fichiers ouverts si besoin
         if(ct->cmd[i]->input != STDIN){
             if(close(ct->cmd[i]->input)){
-                fprintf(stderr, "Erreur lors de la fermeture du fichier d'input de %s : %s", ct->cmd[i]->cmd, strerror(errno));
+                fprintf(stderr, "Erreur lors de la fermeture du fichier d'input de %s : %s\n", ct->cmd[i]->cmd, strerror(errno));
             }
         }
         if(ct->cmd[i]->output != STDOUT){
             if(close(ct->cmd[i]->output)){
-                fprintf(stderr, "Erreur lors de la fermeture du fichier d'output de %s : %s", ct->cmd[i]->cmd, strerror(errno));
+                fprintf(stderr, "Erreur lors de la fermeture du fichier d'output de %s : %s\n", ct->cmd[i]->cmd, strerror(errno));
             }
         }
         if(ct->cmd[i]->error != STDERR){
             if(close(ct->cmd[i]->error)){
-                fprintf(stderr, "Erreur lors de la fermeture du fichier d'error de %s : %s", ct->cmd[i]->cmd, strerror(errno));
+                fprintf(stderr, "Erreur lors de la fermeture du fichier d'error de %s : %s\n", ct->cmd[i]->cmd, strerror(errno));
             }
         }
         //Supprime la ligne de commande