Changeset 2275 in MondoRescue


Ignore:
Timestamp:
Jul 13, 2009, 2:34:31 AM (15 years ago)
Author:
Bruno Cornec
Message:

r3261@localhost: bruno | 2009-07-13 00:27:16 +0200
Replace sprintf by mr_asprintf in libmondo-fork.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.10/mondo/src/common/libmondo-fork.c

    r2226 r2275  
    101101
    102102    /*@ buffers      *** */
    103     char *midway_call, *ultimate_call, *tmp, *command, *incoming,
    104         *old_stderr, *cd_number_str;
     103    char *midway_call, *ultimate_call, *tmp, *incoming, *old_stderr;
     104       
     105    char *cd_number_str = NULL;
     106    char *command = NULL;
    105107    char *p;
    106108    char *tmp1 = NULL;
     109    char *tmp2 = NULL;
    107110
    108111/*@***********   End Variables ***************************************/
     
    123126        fatal_error("Cannot malloc tmp");
    124127    }
    125     if (!(command = malloc(1200))) {
    126         fatal_error("Cannot malloc command");
    127     }
    128128    malloc_string(incoming);
    129129    malloc_string(old_stderr);
    130     malloc_string(cd_number_str);
    131130
    132131    incoming[0] = '\0';
     
    139138    }
    140139
    141     sprintf(cd_number_str, "%d", cd_no);
     140    mr_asprintf(&cd_number_str, "%d", cd_no);
    142141    resolve_naff_tokens(midway_call, tmp1, isofile, "_ISO_");
    143142    resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
     143    mr_free(cd_number_str);
     144
    144145    resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
    145146    log_msg(4, "basic call = '%s'", tmp1);
     
    147148    log_msg(4, "tmp = '%s'", tmp);
    148149    log_msg(4, "ultimate call = '%s'", ultimate_call);
    149     sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
     150    mr_asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
    150151
    151152    log_to_screen
     
    153154    log_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
    154155            what_i_am_doing);
    155     strcpy(tmp, command);
    156156    if (bkpinfo->manual_cd_tray) {
    157         p = strstr(tmp, "2>>");
     157        mr_asprintf(&tmp2, "%s", command);
     158        p = strstr(tmp2, "2>>");
    158159        if (p) {
    159             sprintf(p, "   ");
     160            *p++ = ' ';
     161            *p++ = ' ';
     162            *p++ = ' ';
    160163            while (*p == ' ') {
    161164                p++;
    162165            }
    163             for (; *p != ' '; p++) {
     166            for (; (*p != ' ') && (*p != '\0'); p++) {
    164167                *p = ' ';
    165168            }
    166169        }
    167         strcpy(command, tmp);
     170        mr_free(command);
     171        command = tmp2;
    168172#ifndef _XWIN
    169173        if (!g_text_mode) {
     
    179183            log_msg(2, "Basic call '%s' returned an error.", tmp1);
    180184            popup_and_OK("Press ENTER to continue.");
    181             popup_and_OK
    182                 ("mkisofs and/or cdrecord returned an error. CD was not created");
     185            popup_and_OK("mkisofs and/or cdrecord returned an error. CD was not created");
    183186        }
    184187    }
     
    186189    else {
    187190        log_msg(3, "command = '%s'", command);
    188 //      yes_this_is_a_goto:
    189         retval =
    190             run_external_binary_with_percentage_indicator_NEW
    191             (what_i_am_doing, command);
    192     }
    193 
     191        retval = run_external_binary_with_percentage_indicator_NEW(what_i_am_doing, command);
     192    }
     193    mr_free(command);
    194194    mr_free(tmp1);
    195195    paranoid_free(midway_call);
    196196    paranoid_free(ultimate_call);
    197197    paranoid_free(tmp);
    198     paranoid_free(command);
    199198    paranoid_free(incoming);
    200199    paranoid_free(old_stderr);
    201     paranoid_free(cd_number_str);
    202200    return (retval);
    203201}
     
    256254{
    257255    /*@ buffer ****************************************************** */
    258     char callstr[MAX_STR_LEN * 2];
     256    char *callstr = NULL;
    259257    char incoming[MAX_STR_LEN * 2];
    260258    char tmp[MAX_STR_LEN * 2];
     259    char *tmp1 = NULL;
    261260    char initial_label[MAX_STR_LEN * 2];
    262261
     
    279278        return (1);
    280279    }
    281 //  if (debug_level == TRUE) { debug_level=5; }
    282 
    283     //  assert_string_is_neither_NULL_nor_zerolength(program);
    284280
    285281    if (debug_level <= g_loglevel) {
     
    287283        log_if_failure = TRUE;
    288284    }
    289     sprintf(callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err",
     285    mr_asprintf(&callstr, "%s > %s/mondo-run-prog-thing.tmp 2> %s/mondo-run-prog-thing.err",
    290286             program, bkpinfo->tmpdir, bkpinfo->tmpdir);
    291287    while ((p = strchr(callstr, '\r'))) {
     
    315311                "--------------------------------start of output-----------------------------");
    316312    }
    317     sprintf(callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
     313    mr_free(callstr);
     314
     315    mr_asprintf(&callstr, "cat %s/mondo-run-prog-thing.err >> %s/mondo-run-prog-thing.tmp 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
    318316    if (log_if_failure && system(callstr)) {
    319317        log_OS_error("Command failed");
    320318    }
    321     sprintf(tmp, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
    322     unlink(tmp);
    323     sprintf(tmp, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
    324     fin = fopen(tmp, "r");
     319    mr_free(callstr);
     320
     321    mr_asprintf(&tmp1, "%s/mondo-run-prog-thing.err", bkpinfo->tmpdir);
     322    unlink(tmp1);
     323    mr_free(tmp1);
     324
     325    mr_asprintf(&tmp1, "%s/mondo-run-prog-thing.tmp", bkpinfo->tmpdir);
     326    fin = fopen(tmp1, "r");
    325327    if (fin) {
    326328        for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
    327329             fgets(incoming, MAX_STR_LEN, fin)) {
    328             /* patch by Heiko Schlittermann */
    329330            p = incoming;
    330331            while (p && *p) {
     
    334335                }
    335336            }
    336             /* end of patch */
    337337            strip_spaces(incoming);
    338338            if ((res == 0 && log_if_success)
     
    343343        paranoid_fclose(fin);
    344344    }
    345     unlink(tmp);
     345    unlink(tmp1);
     346    mr_free(tmp1);
     347
    346348    if ((res == 0 && log_if_success) || (res != 0 && log_if_failure)) {
    347349        log_msg(0,
     
    353355        }
    354356    }
    355 //  else
    356 //    { log_msg (0, "-------------------------------ran w/ res=%d------------------------------", res); }
    357357    return (res);
    358358}
     
    379379
    380380    /*@ buffers **************************************************** */
    381     char tmp[MAX_STR_LEN * 2];
    382     char command[MAX_STR_LEN * 2];
    383     char lockfile[MAX_STR_LEN];
     381    char *tmp = NULL;
     382    char *command = NULL;
     383    char *lockfile = NULL;
     384    char tmp1[MAX_STR_LEN *2];
    384385
    385386    /*@ end vars *************************************************** */
     
    387388    assert_string_is_neither_NULL_nor_zerolength(basic_call);
    388389
    389     sprintf(lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
    390 
    391     sprintf(command,
     390    mr_asprintf(&lockfile, "%s/mojo-jojo.bla.bla", bkpinfo->tmpdir);
     391
     392    mr_asprintf(&command,
    392393            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
    393394            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
    394395    open_evalcall_form(what_i_am_doing);
    395     sprintf(tmp, "Executing %s", basic_call);
     396    mr_asprintf(&tmp, "Executing %s", basic_call);
    396397    log_msg(2, tmp);
     398    mr_free(tmp);
     399
    397400    if (!(fin = popen(command, "r"))) {
    398401        log_OS_error("Unable to popen-in command");
    399         sprintf(tmp, "Failed utterly to call '%s'", command);
     402        mr_asprintf(&tmp, "Failed utterly to call '%s'", command);
    400403        log_to_screen(tmp);
     404        mr_free(tmp);
     405        mr_free(command);
     406        mr_free(lockfile);
    401407        return (1);
    402408    }
     409    mr_free(command);
     410
    403411    if (!does_file_exist(lockfile)) {
    404412        log_to_screen("Waiting for external binary to start");
     
    412420    while (does_file_exist(lockfile)) {
    413421        while (!feof(fin)) {
    414             if (!fgets(tmp, 512, fin))
     422            if (!fgets(tmp1, 512, fin))
    415423                break;
    416             log_to_screen(tmp);
     424            log_to_screen(tmp1);
    417425        }
    418426        usleep(500000);
     
    439447    close_evalcall_form();
    440448    unlink(lockfile);
     449    mr_free(lockfile);
     450
    441451    return (retval);
    442452}
     
    458468// if dir=='w' then copy from orig to archived
    459469// if dir=='r' then copy from archived to orig
    460     char *tmp;
    461     char *buf;
    462     char filestr[MAX_STR_LEN];
     470    char *tmp = NULL;
     471    char *tmp1 = NULL;
     472    char *buf = NULL;
     473    char *filestr = NULL;
    463474    long int bytes_to_be_read, bytes_read_in, bytes_written_out =
    464475        0, bufcap, subsliceno = 0;
     
    470481
    471482    log_msg(5, "Opening.");
    472     if (!(tmp = malloc(tmpcap))) {
    473         fatal_error("Failed to malloc() tmp");
    474     }
    475     tmp[0] = '\0';
     483
    476484    bufcap = 256L * 1024L;
    477485    if (!(buf = malloc(bufcap))) {
     
    482490        fin = f_orig;
    483491        fout = f_archived;
    484         sprintf(tmp, "%-64s", PIMP_START_SZ);
     492        mr_asprintf(&tmp, "%-64s", PIMP_START_SZ);
    485493        if (fwrite(tmp, 1, 64, fout) != 64) {
     494            mr_free(tmp);
    486495            fatal_error("Can't write the introductory block");
    487496        }
     497        mr_free(tmp);
     498
    488499        while (1) {
    489500            bytes_to_be_read = bytes_read_in = fread(buf, 1, bufcap, fin);
     
    491502                break;
    492503            }
    493             sprintf(tmp, "%-64ld", bytes_read_in);
     504            mr_asprintf(&tmp, "%-64ld", bytes_read_in);
    494505            if (fwrite(tmp, 1, 64, fout) != 64) {
     506                mr_free(tmp);
    495507                fatal_error("Cannot write introductory block");
    496508            }
     509            mr_free(tmp);
     510
    497511            log_msg(7,
    498512                    "subslice #%ld --- I have read %ld of %ld bytes in from f_orig",
    499513                    subsliceno, bytes_read_in, bytes_to_be_read);
    500514            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
    501             sprintf(tmp, "%-64ld", subsliceno);
     515            mr_asprintf(&tmp, "%-64ld", subsliceno);
    502516            if (fwrite(tmp, 1, 64, fout) != 64) {
     517                mr_free(tmp);
    503518                fatal_error("Cannot write post-thingy block");
    504519            }
     520            mr_free(tmp);
    505521            log_msg(7, "Subslice #%d written OK", subsliceno);
    506522            subsliceno++;
    507523        }
    508         sprintf(tmp, "%-64ld", 0L);
     524        mr_asprintf(&tmp, "%-64ld", 0L);
    509525        if (fwrite(tmp, 1, 64L, fout) != 64L) {
     526            mr_free(tmp);
    510527            fatal_error("Cannot write final introductory block");
    511528        }
     529        mr_free(tmp);
     530
     531        mr_asprintf(&tmp, "%-64s", PIMP_END_SZ);
     532        if (fwrite(tmp, 1, 64, fout) != 64) {
     533            mr_free(tmp);
     534            fatal_error("Can't write the final block");
     535        }
     536        mr_free(tmp);
    512537    } else {
    513538        fin = f_archived;
    514539        fout = f_orig;
    515         if (fread(tmp, 1, 64L, fin) != 64L) {
     540        if (!(tmp1 = malloc(tmpcap))) {
     541            fatal_error("Failed to malloc() tmp");
     542        }
     543        if (fread(tmp1, 1, 64L, fin) != 64L) {
     544            mr_free(tmp1);
    516545            fatal_error("Cannot read the introductory block");
    517546        }
    518         log_msg(5, "tmp is %s", tmp);
    519         if (!strstr(tmp, PIMP_START_SZ)) {
     547        log_msg(5, "tmp1 is %s", tmp1);
     548        if (!strstr(tmp1, PIMP_START_SZ)) {
     549            mr_free(tmp1);
    520550            fatal_error("Can't find intro blk");
    521551        }
    522         if (fread(tmp, 1, 64L, fin) != 64L) {
     552        if (fread(tmp1, 1, 64L, fin) != 64L) {
     553            mr_free(tmp1);
    523554            fatal_error("Cannot read introductory blk");
    524555        }
    525         bytes_to_be_read = atol(tmp);
     556        bytes_to_be_read = atol(tmp1);
    526557        while (bytes_to_be_read > 0) {
    527             log_msg(7, "subslice#%ld, bytes=%ld", subsliceno,
    528                     bytes_to_be_read);
     558            log_msg(7, "subslice#%ld, bytes=%ld", subsliceno, bytes_to_be_read);
    529559            bytes_read_in = fread(buf, 1, bytes_to_be_read, fin);
    530560            if (bytes_read_in != bytes_to_be_read) {
    531                 fatal_error
    532                     ("Danger, WIll Robinson. Failed to read whole subvol from archives.");
     561                mr_free(tmp1);
     562                fatal_error("Danger, WIll Robinson. Failed to read whole subvol from archives.");
    533563            }
    534564            bytes_written_out += fwrite(buf, 1, bytes_read_in, fout);
    535             if (fread(tmp, 1, 64, fin) != 64) {
     565            if (fread(tmp1, 1, 64, fin) != 64) {
     566                mr_free(tmp1);
    536567                fatal_error("Cannot read post-thingy block");
    537568            }
    538             if (atol(tmp) != subsliceno) {
    539                 log_msg(1, "Wanted subslice %ld but got %ld ('%s')",
    540                         subsliceno, atol(tmp), tmp);
     569            if (atol(tmp1) != subsliceno) {
     570                log_msg(1, "Wanted subslice %ld but got %ld ('%s')", subsliceno, atol(tmp1), tmp1);
    541571            }
    542572            log_msg(7, "Subslice #%ld read OK", subsliceno);
    543573            subsliceno++;
    544             if (fread(tmp, 1, 64, fin) != 64) {
     574            if (fread(tmp1, 1, 64, fin) != 64) {
     575                mr_free(tmp1);
    545576                fatal_error("Cannot read introductory block");
    546577            }
    547             bytes_to_be_read = atol(tmp);
    548         }
    549     }
    550 
    551 //  log_msg(4, "Written %ld of %ld bytes", bytes_written_out, bytes_read_in);
    552 
    553     if (direction == 'w') {
    554         sprintf(tmp, "%-64s", PIMP_END_SZ);
    555         if (fwrite(tmp, 1, 64, fout) != 64) {
    556             fatal_error("Can't write the final block");
    557         }
    558     } else {
    559         log_msg(1, "tmpA is %s", tmp);
    560         if (!strstr(tmp, PIMP_END_SZ)) {
    561             if (fread(tmp, 1, 64, fin) != 64) {
     578            bytes_to_be_read = atol(tmp1);
     579        }
     580        log_msg(1, "tmpA is %s", tmp1);
     581        if (!strstr(tmp1, PIMP_END_SZ)) {
     582            if (fread(tmp1, 1, 64, fin) != 64) {
     583                mr_free(tmp1);
    562584                fatal_error("Can't read the final block");
    563585            }
    564             log_msg(5, "tmpB is %s", tmp);
    565             if (!strstr(tmp, PIMP_END_SZ)) {
    566                 sprintf(filestr, "%s/out.leftover", bkpinfo->tmpdir);
     586            log_msg(5, "tmpB is %s", tmp1);
     587            if (!strstr(tmp1, PIMP_END_SZ)) {
     588                mr_asprintf(&filestr, "%s/out.leftover", bkpinfo->tmpdir);
    567589                ftmp = fopen(filestr, "w");
    568                 bytes_read_in = fread(tmp, 1, 64, fin);
     590                mr_free(filestr);
     591
     592                bytes_read_in = fread(tmp1, 1, 64, fin);
    569593                log_msg(1, "bytes_read_in = %ld", bytes_read_in);
    570 //      if (bytes_read_in!=128+64) { fatal_error("Can't read the terminating block"); }
    571                 fwrite(tmp, 1, bytes_read_in, ftmp);
    572                 sprintf(tmp, "I am here - %lld", (long long)ftello(fin));
    573 //    log_msg(0, tmp);
    574                 fread(tmp, 1, tmpcap, fin);
    575                 log_msg(0, "tmp = '%s'", tmp);
    576                 fwrite(tmp, 1, tmpcap, ftmp);
     594
     595                fwrite(tmp1, 1, bytes_read_in, ftmp);
     596                fread(tmp1, 1, tmpcap, fin);
     597                log_msg(0, "tmp1 = '%s'", tmp1);
     598                fwrite(tmp1, 1, tmpcap, ftmp);
    577599                fclose(ftmp);
     600                mr_free(tmp1);
    578601                fatal_error("Missing terminating block");
    579602            }
    580603        }
     604        mr_free(tmp1);
    581605    }
    582606
    583607    paranoid_free(buf);
    584     paranoid_free(tmp);
    585608    log_msg(3, "Successfully copied %ld bytes", bytes_written_out);
    586609    return (retval);
     
    600623// BACKUP
    601624    int res = -1;
    602     char*command;
     625    char*command = NULL;
    603626
    604627    if (!does_file_exist(input_device)) {
     
    608631        fatal_error("ntfsclone not found");
    609632    }
    610     malloc_string(command);
    611     sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
     633    mr_asprintf(&command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
    612634    res = run_program_and_log_output(command, 5);
    613     paranoid_free(command);
     635    mr_free(command);
     636
    614637    unlink(output_fname);
    615638    return (res);
     
    649672
    650673    /*@ buffers *********************************************************** */
    651     char *command;
     674    char *command = NULL;
    652675    char *title;
    653676    /*@ pointers ********************************************************** */
     
    662685
    663686    malloc_string(title);
    664     malloc_string(command);
    665687    strcpy(title, tt);
    666     sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
     688    mr_asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
    667689    log_msg(3, "command = '%s'", command);
    668     if ((res =
    669          pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit,
    670                         (void *) command))) {
     690    if ((res = pthread_create(&childthread, NULL, run_prog_in_bkgd_then_exit, (void *) command))) {
    671691        fatal_error("Unable to create an archival thread");
    672692    }
     
    693713        update_evalcall_form(percentage);
    694714    }
     715    mr_free(command);
     716
    695717    log_file_end_to_screen(MONDO_LOGFILE, "");
    696718    close_evalcall_form();
     
    702724    }
    703725    log_msg(3, "Parent res = %d", res);
    704     paranoid_free(command);
    705726    paranoid_free(title);
    706727    return (res);
     
    720741// RESTORE
    721742    int res = -1;
    722     char *command;
     743    char *command = NULL;
    723744
    724745    if ( !find_home_of_exe("ntfsclone")) {
    725746        fatal_error("ntfsclone not found");
    726747    }
    727     malloc_string(command);
    728     sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
     748    mr_asprintf(&command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
    729749    res = run_program_and_log_output(command, 5);
    730     paranoid_free(command);
     750    mr_free(command);
    731751    return (res);
    732752}
Note: See TracChangeset for help on using the changeset viewer.