Ignore:
Timestamp:
Feb 8, 2007, 3:08:10 AM (17 years ago)
Author:
Bruno Cornec
Message:

Memory management improvements again for libmondo-fork.c & libmondo-files.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/mondo/src/common/libmondo-files.c

    r1107 r1113  
    1313#include "libmondo-files.h"
    1414
    15 #include "lib-common-externs.h"
    16 
    1715#include "libmondo-tools-EXT.h"
    18 #include "libmondo-gui-EXT.h"
     16#include "newt-specific-EXT.h"
    1917#include "libmondo-devices-EXT.h"
    2018#include "libmondo-fork-EXT.h"
    2119#include "libmondo-string-EXT.h"
     20#include "mr_mem.h"
    2221
    2322#include "mr_file.h"
     
    4544    /*@ buffers ***************************************************** */
    4645    static char output[MAX_STR_LEN];
    47     char command[MAX_STR_LEN * 2];
    48     char tmp[MAX_STR_LEN];
     46
     47    char *command = NULL;
     48    char *tmp = NULL;
     49    size_t n = 0;
    4950
    5051    /*@ pointers **************************************************** */
     
    5960
    6061    assert_string_is_neither_NULL_nor_zerolength(filename);
     62
    6163    if (does_file_exist(filename)) {
    62         sprintf(command, "md5sum \"%s\"", filename);
     64        mr_asprintf(&command, "md5sum \"%s\"", filename);
    6365        fin = popen(command, "r");
     66        mr_free(command);
     67
    6468        if (fin) {
    6569            (void) fgets(output, MAX_STR_LEN, fin);
     
    6872        }
    6973    } else {
    70         sprintf(tmp, "File '%s' not found; cannot calc checksum",
     74        mr_asprintf(&tmp, "File '%s' not found; cannot calc checksum",
    7175                filename);
    7276        log_it(tmp);
     77        mr_free(tmp);
    7378    }
    7479    if (p) {
     
    124129
    125130    /*@ buffers ***************************************************** */
    126     char command[MAX_STR_LEN * 2];
    127     char incoming[MAX_STR_LEN];
    128     char tmp[MAX_STR_LEN];
     131    char *command = NULL;
     132    char *incoming = NULL;
     133    char *tmp = NULL;
    129134
    130135    /*@ long ******************************************************** */
    131136    long noof_lines = -1L;
    132137
     138    /*@ int ******************************************************** */
     139    size_t n = 0;
     140
    133141    /*@ pointers **************************************************** */
    134142    FILE *fin;
    135143
    136     /*@ initialize [0] to null ******************************************** */
    137     incoming[0] = '\0';
    138 
    139144    assert_string_is_neither_NULL_nor_zerolength(filename);
    140145    if (!does_file_exist(filename)) {
    141         sprintf(tmp,
     146        mr_asprintf(&tmp,
    142147                "%s does not exist, so I cannot found the number of lines in it",
    143148                filename);
    144149        log_it(tmp);
     150        mr_free(tmp);
    145151        return (0);
    146152    }
    147     sprintf(command, "cat %s | wc -l", filename);
     153    mr_asprintf(&command, "cat %s | wc -l", filename);
    148154    if (!does_file_exist(filename)) {
    149155        return (-1);
    150156    }
    151157    fin = popen(command, "r");
     158    mr_free(command);
     159
    152160    if (fin) {
    153161        if (feof(fin)) {
    154162            noof_lines = 0;
    155163        } else {
    156             (void) fgets(incoming, MAX_STR_LEN - 1, fin);
     164            mr_getline(&incoming, &n, fin);
    157165            while (strlen(incoming) > 0
    158166                   && incoming[strlen(incoming) - 1] < 32) {
     
    160168            }
    161169            noof_lines = atol(incoming);
     170            mr_free(incoming);
    162171        }
    163172        paranoid_pclose(fin);
     
    181190
    182191    assert(filename != NULL);
    183     //  assert_string_is_neither_NULL_nor_zerolength(filename);
     192
    184193    if (lstat(filename, &buf)) {
    185194        mr_msg(20, "%s does not exist", filename);
     
    192201
    193202
    194 
    195 
    196 
    197 
    198203/**
    199204 * Modify @p inout (a file containing a list of files) to only contain files
     
    212217    /*@ int ********************************************************* */
    213218    int i;
     219    size_t n = 0;
    214220
    215221    /*@ pointers **************************************************** */
     
    220226
    221227    assert_string_is_neither_NULL_nor_zerolength(inout);
    222     sprintf(infname, "%s.in", inout);
    223     sprintf(outfname, "%s", inout);
    224     sprintf(tmp, "cp -f %s %s", inout, infname);
     228
     229    mr_asprintf(&infname, "%s.in", inout);
     230
     231    mr_asprintf(&tmp, "cp -f %s %s", inout, infname);
    225232    run_program_and_log_output(tmp, FALSE);
     233    mr_free(tmp);
     234
    226235    if (!(fin = fopen(infname, "r"))) {
    227236        log_OS_error("Unable to openin infname");
     237        mr_free(infname);
    228238        return;
    229239    }
     240
     241    mr_asprintf(&outfname, "%s", inout);
    230242    if (!(fout = fopen(outfname, "w"))) {
    231243        log_OS_error("Unable to openout outfname");
     244        mr_free(infname);
     245        mr_free(outfname);
    232246        return;
    233247    }
    234     for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
    235          fgets(incoming, MAX_STR_LEN, fin)) {
     248    mr_free(outfname);
     249
     250    for (mr_getline(&incoming, &n, fin); !feof(fin);
     251         mr_getline(&incoming, &n, fin)) {
    236252        i = strlen(incoming) - 1;
    237253        if (i >= 0 && incoming[i] < 32) {
     
    241257            fprintf(fout, "%s\n", incoming);
    242258        } else {
    243             sprintf(tmp, "Excluding '%s'-nonexistent\n", incoming);
     259            mr_asprintf(&tmp, "Excluding '%s'-nonexistent\n", incoming);
    244260            log_it(tmp);
    245         }
    246     }
     261            mr_free(tmp);
     262        }
     263    }
     264    mr_free(incoming);
    247265    paranoid_fclose(fout);
    248266    paranoid_fclose(fin);
    249267    unlink(infname);
    250 }
    251 
    252 
    253 
    254 
    255 
    256 
    257 
     268    mr_free(infname);
     269}
    258270
    259271
     
    267279int figure_out_kernel_path_interactively_if_necessary(char *kernel)
    268280{
    269     char tmp[MAX_STR_LEN];
    270     char *command;
    271 
     281    char *tmp = NULL;
     282    char *command = NULL;
     283
     284    malloc_string(tmp);
    272285    if (!kernel[0]) {
    273286        strcpy(kernel,
     
    277290    // If we didn't get anything back, check whether mindi raised a fatal error
    278291    if (!kernel[0]) {
    279         malloc_string(command);
    280         strcpy(command, "grep 'Fatal error' /var/log/mindi.log");
     292        mr_asprintf(&command, "grep 'Fatal error' /var/log/mindi.log");
    281293        strcpy(tmp, call_program_and_get_last_line_of_output(command));
    282294        if (strlen(tmp) > 1) {
     
    299311                ("Kernel not found. Please specify with the '-k' flag.");
    300312        }
    301         sprintf(tmp, "User says kernel is at %s", kernel);
    302         log_it(tmp);
     313        log_it("User says kernel is at %s", kernel);
    303314    }
    304315    return (0);
    305316}
    306 
    307 
    308 
    309 
    310317
    311318
     
    324331    static char output[MAX_STR_LEN];
    325332    char *incoming;
    326     char *command;
     333    char *command = NULL;
    327334
    328335    malloc_string(incoming);
    329     malloc_string(command);
    330336    incoming[0] = '\0';
    331337    /*@******************************* */
    332338
    333339    assert_string_is_neither_NULL_nor_zerolength(fname);
    334     sprintf(command, "which %s 2> /dev/null", fname);
     340
     341    mr_asprintf(&command, "which %s 2> /dev/null", fname);
    335342    strcpy(incoming, call_program_and_get_last_line_of_output(command));
     343    mr_free(command);
     344
    336345    if (incoming[0] == '\0') {
    337346        if (system("which file > /dev/null 2> /dev/null")) {
     
    341350            return (NULL);      // forget it :)
    342351        }
    343         sprintf(command,
     352        mr_asprintf(&command,
    344353                "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null",
    345354                incoming);
    346355        strcpy(incoming,
    347356               call_program_and_get_last_line_of_output(command));
     357        mr_free(command);
    348358    }
    349359    if (incoming[0] == '\0')    // yes, it is == '\0' twice, not once :)
    350360    {
    351         sprintf(command, "dirname %s 2> /dev/null", incoming);
     361        mr_asprintf(&command, "dirname %s 2> /dev/null", incoming);
    352362        strcpy(incoming,
    353363               call_program_and_get_last_line_of_output(command));
     364        mr_free(command);
    354365    }
    355366    strcpy(output, incoming);
     
    362373    }
    363374    mr_free(incoming);
    364     mr_free(command);
    365375    if (!output[0]) {
    366376        return (NULL);
     
    371381
    372382
    373 
    374 
    375 
    376 
    377 
    378 
    379383/**
    380384 * Get the last sequence of digits surrounded by non-digits in the first 32k of
     
    397401
    398402    assert_string_is_neither_NULL_nor_zerolength(logfile);
     403
    399404    if (!(fin = fopen(logfile, "r"))) {
    400405        log_OS_error("Unable to open logfile");
     
    410415    for (; len > 0 && isdigit(datablock[len - 1]); len--);
    411416    trackno = atoi(datablock + len);
    412     /*
    413        sprintf(tmp,"datablock=%s; trackno=%d",datablock+len, trackno);
    414        log_it(tmp);
    415      */
    416417    return (trackno);
    417418}
    418 
    419 
    420 
    421 
    422 
    423419
    424420
     
    432428{
    433429
    434     /*@ buffers ***************************************************** */
    435     char tmp[MAX_STR_LEN];
    436     char lastline[MAX_STR_LEN];
    437     char command[MAX_STR_LEN];
    438     /*@ pointers **************************************************** */
    439     char *p;
    440 
    441     /*@ int's ******************************************************* */
     430    char *lastline = NULL;
     431    char *command = NULL;
     432    char *p = NULL;
    442433    int i;
    443434
     435    malloc_string(lastline);
    444436    for (i = NOOF_ERR_LINES - 1;
    445437         i >= 0 && !strstr(err_log_lines[i], "% Done")
    446438         && !strstr(err_log_lines[i], "% done"); i--);
    447439    if (i < 0) {
    448         sprintf(command,
     440        mr_asprintf(&command,
    449441                "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'",
    450442                filename, '%');
    451443        strcpy(lastline,
    452444               call_program_and_get_last_line_of_output(command));
     445        mr_free(command);
    453446        if (!lastline[0]) {
    454447            return (0);
     
    462455        *p = '\0';
    463456    }
    464 //  mr_msg(2, "lastline='%s', ", p, lastline);
    465457    if (!p) {
    466458        return (0);
     
    473465    i = atoi(p);
    474466
    475     sprintf(tmp, "'%s' --> %d", p, i);
    476 //     log_to_screen(tmp);
    477 
    478467    return (i);
    479468}
    480 
    481 
    482 
    483469
    484470
     
    493479    /*@ buffers ***************************************************** */
    494480    static char output[MAX_STR_LEN];
    495     static char command[MAX_STR_LEN * 2];
    496     static char tmp[MAX_STR_LEN];
     481    char *command = NULL;
     482    char *tmp = NULL;
    497483
    498484    /*@ pointers **************************************************** */
    499485    FILE *fin;
     486    size_t n = 0;
    500487
    501488    /*@ end vars **************************************************** */
    502489
    503490    if (!does_file_exist(filename)) {
    504         sprintf(tmp, "Tring to get last line of nonexistent file (%s)",
     491        mr_asprintf(&tmp, _("Tring to get last line of nonexistent file (%s)"),
    505492                filename);
    506493        log_it(tmp);
     494        mr_free(tmp);
    507495        output[0] = '\0';
    508496        return (output);
    509497    }
    510     sprintf(command, "tail -n1 %s", filename);
     498    mr_asprintf(&command, "tail -n1 %s", filename);
    511499    fin = popen(command, "r");
     500    mr_free(command);
     501
    512502    (void) fgets(output, MAX_STR_LEN, fin);
    513503    paranoid_pclose(fin);
     
    517507    return (output);
    518508}
     509
    519510
    520511/**
     
    544535
    545536
    546 
    547 /**
    548  * ?????
    549  * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.
    550  */
    551 int
    552 make_checksum_list_file(char *filelist, char *cksumlist, char *comppath)
    553 {
    554     /*@ pointers **************************************************** */
    555     FILE *fin;
    556     FILE *fout;
    557 
    558     /*@ int   ******************************************************* */
    559     int percentage;
    560     int i;
    561     int counter = 0;
    562 
    563     /*@ buffer ****************************************************** */
    564     char stub_fname[1000];
    565     char curr_fname[1000];
    566     char curr_cksum[1000];
    567     char tmp[1000];
    568 
    569     /*@ long [long] ************************************************* */
    570     off_t filelist_length;
    571     off_t curr_pos;
    572     long start_time;
    573     long current_time;
    574     long time_taken;
    575     long time_remaining;
    576 
    577     /*@ end vars *************************************************** */
    578 
    579     start_time = get_time();
    580     filelist_length = length_of_file(filelist);
    581     sprintf(tmp, "filelist = %s; cksumlist = %s", filelist, cksumlist);
    582     log_it(tmp);
    583     fin = fopen(filelist, "r");
    584     if (fin == NULL) {
    585         log_OS_error("Unable to fopen-in filelist");
    586         log_to_screen("Can't open filelist");
    587         return (1);
    588     }
    589     fout = fopen(cksumlist, "w");
    590     if (fout == NULL) {
    591         log_OS_error("Unable to openout cksumlist");
    592         paranoid_fclose(fin);
    593         log_to_screen("Can't open checksum list");
    594         return (1);
    595     }
    596     for (fgets(stub_fname, 999, fin); !feof(fin);
    597          fgets(stub_fname, 999, fin)) {
    598         if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) {
    599             stub_fname[i] = '\0';
    600         }
    601         sprintf(tmp, "%s%s", comppath, stub_fname);
    602         strcpy(curr_fname, tmp + 1);
    603         strcpy(curr_cksum, calc_file_ugly_minichecksum(curr_fname));
    604         fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);
    605         if (counter++ > 12) {
    606             current_time = get_time();
    607             counter = 0;
    608             curr_fname[37] = '\0';
    609             curr_pos = ftello(fin) / 1024;
    610             percentage = (int) (curr_pos * 100 / filelist_length);
    611             time_taken = current_time - start_time;
    612             if (percentage == 0) {
    613                 /*              printf("%0d%% done      \r",percentage); */
    614             } else {
    615                 time_remaining =
    616                     time_taken * 100 / (long) (percentage) - time_taken;
    617                 sprintf(tmp,
    618                         "%02d%% done   %02d:%02d taken   %02d:%02d remaining  %-37s\r",
    619                         percentage, (int) (time_taken / 60),
    620                         (int) (time_taken % 60),
    621                         (int) (time_remaining / 60),
    622                         (int) (time_remaining % 60), curr_fname);
    623                 log_to_screen(tmp);
    624             }
    625             sync();
    626         }
    627     }
    628     paranoid_fclose(fout);
    629     paranoid_fclose(fin);
    630     log_it("Done.");
    631     return (0);
    632 }
    633 
    634 
    635537/**
    636538 * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>.
     
    638540 * @return The return value of @c mkdir.
    639541 */
     542/* BERLIOS: This function shouldn't call system at all */
    640543int make_hole_for_dir(char *outdir_fname)
    641544{
    642     char tmp[MAX_STR_LEN * 2];
     545    char *tmp;
    643546    int res = 0;
    644547
    645548    assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
    646     sprintf(tmp, "mkdir -p %s", outdir_fname);
     549    mr_asprintf(&tmp, "mkdir -p %s", outdir_fname);
    647550    res = system(tmp);
     551    mr_free(tmp);
    648552    return (res);
    649553}
     
    656560 * @bug Return value unnecessary.
    657561 */
     562/* BERLIOS: This function shouldn't call system at all */
    658563int make_hole_for_file(char *outfile_fname)
    659564{
    660565    /*@ buffer ****************************************************** */
    661     char command[MAX_STR_LEN * 2];
     566    char *command;
    662567
    663568    /*@ int  ******************************************************** */
     
    669574    assert(!strstr(outfile_fname, MNT_CDROM));
    670575    assert(!strstr(outfile_fname, "/dev/cdrom"));
    671     sprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
     576
     577    mr_asprintf(&command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
    672578    res += system(command);
    673     sprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
     579    mr_free(command);
     580
     581    mr_asprintf(&command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
    674582    res += system(command);
    675     sprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
     583    mr_free(command);
     584
     585    mr_asprintf(&command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
    676586    res += system(command);
     587    mr_free(command);
    677588    unlink(outfile_fname);
    678589    return (0);
    679590}
    680 
    681 
    682591
    683592
     
    697606
    698607    /*@ buffers **************************************************** */
    699     char incoming[MAX_STR_LEN];
    700 
     608    char *incoming = NULL;
     609
     610    size_t n = 0;
    701611    /*@ end vars *************************************************** */
    702612
     
    708618        return (0);
    709619    }
    710     (void) fgets(incoming, MAX_STR_LEN - 1, fin);
     620    mr_getline(&incoming, &n, fin);
    711621    while (!feof(fin)) {
    712622        if (strstr(incoming, wildcard)) {
    713623            matches++;
    714624        }
    715         (void) fgets(incoming, MAX_STR_LEN - 1, fin);
     625        mr_getline(&incoming, &n, fin);
    716626    }
    717627    paranoid_fclose(fin);
     628    mr_free(incoming);
    718629    return (matches);
    719630}
    720 
    721 
    722631
    723632
     
    731640void register_pid(pid_t pid, char *name_str)
    732641{
    733     char tmp[MAX_STR_LEN + 1], lockfile_fname[MAX_STR_LEN + 1];
     642    char *tmp = NULL;
     643    char *lockfile_fname = NULL;
    734644    int res;
     645    size_t n = 0;
    735646    FILE *fin;
    736647
    737     sprintf(lockfile_fname, "/var/run/monitas-%s.pid", name_str);
     648    mr_asprintf(&lockfile_fname, "/var/run/monitas-%s.pid", name_str);
    738649    if (!pid) {
    739650        log_it("Unregistering PID");
     
    741652            log_it("Error unregistering PID");
    742653        }
     654        mr_free(lockfile_fname);
    743655        return;
    744656    }
    745657    if (does_file_exist(lockfile_fname)) {
    746         tmp[0] = '\0';
    747658        if ((fin = fopen(lockfile_fname, "r"))) {
    748             (void) fgets(tmp, MAX_STR_LEN, fin);
     659            mr_getline(&tmp, &n, fin);
    749660            paranoid_fclose(fin);
    750661        } else {
     
    752663        }
    753664        pid = (pid_t) atol(tmp);
    754         sprintf(tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid);
     665        mr_free(tmp);
     666
     667        mr_asprintf(&tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid);
    755668        res = system(tmp);
     669        mr_free(tmp);
    756670        if (!res) {
    757671            log_it
     
    760674        }
    761675    }
    762     sprintf(tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),
     676    mr_asprintf(&tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),
    763677            lockfile_fname);
     678    mr_free(lockfile_fname);
     679
    764680    if (system(tmp)) {
    765681        fatal_error("Cannot register PID");
    766682    }
    767 }
    768 
     683    mr_free(tmp);
     684    return;
     685}
    769686
    770687
     
    777694long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
    778695{
    779     char command[MAX_STR_LEN];
    780     char mountlist[MAX_STR_LEN];
    781     char sz_res[MAX_STR_LEN];
    782     long file_len_K;
    783 
    784     sprintf(mountlist, "%s/mountlist.txt", tmpdir);
    785     sprintf(command,
    786             "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'",
     696    char *command = NULL;
     697    char *sz_res = NULL;
     698    long file_len_K = 0L;
     699
     700    malloc_string(sz_res);
     701    mr_asprintf(&command,
     702            "grep '%s ' %s/mountlist.txt | head -n1 | awk '{print $4;}'",
    787703            dev, tmpdir);
    788704    log_it(command);
     
    790706    file_len_K = atol(sz_res);
    791707    mr_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
     708    mr_free(command);
     709    mr_free(sz_res);
    792710    return (file_len_K);
    793711}
     712
    794713
    795714/**
     
    800719long size_of_all_biggiefiles_K(struct s_bkpinfo *bkpinfo)
    801720{
    802     /*@ buffers ***************************************************** */
    803     char *fname;
    804     char *biggielist;
    805     char *comment;
    806     char *tmp;
    807     char *command;
     721    char *fname = NULL;
     722    char *biggielist = NULL;
     723    char *comment = NULL;
     724    char *tmp = NULL;
     725    char *command = NULL;
    808726
    809727    /*@ long ******************************************************** */
    810     long scratchL = 0;
    811     long file_len_K;
     728    long scratchL = 0L;
     729    long file_len_K = 0L;
    812730
    813731    /*@ pointers *************************************************** */
    814732    FILE *fin = NULL;
     733    size_t n = 0;
    815734
    816735    /*@ end vars *************************************************** */
    817736
    818     malloc_string(fname);
    819     malloc_string(biggielist);
    820     malloc_string(comment);
    821737    malloc_string(tmp);
    822     malloc_string(command);
    823738    log_it("Calculating size of all biggiefiles (in total)");
    824     sprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
     739    mr_asprintf(&biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
    825740    log_it("biggielist = %s", biggielist);
    826741    if (!(fin = fopen(biggielist, "r"))) {
     
    829744    } else {
    830745        mr_msg(4, "Reading it...");
    831         for (fgets(fname, MAX_STR_LEN, fin); !feof(fin);
    832              fgets(fname, MAX_STR_LEN, fin)) {
     746        for (mr_getline(&fname, &n, fin); !feof(fin);
     747             mr_getline(&fname, &n, fin)) {
    833748            if (fname[strlen(fname) - 1] <= 32) {
    834749                fname[strlen(fname) - 1] = '\0';
     
    839754                        fatal_error("ntfsresize not found");
    840755                    }
    841                     sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
     756                    mr_asprintf(&command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
    842757                    log_it("command = %s", command);
    843758                    strcpy (tmp, call_program_and_get_last_line_of_output(command));
     759                    mr_free(command);
     760
    844761                    log_it("res of it = %s", tmp);
    845762                    file_len_K = atoll(tmp) / 1024L;
     
    855772                mr_msg(4, "%s --> %ld K", fname, file_len_K);
    856773            }
    857             sprintf(comment,
     774            mr_asprintf(&comment,
    858775                    "After adding %s, scratchL+%ld now equals %ld", fname,
    859776                    file_len_K, scratchL);
    860777            mr_msg(4, comment);
     778            mr_free(comment);
     779
    861780            if (feof(fin)) {
    862781                break;
    863782            }
    864783        }
    865     }
     784        mr_free(fname);
     785    }
     786    mr_free(biggielist);
     787
    866788    log_it("Closing...");
    867789    paranoid_fclose(fin);
    868790    log_it("Finished calculating total size of all biggiefiles");
    869     mr_free(fname);
    870     mr_free(biggielist);
    871     mr_free(comment);
    872791    mr_free(tmp);
    873     mr_free(command);
    874792    return (scratchL);
    875793}
     
    884802{
    885803    /*@ buffer ****************************************************** */
    886     char tmp[MAX_STR_LEN];
    887     char command[MAX_STR_LEN * 2];
     804    char *tmp = NULL;
     805    char *command = NULL;
    888806    long long llres;
     807    size_t n = 0;
    889808    /*@ pointers **************************************************** */
    890     char *p;
    891     FILE *fin;
     809    char *p = NULL;
     810    FILE *fin = NULL;
    892811
    893812    /*@ end vars *************************************************** */
    894813
    895     sprintf(command, "du -sk %s", mountpt);
     814    mr_asprintf(&command, "du -sk %s", mountpt);
    896815    errno = 0;
    897816    fin = popen(command, "r");
     
    900819      llres = 0;
    901820    } else {
    902       (void) fgets(tmp, MAX_STR_LEN, fin);
    903       paranoid_pclose(fin);
    904       p = strchr(tmp, '\t');
    905       if (p) {
    906         *p = '\0';
    907       }
    908       for (p = tmp, llres = 0; *p != '\0'; p++) {
    909         llres *= 10;
    910         llres += (int) (*p - '0');
    911       }
    912     }
    913 
     821        mr_getline(&tmp, &n, fin);
     822        paranoid_pclose(fin);
     823        p = strchr(tmp, '\t');
     824        if (p) {
     825            *p = '\0';
     826        }
     827        for (p = tmp, llres = 0; *p != '\0'; p++) {
     828            llres *= 10;
     829            llres += (int) (*p - '0');
     830        }
     831    }
     832
     833    mr_free(command);
     834    mr_free(tmp);
    914835    return (llres);
    915836}
     
    931852}
    932853
     854
    933855/**
    934856 * Update a reverse CRC checksum to include another character.
     
    947869
    948870
    949 
    950 
    951871/**
    952872 * Check for an executable on the user's system; write a message to the
     
    958878{
    959879    /*@ buffers *** */
    960     char command[MAX_STR_LEN * 2];
    961     char errorstr[MAX_STR_LEN];
    962 
    963 
    964     sprintf(command, "which %s > /dev/null 2> /dev/null", fname);
    965     sprintf(errorstr,
    966             "Please install '%s'. I cannot find it on your system.",
     880    char *command;
     881    char *errorstr;
     882    int res = 0;
     883
     884
     885    mr_asprintf(&command, "which %s > /dev/null 2> /dev/null", fname);
     886    res = system(command);
     887    mr_free(command);
     888
     889    if (res) {
     890        mr_asprintf(&errorstr,
     891            _("Please install '%s'. I cannot find it on your system."),
    967892            fname);
    968     if (system(command)) {
    969893        log_to_screen(errorstr);
     894        mr_free(errorstr);
    970895        log_to_screen
    971             ("There may be hyperlink at http://www.mondorescue.com which");
    972         log_to_screen("will take you to the relevant (missing) package.");
     896            (_("There may be an hyperlink at http://www.mondorescue.org which"));
     897        log_to_screen(_("will take you to the relevant (missing) package."));
    973898        return (1);
    974899    } else {
     
    976901    }
    977902}
    978 
    979 
    980 
    981 
    982903
    983904
     
    1012933
    1013934
    1014 
    1015935/**
    1016936 * Read @p fname into @p contents.
     
    1045965    return (res);
    1046966}
    1047 
    1048 
    1049 
    1050 
    1051 
    1052 
    1053 
    1054967
    1055968
     
    1067980{
    1068981    /*@ Char buffers ** */
    1069     char command[MAX_STR_LEN * 2];
     982    char *command = NULL;
    1070983    char tmp[MAX_STR_LEN];
    1071984    char old_pwd[MAX_STR_LEN];
     
    1074987                        "Copying Mondo's core files to the scratch directory");
    1075988
     989    /* BERLIOS: Why do we need to do it here as well ? */
    1076990    mr_msg(4, "g_mondo_home='%s'", g_mondo_home);
    1077991    if (strlen(g_mondo_home) < 2) {
    1078992        find_and_store_mondoarchives_home(g_mondo_home);
    1079993    }
    1080     sprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,
     994    mr_asprintf(&command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,
    1081995            bkpinfo->scratchdir);
    1082996
     
    1085999        fatal_error("Failed to copy Mondo's stuff to scratchdir");
    10861000    }
     1001    mr_free(command);
     1002
     1003    /* i18n */
     1004    mr_asprintf(&command, CP_BIN " --parents /usr/share/locale/*/LC_MESSAGES/mondo.mo %s",bkpinfo->scratchdir);
     1005    mr_msg(4, "command = %s", command);
     1006    run_program_and_log_output(command, 1);
     1007    mr_free(command);
    10871008
    10881009    sprintf(tmp, "%s/payload.tgz", g_mondo_home);
     
    10921013        (void) getcwd(old_pwd, MAX_STR_LEN - 1);
    10931014        chdir(bkpinfo->scratchdir);
    1094         sprintf(command, "tar -zxvf %s", tmp);
     1015        mr_asprintf(&command, "tar -zxvf %s", tmp);
    10951016        if (run_program_and_log_output(command, FALSE)) {
    10961017            fatal_error("Failed to untar payload");
    10971018        }
     1019        mr_free(command);
    10981020        chdir(old_pwd);
    10991021    }
    11001022
    1101     sprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir,
     1023    mr_asprintf(&command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir,
    11021024            bkpinfo->scratchdir);
    11031025
     
    11051027        fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
    11061028    }
    1107 
    1108     strcpy(tmp,
    1109            call_program_and_get_last_line_of_output("which mondorestore"));
    1110     if (!tmp[0]) {
     1029    mr_free(command);
     1030
     1031    tmp = call_program_and_get_last_line_of_output("which mondorestore");
     1032    if (!tmp) {
    11111033        fatal_error
    11121034            ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
    11131035    }
    1114     sprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
     1036    mr_asprintf(&command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
    11151037    if (run_program_and_log_output(command, FALSE)) {
    11161038        fatal_error("Failed to copy mondorestore to tmpdir");
    11171039    }
    1118 
    1119     sprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
     1040    mr_free(command);
     1041
     1042    mr_asprintf(&command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
    11201043    paranoid_system(command);
     1044    mr_free(command);
    11211045
    11221046    if (bkpinfo->postnuke_tarball[0]) {
    1123         sprintf(command, "cp -f %s %s/post-nuke.tgz",
     1047        mr_asprintf(&command, "cp -f %s %s/post-nuke.tgz",
    11241048                bkpinfo->postnuke_tarball, bkpinfo->tmpdir);
    11251049        if (run_program_and_log_output(command, FALSE)) {
    11261050            fatal_error("Unable to copy post-nuke tarball to tmpdir");
    11271051        }
    1128     }
    1129 
     1052        mr_free(command);
     1053    }
    11301054
    11311055    mvaddstr_and_log_it(g_currentY++, 74, "Done.");
    11321056}
    1133 
    1134 
    1135 
    11361057
    11371058
     
    11561077    char nfs_client_defgw[MAX_STR_LEN];
    11571078    char nfs_server_ipaddr[MAX_STR_LEN];
    1158     char tmp[MAX_STR_LEN];
    1159     char command[MAX_STR_LEN * 2];
     1079    char *tmp = NULL;
     1080    char *command = NULL;
    11601081
    11611082    FILE *fd1 = NULL;
     
    11651086
    11661087    log_it("Storing NFS configuration");
    1167     strcpy(tmp, bkpinfo->nfs_mount);
     1088    mr_asprintf(&tmp, bkpinfo->nfs_mount);
    11681089    p = strchr(tmp, ':');
    11691090    if (!p) {
     
    11731094    *(p++) = '\0';
    11741095    strcpy(nfs_server_ipaddr, tmp);
     1096    mr_free(tmp);
     1097
    11751098    strcpy(nfs_mount, p);
    11761099    /* BERLIOS : there is a bug #67 here as it only considers the first NIC */
    1177     sprintf(command,
     1100    mr_asprintf(&command,
    11781101            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1");
    11791102    strcpy(nfs_dev, call_program_and_get_last_line_of_output(command));
    1180     sprintf(command,
     1103    mr_free(command);
     1104
     1105    mr_asprintf(&command,
    11811106            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2");
    11821107    strcpy(nfs_client_ipaddr,
    11831108           call_program_and_get_last_line_of_output(command));
    1184     sprintf(command,
     1109    mr_free(command);
     1110
     1111    mr_asprintf(&command,
    11851112            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2");
    11861113    strcpy(nfs_client_netmask,
    11871114           call_program_and_get_last_line_of_output(command));
    1188     sprintf(command,
     1115    mr_free(command);
     1116
     1117    mr_asprintf(&command,
    11891118            "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2");
    11901119    strcpy(nfs_client_broadcast,
    11911120           call_program_and_get_last_line_of_output(command));
    1192     sprintf(command,
     1121    mr_free(command);
     1122
     1123    mr_asprintf(&command,
    11931124            "route -n | grep '^0.0.0.0' | awk '{print $2}'");
    11941125    strcpy(nfs_client_defgw,
    11951126           call_program_and_get_last_line_of_output(command));
    1196     sprintf(tmp,
    1197             "nfs_client_ipaddr=%s; nfs_server_ipaddr=%s; nfs_mount=%s",
    1198             nfs_client_ipaddr, nfs_server_ipaddr, nfs_mount);
     1127    mr_free(command);
     1128
     1129    mr_asprintf(&tmp,
     1130            "nfs_client_ipaddr=%s; nfs_client_netmask=%s; nfs_server_ipaddr=%s; nfs_mount=%s; nfs_client_defgw=%s;  ",
     1131            nfs_client_ipaddr, nfs_client_netmask, nfs_server_ipaddr, nfs_mount, nfs_client_defgw);
     1132    log_it(tmp);
     1133    mr_free(tmp);
     1134
    11991135    if (strlen(nfs_dev) < 2) {
    12001136        fatal_error
     
    12121148    if (!strncmp(nfs_dev, "bond", 4)) {
    12131149        log_to_screen("Found bonding device %s; looking for corresponding ethN slave device\n", nfs_dev);
    1214         sprintf(command,
     1150        mr_asprintf(&command,
    12151151                "ifconfig %s | awk '{print $5}' | head -n1", nfs_dev);
    12161152        strcpy(mac_addr, call_program_and_get_last_line_of_output(command));
    1217         sprintf(command,
     1153        mr_free(command);
     1154
     1155        mr_asprintf(&command,
    12181156                "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1", mac_addr,nfs_dev);
    12191157        strcpy(nfs_dev, call_program_and_get_last_line_of_output(command));
     1158        mr_free(command);
     1159
    12201160        log_to_screen("Replacing it with %s\n", nfs_dev);
    12211161    }
    12221162
    12231163    fd1 = mr_fopen(MONDORESTORECFG, "a");
    1224 
    12251164    mr_fprintf(fd1, "nfs-dev %s\n", nfs_dev);
    12261165    mr_fprintf(fd1, "nfs-client-ipaddr %s\n", nfs_client_ipaddr);
     
    12361175    log_it("Finished storing NFS configuration");
    12371176}
    1238 
    1239 
    1240 
    1241 
    12421177
    12431178
     
    12621197{
    12631198    /*@ buffers *************** */
    1264     char tmp[MAX_STR_LEN];
     1199    char *tmp = NULL;
    12651200
    12661201    /*@ long long ************* */
     
    12891224    }
    12901225    if (scratchLL <= 1) {
    1291         sprintf(tmp,
    1292                 "Your backup will probably occupy a single %s. Maybe two.",
    1293                 media_descriptor_string(bkpinfo->backup_media_type));
    1294     } else if (scratchLL > 4) {
    1295         sprintf(tmp,
    1296                 "Your backup will occupy one meeeeellion media! (maybe %s)",
    1297                 number_to_text((int) (scratchLL + 1)));
     1226        mr_asprintf(&tmp,
     1227                _("Your backup will probably occupy a single %s. Maybe two."),
     1228                bkpinfo->backup_media_string);
    12981229    } else {
    1299         sprintf(tmp, "Your backup will occupy approximately %s media.",
     1230        mr_asprintf(&tmp, _("Your backup will occupy approximately %s media."),
    13001231                number_to_text((int) (scratchLL + 1)));
    13011232    }
     
    13031234        log_to_screen(tmp);
    13041235    }
    1305 }
    1306 
    1307 
    1308 /**
    1309  * Get the last suffix of @p instr.
    1310  * If @p instr was "httpd.log.gz", we would return "gz".
    1311  * @param instr The filename to get the suffix of.
    1312  * @return The suffix (without a dot), or "" if none.
    1313  * @note The returned string points to static storage that will be overwritten with each call.
    1314  */
    1315 char *sz_last_suffix(char *instr)
    1316 {
    1317     static char outstr[MAX_STR_LEN];
    1318     char *p;
    1319 
    1320     p = strrchr(instr, '.');
    1321     if (!p) {
    1322         outstr[0] = '\0';
    1323     } else {
    1324         strcpy(outstr, p);
    1325     }
    1326     return (outstr);
     1236    mr_free(tmp);
     1237    return;
    13271238}
    13281239
     
    13361247bool is_this_file_compressed(char *filename)
    13371248{
    1338     char do_not_compress_these[MAX_STR_LEN];
    1339     char tmp[MAX_STR_LEN];
    1340     char *p;
    1341 
    1342     sprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
     1249    char *do_not_compress_these = NULL;
     1250    char *tmp = NULL;
     1251    char *p = NULL;
     1252
     1253    malloc_string(do_not_compress_these);
     1254    mr_asprintf(&tmp, "%s/do-not-compress-these", g_mondo_home);
    13431255    if (!does_file_exist(tmp)) {
     1256        mr_free(tmp);
    13441257        return (FALSE);
    13451258    }
    1346     strcpy(do_not_compress_these, last_line_of_file(tmp));
     1259    mr_free(tmp);
     1260
     1261    strcpy(do_not_compress_these,last_line_of_file(tmp));
    13471262    for (p = do_not_compress_these; p != NULL; p++) {
    1348         strcpy(tmp, p);
     1263        mr_asprintf(&tmp, p);
    13491264        if (strchr(tmp, ' ')) {
    13501265            *(strchr(tmp, ' ')) = '\0';
    13511266        }
    1352         if (!strcmp(sz_last_suffix(filename), tmp)) {   /*printf("MATCH\n"); */
     1267        if (!strcmp(sz_last_suffix(filename), tmp)) {
     1268            mr_free(do_not_compress_these);
     1269            mr_free(tmp);
    13531270            return (TRUE);
    13541271        }
     1272        mr_free(tmp);
     1273
    13551274        if (!(p = strchr(p, ' '))) {
    13561275            break;
    13571276        }
    13581277    }
     1278    mr_free(do_not_compress_these);
    13591279    return (FALSE);
    13601280}
    1361 
    13621281
    13631282
     
    13751294
    13761295
    1377 
    1378 
    13791296/**
    13801297 * Create a small script that mounts /boot, calls @c grub-install, and syncs the disks.
     
    13841301int make_grub_install_scriptlet(char *outfile)
    13851302{
    1386     FILE *fout;
    1387     char *tmp;
     1303    FILE *fout = NULL;
     1304    char *tmp = NULL;
    13881305    int retval = 0;
    13891306
    1390     malloc_string(tmp);
    13911307    if ((fout = fopen(outfile, "w"))) {
    13921308        fprintf(fout,
     
    13941310        paranoid_fclose(fout);
    13951311        mr_msg(2, "Created %s", outfile);
    1396         sprintf(tmp, "chmod +x %s", outfile);
     1312        mr_asprintf(&tmp, "chmod +x %s", outfile);
    13971313        paranoid_system(tmp);
     1314        mr_free(tmp);
     1315
    13981316        retval = 0;
    13991317    } else {
    14001318        retval = 1;
    14011319    }
    1402     mr_free(tmp);
    14031320    return (retval);
    14041321}
Note: See TracChangeset for help on using the changeset viewer.