Ignore:
Timestamp:
Feb 8, 2007, 3:08:10 AM (13 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-fork.c

    r1107 r1113  
    1 /* libmondo-fork.c
    2    $Id$
    3 */
    4 
    5 
     1/* libmondo-fork.c - subroutines for handling forking/pthreads/etc.
     2 * $Id$
     3 */
    64#include "my-stuff.h"
    75#include "mr_mem.h"
     
    97#include "libmondo-fork.h"
    108#include "libmondo-string-EXT.h"
    11 #include "libmondo-gui-EXT.h"
     9#include "newt-specific-EXT.h"
    1210#include "libmondo-files-EXT.h"
    1311#include "libmondo-tools-EXT.h"
    14 #include "lib-common-externs.h"
     12#include "mr_mem.h"
    1513
    1614/*@unused@*/
     
    9694
    9795    /*@ buffers      *** */
    98     char *midway_call, *ultimate_call, *tmp, *command, *incoming,
    99         *old_stderr, *cd_number_str;
    100     char *p;
     96    char *midway_call, *ultimate_call, *tmp;
     97    char *p = NULL;
     98    char *cd_number_str = NULL;
     99    char *command = NULL;
     100   
    101101
    102102/*@***********   End Variables ***************************************/
     
    107107    assert_string_is_neither_NULL_nor_zerolength(isofile);
    108108    assert_string_is_neither_NULL_nor_zerolength(logstub);
     109
    109110    midway_call = mr_malloc(1200);
    110111    ultimate_call = mr_malloc(1200);
    111112    tmp = mr_malloc(1200);
    112     command = mr_malloc(1200);
    113     malloc_string(incoming);
    114     malloc_string(old_stderr);
    115     malloc_string(cd_number_str);
    116 
    117     incoming[0] = '\0';
    118     old_stderr[0] = '\0';
    119 
    120     sprintf(cd_number_str, "%d", cd_no);
     113
     114    mr_asprintf(&cd_number_str, "%d", cd_no);
    121115    resolve_naff_tokens(midway_call, basic_call, isofile, "_ISO_");
    122116    resolve_naff_tokens(tmp, midway_call, cd_number_str, "_CD#_");
     117    mr_free(cd_number_str);
     118
    123119    resolve_naff_tokens(ultimate_call, tmp, MONDO_LOGFILE, "_ERR_");
    124120    mr_msg(4, "basic call = '%s'", basic_call);
     
    126122    mr_msg(4, "tmp = '%s'", tmp);
    127123    mr_msg(4, "ultimate call = '%s'", ultimate_call);
    128     sprintf(command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
     124    mr_asprintf(&command, "%s >> %s", ultimate_call, MONDO_LOGFILE);
    129125
    130126    log_to_screen
    131         ("Please be patient. Do not be alarmed by on-screen inactivity.");
     127        (_("Please be patient. Do not be alarmed by on-screen inactivity."));
    132128    mr_msg(4, "Calling open_evalcall_form() with what_i_am_doing='%s'",
    133129            what_i_am_doing);
    134     strcpy(tmp, command);
    135130    if (bkpinfo->manual_cd_tray) {
    136         p = strstr(tmp, "2>>");
     131        /* Find everything after a 2>> and remove it */
     132        p = strstr(command, "2>>");
    137133        if (p) {
    138             sprintf(p, "   ");
    139             while (*p == ' ') {
    140                 p++;
    141             }
    142             for (; *p != ' '; p++) {
     134            for (; *p != ' ' && *p != '\0'; p++) {
    143135                *p = ' ';
    144136            }
    145137        }
    146         strcpy(command, tmp);
    147138#ifndef _XWIN
    148139        if (!g_text_mode) {
     
    157148        if (retval) {
    158149            mr_msg(2, "Basic call '%s' returned an error.", basic_call);
    159             popup_and_OK("Press ENTER to continue.");
     150            popup_and_OK(_("Press ENTER to continue."));
    160151            popup_and_OK
    161                 ("mkisofs and/or cdrecord returned an error. CD was not created");
     152                (_("mkisofs and/or cdrecord returned an error. CD was not created"));
    162153        }
    163154    }
     
    170161            (what_i_am_doing, command);
    171162    }
     163    mr_free(command);
    172164
    173165    mr_free(midway_call);
    174166    mr_free(ultimate_call);
    175167    mr_free(tmp);
    176     mr_free(command);
    177     mr_free(incoming);
    178     mr_free(old_stderr);
    179     mr_free(cd_number_str);
    180 /*
    181   if (bkpinfo->backup_media_type == dvd && !bkpinfo->please_dont_eject_when_restoring)
    182     {
    183       mr_msg(3, "Ejecting DVD device");
    184       eject_device(bkpinfo->media_device);
    185     }
    186 */
    187168    return (retval);
    188169}
    189 
    190 
    191170
    192171
     
    200179{
    201180    /*@ buffer ****************************************************** */
    202     char callstr[MAX_STR_LEN * 2];
    203     char incoming[MAX_STR_LEN * 2];
    204     char tmp[MAX_STR_LEN * 2];
    205     char initial_label[MAX_STR_LEN * 2];
     181    char *callstr = NULL;
     182    char *incoming = NULL;
    206183
    207184    /*@ int ********************************************************* */
    208     int res;
     185    int res = 0;
     186    size_t n = 0;
    209187    int i;
    210188    int len;
     
    223201        return (1);
    224202    }
    225 //  if (debug_level == TRUE) { debug_level=5; }
    226 
    227     //  assert_string_is_neither_NULL_nor_zerolength(program);
    228203
    229204    if (debug_level <= g_loglevel) {
     
    231206        log_if_failure = TRUE;
    232207    }
    233     sprintf(callstr,
     208    mr_asprintf(&callstr,
    234209            "%s > /tmp/mondo-run-prog-thing.tmp 2> /tmp/mondo-run-prog-thing.err",
    235210            program);
     
    260235                "--------------------------------start of output-----------------------------");
    261236    }
     237    mr_free(callstr);
     238
    262239    if (log_if_failure
    263240        &&
     
    270247    fin = fopen("/tmp/mondo-run-prog-thing.tmp", "r");
    271248    if (fin) {
    272         for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
    273              fgets(incoming, MAX_STR_LEN, fin)) {
     249        for (mr_getline(&incoming, &n, fin); !feof(fin);
     250             mr_getline(&incoming, &n, fin)) {
    274251            /* patch by Heiko Schlittermann */
    275252            p = incoming;
     
    287264            }
    288265        }
     266        mr_free(incoming);
    289267        paranoid_fclose(fin);
    290268    }
     
    303281    return (res);
    304282}
    305 
    306283
    307284
     
    325302
    326303    /*@ buffers **************************************************** */
    327     char tmp[MAX_STR_LEN * 2];
    328     char command[MAX_STR_LEN * 2];
    329     char lockfile[MAX_STR_LEN];
     304    char *tmp = NULL;
     305    char *command = NULL;
     306    char *lockfile = NULL;
    330307
    331308    /*@ end vars *************************************************** */
     
    333310    assert_string_is_neither_NULL_nor_zerolength(basic_call);
    334311
    335     sprintf(lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
     312    mr_asprintf(&lockfile, "/tmp/mojo-jojo.blah.XXXXXX");
    336313    mkstemp(lockfile);
    337     sprintf(command,
     314    mr_asprintf(&command,
    338315            "echo hi > %s ; %s >> %s 2>> %s; res=$?; sleep 1; rm -f %s; exit $res",
    339316            lockfile, basic_call, MONDO_LOGFILE, MONDO_LOGFILE, lockfile);
    340317    open_evalcall_form(what_i_am_doing);
    341     sprintf(tmp, "Executing %s", basic_call);
     318    mr_asprintf(&tmp, "Executing %s", basic_call);
    342319    mr_msg(2, tmp);
     320    mr_free(tmp);
     321
    343322    if (!(fin = popen(command, "r"))) {
    344323        log_OS_error("Unable to popen-in command");
    345         sprintf(tmp, "Failed utterly to call '%s'", command);
     324        mr_asprintf(&tmp, _("Failed utterly to call '%s'"), command);
    346325        log_to_screen(tmp);
     326        mr_free(tmp);
     327        mr_free(lockfile);
     328        mr_free(command);
    347329        return (1);
    348330    }
     331    mr_free(command);
     332
    349333    if (!does_file_exist(lockfile)) {
    350         log_to_screen("Waiting for external binary to start");
     334        log_to_screen(_("Waiting for external binary to start"));
    351335        for (i = 0; i < 60 && !does_file_exist(lockfile); sleep(1), i++) {
    352336            mr_msg(3, "Waiting for lockfile %s to exist", lockfile);
     
    375359    res = pclose(fin);
    376360    /* Log actual pclose errors. */
    377     if (errno) mr_msg(5, "pclose err: %d", errno);
     361    if (errno) {
     362        mr_msg(5, "pclose err: %d", errno);
     363    }
    378364    /* Check if we have a valid status. If we do, extract the called program's exit code. */
    379365    /* If we don't, highlight this fact by returning -1. */
     
    385371    close_evalcall_form();
    386372    unlink(lockfile);
     373    mr_free(lockfile);
     374
    387375    return (retval);
    388376}
    389377
    390378
    391 
    392 
    393379/**
    394  * Apparently unused. @bug This has a purpose, but what?
     380 * Apparently used. @bug This has a purpose, but what?
    395381 */
    396382#define PIMP_START_SZ "STARTSTARTSTART9ff3kff9a82gv34r7fghbkaBBC2T231hc81h42vws8"
     
    404390// if dir=='w' then copy from orig to archived
    405391// if dir=='r' then copy from archived to orig
    406     char *tmp;
    407     char *buf;
     392    char *tmp = NULL;
     393    char *buf = NULL;
    408394    long int bytes_to_be_read, bytes_read_in, bytes_written_out =
    409395        0, bufcap, subsliceno = 0;
    410396    int retval = 0;
    411     FILE *fin;
    412     FILE *fout;
    413     FILE *ftmp;
     397    FILE *fin = NULL;
     398    FILE *fout = NULL;
     399    FILE *ftmp = NULL;
    414400
    415401    mr_msg(5, "Opening.");
     
    526512}
    527513
    528 
    529 
    530 
    531514/**
    532515 * Feed @p input_device through ntfsclone to @p output_fname.
     
    539522// BACKUP
    540523    int res = -1;
    541     char*command;
     524    char *command = NULL;
    542525
    543526    if (!does_file_exist(input_device)) {
     
    547530        fatal_error("ntfsclone not found");
    548531    }
    549     malloc_string(command);
    550     sprintf(command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
     532    mr_asprintf(&command, "ntfsclone --force --save-image --overwrite %s %s", output_fname, input_device);
    551533    res = run_program_and_log_output(command, 5);
    552534    mr_free(command);
     535
    553536    unlink(output_fname);
    554537    return (res);
     
    556539
    557540
    558 
    559 
    560 
    561541int run_external_binary_with_percentage_indicator_OLD(char *tt, char *cmd)
    562542{
    563543
    564     /*@ int *************************************************************** */
    565544    int res = 0;
    566545    int percentage = 0;
     
    569548    int last_pcno = 0;
    570549
    571     /*@ buffers *********************************************************** */
    572     char *command;
     550    char *command = NULL;
    573551    char *tempfile;
    574552    char *title;
     
    577555
    578556    malloc_string(title);
    579     malloc_string(command);
    580557    malloc_string(tempfile);
    581558    assert_string_is_neither_NULL_nor_zerolength(cmd);
     
    586563           call_program_and_get_last_line_of_output
    587564           ("mktemp -q /tmp/mondo.XXXXXXXX"));
    588     sprintf(command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
     565    mr_asprintf(&command, "%s >> %s 2>> %s; rm -f %s", cmd, tempfile, tempfile,
    589566            tempfile);
    590567    mr_msg(3, command);
     
    592569    if (!(pin = popen(command, "r"))) {
    593570        log_OS_error("fmt err");
     571        mr_free(command);
    594572        return (1);
    595573    }
     574    mr_free(command);
     575
    596576    maxpc = 100;
    597577// OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
     
    618598    }
    619599    unlink(tempfile);
    620     mr_free(command);
    621600    mr_free(tempfile);
    622601    mr_free(title);
    623602    return (res);
    624603}
    625 
    626 
    627604
    628605
     
    643620    pthread_exit((void *) (&res));
    644621}
    645 
    646 
    647622
    648623
     
    659634
    660635    /*@ buffers *********************************************************** */
    661     char *command;
     636    char *command = NULL;
    662637    char *title;
    663638    /*@ pointers ********************************************************** */
     
    672647
    673648    malloc_string(title);
    674     malloc_string(command);
    675649    strcpy(title, tt);
    676     sprintf(command, "%s 2>> %s", cmd, MONDO_LOGFILE);
     650    mr_asprintf(&command, "%s 2>> %s", cmd, MONDO_LOGFILE);
    677651    mr_msg(3, "command = '%s'", command);
    678652    if ((res =
     
    703677        update_evalcall_form(percentage);
    704678    }
     679    mr_free(command);
     680
    705681    log_file_end_to_screen(MONDO_LOGFILE, "");
    706682    close_evalcall_form();
     
    712688    }
    713689    mr_msg(3, "Parent res = %d", res);
    714     mr_free(command);
    715690    mr_free(title);
    716691    return (res);
    717692}
    718 
    719 
    720693
    721694
     
    730703// RESTORE
    731704    int res = -1;
    732     char *command;
     705    char *command = NULL;
    733706
    734707    if ( !find_home_of_exe("ntfsclone")) {
    735708        fatal_error("ntfsclone not found");
    736709    }
    737     malloc_string(command);
    738     sprintf(command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
     710    mr_asprintf(&command, "ntfsclone --force --restore-image --overwrite %s %s", output_device, input_fifo);
    739711    res = run_program_and_log_output(command, 5);
    740712    mr_free(command);
Note: See TracChangeset for help on using the changeset viewer.