IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

erreur de segmentation


Sujet :

C

  1. #1
    Membre confirmé

    Profil pro
    Inscrit en
    Mars 2009
    Messages
    349
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Mars 2009
    Messages : 349
    Points : 590
    Points
    590
    Par défaut erreur de segmentation
    bonjour,
    voilà j'ai une erreur de segmentation pourtant il me semble que j'ai alloué correctement la mémoire qui lui est nécessaire.
    Je vous remercie de votre aide.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <string.h>
    #include <regex.h>
     
    void Usage(char const *executable);
    void adresse_variable(void const *ptr);
    void *fn_calloc (void *ptr,size_t const taille1, size_t const taille2);
    void *fn_realloc(void *ptr,size_t const taille);
    void Verif_Chemin(char *chemin);
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL);
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier);
    void Lire_Fichier(char const *URL);
    void Creer_Launcher_XML(char *fichier_src,char *exec, char *startupNotify, char *icon, char *comment);
    void Expression_Reguliere(FILE *file, char const *motif);
     
     
    int main(int argc, char *argv[])
    {
        char *chemin = NULL;
        char **tableau_de_fichier = NULL;
        char *URL = NULL;
        short i = 0;
     
        if ((argc == 1) || (argc > 2))
        {
            Usage(argv[0]);
        }
        else
        {
            chemin = fn_calloc(chemin,(strlen(argv[1]) + 1),sizeof(char));
            strcpy(chemin,argv[1]);
     
            Verif_Chemin(chemin);
     
            tableau_de_fichier = Lire_Repertoire(chemin,tableau_de_fichier);
     
            for (i = 0 ;tableau_de_fichier[i] != NULL ; i++)
            {
                printf("Valeur de i:%d\n\n",i);
     
                printf("Soumission du fichier:\t%s\n\n", tableau_de_fichier[i]);
     
                URL = Concatenation_URL(chemin,tableau_de_fichier[i],URL);
     
                printf("Soumission de l\'URL:\t%s\n\n", URL);
     
                Lire_Fichier(URL);
                free(URL);
            }
     
            free(chemin);
            free(tableau_de_fichier);
     
        }
        exit(EXIT_SUCCESS);
    }
     
    void Usage(char const *executable)
    {
        printf("Usage: %s /Path/to/desktop/directory\n", executable);
        exit(EXIT_FAILURE);
    }
     
    void adresse_variable(void const *ptr)
    {
        printf("L'adresse de la variable est : %p\n", ptr);
    }
     
    void *fn_calloc (void *ptr,size_t const nb_element, size_t const taille)
    {
     
        ptr = calloc(nb_element, taille);
        if (ptr == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);
            exit(EXIT_FAILURE);
        }
     
        return ptr;
    }
     
    void *fn_realloc(void *ptr,size_t const taille)
    {
        char *tmp1 = NULL;
        tmp1 = realloc(tmp1, (taille + 1) * sizeof(char));
     
        if (tmp1 == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);				                /** realloc a échoué : on sort de la boucle                   */
            exit(EXIT_FAILURE);
        }
        else
        {
            ptr = tmp1;
        }
        return ptr;
    }
     
    void Verif_Chemin(char *chemin)
    {
        char *tmp = NULL;
        size_t Longueur_Chemin = strlen(chemin);
     
        if (strcmp(&chemin[Longueur_Chemin-1],"/") != 0)
        {
            tmp = chemin;                                                                                                           /** Allocation temporaire du contenu du tableau */
            chemin = fn_realloc(chemin, (strlen("/")+ strlen(chemin)) );                                                            /** Agrandissement du tableau */
            chemin = tmp;                                                                                                           /** Réatribution du contenu du tableau */
            strcat(chemin,"/");									                                                                    /** Concaténation de 2 chaines                                                 */
        }
    }
     
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL)
    {
        size_t taille = 0;
     
    	taille = strlen(chemin) + strlen(nom_fichier) + 1;
     
    	URL = fn_calloc(URL, taille, sizeof(char));
     
        printf("Fonction Concatenation_URL:\t%ld\t%ld\t%ld\nLe chemin:\t%s\tNom du fichier: %s\n",strlen(chemin),strlen(nom_fichier),taille,chemin,nom_fichier);
     
    	strcpy(URL,chemin);
    	strcat(URL,nom_fichier);
     
    	printf("URL:\t\t%s\n\n",URL);
    	return URL;
    }
     
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier)
    {
        char *nom_fichier = NULL;
        short i = 0;
     
        DIR * rep = opendir(chemin);							                                                                    /** Ouverture du répertoire passez en argument                 */
     
        if (rep != NULL)										                                                                    /** Vérifie l'ouverture du répertoire                                 */
        {
            struct dirent * file;
     
            tableau_de_fichier = (char **)calloc(1,sizeof(char*));
            tableau_de_fichier[i] = fn_calloc(tableau_de_fichier[i],1,sizeof(char));
     
            printf("Liste des fichiers .destop:\n");
            while ((file = readdir(rep)) != NULL)
            {
                nom_fichier = file->d_name;
                if (  strstr(nom_fichier , ".desktop" )  )		                                                                    /** Le fichier porte l'extension desktop                                */
                {
                    printf("\t%d: %s\n",i, nom_fichier);
                    tableau_de_fichier = (char **)realloc(tableau_de_fichier,(i+1) * sizeof(char *));
                    tableau_de_fichier[i] = fn_calloc(tableau_de_fichier[i], sizeof(strlen(nom_fichier) + 1), sizeof(char) );                               /** Agrandissement du tableau */
                    tableau_de_fichier[i] = nom_fichier;
                    /* strcpy(tableau_de_fichier[i],nom_fichier); */
                    i++;
                }
     
            }
        }
        else
        {
            perror("le chemin :spécifié est introuvable !\n");
            exit(EXIT_FAILURE);
        }
     
        closedir(rep);										                                                                    /** Fermeture du répertoire passez en argument                 */
     
        return tableau_de_fichier;
    }
     
    void Lire_Fichier(char const *URL)
    {
        char **motif = NULL;
        short i =0;
    	FILE * file = NULL;
     
    	file = fopen(URL,"r");
                                                                                                                                    /** Ouverture du fichier .desktop en lecture                    */
    	if (file != NULL)
        {
            printf("\nLe fichier %s est ouvert\n",URL);
     
            motif = (char **)calloc(4,sizeof(char*));
     
            motif[0] = fn_calloc(motif[0], sizeof(strlen("Exec=") + 1), sizeof(char) );
            strcpy(motif[0],"Exec=");
     
            motif[1] = fn_calloc(motif[1], sizeof(strlen("StartupNotify=") + 1), sizeof(char) );
            strcpy(motif[1],"StartupNotify=");
     
            motif[2] = fn_calloc(motif[2], sizeof(strlen("Icon=") + 1), sizeof(char) );
            strcpy(motif[2],"Icon=");
     
            motif[3] = fn_calloc(motif[3], sizeof(strlen("Comment=") + 1), sizeof(char) );
            strcpy(motif[3],"Comment=");
     
            for(i = 0; i < 4; i++)
            {
                printf("%s\n",motif[i]);
                Expression_Reguliere(file, motif[i]);
            }
     
            fclose(file);
        }
        else
        {
            fprintf(stderr,"Impossible d'ouvrir le fichier :%s \n",URL);                                                  /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
    }
     
    void Creer_Launcher_XML(char *fichier_src,char *exec, char *startupNotify, char *icon, char *comment)
    {
        FILE* file = NULL;
        file = fopen("test.txt", "w+");							                                                                    /** Ouverture du fichier xml en écriture (vidé de son contenu)                */
     
    	if (file != NULL)
        {
            fclose(file);
        }
        else
        {
            perror("Impossible d'ouvrir le fichier!\n");		                                                                    /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
        (void) fichier_src;
        (void) exec;
        (void) startupNotify;
        (void) icon;
        (void) comment;
    }
     
    void Expression_Reguliere(FILE *file, char const *motif)
    {
        short num_ligne = 0;
        int err = 0;
        int match = 0;
        char *text = NULL;
        size_t taille;
        char ligne[1024];
        regex_t m;
     
            err = regcomp(&m,motif,REG_EXTENDED);
            if (err == 0)
            {
                while((fgets(ligne,1024 ,file)) != NULL)
                {
                    match = regexec(&m,ligne,0,NULL,0);
     
                    if(match == 0)
                    {
                        fprintf(stdout,"Le motif: \'%s\' a été trouvé à la ligne %d\n",motif,num_ligne);
                    }
                    else if (match == REG_NOMATCH)
                    {
                        /*printf ("Le motif: \'%s\' n\'a pas été trouvé\n", motif);*/
                    }
                    else
                    {
                        taille = regerror (err, &m, NULL, 0);
                        text = malloc (sizeof (*text) * taille);
                        if (text)
                        {
                            regerror (err, &m, text, taille);
                            fprintf (stderr, "%s\n", text);
                            free (text);
                        }
                        else
                        {
                            fprintf (stderr, "Memoire insuffisante\n");
                            exit (EXIT_FAILURE);
                        }
                    }
                    num_ligne++;
                }
                regfree(&m);
            }
            rewind(file);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE /usr/share/applications/
    Liste des fichiers .destop:
    	0: liveinst.desktop
    	1: mozilla-firefox.desktop
    	2: setroubleshoot.desktop
    	3: gnome-session-properties.desktop
    	4: gnome-system-log.desktop
    	5: gnome-keyboard.desktop
    Erreur de segmentation
    l'erreur se situe dans la fonction Lire_Repertoire sur la ligne strcpy(tableau_de_fichier[i],nom_fichier);

  2. #2
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tableau_de_fichier[i] = fn_calloc(tableau_de_fichier[i], strlen(nom_fichier) + 1, sizeof(char) );                               /** Agrandissement du tableau */
    A noter qu'il n'y a aucune raison au premier argument de fn_calloc :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    void *fn_calloc (size_t const nb_element, size_t const taille)
    { 
        void * ptr = calloc(nb_element, taille);
        if (ptr == NULL).....
    et donc

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    tableau_de_fichier[i] = fn_calloc(strlen(nom_fichier) + 1, sizeof(char) );                               /** Agrandissement du tableau */
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  3. #3
    Membre confirmé

    Profil pro
    Inscrit en
    Mars 2009
    Messages
    349
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Mars 2009
    Messages : 349
    Points : 590
    Points
    590
    Par défaut
    je te remercie tu as totalement raison comme quoi j'ai besoin d'un regard externe.
    Cependant l'erreur persiste

    le code actuelle:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <string.h>
    #include <regex.h>
     
    void Usage(char const *executable);
    void adresse_variable(void const *ptr);
    void *fn_calloc (size_t const taille1, size_t const taille2);
    void *fn_realloc(void *ptr,size_t const taille);
    void Verif_Chemin(char *chemin);
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL);
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier);
    void Lire_Fichier(char const *URL);
    void Creer_Launcher_XML(char *fichier_src,char *exec, char *startupNotify, char *icon, char *comment);
    void Expression_Reguliere(FILE *file, char const *motif);
     
     
    int main(int argc, char *argv[])
    {
        char *chemin = NULL;
        char **tableau_de_fichier = NULL;
        char *URL = NULL;
        short i = 0;
     
        if ((argc == 1) || (argc > 2))
        {
            Usage(argv[0]);
        }
        else
        {
            chemin = fn_calloc((strlen(argv[1]) + 1),sizeof(char));
            strcpy(chemin,argv[1]);
     
            Verif_Chemin(chemin);
     
            tableau_de_fichier = Lire_Repertoire(chemin,tableau_de_fichier);
     
            for (i = 0 ;tableau_de_fichier[i] != NULL ; i++)
            {
                printf("Valeur de i:%d\n\n",i);
     
                printf("Soumission du fichier:\t%s\n\n", tableau_de_fichier[i]);
     
                URL = Concatenation_URL(chemin,tableau_de_fichier[i],URL);
     
                printf("Soumission de l\'URL:\t%s\n\n", URL);
     
                Lire_Fichier(URL);
                free(URL);
            }
     
            free(chemin);
            free(tableau_de_fichier);
     
        }
        exit(EXIT_SUCCESS);
    }
     
    void Usage(char const *executable)
    {
        printf("Usage: %s /Path/to/desktop/directory\n", executable);
        exit(EXIT_FAILURE);
    }
     
    void adresse_variable(void const *ptr)
    {
        printf("L'adresse de la variable est : %p\n", ptr);
    }
     
    void *fn_calloc (size_t const nb_element, size_t const taille)
    {
     
        void *ptr = calloc(nb_element, taille);
        if (ptr == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);
            exit(EXIT_FAILURE);
        }
     
        return ptr;
    }
     
    void *fn_realloc(void *ptr,size_t const taille)
    {
        char *tmp1 = NULL;
        tmp1 = realloc(tmp1, (taille + 1) * sizeof(char));
     
        if (tmp1 == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);				                /** realloc a échoué : on sort de la boucle                   */
            exit(EXIT_FAILURE);
        }
        else
        {
            ptr = tmp1;
        }
        return ptr;
    }
     
    void Verif_Chemin(char *chemin)
    {
        char *tmp = NULL;
        size_t Longueur_Chemin = strlen(chemin);
     
        if (strcmp(&chemin[Longueur_Chemin-1],"/") != 0)
        {
            tmp = chemin;                                                                                                           /** Allocation temporaire du contenu du tableau */
            chemin = fn_realloc(chemin, (strlen("/")+ strlen(chemin)) );                                                            /** Agrandissement du tableau */
            chemin = tmp;                                                                                                           /** Réatribution du contenu du tableau */
            strcat(chemin,"/");									                                                                    /** Concaténation de 2 chaines                                                 */
        }
    }
     
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL)
    {
        size_t taille = 0;
     
    	taille = strlen(chemin) + strlen(nom_fichier) + 1;
     
    	URL = fn_calloc(taille, sizeof(char));
     
        printf("Fonction Concatenation_URL:\t%ld\t%ld\t%ld\nLe chemin:\t%s\tNom du fichier: %s\n",strlen(chemin),strlen(nom_fichier),taille,chemin,nom_fichier);
     
    	strcpy(URL,chemin);
    	strcat(URL,nom_fichier);
     
    	printf("URL:\t\t%s\n\n",URL);
    	return URL;
    }
     
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier)
    {
        char *nom_fichier = NULL;
        short i = 0;
     
        DIR * rep = opendir(chemin);							                                                                    /** Ouverture du répertoire passez en argument                 */
     
        if (rep != NULL)										                                                                    /** Vérifie l'ouverture du répertoire                                 */
        {
            struct dirent * file;
     
            tableau_de_fichier = (char **)calloc(1,sizeof(char*));
            tableau_de_fichier[i] = fn_calloc(1,sizeof(char));
     
            printf("Liste des fichiers .destop:\n");
            while ((file = readdir(rep)) != NULL)
            {
                nom_fichier = file->d_name;
                if (  strstr(nom_fichier , ".desktop" )  )		                                                                    /** Le fichier porte l'extension desktop                                */
                {
                    printf("\t%d: %s\n",i, nom_fichier);
                    tableau_de_fichier = (char **)realloc(tableau_de_fichier,(i+1) * sizeof(char *));
                    tableau_de_fichier[i] = fn_calloc(sizeof(strlen(nom_fichier) + 1), sizeof(char) );                               /** Agrandissement du tableau */
                    /*tableau_de_fichier[i] = nom_fichier;*/
     
                    strcpy(tableau_de_fichier[i],nom_fichier);
                    i++;
                }
     
            }
        }
        else
        {
            perror("le chemin :spécifié est introuvable !\n");
            exit(EXIT_FAILURE);
        }
     
        closedir(rep);										                                                                    /** Fermeture du répertoire passez en argument                 */
     
        return tableau_de_fichier;
    }
     
    void Lire_Fichier(char const *URL)
    {
        char **motif = NULL;
        short i =0;
    	FILE * file = NULL;
     
    	file = fopen(URL,"r");
                                                                                                                                    /** Ouverture du fichier .desktop en lecture                    */
    	if (file != NULL)
        {
            printf("\nLe fichier %s est ouvert\n",URL);
     
            motif = (char **)calloc(4,sizeof(char*));
     
            motif[0] = fn_calloc(sizeof(strlen("Exec=") + 1), sizeof(char) );
            strcpy(motif[0],"Exec=");
     
            motif[1] = fn_calloc(sizeof(strlen("StartupNotify=") + 1), sizeof(char) );
            strcpy(motif[1],"StartupNotify=");
     
            motif[2] = fn_calloc(sizeof(strlen("Icon=") + 1), sizeof(char) );
            strcpy(motif[2],"Icon=");
     
            motif[3] = fn_calloc(sizeof(strlen("Comment=") + 1), sizeof(char) );
            strcpy(motif[3],"Comment=");
     
            for(i = 0; i < 4; i++)
            {
                printf("%s\n",motif[i]);
                Expression_Reguliere(file, motif[i]);
            }
     
            fclose(file);
        }
        else
        {
            fprintf(stderr,"Impossible d'ouvrir le fichier :%s \n",URL);                                                  /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
    }
     
    void Creer_Launcher_XML(char *fichier_src,char *exec, char *startupNotify, char *icon, char *comment)
    {
        FILE* file = NULL;
        file = fopen("test.txt", "w+");							                                                                    /** Ouverture du fichier xml en écriture (vidé de son contenu)                */
     
    	if (file != NULL)
        {
            fclose(file);
        }
        else
        {
            perror("Impossible d'ouvrir le fichier!\n");		                                                                    /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
        (void) fichier_src;
        (void) exec;
        (void) startupNotify;
        (void) icon;
        (void) comment;
    }
     
    void Expression_Reguliere(FILE *file, char const *motif)
    {
        short num_ligne = 0;
        int err = 0;
        int match = 0;
        char *text = NULL;
        size_t taille;
        char ligne[1024];
        regex_t m;
     
            err = regcomp(&m,motif,REG_EXTENDED);
            if (err == 0)
            {
                while((fgets(ligne,1024 ,file)) != NULL)
                {
                    match = regexec(&m,ligne,0,NULL,0);
     
                    if(match == 0)
                    {
                        fprintf(stdout,"Le motif: \'%s\' a été trouvé à la ligne %d\n",motif,num_ligne);
                    }
                    else if (match == REG_NOMATCH)
                    {
                        /*printf ("Le motif: \'%s\' n\'a pas été trouvé\n", motif);*/
                    }
                    else
                    {
                        taille = regerror (err, &m, NULL, 0);
                        text = malloc (sizeof (*text) * taille);
                        if (text)
                        {
                            regerror (err, &m, text, taille);
                            fprintf (stderr, "%s\n", text);
                            free (text);
                        }
                        else
                        {
                            fprintf (stderr, "Memoire insuffisante\n");
                            exit (EXIT_FAILURE);
                        }
                    }
                    num_ligne++;
                }
                regfree(&m);
            }
            rewind(file);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE /usr/share/applications/
    Liste des fichiers .destop:
    	0: system-config-nfs.desktop
    	1: liveinst.desktop
    	2: mozilla-firefox.desktop
    	3: setroubleshoot.desktop
    	4: gnome-session-properties.desktop
    	5: gnome-system-log.desktop
    Erreur de segmentation

  4. #4
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier)
    {
    ....
        short i = 0;
    ....
            tableau_de_fichier = (char **)calloc(1,sizeof(char*));
    ....
                    tableau_de_fichier = (char **)realloc(tableau_de_fichier,(i+1) * sizeof(char *));
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier)
    {
    ....
        short i = 1;
    ....
            tableau_de_fichier = (char **)calloc(1,sizeof(char*));
    ....
                    tableau_de_fichier = (char **)realloc(tableau_de_fichier,(i+1) * sizeof(char *));
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  5. #5
    Membre confirmé

    Profil pro
    Inscrit en
    Mars 2009
    Messages
    349
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Mars 2009
    Messages : 349
    Points : 590
    Points
    590
    Par défaut
    je te remercie de ton aide.
    j'ai mis la valeur de i à 1
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
     workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE /usr/share/applications
    Liste des fichiers .destop:
    	1: gpk-prefs.desktop
    	2: miro.desktop
    	3: fedora-glade-3.desktop
    	4: fedora-google-reader.desktop
    	5: pidgin.desktop
    	6: nautilus-cd-burner-open-iso.desktop
    	7: livna-config-display.desktop
    	8: fedora-gnash.desktop
    	9: gnome-gtali.desktop
    	10: redhat-system-config-network.desktop
    	11: gnome-keybinding.desktop
    	12: fedora-screenruler.desktop
    *** glibc detected *** workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE: realloc(): invalid next size: 0x0000000001208310 ***
    ======= Backtrace: =========
    /lib64/libc.so.6[0x307b277ec8]
    /lib64/libc.so.6[0x307b27c131]
    /lib64/libc.so.6(realloc+0x12e)[0x307b27cfbe]
    workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE[0x4011d9]
    workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE[0x4014b8]
    /lib64/libc.so.6(__libc_start_main+0xe6)[0x307b21e576]
    workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE[0x400cc9]
    ======= Memory map: ========
    Je suis maudit

  6. #6
    Membre confirmé

    Profil pro
    Inscrit en
    Mars 2009
    Messages
    349
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Mars 2009
    Messages : 349
    Points : 590
    Points
    590
    Par défaut
    le source:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <string.h>
    #include <regex.h>
     
    void Usage(char const *executable);
    void adresse_variable(void const *ptr);
    void *fn_calloc (size_t const taille1, size_t const taille2);
    void *fn_realloc(void *ptr,size_t const taille);
    void Verif_Chemin(char *chemin);
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL);
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier);
    void Lire_Fichier(char const *URL);
    void Creer_Launcher_XML(char *fichier_src,char *exec, char *startupNotify, char *icon, char *comment);
    void Expression_Reguliere(FILE *file, char const *motif);
     
     
    int main(int argc, char *argv[])
    {
        char *chemin = NULL;
        char **tableau_de_fichier = NULL;
        char *URL = NULL;
        short i = 0;
     
        if ((argc == 1) || (argc > 2))
        {
            Usage(argv[0]);
        }
        else
        {
            chemin = fn_calloc((strlen(argv[1]) + 1),sizeof(char));
            strcpy(chemin,argv[1]);
     
            Verif_Chemin(chemin);
     
            tableau_de_fichier = Lire_Repertoire(chemin,tableau_de_fichier);
     
            for (i = 0 ;tableau_de_fichier[i] != NULL ; i++)
            {
                printf("Valeur de i:%d\n\n",i);
     
                printf("Soumission du fichier:\t%s\n\n", tableau_de_fichier[i]);
     
                URL = Concatenation_URL(chemin,tableau_de_fichier[i],URL);
     
                printf("Soumission de l\'URL:\t%s\n\n", URL);
     
                Lire_Fichier(URL);
                free(URL);
            }
     
            free(chemin);
            free(tableau_de_fichier);
     
        }
        exit(EXIT_SUCCESS);
    }
     
    void Usage(char const *executable)
    {
        printf("Usage: %s /Path/to/desktop/directory\n", executable);
        exit(EXIT_FAILURE);
    }
     
    void adresse_variable(void const *ptr)
    {
        printf("L'adresse de la variable est : %p\n", ptr);
    }
     
    void *fn_calloc (size_t const nb_element, size_t const taille)
    {
     
        void *ptr = calloc(nb_element, taille);
        if (ptr == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);
            exit(EXIT_FAILURE);
        }
     
        return ptr;
    }
     
    void *fn_realloc(void *ptr,size_t const taille)
    {
        char *tmp1 = NULL;
        tmp1 = realloc(tmp1, (taille + 1) * sizeof(char));
     
        if (tmp1 == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);				                /** realloc a échoué : on sort de la boucle                   */
            exit(EXIT_FAILURE);
        }
        else
        {
            ptr = tmp1;
        }
        return ptr;
    }
     
    void Verif_Chemin(char *chemin)
    {
        char *tmp = NULL;
        size_t Longueur_Chemin = strlen(chemin);
     
        if (strcmp(&chemin[Longueur_Chemin-1],"/") != 0)
        {
            tmp = chemin;                                                                                                           /** Allocation temporaire du contenu du tableau */
            chemin = fn_realloc(chemin, (strlen("/")+ strlen(chemin)) );                                                            /** Agrandissement du tableau */
            chemin = tmp;                                                                                                           /** Réatribution du contenu du tableau */
            strcat(chemin,"/");									                                                                    /** Concaténation de 2 chaines                                                 */
        }
    }
     
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL)
    {
        size_t taille = 0;
     
    	taille = strlen(chemin) + strlen(nom_fichier) + 1;
     
    	URL = fn_calloc(taille, sizeof(char));
     
        printf("Fonction Concatenation_URL:\t%ld\t%ld\t%ld\nLe chemin:\t%s\tNom du fichier: %s\n",strlen(chemin),strlen(nom_fichier),taille,chemin,nom_fichier);
     
    	strcpy(URL,chemin);
    	strcat(URL,nom_fichier);
     
    	printf("URL:\t\t%s\n\n",URL);
    	return URL;
    }
     
    char **Lire_Repertoire (char const *chemin,char **tableau_de_fichier)
    {
        short i = 1;
        size_t taille_chaine = 0;
     
        DIR * rep = opendir(chemin);							                                                                    /** Ouverture du répertoire passez en argument                 */
     
        if (rep != NULL)										                                                                    /** Vérifie l'ouverture du répertoire                                 */
        {
            struct dirent * file;
     
            tableau_de_fichier = (char **)calloc(1,sizeof(char*));
            tableau_de_fichier[i-1] = fn_calloc(1,sizeof(char));
     
            printf("Liste des fichiers .destop:\n");
            while ((file = readdir(rep)) != NULL)
            {
                if (  strstr(file->d_name , ".desktop" )  )		                                                                    /** Le fichier porte l'extension desktop                                */
                {
                    taille_chaine = sizeof(strlen(file->d_name) + 1);
     
                    tableau_de_fichier = (char **)realloc(tableau_de_fichier,i * sizeof(char *) );                                  /** Agrandissement du tableau */
                    tableau_de_fichier[i-1] = fn_calloc(taille_chaine, sizeof(char) );                                              /** Ajustement du tableau à la chaine ajouté */
     
                    printf("\tFichier: %d: %s\tTaille chaine: %ld\tsizeof chaine %ld \tsieof tableau: %ld\n",i, file->d_name,(strlen(file->d_name) + 1),taille_chaine,sizeof(tableau_de_fichier[i-1]));
     
                    strcpy(tableau_de_fichier[i-1],file->d_name);
                    i++;
                }
     
            }
        }
        else
        {
            perror("le chemin :spécifié est introuvable !\n");
            exit(EXIT_FAILURE);
        }
     
        closedir(rep);										                                                                    /** Fermeture du répertoire passez en argument                 */
     
        return tableau_de_fichier;
    }
     
    void Lire_Fichier(char const *URL)
    {
        char **motif = NULL;
        short i =0;
    	FILE * file = NULL;
     
    	file = fopen(URL,"r");
                                                                                                                                    /** Ouverture du fichier .desktop en lecture                    */
    	if (file != NULL)
        {
            printf("\nLe fichier %s est ouvert\n",URL);
     
            motif = (char **)calloc(4,sizeof(char*));
     
            motif[0] = fn_calloc(sizeof(strlen("Exec=") + 1), sizeof(char) );
            strcpy(motif[0],"Exec=");
     
            motif[1] = fn_calloc(sizeof(strlen("StartupNotify=") + 1), sizeof(char) );
            strcpy(motif[1],"StartupNotify=");
     
            motif[2] = fn_calloc(sizeof(strlen("Icon=") + 1), sizeof(char) );
            strcpy(motif[2],"Icon=");
     
            motif[3] = fn_calloc(sizeof(strlen("Comment=") + 1), sizeof(char) );
            strcpy(motif[3],"Comment=");
     
            for(i = 0; i < 4; i++)
            {
                printf("%s\n",motif[i]);
                Expression_Reguliere(file, motif[i]);
            }
     
            fclose(file);
        }
        else
        {
            fprintf(stderr,"Impossible d'ouvrir le fichier :%s \n",URL);                                                  /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
    }
     
    void Creer_Launcher_XML(char *fichier_src,char *exec, char *startupNotify, char *icon, char *comment)
    {
        FILE* file = NULL;
        file = fopen("test.txt", "w+");							                                                                    /** Ouverture du fichier xml en écriture (vidé de son contenu)                */
     
    	if (file != NULL)
        {
            fclose(file);
        }
        else
        {
            perror("Impossible d'ouvrir le fichier!\n");		                                                                    /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
        (void) fichier_src;
        (void) exec;
        (void) startupNotify;
        (void) icon;
        (void) comment;
    }
     
    void Expression_Reguliere(FILE *file, char const *motif)
    {
        short num_ligne = 0;
        int err = 0;
        int match = 0;
        char *text = NULL;
        size_t taille;
        char ligne[1024];
        regex_t m;
     
            err = regcomp(&m,motif,REG_EXTENDED);
            if (err == 0)
            {
                while((fgets(ligne,1024 ,file)) != NULL)
                {
                    match = regexec(&m,ligne,0,NULL,0);
     
                    if(match == 0)
                    {
                        fprintf(stdout,"Le motif: \'%s\' a été trouvé à la ligne %d\n",motif,num_ligne);
                    }
                    else if (match == REG_NOMATCH)
                    {
                        /*printf ("Le motif: \'%s\' n\'a pas été trouvé\n", motif);*/
                    }
                    else
                    {
                        taille = regerror (err, &m, NULL, 0);
                        text = malloc (sizeof (*text) * taille);
                        if (text)
                        {
                            regerror (err, &m, text, taille);
                            fprintf (stderr, "%s\n", text);
                            free (text);
                        }
                        else
                        {
                            fprintf (stderr, "Memoire insuffisante\n");
                            exit (EXIT_FAILURE);
                        }
                    }
                    num_ligne++;
                }
                regfree(&m);
            }
            rewind(file);
    }
    et avec insight (gdb):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    run /usr/share/applications
    Starting program: /home/Jonathan/workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE /usr/share/applications
     
    Breakpoint 1, main (argc=2, argv=0x7fffb6b8a3d8) at /home/Jonathan/workspace/Create_LaunchBar_XFCE/main.c:21
    Lire_Repertoire (chemin=0x214c010 "/usr/share/applications/", tableau_de_fichier=0x0) at /home/Jonathan/workspace/Create_LaunchBar_XFCE/main.c:134
    fn_calloc (nb_element=8, taille=1) at /home/Jonathan/workspace/Create_LaunchBar_XFCE/main.c:74
    Lire_Repertoire (chemin=0x214c010 "/usr/share/applications/", tableau_de_fichier=0x214d090) at /home/Jonathan/workspace/Create_LaunchBar_XFCE/main.c:156
     
    Program received signal SIGSEGV, Segmentation fault.
    0x000000315167c098 in _int_realloc () from /lib64/libc.so.6
    En console:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    $ workspace/Create_LaunchBar_XFCE/bin/Debug/Create_LaunchBar_XFCE /usr/share/applications/
    Liste des fichiers .destop:
    	Fichier: 1: system-config-nfs.desktop	Taille chaine: 26	sizeof chaine 8 	sieof tableau: 8
    	Fichier: 2: liveinst.desktop	Taille chaine: 17	sizeof chaine 8 	sieof tableau: 8
    	Fichier: 3: mozilla-firefox.desktop	Taille chaine: 24	sizeof chaine 8 	sieof tableau: 8
    	Fichier: 4: setroubleshoot.desktop	Taille chaine: 23	sizeof chaine 8 	sieof tableau: 8
    	Fichier: 5: gnome-session-properties.desktop	Taille chaine: 33	sizeof chaine 8 	sieof tableau: 8
    Erreur de segmentation

  7. #7
    Expert éminent sénior
    Avatar de diogene
    Homme Profil pro
    Enseignant Chercheur
    Inscrit en
    Juin 2005
    Messages
    5 761
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 761
    Points : 13 926
    Points
    13 926
    Par défaut
    La grosse erreur est ici :
    Cette fonction ne fait pas une réallocation, mais une allocation
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    void *fn_realloc(void *ptr,size_t const taille)
    {
        char *tmp1 = NULL;
        tmp1 = realloc(tmp1, (taille + 1) * sizeof(char));
    ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    void *fn_realloc(void *ptr,size_t const taille)
    {
        char *tmp1 = realloc(ptr, (taille + 1) * sizeof(char));
    ...
    Erreur de taille d'allocation
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    void Verif_Chemin(char *chemin)
    {
    ....
            chemin = fn_realloc(chemin, (strlen("/")+ strlen(chemin)+1 )        /** Agrandissement du tableau */
            strcat(chemin,"/");			/** Concaténation de 2 chaines */
    et d'après la fonction fn_realloc (qui arrête le programme en cas d'erreur), il suffit de
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    void Verif_Chemin(char *chemin)
    {
        size_t Longueur_Chemin = strlen(chemin); 
        if (strcmp(chemin + Longueur_Chemin-1,"/") != 0)
        {
            chemin = fn_realloc(chemin, (strlen("/")+ Longueur_Chemin+1)        /** Agrandissement du tableau */
            strcat(chemin,"/");			/** Concaténation de 2 chaines */
        }
    }
    tableau_de_fichier n'a pas à être un paramètre de la fonction Lire_Repertoire, puisque sa valeur initiale n'est pas utilisée
    En fait, il me semble que tu compliques le code en faisant tes allocations "par anticipation", avant d'en avoir besoin.

    Je verrai quelque chose du genre :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    char **Lire_Repertoire (char const *chemin)
    {
        char **tableau_de_fichier = NULL ;
        DIR * rep = opendir(chemin);                  /** Ouverture du répertoire passé en argument */ 
        if (rep != NULL)	                          /** Vérifie l'ouverture du répertoire         */
        {
            struct dirent * file;
            size_t i = 0 ;
            tableau_de_fichier = fn_calloc(1,sizeof(char *));   /* Pour marquer la fin de la liste */
            tableau_de_fichier[0] = NULL ;                      /* <-----marque la fin de la liste */ 
            printf("Liste des fichiers .destop:\n");
            while ((file = readdir(rep)) != NULL)
            {
                char * nom_fichier = file->d_name;
                if (  strstr(nom_fichier , ".desktop" )!= NULL  )	/** Le fichier porte l'extension desktop */
                {
                    printf("\t%d: %s\n",i, nom_fichier);
                    tableau_de_fichier[i] = fn_calloc(strlen(nom_fichier) + 1, sizeof(char) );  /* remplace le NULL */
                    strcpy(tableau_de_fichier[i],nom_fichier);
                    i++;
                    tableau_de_fichier = realloc(tableau_de_fichier,(i+1) * sizeof(char *)); /* Pour marquer la fin de la liste */
                    tableau_de_fichier[i] = NULL;                                            /* <-----marque la fin de la liste */ 
                }
            }
            closedir(rep);   /** Fermeture du répertoire passez en argument */ 
        }
        else
        {
            perror("le chemin :spécifié est introuvable !\n");
            exit(EXIT_FAILURE);
        }
        return tableau_de_fichier;
    }
    Publication : Concepts en C

    Mon avatar : Glenn Gould

    --------------------------------------------------------------------------
    Une réponse vous a été utile ? Remerciez son auteur en cliquant le pouce vert !

  8. #8
    Membre confirmé

    Profil pro
    Inscrit en
    Mars 2009
    Messages
    349
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Mars 2009
    Messages : 349
    Points : 590
    Points
    590
    Par défaut
    Mille merci, je me suis embrouillé et plus que je travaillé dessus moins je voyais l'erreur.
    je te remercie infiniment pour ton aide, je vais retavailler un peu tout ça pour bien comprendre mes erreurs.
    je met à disposition le source si ça peut aider.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    #include <stdio.h>
    #include <stdlib.h>
    #include <dirent.h>
    #include <string.h>
    #include <regex.h>
     
    void Usage(char const *executable);
    void adresse_variable(void const *ptr);
    void *fn_calloc (size_t const taille1, size_t const taille2);
    char *fn_realloc(char *ptr,size_t const taille);
    char **fn_realloc2(char **ptr,size_t const taille);
    void Verif_Chemin(char *chemin);
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL);
    char **Lire_Repertoire (char const *chemin);
    char **Lire_Fichier(char const *URL);
    void Creer_Launcher_XML(char **contenu, char const *fichier_dest);
    char *Creer_Nom_Fichier(char const *nom_fichier_desktop);
    char *Expression_Reguliere(char const *motif, char const *chaine);
    char *Expression_Reguliere_Fichier(FILE *file, char const *motif);
     
     
    int main(int argc, char *argv[])
    {
        char *chemin = NULL;
        char **tableau_de_fichier = NULL;
        char *URL = NULL;
        short i = 0;
        char **resultat = NULL;
        char *nom_fichier = NULL;
     
        if ((argc == 1) || (argc > 2))
        {
            Usage(argv[0]);
        }
        else
        {
            chemin = fn_calloc((strlen(argv[1]) + 1),sizeof(char));
            strcpy(chemin,argv[1]);
     
            Verif_Chemin(chemin);
     
            tableau_de_fichier = Lire_Repertoire(chemin);
     
            for (i = 0 ;tableau_de_fichier[i] != NULL ; i++)
            {
                printf("\n--------------------------------------------------------------------------------------------------------\n");
                printf("Fichier n°: %d\n\n",i);
     
                printf("Soumission du fichier:\t%s\n\n", tableau_de_fichier[i]);
     
                URL = Concatenation_URL(chemin,tableau_de_fichier[i],URL);
     
                printf("Soumission de l\'URL:\t%s\n\n", URL);
     
                resultat = Lire_Fichier(URL);
     
                nom_fichier = Creer_Nom_Fichier(tableau_de_fichier[i]);
                printf("nom du fichier: %s\n",nom_fichier);
     
                if (nom_fichier != '\0')
                {
                    Creer_Launcher_XML(resultat,nom_fichier);
                }
                else
                {
                    fprintf(stderr,"La fonction Creer_Nom_Fichier à rencontrer probablement un fichier non gérer: %s",tableau_de_fichier[i]);
                }
     
                free(URL);
            }
     
            free(chemin);
            free(tableau_de_fichier);
     
        }
        exit(EXIT_SUCCESS);
    }
     
    void Usage(char const *executable)
    {
        printf("Usage: %s /Path/to/desktop/directory\n", executable);
        exit(EXIT_FAILURE);
    }
     
    void adresse_variable(void const *ptr)
    {
        printf("L'adresse de la variable est : %p\n", ptr);
    }
     
    void *fn_calloc (size_t const nb_element, size_t const taille)
    {
     
        void *ptr = calloc(nb_element, taille);
        if (ptr == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);
            exit(EXIT_FAILURE);
        }
     
        return ptr;
    }
     
    char *fn_realloc(char *ptr,size_t const taille)
    {
        char *tmp1 = NULL;
        tmp1 = realloc(ptr, (taille + 1) * sizeof(char));
     
        if (tmp1 == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);				                /** realloc a échoué : on sort de la boucle                   */
            exit(EXIT_FAILURE);
        }
        else
        {
            ptr = tmp1;
        }
        return ptr;
    }
     
    char **fn_realloc2(char **ptr,size_t const taille)
    {
        char **tmp1 = NULL;
        tmp1 = realloc(ptr, (taille + 1) * sizeof(char *));
     
        if (tmp1 == NULL)
        {
            fprintf(stderr,"Allocation impossible dans le fichier :%s ligne : %d",__FILE__,__LINE__);				                /** realloc a échoué : on sort de la boucle                   */
            exit(EXIT_FAILURE);
        }
        else
        {
            ptr = tmp1;
        }
        return ptr;
    }
     
    void Verif_Chemin(char *chemin)
    {
        char *tmp = NULL;
        size_t Longueur_Chemin = strlen(chemin);
     
        if (strcmp(chemin + Longueur_Chemin-1,"/") != 0)
     
        {
            tmp = chemin;                                                                                                           /** Allocation temporaire du contenu du tableau */
            chemin = fn_realloc(chemin, (strlen("/")+ Longueur_Chemin+1));                                                           /** Agrandissement du tableau */
            strcat(chemin,"/");									                                                                    /** Concaténation de 2 chaines                                                 */
        }
    }
     
    void *Concatenation_URL(char const *chemin,char const *nom_fichier,char *URL)
    {
        size_t taille = 0;
     
    	taille = strlen(chemin) + strlen(nom_fichier) + 1;
     
    	URL = fn_calloc(taille, sizeof(char));
     
        printf("Fonction Concatenation_URL:\t%zu\t%zu\t%zu\nLe chemin:\t%s\tNom du fichier: %s\n",strlen(chemin),strlen(nom_fichier),taille,chemin,nom_fichier);
     
    	strcpy(URL,chemin);
    	strcat(URL,nom_fichier);
     
    	printf("URL:\t\t%s\n\n",URL);
    	return URL;
    }
     
    char **Lire_Repertoire (char const *chemin)
    {
        char **tableau_de_fichier = NULL ;
        DIR * rep = opendir(chemin);                                                            /** Ouverture du répertoire passé en argument */
        if (rep != NULL)	                                                                    /** Vérifie l'ouverture du répertoire         */
        {
            struct dirent * file;
            size_t i = 0 ;
     
            tableau_de_fichier = fn_calloc(1,sizeof(char *));                                   /** Pour marquer la fin de la liste */
            tableau_de_fichier[0] = NULL ;                                                      /** <-----marque la fin de la liste */
     
            printf("Liste des fichiers .destop:\n");
     
            while ((file = readdir(rep)) != NULL)
            {
                char * nom_fichier = file->d_name;
     
                if (  strstr(nom_fichier , ".desktop" )!= NULL  )                               /** Le fichier porte l'extension desktop */
                {
                    printf("\t%zu: %s\n",i, nom_fichier);
     
                    tableau_de_fichier[i] = fn_calloc(strlen(nom_fichier) + 1, sizeof(char) );  /** remplace le NULL */
     
                    strcpy(tableau_de_fichier[i],nom_fichier);
     
                    i++;
     
                    tableau_de_fichier = fn_realloc2(tableau_de_fichier,(i+1) * sizeof(char *));    /** Pour marquer la fin de la liste */
                    tableau_de_fichier[i] = NULL;                                               /** <-----marque la fin de la liste */
                }
            }
            closedir(rep);                                                                      /** Fermeture du répertoire passez en argument */
        }
        else
        {
            perror("le chemin :spécifié est introuvable !\n");
            exit(EXIT_FAILURE);
        }
        return tableau_de_fichier;
    }
     
    char **Lire_Fichier(char const *URL)
    {
        char **motif = NULL;
        short i =0;
    	FILE * file = NULL;
    	char **resultat = NULL;
     
    	file = fopen(URL,"r");
                                                                                                                                    /** Ouverture du fichier .desktop en lecture                    */
    	if (file != NULL)
        {
            printf("\nLe fichier %s est ouvert\n",URL);
     
            motif = (char **)calloc(4,sizeof(char*));
     
            motif[0] = "(Exec=.*[^\n])";
            motif[1] = "(StartupNotify=[.*[^\n])";
            motif[2] = "(Icon=.*[^\n])";
            motif[3] = "(Comment=.*[^\n])";
     
            resultat = (char **)calloc(4,sizeof(char*));
     
            for(i = 0; i < 4; i++)
            {
                resultat[i] = Expression_Reguliere_Fichier(file, motif[i]);
            }
     
            fclose(file);
        }
        else
        {
            fprintf(stderr,"Impossible d'ouvrir le fichier :%s \n",URL);                                                            /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
        return resultat;
     
    }
     
    void Creer_Launcher_XML(char **contenu, char const *fichier_dest)
    {
        short i     = 0;
        FILE* file  = NULL;
        file        = fopen(fichier_dest, "w+");							                                                        /** Ouverture du fichier xml en écriture (vidé de son contenu)                */
     
        printf("%s\n",fichier_dest);
     
    	if (file != NULL)
        {
            fprintf(file,"[Entry 0]\n");
            for(i = 0; i < 4; i++)
            {
                if (contenu[i] != NULL)
                {
                    fprintf(file,"%s\n",contenu[i]);
                }
            }
            fclose(file);
        }
        else
        {
            perror("Impossible d'ouvrir le fichier!\n");		                                                                    /** On affiche un message d'erreur                                              */
            exit(EXIT_FAILURE);
        }
     
    }
     
    char *Creer_Nom_Fichier(char const *nom_fichier_desktop)
    {
            char *nom_fichier   = NULL;
            char *motif         = NULL;
            char *sous_chaine   = NULL;
            size_t taille_chaine = 0;
     
            motif = "(.*[^desktop]|$)";
     
            sous_chaine = Expression_Reguliere(motif, nom_fichier_desktop);
     
            if (sous_chaine != '\0')
            {
                taille_chaine = strlen(".config/xfce4/panel/launcher-") + strlen(sous_chaine) + strlen("rc") + 1;
     
                nom_fichier = fn_calloc(taille_chaine , sizeof(char));
     
                strcpy(nom_fichier , ".config/xfce4/panel/launcher-");
                strcat(nom_fichier , sous_chaine);
                strcat(nom_fichier , "rc");
     
                return nom_fichier;
            }
            else
            {
                return '\0';
            }
    }
     
    char *Expression_Reguliere(char const *motif, char const *chaine)
    {
        int err             = 0;
        int match           = 0;
        char *text          = NULL;
        size_t taille;
        regex_t m;
        size_t nmatch       = 0;
        regmatch_t *pmatch  = NULL;
        char *site          = NULL;
     
     
            err = regcomp(&m,motif,REG_EXTENDED);
            if (err == 0)
            {
                nmatch = m.re_nsub;
                pmatch = fn_calloc (nmatch,sizeof (*pmatch));
     
                match = regexec(&m,chaine, nmatch, pmatch, 0);
     
                if(match == 0)
                {
                    int start = pmatch[0].rm_so;
                    int end = pmatch[0].rm_eo;
                    size_t size = end - start;
     
                    site = fn_calloc ((size + 1) , sizeof (*site));
     
                    strncpy (site, &chaine[start], size);
                    site[size] = '\0';
     
                    printf ("\x1b" "[0;31;47m\n" );                                                         /** rouge sur fond blanc */
                    fprintf(stdout,"La chaine: \'%s\' a été trouvé.\n",site);
                    printf ("\x1b" "[0;30;47m\n" );                                                         /** noir sur fond blanc */
     
                }
                else if (match == REG_NOMATCH)
                {
                    /*printf ("Le motif: \'%s\' n\'a pas été trouvé\n", motif);*/
                }
                else
                {
                    taille = regerror (err, &m, NULL, 0);
                    text = malloc (sizeof (*text) * taille);
                    if (text)
                    {
                        regerror (err, &m, text, taille);
                        fprintf (stderr, "%s\n", text);
                        free (text);
                    }
                    else
                    {
                        fprintf (stderr, "Memoire insuffisante\n");
                        exit (EXIT_FAILURE);
                    }
                }
     
                regfree(&m);
            }
     
            if (site)
            {
                return site;
            }
            else
            {
                return '\0';
            }
     
    }
     
     
    char *Expression_Reguliere_Fichier(FILE *file, char const *motif)
    {
        short num_ligne = 0;
        int err = 0;
        int match = 0;
        char *text = NULL;
        size_t taille;
        char ligne[1024];
        regex_t m;
        size_t nmatch = 0;
        regmatch_t *pmatch = NULL;
        char *site = NULL;
     
     
            err = regcomp(&m,motif,REG_EXTENDED);
            if (err == 0)
            {
     
                nmatch = m.re_nsub;
                pmatch = fn_calloc (nmatch,sizeof (*pmatch));
     
                while((fgets(ligne,1024 ,file)) != NULL)
                {
                    match = regexec(&m,ligne, nmatch, pmatch, 0);
     
                    if(match == 0)
                    {
                        int start = pmatch[0].rm_so;
                        int end = pmatch[0].rm_eo;
                        size_t size = end - start;
     
                        site = fn_calloc ((size + 1) , sizeof (*site));
     
                        strncpy (site, &ligne[start], size);
                        site[size] = '\0';
     
                        printf ("\x1b" "[0;31;47m\n" );                                                         /** rouge sur fond blanc */
                        fprintf(stdout,"La chaine: \'%s\' a été trouvé à la ligne : %d.\n",site,num_ligne);
                        printf ("\x1b" "[0;30;47m\n" );                                                         /** noir sur fond blanc */
     
                    }
                    else if (match == REG_NOMATCH)
                    {
                        /*printf ("Le motif: \'%s\' n\'a pas été trouvé\n", motif);*/
                    }
                    else
                    {
                        taille = regerror (err, &m, NULL, 0);
                        text = malloc (sizeof (*text) * taille);
                        if (text)
                        {
                            regerror (err, &m, text, taille);
                            fprintf (stderr, "%s\n", text);
                            free (text);
                        }
                        else
                        {
                            fprintf (stderr, "Memoire insuffisante\n");
                            exit (EXIT_FAILURE);
                        }
                    }
                    num_ligne++;
                }
     
                regfree(&m);
            }
     
            rewind(file);
     
            if (site)
            {
                return site;
            }
            else
            {
                return '\0';
            }
     
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Erreurs de segmentation !
    Par anti-conformiste dans le forum Applications et environnements graphiques
    Réponses: 16
    Dernier message: 18/10/2005, 11h11
  2. Erreur de segmentation
    Par Trunks dans le forum C
    Réponses: 3
    Dernier message: 06/10/2005, 18h28
  3. Erreur de segmentation (Inconnue)
    Par Dark-Meteor dans le forum C
    Réponses: 5
    Dernier message: 08/09/2005, 13h42
  4. [Dev-C++] Erreur de segmentation...
    Par sas dans le forum Dev-C++
    Réponses: 11
    Dernier message: 26/03/2005, 14h25
  5. erreur de segmentation
    Par transistor49 dans le forum C++
    Réponses: 10
    Dernier message: 15/03/2005, 11h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo