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 :

Corruption de mémoire


Sujet :

C

  1. #1
    Membre à l'essai
    Inscrit en
    Janvier 2011
    Messages
    20
    Détails du profil
    Informations forums :
    Inscription : Janvier 2011
    Messages : 20
    Points : 13
    Points
    13
    Par défaut Corruption de mémoire
    Bonjour,
    J'ai une fonction qui traite des fichiers. Lors de la première exécution de cette dernière, aucune problème. Si elle est lancée une seconde fois, alors voici l'erreur qui s'affiche avec erreur de segmentation :
    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
    *** glibc detected *** ./cw: malloc(): memory corruption: 0x00000000022d4de0 ***
    ======= Backtrace: =========
    /lib/x86_64-linux-gnu/libc.so.6[0x309b076d76]
    /lib/x86_64-linux-gnu/libc.so.6[0x309b079de3]
    /lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x70)[0x309b07bb90]
    ./cw[0x4017ee]
    ./cw[0x401db6]
    ./cw[0x401f85]
    ./cw[0x401f5a]
    ======= Memory map: ========
    00400000-00404000 r-xp 00000000 fe:00 22944647                           /home/xavier/
    00603000-00604000 rw-p 00003000 fe:00 22944647                           /home/xavier/
    022b9000-022eb000 rw-p 00000000 00:00 0                                  [heap]
    3099a00000-3099a20000 r-xp 00000000 08:01 5268                           /lib/x86_64-l
    3099c1f000-3099c20000 r--p 0001f000 08:01 5268                           /lib/x86_64-l
    3099c20000-3099c21000 rw-p 00020000 08:01 5268                           /lib/x86_64-l
    3099c21000-3099c22000 rw-p 00000000 00:00 0 
    309b000000-309b180000 r-xp 00000000 08:01 5269                           /lib/x86_64-l
    o
    309b180000-309b380000 ---p 00180000 08:01 5269                           /lib/x86_64-l
    o
    309b380000-309b384000 r--p 00180000 08:01 5269                           /lib/x86_64-l
    o
    309b384000-309b385000 rw-p 00184000 08:01 5269                           /lib/x86_64-l
    o
    309b385000-309b38a000 rw-p 00000000 00:00 0 
    30a1000000-30a1015000 r-xp 00000000 08:01 5246                           /lib/x86_64-l
    .1
    30a1015000-30a1215000 ---p 00015000 08:01 5246                           /lib/x86_64-l
    .1
    30a1215000-30a1216000 rw-p 00015000 08:01 5246                           /lib/x86_64-l
    .1
    7f1394000000-7f1394021000 rw-p 00000000 00:00 0 
    7f1394021000-7f1398000000 ---p 00000000 00:00 0 
    7f1399b30000-7f1399b33000 rw-p 00000000 00:00 0 
    7f1399b4e000-7f1399b51000 rw-p 00000000 00:00 0 
    7fff34297000-7fff342b8000 rw-p 00000000 00:00 0                          [stack]
    7fff343dc000-7fff343dd000 r-xp 00000000 00:00 0                          [vdso]
    ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
    test/test1/sousdossier/index.mdAbandon
    J'ai tenté de mieux comprendre ce qu'il se passe, mais je ne vois rien qui corresponde au fait qu'elle soit lancée une seconde fois.
    Voici la backtrace avec gdb :
    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
    #0  0x000000309b032475 in *__GI_raise (sig=<optimized out>) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    #1  0x000000309b0356f0 in *__GI_abort () at abort.c:92
    #2  0x000000309b06d52b in __libc_message (do_abort=<optimized out>, fmt=<optimized out>)
        at ../sysdeps/unix/sysv/linux/libc_fatal.c:189
    #3  0x000000309b076d76 in malloc_printerr (action=3, str=0x309b14bc14 "malloc(): memory corruption", 
        ptr=<optimized out>) at malloc.c:6283
    #4  0x000000309b079de3 in _int_malloc (av=0x309b385e60, bytes=<optimized out>) at malloc.c:4396
    #5  0x000000309b07bb90 in *__GI___libc_malloc (bytes=43) at malloc.c:3660
    #6  0x000000309b06c3b0 in _IO_vasprintf (result_ptr=0x7ffffffeb950, format=<optimized out>, 
        args=0x7ffffffeb958) at vasprintf.c:78
    #7  0x0000000000400edd in smprintf (fmt=0x40281b "%s \"%s\"") at cw.c:67
    #8  0x0000000000401940 in convert (sf=0x621190 "test/test1/sousdossier/index.md", 
        of=0x624900 "test.static/test1/sousdossier/index.html") at cw.c:245
    #9  0x0000000000401dd8 in processfile (inpath=0x621190 "test/test1/sousdossier/index.md", 
        outpath=0x624830 "test.static/test1/sousdossier/index.md") at cw.c:392
    #10 0x0000000000401f95 in process (sourcedir=0x623830 "test/test1/sousdossier", 
        outdir=0x624760 "test.static/test1/sousdossier", totouch=1) at cw.c:436
    #11 0x0000000000401f6a in process (sourcedir=0x620180 "test/test1", outdir=0x6236c0 "test.static/test1", 
        totouch=1) at cw.c:431
    #12 0x0000000000401f6a in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:431
    #13 0x0000000000401b7a in convert (sf=0x620030 "test/test1/sousdossier2/sousfichier.md", 
        of=0x620120 "test.static/test1/sousdossier2/sousfichier.html") at cw.c:333
    #14 0x0000000000401dd8 in processfile (inpath=0x620030 "test/test1/sousdossier2/sousfichier.md", 
        outpath=0x6200e0 "test.static/test1/sousdossier2/sousfichier.md") at cw.c:392
    #15 0x0000000000401f95 in process (sourcedir=0x61f350 "test/test1/sousdossier2", 
        outdir=0x61ff30 "test.static/test1/sousdossier2", totouch=1) at cw.c:436
    #16 0x0000000000401f6a in process (sourcedir=0x61eb90 "test/test1", outdir=0x61bb30 "test.static/test1", 
        totouch=1) at cw.c:431
    #17 0x0000000000401f6a in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:431
    #18 0x0000000000401b7a in convert (sf=0x61bb00 "test/test1/sousdossier2/index.md", 
        of=0x61ad10 "test.static/test1/sousdossier2/index.html") at cw.c:333
    #19 0x0000000000401dd8 in processfile (inpath=0x61bb00 "test/test1/sousdossier2/index.md", 
        outpath=0x61bbd0 "test.static/test1/sousdossier2/index.md") at cw.c:392
    #20 0x0000000000401f95 in process (sourcedir=0x61b9d0 "test/test1/sousdossier2", 
        outdir=0x61bad0 "test.static/test1/sousdossier2", totouch=1) at cw.c:436
    #21 0x0000000000401f6a in process (sourcedir=0x61a950 "test/test1", outdir=0x6144e0 "test.static/test1", 
        totouch=1) at cw.c:431
    #22 0x0000000000401f6a in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:431
    #23 0x0000000000401b7a in convert (sf=0x616b20 "test/test1/Who_v2.md", 
        of=0x6178d0 "test.static/test1/Who_v2.html") at cw.c:333
    #24 0x0000000000401dd8 in processfile (inpath=0x616b20 "test/test1/Who_v2.md", 
        outpath=0x6177b0 "test.static/test1/Who_v2.md") at cw.c:392
    #25 0x0000000000401f95 in process (sourcedir=0x616760 "test/test1", outdir=0x615440 "test.static/test1", 
        totouch=1) at cw.c:436
    #26 0x0000000000401f6a in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:431
    #27 0x0000000000401b7a in convert (sf=0x612ae0 "test/test1/Mateteestmalade.md", 
        of=0x6136c0 "test.static/test1/Mateteestmalade.html") at cw.c:333
    #28 0x0000000000401dd8 in processfile (inpath=0x612ae0 "test/test1/Mateteestmalade.md", 
        outpath=0x613660 "test.static/test1/Mateteestmalade.md") at cw.c:392
    #29 0x0000000000401f95 in process (sourcedir=0x60f8c0 "test/test1", outdir=0x612570 "test.static/test1", 
        totouch=1) at cw.c:436
    #30 0x0000000000401f6a in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:431
    #31 0x0000000000401b7a in convert (sf=0x60b750 "test/test1/Who.md", 
        of=0x60f530 "test.static/test1/Who.html") at cw.c:333
    #32 0x0000000000401dd8 in processfile (inpath=0x60b750 "test/test1/Who.md", 
        outpath=0x60f4d0 "test.static/test1/Who.md") at cw.c:392
    #33 0x0000000000401f95 in process (sourcedir=0x60e990 "test/test1", outdir=0x608670 "test.static/test1", 
        totouch=1) at cw.c:436
    #34 0x0000000000401f6a in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:431
    #35 0x0000000000401b7a in convert (sf=0x6086c0 "test/test1/index.md", 
        of=0x60bd00 "test.static/test1/index.html") at cw.c:333
    #36 0x0000000000401dd8 in processfile (inpath=0x6086c0 "test/test1/index.md", 
        outpath=0x60abe0 "test.static/test1/index.md") at cw.c:392
    #37 0x0000000000401f95 in process (sourcedir=0x606d60 "test/test1", outdir=0x6055a0 "test.static/test1", 
        totouch=1) at cw.c:436
    #38 0x0000000000401f6a in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:431
    #39 0x0000000000401b7a in convert (sf=0x606bb0 "test/index.md", of=0x6040e0 "test.static/index.html")
        at cw.c:333
    #40 0x0000000000401dd8 in processfile (inpath=0x606bb0 "test/index.md", 
        outpath=0x6044b0 "test.static/index.md") at cw.c:392
    #41 0x0000000000401f95 in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=1)
        at cw.c:436
    #42 0x0000000000401b7a in convert (sf=0x604190 "test/essai.md", of=0x604230 "test.static/essai.html")
        at cw.c:333
    #43 0x0000000000401dd8 in processfile (inpath=0x604190 "test/essai.md", 
        outpath=0x6041b0 "test.static/essai.md") at cw.c:392
    #44 0x0000000000401f95 in process (sourcedir=0x604010 "test", outdir=0x604030 "test.static", totouch=0)
        at cw.c:436
    #45 0x00000000004024e2 in main (argc=2, argv=0x7fffffffe768) at cw.c:584
    Et voici le code tout entier :
    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
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
    554
    555
    556
    557
    558
    559
    560
    561
    562
    563
    564
    565
    566
    567
    568
    569
    570
    571
    572
    573
    574
    575
    576
    577
    578
    579
    580
    581
    582
    583
    584
    585
    586
    587
    588
    589
    590
    /* 
     * Créé le 21 juin 2013 19:37:47
     * Filename:  file.c
     *
     * Description:   
     *
     * Version:  0.1
     * Licence:  GPLv3
     * Author:  © Xavier Cartron (XC) 2013, thuban@yeuxdelibad.net
     */
     
     
    #define _BSD_SOURCE
    #define _GNU_SOURCE
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>
    #include <errno.h>
    #include <dirent.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <libgen.h>
    #include <time.h>
    #include <utime.h>
    #include "config.h"
     
    #define VERSION "0.1"
    #define NAME "cw"
     
    struct FileList
    {
        int nb;
        char ** list;
    };
     
    static char *sd = NULL;
    static char *od = NULL;
     
    void freefilelist(struct FileList f);
    int countdir( char *path);
    void listdir( char *path, struct FileList *filelist);
    int isdir( char *path);
    int isfile( char *path);
    void help();
    void process(char *sourcedir,char *outdir, int totouch);
    void processfile(char *inpath, char *outpath);
    char * getext( char *filename);
    int copy( char *src,  char *tgt);
    void convert(char *sf, char *of);
    char * exttoext(char *md, char *oldext, char *newext);
    int isfresher(char *src, char *out);
    char *spaceto_(char *str);
    void touch(char *path);
    char *smprintf(char *fmt, ...);
    char *genmenu(char *sourcedir, char *curfile);
    char *gennav(char *sourcedir, char *curdir, char *curfile);
     
    char *  
    smprintf(char *fmt, ...)
    {
        va_list fmtargs;
    	char *buf = NULL;
     
    	va_start(fmtargs, fmt);
    	if (vasprintf(&buf, fmt, fmtargs) == -1){
    		fprintf(stderr, "malloc vasprintf\n");
    		exit(1);
        }
    	va_end(fmtargs);
     
    	return buf;
     
    }
     
    char *
    genmenu(char *sourcedir, char *curfile)
    {
        struct FileList dlst = {0, NULL};
        char *tmpsrc = NULL;
        char *menu = NULL;
     
        listdir(sourcedir, &dlst);	
     
        menu = smprintf("<ul>\n");
     
            for (int i=0; i<dlst.nb; i++)
            {
                tmpsrc = smprintf("%s/%s",sourcedir, dlst.list[i]);
                if (isfile(tmpsrc))
                {
                    if ((isdir(tmpsrc)) && (isfile(smprintf("%s/index%s",tmpsrc,EXT))) )
                    {
                        if (strstr(curfile, dlst.list[i]) != NULL)
                            menu =  smprintf("%s<li class=\"toShow\"><a class=\"thisPath\" href=\"/%s/index.html\">%s</a></li>\n",
                                        menu, spaceto_(dlst.list[i]),dlst.list[i]);
                        else
                            menu = smprintf("%s<li><a href=\"/%s/index.html\">%s</a></li>\n",
                                        menu, spaceto_(dlst.list[i]),dlst.list[i]);
                    }
                    else if ((strcmp(getext(dlst.list[i]),EXT) == 0) &&
                            (strcmp(dlst.list[i],smprintf("index%s",EXT)) != 0))
                    {
                        if (strcmp(tmpsrc, curfile) == 0)
                        {
                            menu = smprintf("%s<li class=\"toShow\"><a class=\"thisPath\" href=\"/%s\">%s</a></li>\n",
                                    menu,
                                    spaceto_(exttoext(dlst.list[i], EXT,".html")),
                                    exttoext(dlst.list[i], EXT,""));
                        }
                        else
                        {
                            menu = smprintf("%s<li><a href=\"/%s\">%s</a></li>\n",
                                    menu,
                                    spaceto_(exttoext(dlst.list[i], EXT,".html")),
                                    exttoext(dlst.list[i], EXT,""));
                        }
                    }
                }
                free(tmpsrc);
            }
            freefilelist(dlst);
        menu = smprintf("%s</ul>\n",menu);
        return menu;
    }
     
    char *
    gennav(char *sourcedir, char *curdir, char *curfile)
    {
        struct FileList dlst = {0, NULL};
        char *tmpsrc = NULL;
        char *newdir = NULL;
        char *nav = NULL;
     
        listdir(sourcedir, &dlst);	
     
        nav = smprintf("<ul>\n");
     
            for (int i=0; i<dlst.nb; i++)
            {
                tmpsrc = smprintf("%s/%s",sourcedir, dlst.list[i]);
                if (isfile(tmpsrc))
                {
                    if ((isdir(tmpsrc)) && (isfile(smprintf("%s/index%s",tmpsrc,EXT))) )
                    {
                        if (strstr(curfile, dlst.list[i]) != NULL)
                            nav =  smprintf("%s<li class=\"toShow\"><a class=\"thisPath\" href=\"%s/%s/index.html\">%s</a>\n",
                                nav, spaceto_(curdir),spaceto_(dlst.list[i]),dlst.list[i]);
                        else
                            nav = smprintf("%s<li><a href=\"%s/%s/index.html\">%s</a>\n",
                                    nav, spaceto_(curdir),spaceto_(dlst.list[i]),dlst.list[i]);
     
                        newdir = smprintf("%s/%s",curdir,dlst.list[i]);
                        nav = smprintf("%s%s\n",nav,gennav(tmpsrc, newdir, curfile));
                        nav = smprintf("%s</li>\n",nav);
                        free(newdir);
                    }
                    else if ((strcmp(getext(dlst.list[i]),EXT) == 0) &&
                            (strcmp(dlst.list[i],smprintf("index%s",EXT)) != 0))
                    {
                        if (strcmp(tmpsrc, curfile) == 0)
                        {
                            nav = smprintf("%s<li class=\"toShow\"><a class=\"thisPath\" href=\"%s/%s\">%s</a></li>\n",
                                    nav,
                                    spaceto_(curdir),spaceto_(exttoext(dlst.list[i], EXT,".html")),
                                    exttoext(dlst.list[i], EXT,""));
                        }
                        else
                        {
                            nav = smprintf("%s<li><a href=\"%s/%s\">%s</a></li>\n",
                                    nav,
                                    spaceto_(curdir),spaceto_(exttoext(dlst.list[i], EXT,".html")),
                                    exttoext(dlst.list[i], EXT,""));
                        }
                    }
                }
                free(tmpsrc);
            }
            freefilelist(dlst);
        nav = smprintf("%s</ul>\n",nav);
        return nav;
    }
     
     
    char * 
    spaceto_(char *str)
    {
        char *buf = NULL;
        buf = malloc (sizeof(char) * (strlen(str)+1));
        strcpy(buf,str);
        for (int i=0; i <= strlen(buf); i++)
                if (buf[i] == ' ') buf[i] = '_';
        return buf;
    }
     
    char *
    exttoext(char *md, char *oldext, char *newext)
    {
        char *b = NULL;
        char *html = malloc(sizeof(char) * (strlen(md) + strlen(newext) - strlen(oldext)));
     
        strcpy(html, md);
        b = strstr(html, oldext);
        if (b == NULL)
        {
            free(html);
            return md;
        }
        strcpy(b, newext);
        return html;
    }
     
    int
    isfresher(char *src, char *out)
    {
        struct stat stsf, stof; 
        stat(src, &stsf);
        stat(out, &stof);
        if ((stsf.st_mtime > stof.st_mtime) || !(isfile(out)))
            return 1;
        else
            return 0;
    }
     
    void
    touch(char *path)
    {
        struct utimbuf new_times;
     
        new_times.modtime = time(NULL);    /* set mtime to current time */
        utime(path, &new_times);
    }
     
    void 
    convert(char *sf, char *of)
    {
        FILE *pp;
        FILE *out;
        char buf[BUFSIZ];
        int regenmenu = 0;
        char *menu = NULL;
        char *nav = NULL;
     
        char *c = smprintf("%s \"%s\"", converter, sf);
        char *sitemap = smprintf("%s%s",sd,SITEMAP);
     
     
        /*regenerate menu if new file*/
        if (!(isfile(of)) && (strcmp(sf,sitemap) != 0) )
            regenmenu = 1;
     
        if (MENU)
            menu = genmenu(sd, sf);
        else
            menu = smprintf("");
     
        if (NAV)
            nav = gennav(sd, "", sf);
        else
            nav = smprintf("");
     
        if (BUILTIN_CONVERTER)
        {}
        else
        {
            if ((out = fopen(of, "w+")) != NULL)
            {
                fprintf(out,
    "<!doctype html>\n\
    <html>\n\
        <head> \n\
            %s \n\
            <title>%s</title> \n\
            <meta name=\"author\" content=\"%s\" />\n\
        </head> \n", HEADER, SITETITLE, AUTHOR);
                        // change sitetitle to page title
     
                fprintf(out,
    "<body>\n\
        <div class=\"header\">\n\
            <h1 class=\"headerTitle\">\n\
            <a href=\"%s\">%s</a>\n\
            <a id=\"topOfPage\"></a>\n\
            <span class=\"headerSubtitle\">%s</span>\n\
            <span class=\"midHeader\">%s</span>\n\
            </h1>\n\
        </div>\n", ROOTPAGE, SITETITLE, SITESUBTITLE, BANNER);
                fprintf(out,
    "<div id=\"menu\">\n\
            %s\n\
    </div>\n\
    <div id=\"nav\">\n\
            %s\n\
    </div>\n\
        <div id=\"content\">\n\
        <div id=\"main\">\n\
            %s\n", menu, nav, TOPTEMPLATE);
     
                /* put sitemap */
                if (strcmp(sf,sitemap) == 0)
                    fprintf(out,"%s\n",nav);
                /* convert markdown */
                else
                {
                    if ((pp = popen(c, "r")) != NULL)
                    {
                        while (fgets(buf, sizeof(buf), pp))
                            fputs(buf, out);
                        pclose(pp);
                    }
                }
                /* end the page */
                fprintf(out,
    "        %s\n\
        </div>\n\
        </div>\n\
        <div id=\"footer\">\n\
            %s\n\
        </div>\n\
    </body>\n\
    </html>", BOTTOMTEMPLATE, FOOTER);
     
                    fclose(out);
            }
        }
        free(c);
        free(menu);
        free(nav);
        printf(" ...DONE\n");
     
        /* pour montrer à developpez.org*/
        process(sd,od, 1);
        if (regenmenu)
        {
            printf("... Regenerate menu(s)\n");
            touch(sitemap);
            process(sd, od, 1);
        }
        free(sitemap);
    }
     
    char *
    getext(char *filename)
    {
        char *ext = strrchr(filename, '.');
        if (!ext) 
            return "";
        else 
            return ext;
    }
     
     
    int 
    copy(char *src, char *tgt)
    {
        FILE* fs;
        FILE* fo;
        char buffer[BUFSIZ];
        int nb;
     
        if ((fs = fopen(src, "rb")) == NULL) 
        {
            fprintf(stderr, "Can't open %s\n", src);
            return 1;
        }
     
        if ((fo = fopen(tgt, "wb+")) == NULL)
        {
            fclose(fs);
            fprintf(stderr, "Can't copy %s\n", tgt);
            return 1;
        }
     
        while ((nb = fread(buffer, 1, BUFSIZ, fs)) != 0)
            fwrite(buffer, 1, nb, fo);
     
        fclose(fo);
        fclose(fs);
        printf(" ...done.\n");
        return 0;
    }
     
    void
    processfile(char *inpath, char *outpath)
    {
        char *sitemap = smprintf("%s%s",sd,SITEMAP);
     
        if ((strcmp(getext(inpath), EXT) == 0) ||
            (strcmp(inpath,sitemap) == 0) )
            if (isfresher(inpath,exttoext(outpath,EXT,".html")))
                convert(inpath, exttoext(outpath,EXT,".html"));
            else
                printf(" - nothing to do \n");
        else
            if (isfresher(inpath,outpath))
                copy(inpath, outpath);
            else
                printf(" - nothing to do \n");
        free(sitemap);
    }
     
    void
    process(char *sourcedir, char *outdir, int totouch)
    {
        struct FileList dlst = {0, NULL};
        listdir(sourcedir, &dlst);	
        char *tmpsrc = NULL;
        char *tmpout = NULL;
     
     
        for (int i=0; i<dlst.nb; i++)
        {
            tmpsrc = smprintf("%s/%s",sourcedir, dlst.list[i]);
            tmpout = smprintf("%s/%s",outdir, dlst.list[i]);
            tmpout = spaceto_(tmpout);
     
            if (totouch)
                touch(tmpsrc);
     
            if (isfile(tmpsrc))
            {
                if (isdir(tmpsrc))
                {
                    if (! isdir(tmpout))
                    {
                        printf("... Creating directory\n");
                        mkdir(tmpout, 0777);
                    }
                    printf("%s\n",tmpsrc);
                    process(tmpsrc, tmpout, totouch);
                }
                else
                {
                    printf("%s",tmpsrc);
                    processfile(tmpsrc, tmpout);
                }
            }
            free(tmpsrc);
            free(tmpout);
        }
        freefilelist(dlst);
    }
     
    void
    freefilelist(struct FileList f)
    {
        for (int i=0; i<f.nb; i++)
            free(f.list[i]);
        free(f.list);
    }
     
    int
    countdir( char *path)
    // return number of files in path
    {
     
        int num = 0;
        struct dirent *ent = NULL;
        DIR *d = opendir(path);
        if (d == NULL)
            perror("Open dir");
     
        while ((ent = readdir(d)) != NULL)
            num++;
        closedir(d);
        return num;
    }
     
    void 
    listdir( char *path, struct FileList *filelist)
    {
        struct dirent *ent = NULL;
        int i = 0;
     
        DIR *d = opendir(path);
        if (d == NULL)
            perror("Open dir");
     
        // Allocate number of files
        filelist->list = malloc(sizeof(char*) * countdir(path));
     
        // Put file names in dirlist
        rewinddir(d);
        while ((ent = readdir(d)) != NULL)
        {
            //skip . and ..
            if ( (strcmp(ent->d_name, ".") == 0) ||
               (strcmp(ent->d_name, "..") == 0) )
                continue;
            // Allocate enough place for filename
            filelist->list[i] = malloc( sizeof(char) * (strlen(ent->d_name)+1));
                                        //+ sizeof(path) + sizeof(char));
            strcpy(filelist->list[i], ent->d_name);
            i++;
        }
        closedir(d);
     
        filelist->nb = i;
    }
     
    void 
    help()
    {
        printf(" %s version %s by Xavier Cartron (compiled at %s the %s)\n",
                NAME,VERSION, __TIME__, __DATE__);
        printf(" usage : %s <source directory> <output directory> (optional) \n", NAME);
        exit(0);
    }
     
    int 
    isdir( char *path)
        // return 1 if path is a directory
    {
        DIR *d = opendir(path);
        if (d == NULL)
            return 0;
        else
        {
            closedir(d);
            return 1;
        }
    }
     
    int
    isfile( char *path)
    {
        FILE * fp = fopen(path, "rb");
        if (fp == NULL)
            return 0;
        else
        {
            fclose(fp);
            return 1;
        }
    }
     
    int main(int argc, char *argv[])
    {
        char *sitemap = NULL;
     
        if ((argc < 2) || (argc > 3))
            help();
        else
        {
            sd = malloc(strlen(argv[1]) + 1);
            sprintf(sd, argv[1]);
            printf("... Source : %s\n",sd);
            if (argc == 3)
            {
                od = malloc(strlen(argv[2]) + 1);
                sprintf(od,argv[2]);
            }
            else
            {
                // ".static" = 8 char in memory
                od = malloc(strlen(argv[1]) + sizeof(char)*8);
                sprintf(od, "%s.static",argv[1]);
            }
            printf("... Target : %s\n",od);
        }
        //testing directories existence
        if (! isdir(sd))
        {
            fprintf(stderr,"%s is not a directory\n", sd);
            exit(1);
        }
     
        if (! isdir(od))
        {
            printf("... %s don't exist. Creating %s\n", od, od);
            mkdir(od, 0777);
        }
     
        sitemap = smprintf("%s%s",sd,SITEMAP);
     
        if (strlen(SITEMAP) > 0)
        {
            printf("... Prepare sitemap generation\n");
            printf("%s\n",sitemap);
            if (!(isfile(sitemap)))
                fclose(fopen(sitemap, "w"));
        }
        process(sd,od, 0);
     
        free(sitemap);
        free(sd);
        free(od);
    	return 0;
    }
    Ça fait beaucoup d'un coup et c'est imbuvable, je le reconnais... Mais là je ne sais plus où chercher.

  2. #2
    Membre expérimenté Avatar de edgarjacobs
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2011
    Messages
    625
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 64
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Mai 2011
    Messages : 625
    Points : 1 564
    Points
    1 564
    Par défaut
    Bonjour,

    Je n'ai pas été plus loin que ça, mais déjà il manque un argument:
    Demande à ton compilateur de te donner le maximum de warnings....

    Edit: tous tes sprintf sont sur le même modèle.... pourquoi ne pas utiliser strcpy(...)?

    Edit 2: mkdir(od, 0777): ça, c'est vraiment sale: utlise les flags fournis par ta librairie.
    On écrit "J'ai tort" ; "tord" est la conjugaison du verbre "tordre" à la 3ème personne de l'indicatif présent

  3. #3
    Membre expert
    Avatar de Metalman
    Homme Profil pro
    Enseignant-Chercheur
    Inscrit en
    Juin 2005
    Messages
    1 049
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Hauts de Seine (Île de France)

    Informations professionnelles :
    Activité : Enseignant-Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Juin 2005
    Messages : 1 049
    Points : 3 532
    Points
    3 532
    Par défaut
    Si tu as oublié les flags dont parle edgarjacobs, les voici :
    gcc -Wall -Wextra -Werror -std=c99 -pedantic mes_sources.c
    Le code est assez long, donc c'est ardu de trouver des problèmes dans la logique ^^'
    (et corriger les problèmes en dehors de la logique sera un gros + déjà ! )
    --
    Metalman !

    Attendez 5 mins après mes posts... les EDIT vont vite avec moi...
    Les flags de la vie : gcc -W -Wall -Werror -ansi -pedantic mes_sources.c
    gcc -Wall -Wextra -Werror -std=c99 -pedantic mes_sources.c
    (ANSI retire quelques fonctions comme strdup...)
    L'outil de la vie : valgrind --show-reachable=yes --leak-check=full ./mon_programme
    Et s'assurer que la logique est bonne "aussi" !

    Ma page Developpez.net

  4. #4
    Responsable 2D/3D/Jeux


    Avatar de LittleWhite
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Mai 2008
    Messages
    26 860
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 26 860
    Points : 219 064
    Points
    219 064
    Billets dans le blog
    120
    Par défaut
    Bonjour,

    L'utilisation de valgrind aidera beaucoup pour les problèmes de mémoire
    Vous souhaitez participer à la rubrique 2D/3D/Jeux ? Contactez-moi

    Ma page sur DVP
    Mon Portfolio

    Qui connaît l'erreur, connaît la solution.

  5. #5
    Expert éminent sénior
    Avatar de Sve@r
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Février 2006
    Messages
    12 690
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Oise (Picardie)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 12 690
    Points : 30 986
    Points
    30 986
    Billets dans le blog
    1
    Par défaut
    Citation Envoyé par Thuban Voir le message
    Et voici le code tout entier :
    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
    char *  
    smprintf(char *fmt, ...)
    {
        va_list fmtargs;
    	char *buf = NULL;
    
    	va_start(fmtargs, fmt);
    	if (vasprintf(&buf, fmt, fmtargs) == -1){
    		fprintf(stderr, "malloc vasprintf\n");
    		exit(1);
    		// On ne sort jamais d'une fonction paexit(). On remonte à l'appelant qui remonte à son appelant etc etc etc;
        }
    	va_end(fmtargs);
    
    	return buf;
    
    }
    
    
    char * 
    spaceto_(char *str)
    {
        char *buf = NULL;
        buf = malloc (sizeof(char) * (strlen(str)+1));
        // Tester buf == NULL peut-être...
        strcpy(buf,str);
        for (int i=0; i <= strlen(buf); i++)
                if (buf[i] == ' ') buf[i] = '_';
        return buf;
    }
    
    char *
    exttoext(char *md, char *oldext, char *newext)
    {
        char *b = NULL;
        char *html = malloc(sizeof(char) * (strlen(md) + strlen(newext) - strlen(oldext)));
        // Pareil
    
        strcpy(html, md);
        b = strstr(html, oldext);
        if (b == NULL)
        {
            free(html);
            return md;
        }
        strcpy(b, newext);
        return html;
    }
    
    void
    process(char *sourcedir, char *outdir, int totouch)
    {
        struct FileList dlst = {0, NULL};
        listdir(sourcedir, &dlst);	
        char *tmpsrc = NULL;
        char *tmpout = NULL;
    
        for (int i=0; i<dlst.nb; i++)
        {
            tmpsrc = smprintf("%s/%s",sourcedir, dlst.list[i]);
            tmpout = smprintf("%s/%s",outdir, dlst.list[i]);
    
            tmpout = spaceto_(tmpout);
            /* Est-ce que smprintf() alloue la mémoire ? 
                Si oui vu comme spaceto_() est codé, cette mémoire est alors perdue
                Si non alors clash assuré
                Quoi qu'il en soit, gros gros pb ici       */
    
            if (totouch)
                touch(tmpsrc);
    
            if (isfile(tmpsrc))
            {
                if (isdir(tmpsrc)) // Peut-il être à la fois "file" et "dir" ???
                {
                    if (! isdir(tmpout))
                    {
                        printf("... Creating directory\n");
                        mkdir(tmpout, 0777);
                    }
                    printf("%s\n",tmpsrc);
                    process(tmpsrc, tmpout, totouch);
                }
                else
                {
                    printf("%s",tmpsrc);
                    processfile(tmpsrc, tmpout);
                }
            }
            free(tmpsrc);
            free(tmpout);
        }
        freefilelist(dlst);
    }
    
    
    int
    isfile( char *path)
    {
        FILE * fp = fopen(path, "rb");
        if (fp == NULL)
            return 0;
        else
        {
            fclose(fp);
            return 1;
        }
        // Moui. Tu as "stat()" qui fait ça en mieux...
    }
    Ça fait beaucoup d'un coup et c'est imbuvable, je le reconnais... Mais là je ne sais plus où chercher.
    Bonjour
    Bienvenue dans le monde du "comportement indéterminé". J'ai mis en rouge mes remarques dans ton code mais va te falloir examiner chaque pointeur à la loupe avec tous ces malloc() non vérifiés et ces appels de fonctions où le retour écrase un pointeur déjà alloué. Mais je crois que le principal pb c'est qu'il y a trop de commentaires...
    Mon Tutoriel sur la programmation «Python»
    Mon Tutoriel sur la programmation «Shell»
    Sinon il y en a pleins d'autres. N'oubliez pas non plus les différentes faq disponibles sur ce site
    Et on poste ses codes entre balises [code] et [/code]

  6. #6
    Membre régulier Avatar de Mipwiq
    Homme Profil pro
    Inscrit en
    Avril 2013
    Messages
    42
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Avril 2013
    Messages : 42
    Points : 81
    Points
    81
    Par défaut
    Comme le dit Sve@r il y a trop d'appel de fonctions sans vérification des valeurs d'erreurs qu'elles peuvent retourner.

    Un petit conseil pour éviter de faire des erreurs du genre prend l'habitude à chaque fois que tu utilises une fonction de prendre le temps de regarder sont manuel (il y a toujours un moyen de les trouver), cela devrait t'aider grandement et souvent te permettre de mieux comprendre leur fonctionnement et leurs divers comportement.

  7. #7
    Modérateur

    Avatar de Bktero
    Homme Profil pro
    Développeur en systèmes embarqués
    Inscrit en
    Juin 2009
    Messages
    4 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 36
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur en systèmes embarqués

    Informations forums :
    Inscription : Juin 2009
    Messages : 4 481
    Points : 13 679
    Points
    13 679
    Billets dans le blog
    1
    Par défaut
    Elles sont même disponibles en français sur Developpez : http://man.developpez.com/

    Voir aussi pour la liste des fonctions de la bibliothèque C :
    http://www.developpez.net/forums/d13...nux-man-pages/

    A vos marques-pages !

Discussions similaires

  1. Trackeur de bug et corruption de mémoire
    Par yann458 dans le forum Visual C++
    Réponses: 1
    Dernier message: 15/02/2012, 10h54
  2. Réponses: 29
    Dernier message: 05/09/2010, 12h11
  3. fichier mappé en mémoire
    Par WinBernardo dans le forum Delphi
    Réponses: 7
    Dernier message: 01/12/2006, 09h38
  4. Vitesse de la mémoire vidéo
    Par Anonymous dans le forum x86 16-bits
    Réponses: 3
    Dernier message: 06/06/2002, 20h20
  5. Problème avec la mémoire virtuelle
    Par Anonymous dans le forum CORBA
    Réponses: 13
    Dernier message: 16/04/2002, 16h10

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