Browse Source

:fire: Suppr flag lncurses

Loquicom 6 years ago
parent
commit
50b4e7eb23
9 changed files with 496 additions and 496 deletions
  1. 54 54
      .gitignore
  2. 2 2
      README.md
  3. 37 37
      constante.h
  4. 1 1
      makefile
  5. 36 36
      mysh.c
  6. 197 197
      parser.c
  7. 40 40
      parser.h
  8. 116 116
      str.c
  9. 13 13
      str.h

+ 54 - 54
.gitignore

@@ -1,55 +1,55 @@
-# Prerequisites
-*.d
-
-# Object files
-*.o
-*.ko
-*.obj
-*.elf
-
-# Linker output
-*.ilk
-*.map
-*.exp
-
-# Precompiled Headers
-*.gch
-*.pch
-
-# Libraries
-*.lib
-*.a
-*.la
-*.lo
-
-# Shared objects (inc. Windows DLLs)
-*.dll
-*.so
-*.so.*
-*.dylib
-
-# Executables
-*.exe
-*.out
-*.app
-*.i*86
-*.x86_64
-*.hex
-
-# Debug files
-*.dSYM/
-*.su
-*.idb
-*.pdb
-
-# Kernel Module Compile Results
-*.mod*
-*.cmd
-.tmp_versions/
-modules.order
-Module.symvers
-Mkfile.old
-dkms.conf
-/.directory
-/nbproject
+# Prerequisites
+*.d
+
+# Object files
+*.o
+*.ko
+*.obj
+*.elf
+
+# Linker output
+*.ilk
+*.map
+*.exp
+
+# Precompiled Headers
+*.gch
+*.pch
+
+# Libraries
+*.lib
+*.a
+*.la
+*.lo
+
+# Shared objects (inc. Windows DLLs)
+*.dll
+*.so
+*.so.*
+*.dylib
+
+# Executables
+*.exe
+*.out
+*.app
+*.i*86
+*.x86_64
+*.hex
+
+# Debug files
+*.dSYM/
+*.su
+*.idb
+*.pdb
+
+# Kernel Module Compile Results
+*.mod*
+*.cmd
+.tmp_versions/
+modules.order
+Module.symvers
+Mkfile.old
+dkms.conf
+/.directory
+/nbproject
 /mysh

+ 2 - 2
README.md

@@ -1,2 +1,2 @@
-# SEC
-Projet final SEC Master 1 Informatique Artois
+# SEC
+Projet final SEC Master 1 Informatique Artois

+ 37 - 37
constante.h

@@ -1,37 +1,37 @@
-/* 
- * File:   constante.h
- * Author: Arthur Brandao
- *
- * Created on 31 octobre 2018
- */
-
-#ifndef CONSTANTE_H
-#define CONSTANTE_H
-
-/* --- General --- */
-#define SHELL_ERR -1
-#define SHELL_FAIL 0
-#define SHELL_OK 1
-
-/* --- Fichier --- */
-#define STDIN 0
-#define STDOUT 1
-#define STDERR 2
-
-/* --- Separateur commande --- */
-#define SHELL_END 0 //Aucune autre commande après
-#define SHELL_NONE 1 //Aucun lien entre les 2 commandes
-#define SHELL_IF 2 //La commande suivante s'execute si l'actuel reussis
-#define SHELL_ELSE 3 //La commande suivante s'execute si l'actuel échou
-#define SHELL_PIPE 4 //Envoi des données de sorties vers la commande suivante
-
-/* --- Boolean --- */
-#define boolean int
-#define true 1
-#define false 0
-
-/* --- Null --- */
-#define null NULL
-
-#endif /* CONSTANTE_H */
-
+/* 
+ * File:   constante.h
+ * Author: Arthur Brandao
+ *
+ * Created on 31 octobre 2018
+ */
+
+#ifndef CONSTANTE_H
+#define CONSTANTE_H
+
+/* --- General --- */
+#define SHELL_ERR -1
+#define SHELL_FAIL 0
+#define SHELL_OK 1
+
+/* --- Fichier --- */
+#define STDIN 0
+#define STDOUT 1
+#define STDERR 2
+
+/* --- Separateur commande --- */
+#define SHELL_END 0 //Aucune autre commande après
+#define SHELL_NONE 1 //Aucun lien entre les 2 commandes
+#define SHELL_IF 2 //La commande suivante s'execute si l'actuel reussis
+#define SHELL_ELSE 3 //La commande suivante s'execute si l'actuel échou
+#define SHELL_PIPE 4 //Envoi des données de sorties vers la commande suivante
+
+/* --- Boolean --- */
+#define boolean int
+#define true 1
+#define false 0
+
+/* --- Null --- */
+#define null NULL
+
+#endif /* CONSTANTE_H */
+

+ 1 - 1
makefile

@@ -27,7 +27,7 @@ CC = gcc
 CCFLAGS_STD = -Wall -O3 -Werror -ansi -pedantic -std=c11
 CCFLAGS_DEBUG = -D _DEBUG_
 CCFLAGS = $(CCFLAGS_STD)
-CCLIBS = -lncurses
+CCLIBS =
 
 #
 # REGLES

+ 36 - 36
mysh.c

@@ -1,36 +1,36 @@
-/* 
- * File:   mysh.c
- * Author: Arthur Brandao
- *
- * Created on 31 octobre 2018, 12:43
- */
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include "parser.h"
-
-
-void test_write(){
-    char* a = "azerty\n";
-    int tmp = write(1, a, strlen(a));
-    printf("%d\n", tmp);
-}
-
-int main(int argc, char* argv[]) {
-
-    CommandTab ct;
-    char str[500];
-    int a;
-    //Recup ligne
-    printf("%s\n", fgets(str, 500, stdin));
-    //Separe les commandes
-    a = parse_line(&ct, str);
-    printf("Result : %d\n\n", a);
-    //Parse les commandes
-    a = parse_all_command(&ct);
-    printf("Result : %d\n\n", a);
-    //Supprime
-    clean_command(&ct);
-    return (EXIT_SUCCESS);
-}
-
+/* 
+ * File:   mysh.c
+ * Author: Arthur Brandao
+ *
+ * Created on 31 octobre 2018, 12:43
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "parser.h"
+
+
+void test_write(){
+    char* a = "azerty\n";
+    int tmp = write(1, a, strlen(a));
+    printf("%d\n", tmp);
+}
+
+int main(int argc, char* argv[]) {
+
+    CommandTab ct;
+    char str[500];
+    int a;
+    //Recup ligne
+    printf("%s\n", fgets(str, 500, stdin));
+    //Separe les commandes
+    a = parse_line(&ct, str);
+    printf("Result : %d\n\n", a);
+    //Parse les commandes
+    a = parse_all_command(&ct);
+    printf("Result : %d\n\n", a);
+    //Supprime
+    clean_command(&ct);
+    return (EXIT_SUCCESS);
+}
+

+ 197 - 197
parser.c

@@ -1,197 +1,197 @@
-/* 
- * File:   parser.c
- * Author: Arthur Brandao
- *
- * Created on 31 octobre 2018
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include "parser.h"
-
-/* --- Fonctions privées --- */
-int nb_commands(char* line){
-    //Initialisation variable
-    int compteur = 0;
-    //Parcours chaine pour chercher séparateur et compter le nombre de commande
-    while(*line){
-        //Si un ;
-        if(*line == ';'){
-            compteur++;
-        }
-        //Si | ou ||
-        else if(*line == '|'){
-            //Verif que ce n'est pas le dernier carac
-            if(*(line + 1) == '\0'){
-                return SHELL_ERR;
-            }
-            //Si un || on avance de 1 en plus
-            else if(*(line + 1) == '|'){
-                //Si à la fin de la chaine
-                if(*(line + 2) == '\0'){
-                    return SHELL_ERR;
-                }
-                line++;
-            }
-            compteur++;
-        }
-        //Si un &&
-        if(*line == '&'){
-            //Si celui d'apres est bien un &&
-            if(*(line + 1) == '&'){
-                line++;
-                compteur++;
-            }
-            //Sinon il doit y avoir un vide pour etre le & du bck ou un > avant
-            else if(*(line + 1) != '\0' && *(line - 1) != '>'){
-                return SHELL_ERR;
-            }
-        }
-        line++;
-    }
-    //Ajoute la dernière commande
-    compteur++;
-    return compteur;
-}
-
-/**
- * Recup la 1er commande
- * @param c Structure commande à initialiser
- * @param line Ligne avec la commande
- * @return int Le décallage à effectuer dans line
- */
-int get_command(Command* c, char* line){
-    //Declaration variable
-    char* deb = line, * old;
-    int length = 0, separator = 0, next = SHELL_NONE;
-    //Parcours chaine pour chercher un séparateur
-    while(*line){
-        //Si un ;
-        if(*line == ';'){
-            separator = 1;
-            break;
-        }
-        //Si | ou ||
-        else if(*line == '|'){
-            //Si 1 ou 2 |
-            if(*(line + 1) == '|'){
-                separator = 2;
-                next = SHELL_ELSE;
-            } else {
-                separator = 1;
-                next = SHELL_PIPE;
-            }
-            break;
-        }
-        //Si un &&
-        if(*line == '&'){
-            //Si celui d'apres est bien un &&
-            if(*(line + 1) == '&'){
-                separator = 2;
-                next = SHELL_IF;
-                break;
-            }
-        }
-        length++;
-        line++;
-    }
-    //Verif si c'est la dernière commande
-    if(!*line){
-        next = SHELL_END;
-    }
-    //Allocation memoire et copie chaine
-    c->cmd = malloc(sizeof(char) * (length + 1));
-    strncpy(c->cmd, deb, length);
-    c->next = next;
-    c->argc = 0;
-    c->input = STDIN;
-    c->output = STDOUT;
-    c->error = STDERR;
-    //Trim et supprime l'ancienne chaine
-    old = c->cmd;
-    c->cmd = rtrim(c->cmd, ' ');
-    free(old);
-    //Retour
-    return length + separator;
-}
-
-/* --- Fonctions publiques --- */
-int parse_line(CommandTab* ct, char* line){
-    //Declaration variable
-    int compteur, tmp;
-    //Nettoyage ligne
-    line = trim(mtrim(line, '\n'));
-    //Compte le nombre de commande dans la ligne
-    compteur = nb_commands(line);
-    if(compteur == SHELL_ERR){
-        return SHELL_ERR;
-    }
-    //Initialisation structure
-    ct->cmd = malloc(sizeof(Command*) * compteur);
-    ct->length = compteur;
-    //Recupération de chaque commande
-    for(int i = 0; i < compteur; i++){
-        ct->cmd[i] = malloc(sizeof(Command));
-        tmp = get_command(ct->cmd[i], line);
-        line += tmp;
-        //Si pas dernière commande on trim
-        if(i + 1 < compteur){
-            line = ltrim(line, ' ');
-        }
-    }
-    //Retour
-    return SHELL_OK;
-}
-
-int parse_command(Command* c){
-    return 1;
-}
-
-int parse_all_command(CommandTab* ct){
-    int tmp;
-    for(int i = 0; i < ct->length; i++){
-        tmp = parse_command(ct->cmd[i]);
-        if(tmp != SHELL_OK){
-            return SHELL_FAIL;
-        }
-    }
-    return SHELL_OK;
-}
-
-void clean_command(CommandTab* ct){
-    extern int errno;
-    //Vide le tableau
-    for(int i = 0; i < ct->length; i++){
-        //Si la commande a été parsée on vide les arguments
-        if(ct->cmd[i]->argc > 0){
-            ct->cmd[i]->name = NULL;
-            for(int j = 0; j < ct->cmd[i]->argc; j++){
-                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));
-            }
-        }
-        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));
-            }
-        }
-        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));
-            }
-        }
-        //Supprime la ligne de commande
-        free(ct->cmd[i]->cmd);   
-        //Supprime la structure
-        free(ct->cmd[i]);
-    }
-    //Met à 0 la taille du tableau
-    ct->length = 0;
-}
+/* 
+ * File:   parser.c
+ * Author: Arthur Brandao
+ *
+ * Created on 31 octobre 2018
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include "parser.h"
+
+/* --- Fonctions privées --- */
+int nb_commands(char* line){
+    //Initialisation variable
+    int compteur = 0;
+    //Parcours chaine pour chercher séparateur et compter le nombre de commande
+    while(*line){
+        //Si un ;
+        if(*line == ';'){
+            compteur++;
+        }
+        //Si | ou ||
+        else if(*line == '|'){
+            //Verif que ce n'est pas le dernier carac
+            if(*(line + 1) == '\0'){
+                return SHELL_ERR;
+            }
+            //Si un || on avance de 1 en plus
+            else if(*(line + 1) == '|'){
+                //Si à la fin de la chaine
+                if(*(line + 2) == '\0'){
+                    return SHELL_ERR;
+                }
+                line++;
+            }
+            compteur++;
+        }
+        //Si un &&
+        if(*line == '&'){
+            //Si celui d'apres est bien un &&
+            if(*(line + 1) == '&'){
+                line++;
+                compteur++;
+            }
+            //Sinon il doit y avoir un vide pour etre le & du bck ou un > avant
+            else if(*(line + 1) != '\0' && *(line - 1) != '>'){
+                return SHELL_ERR;
+            }
+        }
+        line++;
+    }
+    //Ajoute la dernière commande
+    compteur++;
+    return compteur;
+}
+
+/**
+ * Recup la 1er commande
+ * @param c Structure commande à initialiser
+ * @param line Ligne avec la commande
+ * @return int Le décallage à effectuer dans line
+ */
+int get_command(Command* c, char* line){
+    //Declaration variable
+    char* deb = line, * old;
+    int length = 0, separator = 0, next = SHELL_NONE;
+    //Parcours chaine pour chercher un séparateur
+    while(*line){
+        //Si un ;
+        if(*line == ';'){
+            separator = 1;
+            break;
+        }
+        //Si | ou ||
+        else if(*line == '|'){
+            //Si 1 ou 2 |
+            if(*(line + 1) == '|'){
+                separator = 2;
+                next = SHELL_ELSE;
+            } else {
+                separator = 1;
+                next = SHELL_PIPE;
+            }
+            break;
+        }
+        //Si un &&
+        if(*line == '&'){
+            //Si celui d'apres est bien un &&
+            if(*(line + 1) == '&'){
+                separator = 2;
+                next = SHELL_IF;
+                break;
+            }
+        }
+        length++;
+        line++;
+    }
+    //Verif si c'est la dernière commande
+    if(!*line){
+        next = SHELL_END;
+    }
+    //Allocation memoire et copie chaine
+    c->cmd = malloc(sizeof(char) * (length + 1));
+    strncpy(c->cmd, deb, length);
+    c->next = next;
+    c->argc = 0;
+    c->input = STDIN;
+    c->output = STDOUT;
+    c->error = STDERR;
+    //Trim et supprime l'ancienne chaine
+    old = c->cmd;
+    c->cmd = rtrim(c->cmd, ' ');
+    free(old);
+    //Retour
+    return length + separator;
+}
+
+/* --- Fonctions publiques --- */
+int parse_line(CommandTab* ct, char* line){
+    //Declaration variable
+    int compteur, tmp;
+    //Nettoyage ligne
+    line = trim(mtrim(line, '\n'));
+    //Compte le nombre de commande dans la ligne
+    compteur = nb_commands(line);
+    if(compteur == SHELL_ERR){
+        return SHELL_ERR;
+    }
+    //Initialisation structure
+    ct->cmd = malloc(sizeof(Command*) * compteur);
+    ct->length = compteur;
+    //Recupération de chaque commande
+    for(int i = 0; i < compteur; i++){
+        ct->cmd[i] = malloc(sizeof(Command));
+        tmp = get_command(ct->cmd[i], line);
+        line += tmp;
+        //Si pas dernière commande on trim
+        if(i + 1 < compteur){
+            line = ltrim(line, ' ');
+        }
+    }
+    //Retour
+    return SHELL_OK;
+}
+
+int parse_command(Command* c){
+    return 1;
+}
+
+int parse_all_command(CommandTab* ct){
+    int tmp;
+    for(int i = 0; i < ct->length; i++){
+        tmp = parse_command(ct->cmd[i]);
+        if(tmp != SHELL_OK){
+            return SHELL_FAIL;
+        }
+    }
+    return SHELL_OK;
+}
+
+void clean_command(CommandTab* ct){
+    extern int errno;
+    //Vide le tableau
+    for(int i = 0; i < ct->length; i++){
+        //Si la commande a été parsée on vide les arguments
+        if(ct->cmd[i]->argc > 0){
+            ct->cmd[i]->name = NULL;
+            for(int j = 0; j < ct->cmd[i]->argc; j++){
+                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));
+            }
+        }
+        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));
+            }
+        }
+        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));
+            }
+        }
+        //Supprime la ligne de commande
+        free(ct->cmd[i]->cmd);   
+        //Supprime la structure
+        free(ct->cmd[i]);
+    }
+    //Met à 0 la taille du tableau
+    ct->length = 0;
+}

+ 40 - 40
parser.h

@@ -1,40 +1,40 @@
-/* 
- * File:   parser.h
- * Author: Arthur Brandao
- *
- * Created on 31 octobre 2018
- */
-
-#ifndef PARSER_H
-#define PARSER_H
-
-/* --- Include --- */
-#include "constante.h"
-#include "str.h"
-
-/* --- Structure --- */
-typedef struct{
-    char* cmd; //La commande en string
-    char* name; //Le nom de la commande
-    int argc; //Le nombre d'argument
-    char** argv; //Les arguments
-    int input; //Descripteur de fichier d'entré
-    int output; //Descripteur de fichier de sortie
-    int error; //Descripteur de fihier d'erreur
-    boolean bck; //En fond ou non
-    int next; //Lien avec la prochaine commande
-}Command;
-
-typedef struct{
-    Command** cmd; //Tableau avec toutes les commandes
-    int length; //Taille du tableau
-}CommandTab;
-
-/* --- Fonctions --- */
-int parse_line(CommandTab*, char*);
-int parse_command(Command*);
-int parse_all_command(CommandTab*);
-void clean_command(CommandTab*);
-
-#endif /* PARSER_H */
-
+/* 
+ * File:   parser.h
+ * Author: Arthur Brandao
+ *
+ * Created on 31 octobre 2018
+ */
+
+#ifndef PARSER_H
+#define PARSER_H
+
+/* --- Include --- */
+#include "constante.h"
+#include "str.h"
+
+/* --- Structure --- */
+typedef struct{
+    char* cmd; //La commande en string
+    char* name; //Le nom de la commande
+    int argc; //Le nombre d'argument
+    char** argv; //Les arguments
+    int input; //Descripteur de fichier d'entré
+    int output; //Descripteur de fichier de sortie
+    int error; //Descripteur de fihier d'erreur
+    boolean bck; //En fond ou non
+    int next; //Lien avec la prochaine commande
+}Command;
+
+typedef struct{
+    Command** cmd; //Tableau avec toutes les commandes
+    int length; //Taille du tableau
+}CommandTab;
+
+/* --- Fonctions --- */
+int parse_line(CommandTab*, char*);
+int parse_command(Command*);
+int parse_all_command(CommandTab*);
+void clean_command(CommandTab*);
+
+#endif /* PARSER_H */
+

+ 116 - 116
str.c

@@ -1,117 +1,117 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include "str.h"
-
-char** str_split(char* str, const char delim, int* length) {
-    char** res;
-    char* signet, * tmp = str;
-    char last = 0;
-    int compteur = 0, nb = 1, taille, count = 0;
-    //Compte le nombre d'element
-    while (*tmp) {
-        if (*tmp == delim && last != delim) {
-            nb++;
-        }
-        last = *tmp;
-        tmp++;
-    }
-    //Creation du tableau
-    res = malloc(sizeof (char*) * (nb + 1));
-    //Decoupage
-    tmp = str;
-    while (*tmp) {
-        //Si c'est le dernier mot
-        if (compteur == nb - 1) {
-            //Ajoute tous ce qui reste
-            res[compteur] = malloc(sizeof (char) * (strlen(str) - count));
-            int i = 0;
-            while(*tmp){
-                res[compteur][i++] = *tmp;
-                tmp++;
-            }
-        } else {
-            //Recup la taille du mot
-            signet = tmp;
-            taille = 0;
-            while (*tmp != delim) {
-                taille++;
-                tmp++;
-                count++;
-            }
-            //Creation du mot
-            res[compteur] = malloc(sizeof (char) * taille);
-            //Ajout du mot
-            for (int i = 0; i < taille; i++) {
-                res[compteur][i] = *signet;
-                signet++;
-            }
-            compteur++;
-            //Passe les delimiteurs consecutif
-            while (*tmp == delim) {
-                tmp++;
-                count++;
-            }
-        }
-    }
-    //Ajoute NULL à la fin
-    res[nb + 1] = NULL;
-    //Retour nombre de mot et tableau
-    *length = nb;
-    return res;
-}
-
-char* trim(char* str){
-    return ltrim(rtrim(str, ' '), ' ');
-}
-
-char* mtrim(char* str, char mask){
-    return ltrim(rtrim(str, mask), mask);
-}
-
-char* ltrim(char* str, char mask){
-    //Variable
-    int cmpt = 0;
-    char* res;
-    //Compte le nombre d'espace
-    while(str[cmpt] == mask){
-        cmpt++;
-    }
-    //Si aucun espace au debut
-    if(cmpt == 0){
-        res = malloc(sizeof(char) * strlen(str));
-        strcpy(res, str);
-        return res;
-    }
-    //Sinon creation nouvelle chaine
-    res = malloc(sizeof(char) * (strlen(str) - cmpt + 1));
-    for(int i = 0, j = cmpt; i < (strlen(str) - cmpt); i++, j++){
-        res[i] = str[j];
-    }
-    //Retour nouvelle chaine
-    return res;
-}
-
-char* rtrim(char* str, char mask){
-    //Variable
-    int cmpt = strlen(str) - 1;
-    char* res;
-    //Compte le nombre d'espace
-    while(str[cmpt] == mask){
-        cmpt--;
-    }
-    //Si aucun espace au debut
-    if(cmpt == strlen(str) - 1){
-        res = malloc(sizeof(char) * strlen(str));
-        strcpy(res, str);
-        return res;
-    }
-    cmpt++;
-    //Sinon creation nouvelle chaine
-    res = malloc(sizeof(char) * (cmpt + 2));
-    for(int i = 0; i < cmpt; i++){
-        res[i] = str[i];
-    }
-    //Retour nouvelle chaine
-    return res;
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "str.h"
+
+char** str_split(char* str, const char delim, int* length) {
+    char** res;
+    char* signet, * tmp = str;
+    char last = 0;
+    int compteur = 0, nb = 1, taille, count = 0;
+    //Compte le nombre d'element
+    while (*tmp) {
+        if (*tmp == delim && last != delim) {
+            nb++;
+        }
+        last = *tmp;
+        tmp++;
+    }
+    //Creation du tableau
+    res = malloc(sizeof (char*) * (nb + 1));
+    //Decoupage
+    tmp = str;
+    while (*tmp) {
+        //Si c'est le dernier mot
+        if (compteur == nb - 1) {
+            //Ajoute tous ce qui reste
+            res[compteur] = malloc(sizeof (char) * (strlen(str) - count));
+            int i = 0;
+            while(*tmp){
+                res[compteur][i++] = *tmp;
+                tmp++;
+            }
+        } else {
+            //Recup la taille du mot
+            signet = tmp;
+            taille = 0;
+            while (*tmp != delim) {
+                taille++;
+                tmp++;
+                count++;
+            }
+            //Creation du mot
+            res[compteur] = malloc(sizeof (char) * taille);
+            //Ajout du mot
+            for (int i = 0; i < taille; i++) {
+                res[compteur][i] = *signet;
+                signet++;
+            }
+            compteur++;
+            //Passe les delimiteurs consecutif
+            while (*tmp == delim) {
+                tmp++;
+                count++;
+            }
+        }
+    }
+    //Ajoute NULL à la fin
+    res[nb + 1] = NULL;
+    //Retour nombre de mot et tableau
+    *length = nb;
+    return res;
+}
+
+char* trim(char* str){
+    return ltrim(rtrim(str, ' '), ' ');
+}
+
+char* mtrim(char* str, char mask){
+    return ltrim(rtrim(str, mask), mask);
+}
+
+char* ltrim(char* str, char mask){
+    //Variable
+    int cmpt = 0;
+    char* res;
+    //Compte le nombre d'espace
+    while(str[cmpt] == mask){
+        cmpt++;
+    }
+    //Si aucun espace au debut
+    if(cmpt == 0){
+        res = malloc(sizeof(char) * strlen(str));
+        strcpy(res, str);
+        return res;
+    }
+    //Sinon creation nouvelle chaine
+    res = malloc(sizeof(char) * (strlen(str) - cmpt + 1));
+    for(int i = 0, j = cmpt; i < (strlen(str) - cmpt); i++, j++){
+        res[i] = str[j];
+    }
+    //Retour nouvelle chaine
+    return res;
+}
+
+char* rtrim(char* str, char mask){
+    //Variable
+    int cmpt = strlen(str) - 1;
+    char* res;
+    //Compte le nombre d'espace
+    while(str[cmpt] == mask){
+        cmpt--;
+    }
+    //Si aucun espace au debut
+    if(cmpt == strlen(str) - 1){
+        res = malloc(sizeof(char) * strlen(str));
+        strcpy(res, str);
+        return res;
+    }
+    cmpt++;
+    //Sinon creation nouvelle chaine
+    res = malloc(sizeof(char) * (cmpt + 2));
+    for(int i = 0; i < cmpt; i++){
+        res[i] = str[i];
+    }
+    //Retour nouvelle chaine
+    return res;
 }

+ 13 - 13
str.h

@@ -1,13 +1,13 @@
-#ifndef STR_H
-#define STR_H
-
-#include <string.h>
-
-char** str_split(char*, const char, int*);
-char* trim(char*);
-char* mtrim(char*, char);
-char* ltrim(char*, char);
-char* rtrim(char*, char);
-
-#endif /* STR_H */
-
+#ifndef STR_H
+#define STR_H
+
+#include <string.h>
+
+char** str_split(char*, const char, int*);
+char* trim(char*);
+char* mtrim(char*, char);
+char* ltrim(char*, char);
+char* rtrim(char*, char);
+
+#endif /* STR_H */
+