Changeset 146 in MondoRescue for trunk/mondo/mondo/common/libmondo-devices.c


Ignore:
Timestamp:
Dec 1, 2005, 10:00:14 AM (18 years ago)
Author:
bcornec
Message:

MONDO_LOGFILE used each time now
memory management on libmondo-devices.c
some splint improvements

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/mondo/common/libmondo-devices.c

    r133 r146  
    1919#include "libmondo-stream-EXT.h"
    2020
     21#include <sys/ioctl.h>
    2122#include <sys/types.h>
    2223#ifdef __FreeBSD__
     
    104105    bool is_this_a_ramdisk = FALSE;
    105106
    106     malloc_string(tmp);
    107     malloc_string(comment);
    108     strcpy(tmp, where_is_root_mounted());
    109     sprintf(comment, "root is mounted at %s\n", tmp);
     107    asprintf(&tmp, where_is_root_mounted());
     108    asprintf(&comment, "root is mounted at %s\n", tmp);
    110109    log_msg(0, comment);
     110    paranoid_free(comment);
     111
    111112    log_msg(0,
    112113            "No, Schlomo, that doesn't mean %s is the root partition. It's just a debugging message. Relax. It's part of am_I_in_disaster_recovery_mode().",
     
    127128    }
    128129#endif
     130    paranoid_free(tmp);
    129131
    130132    if (is_this_a_ramdisk) {
     
    139141        is_this_a_ramdisk = TRUE;
    140142    }
    141     paranoid_free(tmp);
    142     paranoid_free(comment);
    143143    log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk);
    144144    return (is_this_a_ramdisk);
    145145}
    146 
    147 
    148 
    149146
    150147
     
    157154static char *bkptype_to_string(t_bkptype bt)
    158155{
    159     static char output[MAX_STR_LEN / 4];
     156    static char *output = NULL;
     157
     158    paranoid_free(output);
     159
    160160    switch (bt) {
    161161    case none:
    162         strcpy(output, "none");
     162        asprintf(&output, "none");
    163163        break;
    164164    case iso:
    165         strcpy(output, "iso");
     165        asprintf(&output, "iso");
    166166        break;
    167167    case cdr:
    168         strcpy(output, "cdr");
     168        asprintf(&output, "cdr");
    169169        break;
    170170    case cdrw:
    171         strcpy(output, "cdrw");
     171        asprintf(&output, "cdrw");
    172172        break;
    173173    case cdstream:
    174         strcpy(output, "cdstream");
     174        asprintf(&output, "cdstream");
    175175        break;
    176176    case nfs:
    177         strcpy(output, "nfs");
     177        asprintf(&output, "nfs");
    178178        break;
    179179    case tape:
    180         strcpy(output, "tape");
     180        asprintf(&output, "tape");
    181181        break;
    182182    case udev:
    183         strcpy(output, "udev");
     183        asprintf(&output, "udev");
    184184        break;
    185185    default:
    186         strcpy(output, "default");
    187     }
    188     return (output);
    189 }
    190 
     186        asprintf(&output, "default");
     187    }
     188    return (&output);
     189}
    191190
    192191
     
    205204    int res1 = 0, res2 = 0;
    206205
    207     malloc_string(command);
    208 
    209206    if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
    210207        && g_backup_media_type != udev) {
    211         sprintf(command, "mt -f %s offline", dev);
     208        asprintf(&command, "mt -f %s offline", dev);
    212209        res1 = run_program_and_log_output(command, 1);
     210        paranoid_free(command);
    213211    } else {
    214212        res1 = 0;
     
    217215#ifdef __FreeBSD__
    218216    if (strstr(dev, "acd")) {
    219         sprintf(command, "cdcontrol -f %s eject", dev);
    220     } else {
    221         sprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`",
     217        asprintf(&command, "cdcontrol -f %s eject", dev);
     218    } else {
     219        asprintf(&command, "camcontrol eject `echo %s | sed 's|/dev/||'`",
    222220                dev);
    223221    }
    224222#else
    225     sprintf(command, "eject %s", dev);
     223    asprintf(&command, "eject %s", dev);
    226224#endif
    227225
     
    236234}
    237235
     236
    238237/**
    239238 * Load (inject) the tray of the specified CD device.
     
    246245    int i;
    247246
    248     malloc_string(command);
    249 
    250 
    251247#ifdef __FreeBSD__
    252248    if (strstr(dev, "acd")) {
    253         sprintf(command, "cdcontrol -f %s close", dev);
    254     } else {
    255         sprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`",
     249        asprintf(&command, "cdcontrol -f %s close", dev);
     250    } else {
     251        asprintf(&command, "camcontrol load `echo %s | sed 's|/dev/||'`",
    256252                dev);
    257253    }
    258254#else
    259     sprintf(command, "eject -t %s", dev);
     255    asprintf(&command, "eject -t %s", dev);
    260256#endif
    261257    i = run_program_and_log_output(command, FALSE);
     
    275271    /*@ buffers *********************************************************** */
    276272    char *tmp;
    277 
    278     malloc_string(tmp);
     273    bool ret;
     274
    279275    assert_string_is_neither_NULL_nor_zerolength(device);
    280276
    281     sprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
    282 
     277    asprintf(&tmp, "ls %s > /dev/null 2> /dev/null", device);
     278
     279    if (system(tmp)) {
     280        ret = FALSE;
     281    } else {
     282        ret = TRUE;
     283    }
    283284    paranoid_free(tmp);
    284     if (system(tmp)) {
    285         return (FALSE);
    286     } else {
    287         return (TRUE);
    288     }
     285    return(ret);
    289286}
    290287
     
    317314    /*@ buffers **************************************************** */
    318315    char *program;
    319     char *incoming;
     316    char *incoming = NULL;
    320317    char *searchstr;
    321     char *tmp;
    322318
    323319    /*@ ints ******************************************************* */
    324320    int res = 0;
     321    int n = 0;
    325322
    326323    /*@ pointers *************************************************** */
     
    332329    assert(partno >= 0 && partno < 999);
    333330
    334     malloc_string(program);
    335     malloc_string(incoming);
    336331    malloc_string(searchstr);
    337     malloc_string(tmp);
    338332
    339333#ifdef __FreeBSD__
    340334    // We assume here that this is running from mondorestore. (It is.)
    341     sprintf(program, "ls %s >/dev/null 2>&1", drive,
    342             build_partition_name(tmp, drive, partno));
    343     return system(program);
    344 #else
    345     tmp[0] = '\0';
    346 #endif
    347 
    348     sprintf(program, "parted2fdisk -l %s 2> /dev/null", drive);
     335    asprintf(&program, "ls %s >/dev/null 2>&1", drive);
     336    res = system(program);
     337    paranoid_free(program);
     338    return(res);
     339#endif
     340
     341    asprintf(&program, "parted2fdisk -l %s 2> /dev/null", drive);
    349342    fin = popen(program, "r");
    350343    if (!fin) {
    351344        log_it("program=%s", program);
    352345        log_OS_error("Cannot popen-in program");
     346        paranoid_free(program);
    353347        return (0);
    354348    }
     349    paranoid_free(program);
     350
    355351    (void) build_partition_name(searchstr, drive, partno);
    356352    strcat(searchstr, " ");
    357     for (res = 0; !res && fgets(incoming, MAX_STR_LEN - 1, fin);) {
     353    for (res = 0; !res && getline(&incoming, &n, fin);) {
    358354        if (strstr(incoming, searchstr)) {
    359355            res = 1;
    360356        }
    361357    }
     358    paranoid_free(incoming);
     359
    362360    if (pclose(fin)) {
    363361        log_OS_error("Cannot pclose fin");
    364362    }
    365     paranoid_free(program);
    366     paranoid_free(incoming);
    367363    paranoid_free(searchstr);
    368     paranoid_free(tmp);
    369364    return (res);
    370365}
    371 
    372 
    373 
    374366
    375367
     
    386378
    387379    /*@ end vars *************************************************** */
    388     int i;
     380    int ret;
    389381
    390382    assert_string_is_neither_NULL_nor_zerolength(dev);
    391383    assert_string_is_neither_NULL_nor_zerolength(str);
    392384
    393     malloc_string(command);
    394     sprintf(command,
     385    asprintf(&command,
    395386            "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
    396387            dev, str);
    397     i = system(command);
     388    ret = system(command);
    398389    paranoid_free(command);
    399     if (i) {
     390    if (ret) {
    400391        return (FALSE);
    401392    } else {
     
    403394    }
    404395}
     396
    405397
    406398/**
     
    416408    char *command;
    417409    /*@ end vars *************************************************** */
    418     int i;
    419 
    420     malloc_string(command);
    421     sprintf(command,
     410    int ret;
     411
     412    asprintf(&command,
    422413            "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
    423414            dev, n, str);
    424     i = system(command);
     415    ret = system(command);
    425416    paranoid_free(command);
    426     if (i) {
     417
     418    if (ret) {
    427419        return (FALSE);
    428420    } else {
     
    430422    }
    431423}
    432 
    433424
    434425
     
    490481
    491482
    492 
    493 
    494 
    495 /**
    496  * Locate a CD-R/W writer's SCSI node.
    497  * @param cdrw_device SCSI node will be placed here.
    498  * @return 0 for success, nonzero for failure.
    499  */
    500 
    501 
    502483/**
    503484 * Locate a CD-R/W writer's SCSI node.
     
    513494    char *command;
    514495
    515     malloc_string(comment);
    516     malloc_string(tmp);
    517     malloc_string(cdr_exe);
    518     malloc_string(command);
    519496    if (g_cdrw_drive_is_here[0]) {
    520497        strcpy(cdrw_device, g_cdrw_drive_is_here);
    521498        log_msg(3, "Been there, done that. Returning %s", cdrw_device);
    522         paranoid_free(comment);
    523         paranoid_free(tmp);
    524         paranoid_free(cdr_exe);
    525         paranoid_free(command);
    526499        return (0);
    527500    }
     
    529502        log_msg(1,
    530503                "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
    531         paranoid_free(comment);
    532         paranoid_free(tmp);
    533         paranoid_free(cdr_exe);
    534         paranoid_free(command);
    535504        return (1);
    536505    }
    537506    run_program_and_log_output("insmod ide-scsi", -1);
    538507    if (find_home_of_exe("cdrecord")) {
    539         strcpy(cdr_exe, "cdrecord");
    540     } else {
    541         strcpy(cdr_exe, "dvdrecord");
    542     }
    543     tmp[0] = '\0';
     508        asprintf(&cdr_exe, "cdrecord");
     509    } else {
     510        asprintf(&cdr_exe, "dvdrecord");
     511    }
    544512    if (find_home_of_exe(cdr_exe)) {
    545         sprintf(command,
     513        asprintf(&command,
    546514                "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1",
    547515                cdr_exe);
    548         strcpy(tmp, call_program_and_get_last_line_of_output(command));
    549     }
     516        asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     517        paranoid_free(command);
     518    } else {
     519        asprintf(&tmp, "");
     520    }
     521    paranoid_free(cdr_exe);
     522
    550523    if (strlen(tmp) < 2) {
     524        paranoid_free(tmp);
     525        return 1;
     526    } else {
     527        strcpy(cdrw_device, tmp);
     528        paranoid_free(tmp);
     529
     530        asprintf(&comment, "Found CDRW device - %s", cdrw_device);
     531        log_it(comment);
    551532        paranoid_free(comment);
    552         paranoid_free(tmp);
    553         paranoid_free(cdr_exe);
    554         paranoid_free(command);
    555         return 1;
    556     } else {
    557         strcpy(cdrw_device, tmp);
    558         sprintf(comment, "Found CDRW device - %s", cdrw_device);
    559         log_it(comment);
     533
    560534        strcpy(g_cdrw_drive_is_here, cdrw_device);
    561         paranoid_free(comment);
    562         paranoid_free(tmp);
    563         paranoid_free(cdr_exe);
    564         paranoid_free(command);
    565535        return (0);
    566536    }
    567537}
    568 
    569 
    570538
    571539
     
    586554    char *q;
    587555    char *r;
     556    int n = 0;
    588557    int retval = 0;
    589558
     
    592561
    593562    /*@ buffers ***************************************************** */
    594     char *tmp;
     563    char *tmp = NULL;
    595564    char *cdr_exe;
    596     char *phrase_one;
    597     char *phrase_two;
     565#ifndef __FreeBSD__
     566    char *phrase_two = NULL;
     567    char *dvd_last_resort = NULL;
     568#endif
    598569    char *command;
    599     char *dvd_last_resort;
    600570    char *mountpoint;
    601571    static char the_last_place_i_found_it[MAX_STR_LEN] = "";
    602572
    603573    /*@ intialize *************************************************** */
    604     malloc_string(tmp);
    605     malloc_string(cdr_exe);
    606     malloc_string(phrase_one);
    607     malloc_string(phrase_two);
    608     malloc_string(command);
    609     malloc_string(dvd_last_resort);
    610     malloc_string(mountpoint);
    611574
    612575    output[0] = '\0';
    613     phrase_one[0] = '\0';
    614     phrase_two[0] = '\0';
    615     dvd_last_resort[0] = '\0';
    616576
    617577    /*@ end vars **************************************************** */
     
    620580        strcpy(output, g_cdrom_drive_is_here);
    621581        log_msg(3, "Been there, done that. Returning %s", output);
    622         retval = 0;
    623         goto end_of_find_cdrom_device;
     582        return(0);
    624583    }
    625584    if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
     
    628587                "find_cdrom_device() --- returning last found location - '%s'",
    629588                output);
    630         retval = 0;
    631         goto end_of_find_cdrom_device;
    632     }
    633 
    634     sprintf(mountpoint, "/tmp/cd.%d", (int) (random() % 32767));
    635     make_hole_for_dir(mountpoint);
     589        return(0);
     590    }
    636591
    637592    if (find_home_of_exe("cdrecord")) {
    638         strcpy(cdr_exe, "cdrecord");
    639     } else {
    640         strcpy(cdr_exe, "dvdrecord");
    641     }
    642     tmp[0] = '\0';
     593        asprintf(&cdr_exe, "cdrecord");
     594    } else {
     595        asprintf(&cdr_exe, "dvdrecord");
     596    }
    643597    if (!find_home_of_exe(cdr_exe)) {
    644598        strcpy(output, "/dev/cdrom");
     
    646600        if (!does_device_exist(output)) {
    647601            log_msg(4, "That didn't work. Sorry.");
    648             retval = 1;
    649             goto end_of_find_cdrom_device;
     602            paranoid_free(cdr_exe);
     603            return(1);
    650604        } else {
    651             retval = 0;
    652             goto end_of_find_cdrom_device;
    653         }
    654     }
    655 
    656     sprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
     605            paranoid_free(cdr_exe);
     606            return(0);
     607        }
     608    }
     609
     610    asprintf(&command, "%s -scanbus 2> /dev/null", cdr_exe);
    657611    fin = popen(command, "r");
    658612    if (!fin) {
    659613        log_msg(4, "command=%s", command);
    660614        log_OS_error("Cannot popen command");
     615        paranoid_free(cdr_exe);
     616        paranoid_free(command);
    661617        return (1);
    662618    }
    663     for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
    664          fgets(tmp, MAX_STR_LEN, fin)) {
     619    paranoid_free(command);
     620
     621    for (getline(&tmp, &n, fin); !feof(fin);
     622         getline(&tmp, &n, fin)) {
    665623        p = strchr(tmp, '\'');
    666624        if (p) {
     
    669627                for (r = q; *(r - 1) == ' '; r--);
    670628                *r = '\0';
    671                 strcpy(phrase_one, p);
    672629                p = strchr(++q, '\'');
    673630                if (p) {
     
    678635                        }
    679636                        *q = '\0';
    680                         strcpy(phrase_two, p);
     637#ifndef __FreeBSD__
     638                        paranoid_free(phrase_two);
     639                        asprintf(&phrase_two, p);
     640#endif
    681641                    }
    682642                }
     
    685645    }
    686646    paranoid_pclose(fin);
     647    paranoid_free(tmp);
     648    tmp = NULL;
     649    n = 0;
    687650
    688651#ifndef __FreeBSD__
     
    690653        log_msg(4, "Not running phase two. String is empty.");
    691654    } else {
    692         sprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
     655        asprintf(&command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
    693656        fin = popen(command, "r");
    694657        if (!fin) {
    695658            log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
    696659        } else {
    697             for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
    698                  fgets(tmp, MAX_STR_LEN, fin)) {
     660            for (getline(&tmp, &n, fin); !feof(fin);
     661                 getline(&tmp, &n, fin)) {
    699662                log_msg(5, "--> '%s'", tmp);
    700663                if (tmp[0] != ' ' && tmp[1] != ' ') {
     
    703666                        *p = '\0';
    704667                        if (strstr(tmp, "DVD")) {
    705                             sprintf(dvd_last_resort, "/dev/%s", tmp);
     668                            paranoid_free(dvd_last_resort);
     669                            asprintf(&dvd_last_resort, "/dev/%s", tmp);
    706670                            log_msg(4,
    707671                                    "Ignoring '%s' because it's a DVD drive",
     
    714678                }
    715679            }
     680            paranoid_free(tmp);
    716681            paranoid_pclose(fin);
    717682        }
    718     }
     683        paranoid_free(command);
     684    }
     685    paranoid_free(phrase_two);
    719686
    720687#endif
     
    745712                                                              "/dev/cd1")))
    746713                                {
    747                                     retval = 1;
    748                                     goto end_of_find_cdrom_device;
     714                                    paranoid_free(cdr_exe);
     715                                    return(1);
    749716                                }
    750717                            }
     
    756723    }
    757724#else
    758     if (!found_it && strlen(dvd_last_resort) > 0) {
    759         log_msg(4, "Well, I'll use the DVD - %s - as a last resort",
    760                 dvd_last_resort);
    761         strcpy(output, dvd_last_resort);
    762         found_it = TRUE;
    763     }
     725    if (dvd_last_resort != NULL) {
     726        if (!found_it && strlen(dvd_last_resort) > 0) {
     727            log_msg(4, "Well, I'll use the DVD - %s - as a last resort",
     728                    dvd_last_resort);
     729            strcpy(output, dvd_last_resort);
     730            found_it = TRUE;
     731        }
     732    }
     733    paranoid_free(dvd_last_resort);
     734
    764735    if (found_it) {
    765         sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null",
     736        asprintf(&tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null",
    766737                strrchr(output, '/') + 1);
    767738        if (system(tmp) == 0) {
     
    772743            output[0] = '\0';
    773744        }
     745        paranoid_free(tmp);
    774746    }
    775747
     
    811783                                                                  g_cdrw_drive_is_here)))
    812784                                    {
    813                                         retval = 1;
    814                                         goto end_of_find_cdrom_device;
     785                                        paranoid_free(cdr_exe);
     786                                        return(1);
    815787                                    }
    816788                                }
     
    824796#endif
    825797
     798    asprintf(&mountpoint, "/tmp/cd.%d", (int) (random() % 32767));
     799    make_hole_for_dir(mountpoint);
     800
    826801    if (found_it && try_to_mount) {
    827802        if (mount_CDROM_here(output, mountpoint)) {
     
    829804            found_it = FALSE;
    830805        } else {
    831             sprintf(tmp, "%s/archives", mountpoint);
     806            asprintf(&tmp, "%s/archives", mountpoint);
    832807            if (!does_file_exist(tmp)) {
    833808                log_msg(4, "[Cardigans] I'll take it back");
    834809                found_it = FALSE;
    835810            } else {
    836                 sprintf(command, "umount %s", output);
     811                asprintf(&command, "umount %s", output);
    837812                paranoid_system(command);
     813                paranoid_free(command);
    838814                log_msg(4, "I'm confident the Mondo CD is in %s", output);
    839815            }
     816            paranoid_free(tmp);
    840817        }
    841818    }
    842819    unlink(mountpoint);
     820    paranoid_free(mountpoint);
    843821
    844822    if (found_it) {
     
    850828        strcpy(the_last_place_i_found_it, output);
    851829        strcpy(g_cdrom_drive_is_here, output);
    852         retval = 0;
    853         goto end_of_find_cdrom_device;
    854     }
    855 
    856     sprintf(command,
     830        return(0);
     831    }
     832
     833    asprintf(&command,
    857834            "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2",
    858835            cdr_exe, g_cdrw_drive_is_here);
    859836    log_msg(1, "command=%s", command);
    860     strcpy(tmp, call_program_and_get_last_line_of_output(command));
     837    asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     838    paranoid_free(command);
     839
    861840    if (tmp[0]) {
    862841        strcpy(output, tmp);
    863842        log_msg(4, "Finally found it at %s", output);
    864843        retval = 0;
    865         goto end_of_find_cdrom_device;
    866844    } else {
    867845        log_msg(4, "Still couldn't find it.");
    868846        retval = 1;
    869         goto end_of_find_cdrom_device;
    870     }
    871   end_of_find_cdrom_device:
     847    }
    872848    paranoid_free(tmp);
    873849    paranoid_free(cdr_exe);
    874     paranoid_free(phrase_one);
    875     paranoid_free(phrase_two);
    876     paranoid_free(command);
    877     paranoid_free(dvd_last_resort);
    878     paranoid_free(mountpoint);
    879850    return (retval);
    880851}
    881852
    882853
    883 
    884 
    885 
    886854int find_dvd_device(char *output, bool try_to_mount)
    887855{
    888     char *command;
    889856    char *tmp;
    890857    int retval = 0, devno = -1;
    891 
    892     malloc_string(command);
    893     malloc_string(tmp);
    894858
    895859    if (g_dvd_drive_is_here[0]) {
     
    899863    }
    900864
    901     sprintf(tmp, call_program_and_get_last_line_of_output
     865    asprintf(&tmp, call_program_and_get_last_line_of_output
    902866            ("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
    903867        );
    904868    log_msg(5, "tmp = '%s'", tmp);
    905     if (!tmp[0])
    906         sprintf(tmp, call_program_and_get_last_line_of_output
     869    if (!tmp[0]) {
     870        paranoid_free(tmp);
     871        asprintf(&tmp, call_program_and_get_last_line_of_output
    907872                ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
    908873            );
     874    }
    909875    if (tmp[0]) {
    910876        devno = atoi(tmp) - 1;
    911877    }
     878    paranoid_free(tmp);
     879
    912880    if (devno >= 0) {
    913881        retval = 0;
     
    926894}
    927895
    928 
    929 
    930 
    931 
    932 #include <sys/ioctl.h>
    933896
    934897/**
     
    1015978        log_msg(1, "Failed to get harddisk geometry, using old mode");
    1016979    }
    1017 /* 
    1018   if ((fd = open (drive, O_RDONLY)) != -1) {
    1019       if (ioctl (fd, HDIO_GETGEO, &hdgeo) != -1)  {
    1020       close (fd);
    1021       log_msg (2, "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB", drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors, (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024));
    1022       if ( hdgeo.cylinders && hdgeo.heads && hdgeo.sectors ) {
    1023           outvalB = ((long) (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024));
    1024       }
    1025       }
    1026       close (fd);
    1027  */
    1028980#endif
    1029981
     
    1041993}
    1042994
    1043 /* The old version */
    1044 #if 0
    1045 long get_phys_size_of_drive(char *drive)
    1046 {
    1047     /*@ pointers **************************************************** */
    1048 #if linux
    1049     FILE *fin;
    1050     char *p;
    1051     char *q;
    1052     char *r;
    1053     /*@ buffers ***************************************************** */
    1054     char *tmp;
    1055     char *command;
    1056 
    1057     /*@ long ******************************************************** */
    1058     long outL;
    1059     long tempLa;
    1060     long tempLb;
    1061     long tempLc;
    1062 
    1063 #endif
    1064 
    1065     struct hd_geometry hdgeo;
    1066     int fd;
    1067 
    1068 #ifdef __FreeBSD__
    1069     off_t o;
    1070 
    1071     if ((fd = open(drive, O_RDONLY)) != -1) {
    1072         if (ioctl(fd, DIOCGMEDIASIZE, &o) != -1) {
    1073             close(fd);
    1074             return (long) (o / (off_t) (1024 * 1024));
    1075         }
    1076         close(fd);
    1077     }
    1078     log_msg(4, "drive = %s, error = %s", drive, strerror(errno));
    1079     fatal_error("GPSOD: Unable to get size of drive");
    1080 #else
    1081 
    1082     malloc_string(tmp);
    1083     malloc_string(command);
    1084 
    1085     if ((fd = open(drive, O_RDONLY)) != -1) {
    1086         if (ioctl(fd, HDIO_GETGEO, &hdgeo) != -1) {
    1087             close(fd);
    1088             log_msg(2,
    1089                     "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB",
    1090                     drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors,
    1091                     (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 /
    1092                      1024));
    1093             if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
    1094                 return ((long)
    1095                         (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors /
    1096                          2 / 1024));
    1097             }
    1098         }
    1099         close(fd);
    1100     }
    1101 
    1102     assert_string_is_neither_NULL_nor_zerolength(drive);
    1103 
    1104     sprintf(command,
    1105             "parted2fdisk -l %s | head -n4 | tr -s '\n' '\t' | tr -s ' ' '\t' | cut -f8,14,16",
    1106             drive);
    1107     strcpy(tmp, call_program_and_get_last_line_of_output(command));
    1108     if (tmp[0]) {
    1109         p = tmp;
    1110         q = strchr(p, ' ');
    1111         if (q) {
    1112             *(q++) = '\0';
    1113             r = strchr(q, ' ');
    1114             if (r) {
    1115                 *(r++) = '\0';
    1116                 tempLa = atol(p);
    1117                 tempLb = atol(q);
    1118                 tempLc = atol(r);
    1119                 outL = tempLa * tempLb / 1024 * tempLc / 1024;
    1120                 if (outL > 100) {
    1121                     paranoid_free(tmp);
    1122                     paranoid_free(command);
    1123                     return (outL);
    1124                 }
    1125             }
    1126         }
    1127     }
    1128 
    1129     /* try to grep for 'Drive xxxx: yyy MB' */
    1130     sprintf(command,
    1131             "parted2fdisk -l %s | grep MB | tr -s ' ' '\t' | cut -f3",
    1132             drive);
    1133     strcpy(tmp, call_program_and_get_last_line_of_output(command));
    1134     if (atol(tmp) > 0) {
    1135         paranoid_free(tmp);
    1136         paranoid_free(command);
    1137         return (atol(tmp));
    1138     }
    1139 
    1140     /* else, do it the old-fashioned way */
    1141     p = strrchr(drive, (int) '/');
    1142     if (p) {
    1143         strcpy(tmp, p + 1);
    1144     } else {
    1145         paranoid_free(tmp);
    1146         paranoid_free(command);
    1147         return (-1);
    1148     }
    1149     sprintf(command, "dmesg | grep %s 2> /dev/null", tmp);
    1150     if (!(fin = popen(command, "r"))) {
    1151         log_OS_error("Cannot popen dmesg command");
    1152     } else {
    1153         fgets(tmp, MAX_STR_LEN - 1, fin);
    1154         while (!feof(fin) && !strstr(tmp, "GB") && !strstr(tmp, "MB")) {
    1155             fgets(tmp, MAX_STR_LEN - 1, fin);
    1156         }
    1157         if (pclose(fin)) {
    1158             log_OS_error("Cannot pclose dmesg fin");
    1159         }
    1160     }
    1161     if (!(p = strstr(tmp, "GB")) && !(p = strstr(tmp, "MB"))) {
    1162         log_msg(3, "Cannot find %s's size: dmesg isn't helping either.",
    1163                 drive);
    1164         paranoid_free(tmp);
    1165         paranoid_free(command);
    1166         return (-1);
    1167     }
    1168     for (; !isdigit(*(p - 1)); p--);
    1169     *p = '\0';
    1170     for (p--; isdigit(*(p - 1)); p--);
    1171     outL = atol(p);
    1172     if (outL <= 0) {
    1173         paranoid_free(tmp);
    1174         paranoid_free(command);
    1175         return (-1);
    1176     }
    1177     if (strstr(tmp, "GB")) {
    1178         outL = outL * 1024;
    1179     }
    1180     paranoid_free(tmp);
    1181     paranoid_free(command);
    1182     return (outL * 19 / 20);
    1183 #endif
    1184 }
    1185 #endif                          /* 0 */
    1186 
    1187 
    1188 
    1189 
    1190995
    1191996/**
     
    12021007
    12031008    FILE *pin;
    1204     int retval;
     1009    bool retval;
    12051010    malloc_string(good_formats);
    1206     malloc_string(command);
    1207     malloc_string(format_sz);
    1208 
    12091011    assert_string_is_neither_NULL_nor_zerolength(format);
    12101012
    1211     sprintf(format_sz, "%s ", format);
     1013    asprintf(&format_sz, "%s ", format);
    12121014
    12131015#ifdef __FreeBSD__
    1214     sprintf(command,
     1016    asprintf(&command,
    12151017            "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
    12161018#else
    1217     sprintf(command,
     1019    asprintf(&command,
    12181020            "cat /proc/filesystems | grep -v nodev | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
    12191021#endif
    12201022
    12211023    pin = popen(command, "r");
     1024    paranoid_free(command);
     1025
    12221026    if (!pin) {
    12231027        log_OS_error("Unable to read good formats");
    1224         retval = 0;
     1028        retval = FALSE;
    12251029    } else {
    12261030        strcpy(good_formats, " ");
     
    12321036        strcat(good_formats, " swap lvm raid ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
    12331037        if (strstr(good_formats, format_sz)) {
    1234             retval = 1;
     1038            retval = TRUE;
    12351039        } else {
    1236             retval = 0;
     1040            retval = FALSE;
    12371041        }
    12381042    }
    12391043    paranoid_free(good_formats);
    1240     paranoid_free(command);
    12411044    paranoid_free(format_sz);
    12421045    return (retval);
     
    12581061
    12591062    /*@ buffers ***************************************************** */
    1260     char *incoming;
     1063    char *incoming = NULL;
    12611064    char *device_with_tab;
    12621065    char *device_with_space;
    12631066    char *tmp;
    1264     int retval = 0;
     1067    int n = 0;
    12651068
    12661069#ifdef __FreeBSD__
     
    12721075    /*@ end vars **************************************************** */
    12731076
    1274     malloc_string(incoming);
    1275     malloc_string(device_with_tab);
    1276     malloc_string(device_with_space);
    1277     malloc_string(tmp);
    12781077    assert(device_raw != NULL);
    12791078//  assert_string_is_neither_NULL_nor_zerolength(device_raw);
     
    12811080        log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
    12821081                device_raw);
    1283         sprintf(tmp, "/%s", device_raw);
    1284     } else {
    1285         strcpy(tmp, device_raw);
     1082        asprintf(&tmp, "/%s", device_raw);
     1083    } else {
     1084        asprintf(&tmp, device_raw);
    12861085    }
    12871086    log_msg(1, "Is %s mounted?", tmp);
     
    12891088        log_msg(1,
    12901089                "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
    1291         return (0);
    1292     }
    1293     sprintf(device_with_tab, "%s\t", tmp);
    1294     sprintf(device_with_space, "%s ", tmp);
     1090        return (FALSE);
     1091    }
     1092    asprintf(&device_with_tab, "%s\t", tmp);
     1093    asprintf(&device_with_space, "%s ", tmp);
     1094    paranoid_free(tmp);
    12951095
    12961096    if (!(fin = popen("mount", "r"))) {
     
    12981098        return (FALSE);
    12991099    }
    1300     for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);
    1301          fgets(incoming, MAX_STR_LEN - 1, fin)) {
     1100    for (getline(&incoming, &n, fin); !feof(fin);
     1101         getline(&incoming, &n, fin)) {
    13021102        if (strstr(incoming, device_with_space) //> incoming
    13031103            || strstr(incoming, device_with_tab))   // > incoming)
    13041104        {
    13051105            paranoid_pclose(fin);
    1306             retval = 1;
    1307             goto end_of_func;
    1308         }
    1309     }
     1106            return(TRUE);
     1107        }
     1108    }
     1109    paranoid_free(incoming);
     1110    paranoid_free(device_with_tab);
    13101111    paranoid_pclose(fin);
    1311     sprintf(tmp, "%s | grep -w \"%s\" > /dev/null 2> /dev/null",
     1112
     1113    asprintf(&tmp, "%s | grep -w \"%s\" > /dev/null 2> /dev/null",
    13121114            SWAPLIST_COMMAND, device_with_space);
     1115    paranoid_free(device_with_space);
     1116
    13131117    log_msg(4, "tmp (command) = '%s'", tmp);
    13141118    if (!system(tmp)) {
    1315         retval = 1;
    1316         goto end_of_func;
    1317     }
    1318   end_of_func:
    1319     paranoid_free(incoming);
    1320     paranoid_free(device_with_tab);
    1321     paranoid_free(device_with_space);
     1119        paranoid_free(tmp);
     1120        return(TRUE);
     1121    }
    13221122    paranoid_free(tmp);
    1323     return (retval);
    1324 }
     1123    return (FALSE);
     1124}
     1125
    13251126
    13261127#ifdef __FreeBSD__
     
    13291130 * Create a loopback device for specified @p fname.
    13301131 * @param fname The file to associate with a device.
    1331  * @return /dev entry for the device, or NULL if it couldn't be allocated.
     1132 * @return /dev entry for the device (to be freed by the caller),
     1133 * or NULL if it couldn't be allocated.
    13321134 */
    13331135char *make_vn(char *fname)
    13341136{
    1335     char *device = (char *) malloc(MAX_STR_LEN);
    1336     char *mddevice = (char *) malloc(32);
    1337     char command[MAX_STR_LEN];
     1137    char *device;
     1138    char *mddevice = NULL;
     1139    char *command = NULL;
    13381140    int vndev = 2;
     1141
    13391142    if (atoi
    13401143        (call_program_and_get_last_line_of_output
    13411144         ("/sbin/sysctl -n kern.osreldate")) < 500000) {
    13421145        do {
    1343             sprintf(mddevice, "vn%ic", vndev++);
    1344             sprintf(command, "vnconfig %s %s", mddevice, fname);
     1146            paranoid_free(mddevice);
     1147            asprintf(&mddevice, "vn%ic", vndev++);
     1148
     1149            paranoid_free(command);
     1150            asprintf(&command, "vnconfig %s %s", mddevice, fname);
     1151
    13451152            if (vndev > 10) {
     1153                paranoid_free(command);
     1154                paranoid_free(mddevice);
    13461155                return NULL;
    13471156            }
    13481157        }
    13491158        while (system(command));
    1350     } else {
    1351         sprintf(command, "mdconfig -a -t vnode -f %s", fname);
    1352         mddevice = call_program_and_get_last_line_of_output(command);
     1159        paranoid_free(command);
     1160    } else {
     1161        asprintf(&command, "mdconfig -a -t vnode -f %s", fname);
     1162        asprintf(&mddevice, call_program_and_get_last_line_of_output(command));
     1163        paranoid_free(command);
     1164
    13531165        if (!strstr(mddevice, "md")) {
     1166            paranoid_free(mddevice);
    13541167            return NULL;
    13551168        }
    13561169    }
    1357     sprintf(device, "/dev/%s", mddevice);
    1358     return device;
    1359 }
    1360 
     1170    asprintf(&device, "/dev/%s", mddevice);
     1171    paranoid_free(mddevice);
     1172    return(device);
     1173}
    13611174
    13621175
     
    13711184int kick_vn(char *dname)
    13721185{
    1373     char command[MAX_STR_LEN];
     1186    char *command;
     1187    int ret = 0;
    13741188
    13751189    if (strncmp(dname, "/dev/", 5) == 0) {
     
    13801194        (call_program_and_get_last_line_of_output
    13811195         ("/sbin/sysctl -n kern.osreldate")) < 500000) {
    1382         sprintf(command, "vnconfig -d %s", dname);
    1383         return system(command);
    1384     } else {
    1385         sprintf(command, "mdconfig -d -u %s", dname);
    1386         return system(command);
     1196        asprintf(&command, "vnconfig -d %s", dname);
     1197        ret = system(command);
     1198        paranoid_free(command);
     1199        return(ret);
     1200    } else {
     1201        asprintf(&command, "mdconfig -d -u %s", dname);
     1202        ret = system(command);
     1203        paranoid_free(command);
     1204        return(ret);
    13871205    }
    13881206     /*NOTREACHED*/ return 255;
     
    14011219    /*@ buffer ****************************************************** */
    14021220    char *command;
    1403     char *dev;
    1404     char *options;
    14051221    int retval;
    14061222
    1407     malloc_string(command);
    1408     malloc_string(dev);
    1409     malloc_string(options);
    14101223    assert_string_is_neither_NULL_nor_zerolength(device);
    14111224    assert_string_is_neither_NULL_nor_zerolength(mountpoint);
    14121225
    14131226    make_hole_for_dir(mountpoint);
    1414     strcpy(options, "ro");
    14151227    if (isdigit(device[0])) {
    14161228        find_cdrom_device(device, FALSE);
    1417     } else {
    1418         strcpy(dev, device);
    14191229    }
    14201230    if (g_ISO_restore_mode) {
    14211231
    14221232#ifdef __FreeBSD__
    1423         strcpy(dev, make_vn(device));
     1233        char *dev;
     1234
     1235        dev = make_vn(device));
    14241236        if (!dev) {
    1425             sprintf(command, "Unable to mount ISO (make_vn(%s) failed)",
     1237            asprintf(&command, "Unable to mount ISO (make_vn(%s) failed)",
    14261238                    device);
    14271239            fatal_error(command);
    14281240        }
    14291241        strcpy(device, dev);
    1430 #else
    1431         strcat(options, ",loop");
    1432 #endif
    1433 
    1434     }
     1242        paranoid_free(dev);
     1243#endif
     1244    }
     1245
    14351246    log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device,
    14361247            mountpoint);
     
    14381249
    14391250#ifdef __FreeBSD__
    1440     sprintf(command, "mount_cd9660 -r %s %s 2>> %s",
     1251    asprintf(&command, "mount_cd9660 -r %s %s 2>> %s",
    14411252            device, mountpoint, MONDO_LOGFILE);
    1442 
    14431253#else
    1444     sprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s",
    1445             device, options, mountpoint, MONDO_LOGFILE);
     1254    asprintf(&command, "mount %s -o ro,loop -t iso9660 %s 2>> %s",
     1255            device, mountpoint, MONDO_LOGFILE);
    14461256#endif
    14471257
     
    14521262    retval = system(command);
    14531263    log_msg(1, "system(%s) returned %d", command, retval);
    1454 
    14551264    paranoid_free(command);
    1456     paranoid_free(dev);
    1457     paranoid_free(options);
     1265
    14581266    return (retval);
    14591267}
    1460 
    1461 
    1462 
    1463 
    14641268
    14651269
     
    14971301        return;
    14981302    }
    1499     malloc_string(tmp);
    1500     malloc_string(request);
    1501     sprintf(tmp, "mkdir -p " MNT_CDROM);
     1303
     1304    asprintf(&tmp, "mkdir -p " MNT_CDROM);
    15021305    run_program_and_log_output(tmp, 5);
     1306    paranoid_free(tmp);
     1307
    15031308    if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
    15041309        || bkpinfo->backup_media_type == nfs) {
     
    15111316        }
    15121317        system("mkdir -p /tmp/isodir &> /dev/null");
    1513         sprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir,
     1318        asprintf(&tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir,
    15141319                bkpinfo->nfs_remote_dir, bkpinfo->prefix,
    15151320                cd_number_i_want);
    15161321        if (!does_file_exist(tmp)) {
    1517             sprintf(tmp, "/tmp/isodir/%s/%s-%d.iso",
     1322            paranoid_free(tmp);
     1323            asprintf(&tmp, "/tmp/isodir/%s/%s-%d.iso",
    15181324                    bkpinfo->nfs_remote_dir, bkpinfo->prefix,
    15191325                    cd_number_i_want);
     
    15291335            fatal_error("Mommy!");
    15301336        }
    1531 //    g_current_media_number = cd_number_i_want;
    1532 //    return;
     1337        paranoid_free(tmp);
    15331338    }
    15341339    if ((res = what_number_cd_is_this(bkpinfo)) != cd_number_i_want) {
    15351340        log_msg(3, "Currently, we hold %d but we want %d", res,
    15361341                cd_number_i_want);
    1537         sprintf(tmp, "Insisting on %s #%d",
     1342        asprintf(&tmp, "Insisting on %s #%d",
    15381343                media_descriptor_string(bkpinfo->backup_media_type),
    15391344                cd_number_i_want);
    1540         sprintf(request, "Please insert %s #%d and press Enter.",
     1345        asprintf(&request, "Please insert %s #%d and press Enter.",
    15411346                media_descriptor_string(bkpinfo->backup_media_type),
    15421347                cd_number_i_want);
    15431348        log_msg(3, tmp);
     1349        paranoid_free(tmp);
     1350
    15441351        while (what_number_cd_is_this(bkpinfo) != cd_number_i_want) {
    15451352            paranoid_system("sync");
     
    15671374            paranoid_system("sync");
    15681375        }
     1376        paranoid_free(request);
     1377
    15691378        log_msg(1, "Thankyou. Proceeding...");
    15701379        g_current_media_number = cd_number_i_want;
    15711380    }
    1572     paranoid_free(tmp);
    1573     paranoid_free(request);
    1574 }
    1575 
     1381}
    15761382/* @} - end of deviceGroup */
    1577 
    1578 
    1579 
    1580 
    15811383
    15821384
     
    15981400{
    15991401    char *tmp;
    1600     char *sz_size;
     1402    char *sz_size = NULL;
    16011403    char *command;
    16021404    char *comment;
    1603     char *prompt;
     1405    char *prompt = NULL;
    16041406    int i;
    16051407    FILE *fin;
    16061408
    1607     malloc_string(tmp);
    1608     malloc_string(sz_size);
    1609     malloc_string(command);
    1610     malloc_string(comment);
    16111409    assert(bkpinfo != NULL);
    1612     sz_size[0] = '\0';
    16131410    bkpinfo->nonbootable_backup = FALSE;
    16141411
     
    16671464                finish(1);
    16681465            }
    1669             sprintf(comment, "What speed is your %s (re)writer?",
     1466            asprintf(&comment, "What speed is your %s (re)writer?",
    16701467                    media_descriptor_string(bkpinfo->backup_media_type));
    16711468            if (bkpinfo->backup_media_type == dvd) {
    16721469                find_dvd_device(bkpinfo->media_device, FALSE);
    1673                 strcpy(tmp, "1");
    1674                 sprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE);  // 4.7 salesman's GB = 4.482 real GB = 4582 MB
     1470                asprintf(&tmp, "1");
     1471                asprintf(&sz_size, "%d", DEFAULT_DVD_DISK_SIZE);    // 4.7 salesman's GB = 4.482 real GB = 4582 MB
    16751472                log_msg(1, "Setting to DVD defaults");
    16761473            } else {
    16771474                strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
    1678                 strcpy(tmp, "4");
    1679                 strcpy(sz_size, "650");
     1475                asprintf(&tmp, "4");
     1476                asprintf(&sz_size, "650");
    16801477                log_msg(1, "Setting to CD defaults");
    16811478            }
     
    16861483                }
    16871484            }
     1485            paranoid_free(comment);
     1486
    16881487            bkpinfo->cdrw_speed = atoi(tmp);    // if DVD then this shouldn't ever be used anyway :)
    1689             sprintf(comment,
     1488            paranoid_free(tmp);
     1489
     1490            asprintf(&comment,
    16901491                    "How much data (in Megabytes) will each %s store?",
    16911492                    media_descriptor_string(bkpinfo->backup_media_type));
     1493
    16921494            if (!popup_and_get_string("Size", comment, sz_size, 5)) {
    16931495                log_to_screen("User has chosen not to backup the PC");
    16941496                finish(1);
    16951497            }
     1498            paranoid_free(comment);
     1499
    16961500            for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
    16971501                bkpinfo->media_size[i] = atoi(sz_size);
     
    17011505                finish(1);
    17021506            }
     1507            paranoid_free(sz_size);
    17031508        }
    17041509    case cdstream:
     
    17181523                log_msg(1, "bkpinfo->media_device = %s",
    17191524                        bkpinfo->media_device);
    1720                 sprintf(comment,
     1525                asprintf(&comment,
    17211526                        "Please specify your %s drive's /dev entry",
    17221527                        media_descriptor_string(bkpinfo->
     
    17281533                    finish(1);
    17291534                }
     1535                paranoid_free(comment);
    17301536            }
    17311537            log_msg(2, "%s device found at %s",
     
    17371543            }
    17381544            if (bkpinfo->media_device[0]) {
    1739                 sprintf(tmp,
     1545                asprintf(&tmp,
    17401546                        "I think I've found your %s burner at SCSI node %s; am I right on the money?",
    17411547                        media_descriptor_string(bkpinfo->
     
    17451551                    bkpinfo->media_device[0] = '\0';
    17461552                }
     1553                paranoid_free(tmp);
    17471554            }
    17481555            if (!bkpinfo->media_device[0]) {
     
    17961603                }
    17971604            }
    1798             sprintf(tmp,
    1799                     "I think I've found your tape streamer at %s; am I right on the money?",
    1800                     bkpinfo->media_device);
    1801         }
    1802         if (bkpinfo->media_device[0]) {
    1803             sprintf(tmp,
     1605            asprintf(&tmp,
    18041606                    "I think I've found your tape streamer at %s; am I right on the money?",
    18051607                    bkpinfo->media_device);
     
    18071609                bkpinfo->media_device[0] = '\0';
    18081610            }
     1611            paranoid_free(tmp);
    18091612        }
    18101613        if (!bkpinfo->media_device[0]) {
     
    18171620            }
    18181621        }
    1819         sprintf(tmp, "ls -l %s", bkpinfo->media_device);
     1622        asprintf(&tmp, "ls -l %s", bkpinfo->media_device);
    18201623        if (run_program_and_log_output(tmp, FALSE)) {
    18211624            log_to_screen("User has not specified a valid /dev entry");
    18221625            finish(1);
    18231626        }
     1627        paranoid_free(tmp);
    18241628        log_msg(4, "sz_size = %s", sz_size);
    18251629        sz_size[0] = '\0';
    1826 /*
    1827     if ((size_sz[0]=='\0' || atol(size_sz)==0) && archiving_to_media)
    1828       {
    1829         if (!popup_and_get_string("Tape size", "How much COMPRESSED data will one of your tape cartridges hold? (e.g. 4GB for 4 gigabytes)", size_sz, 16))
    1830           { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1831       }
    1832 */
    1833         if (sz_size[0] == '\0') {
    1834             bkpinfo->media_size[0] = 0;
    1835         } else {
    1836             bkpinfo->media_size[0] =
    1837                 friendly_sizestr_to_sizelong(sz_size) / 2 - 50;
    1838         }
     1630        bkpinfo->media_size[0] = 0;
    18391631        log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
    18401632        if (bkpinfo->media_size[0] <= 0) {
     
    18861678            if (bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] == '/')
    18871679                bkpinfo->nfs_mount[strlen(bkpinfo->nfs_mount) - 1] = '\0';
    1888             sprintf(command, "mount | grep \"%s \" | cut -d' ' -f3",
     1680            asprintf(&command, "mount | grep \"%s \" | cut -d' ' -f3",
    18891681                    bkpinfo->nfs_mount);
    18901682            strcpy(bkpinfo->isodir,
    18911683                   call_program_and_get_last_line_of_output(command));
     1684            paranoid_free(command);
    18921685        }
    18931686        if (bkpinfo->disaster_recovery) {
     
    19031696            sprintf(bkpinfo->isodir, "/tmp/isodir.mondo.%d",
    19041697                    (int) (random() % 32768));
    1905             sprintf(command, "mkdir -p %s", bkpinfo->isodir);
     1698            asprintf(&command, "mkdir -p %s", bkpinfo->isodir);
    19061699            run_program_and_log_output(command, 5);
    1907             sprintf(tmp, "mount %s -t nfs %s", bkpinfo->nfs_mount,
     1700            paranoid_free(command);
     1701
     1702            asprintf(&tmp, "mount %s -t nfs %s", bkpinfo->nfs_mount,
    19081703                    bkpinfo->isodir);
    19091704            run_program_and_log_output(tmp, 5);
     1705            paranoid_free(tmp);
    19101706            malloc_string(g_selfmounted_isodir);
    19111707            strcpy(g_selfmounted_isodir, bkpinfo->isodir);
     
    19161712            finish(1);
    19171713        }
    1918         strcpy(tmp, bkpinfo->nfs_remote_dir);
     1714        asprintf(&tmp, bkpinfo->nfs_remote_dir);
    19191715        if (!popup_and_get_string
    19201716            ("Directory", "Which directory within that mountpoint?", tmp,
     
    19241720        }
    19251721        strcpy(bkpinfo->nfs_remote_dir, tmp);
     1722        paranoid_free(tmp);
     1723
    19261724        // check whether writable - we better remove surrounding spaces for this
    19271725        strip_spaces(bkpinfo->nfs_remote_dir);
    1928         sprintf(command, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir,
     1726        asprintf(&command, "echo hi > %s/%s/.dummy.txt", bkpinfo->isodir,
    19291727                bkpinfo->nfs_remote_dir);
    19301728        while (run_program_and_log_output(command, FALSE)) {
    1931             strcpy(tmp, bkpinfo->nfs_remote_dir);
     1729            paranoid_free(tmp);
     1730            paranoid_free(prompt);
     1731            asprintf(&tmp, bkpinfo->nfs_remote_dir);
    19321732            asprintf(&prompt,
    19331733                     "Directory '%s' under mountpoint '%s' does not exist or is not writable. You can fix this or change the directory and retry or cancel the backup.",
     
    19441744                     bkpinfo->isodir, bkpinfo->nfs_remote_dir);
    19451745        }
     1746        paranoid_free(command);
     1747        paranoid_free(tmp);
     1748        paranoid_free(prompt);
     1749
    19461750        for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
    19471751            bkpinfo->media_size[i] = 650;
     
    19771781                    bkpinfo->media_size[i] = atoi(sz_size);
    19781782                }
     1783                paranoid_free(sz_size);
     1784
    19791785                if (!popup_and_get_string
    19801786                    ("Prefix.",
     
    20631869            finish(1);
    20641870        }
    2065         strcpy(tmp, list_of_NFS_mounts_only());
     1871        tmp = list_of_NFS_mounts_only();
    20661872        if (strlen(tmp) > 2) {
    20671873            if (bkpinfo->exclude_paths[0]) {
     
    20701876            strncpy(bkpinfo->exclude_paths, tmp, MAX_STR_LEN);
    20711877        }
     1878        paranoid_free(tmp);
    20721879// NTFS
    2073         strcpy(tmp,
     1880        asprintf(&tmp,
    20741881               call_program_and_get_last_line_of_output
    20751882               ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
     
    20841891            strncpy(bkpinfo->image_devs, tmp, MAX_STR_LEN / 4);
    20851892        }
    2086 
     1893        paranoid_free(tmp);
    20871894
    20881895        if (!popup_and_get_string
     
    21241931#else
    21251932    if (bkpinfo->backup_media_type == nfs) {
    2126         sprintf(tmp, "mount | grep \"%s\" | cut -d' ' -f3",
    2127                 bkpinfo->nfs_mount);
    2128 //      strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output(tmp));
    21291933        log_msg(3, "I think the NFS mount is mounted at %s",
    21301934                bkpinfo->isodir);
     
    21541958        }
    21551959    }
    2156     paranoid_free(tmp);
    2157     paranoid_free(sz_size);
    2158     paranoid_free(command);
    2159     paranoid_free(comment);
    2160     paranoid_free(prompt);
    21611960    return (0);
    21621961}
    2163 
    2164 
    2165 
    2166 
    2167 /**
    2168  * @addtogroup utilityGroup
    2169  * @{
    2170  */
    2171 /**
    2172  * Get a space-separated list of NFS devices and mounts.
    2173  * @return The list created.
    2174  * @note The return value points to static data that will be overwritten with each call.
    2175  */
    2176 char *list_of_NFS_devices_and_mounts(void)
    2177 {
    2178     char *exclude_these_devices;
    2179     char *exclude_these_directories;
    2180     static char result_sz[512];
    2181 
    2182     malloc_string(exclude_these_devices);
    2183     malloc_string(exclude_these_directories);
    2184     strcpy(exclude_these_directories,
    2185            call_program_and_get_last_line_of_output
    2186            ("mount -t coda,ncpfs,nfs,smbfs,cifs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2187     strcpy(exclude_these_devices,
    2188            call_program_and_get_last_line_of_output
    2189            ("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs|cifs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2190     sprintf(result_sz, "%s %s", exclude_these_directories,
    2191             exclude_these_devices);
    2192     paranoid_free(exclude_these_devices);
    2193     paranoid_free(exclude_these_directories);
    2194     return (result_sz);
    2195 }
    2196 
    2197 
    21981962
    21991963
     
    22081972    char *exclude_these_devices;
    22091973    char *exclude_these_directories;
    2210     static char result_sz[512];
    2211 
    2212     malloc_string(exclude_these_devices);
    2213     malloc_string(exclude_these_directories);
    2214     strcpy(exclude_these_directories,
     1974    char *result_sz;
     1975
     1976    asprintf(&exclude_these_directories,
    22151977           call_program_and_get_last_line_of_output
    22161978           ("mount -t coda,ncpfs,nfs,smbfs,cifs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2217     strcpy(exclude_these_devices,
     1979    asprintf(&exclude_these_devices,
    22181980           call_program_and_get_last_line_of_output
    22191981           ("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs|cifs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2220     sprintf(result_sz, "%s", exclude_these_directories);
     1982    asprintf(&result_sz, "%s", exclude_these_directories);
    22211983    paranoid_free(exclude_these_devices);
    22221984    paranoid_free(exclude_these_directories);
     
    22241986}
    22251987
     1988
    22261989/* @} - end of utilityGroup */
    2227 
    2228 
    2229 
    2230 
    22311990
    22321991/**
     
    22412000    char *tmp;
    22422001
    2243     malloc_string(tmp);
    22442002    assert_string_is_neither_NULL_nor_zerolength(stub);
    22452003
     
    22482006    make_hole_for_file(store_name_here);
    22492007    mkfifo(store_name_here, S_IRWXU | S_IRWXG);
    2250     sprintf(tmp, "chmod 770 %s", store_name_here);
     2008    asprintf(&tmp, "chmod 770 %s", store_name_here);
    22512009    paranoid_system(tmp);
    22522010    paranoid_free(tmp);
    22532011}
    2254 
    2255 
    2256 
    2257 
    22582012
    22592013
     
    22682022    char *tmp, *command, *sz;
    22692023
    2270     malloc_string(tmp);
    2271     malloc_string(command);
    2272     malloc_string(sz);
    22732024    assert(bkpinfo != NULL);
    22742025
    22752026#ifdef __FreeBSD__
    2276     strcpy(tmp,
     2027    asprintf(&tmp,
    22772028           call_program_and_get_last_line_of_output
    22782029           ("df -m -t nonfs,msdosfs,ntfs,smbfs,smb,cifs | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
    22792030#else
    2280     strcpy(tmp,
     2031    asprintf(&tmp,
    22812032           call_program_and_get_last_line_of_output
    22822033           ("df -m -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs | sed 's/                  /devdev/' | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | grep -v /dev/shm | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
     
    22842035
    22852036    if (tmp[0] != '/') {
    2286         strcpy(sz, tmp);
    2287         strcpy(tmp, "/");
    2288         strcat(tmp, sz);
     2037        asprintf(&sz, "/%s", tmp);
     2038        paranoid_free(tmp);
     2039        tmp = sz;
    22892040    }
    22902041    if (!tmp[0]) {
     
    23022053            bkpinfo->scratchdir);
    23032054
    2304     sprintf(command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp);
     2055    asprintf(&command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp);
     2056    paranoid_free(tmp);
     2057
    23052058    paranoid_system(command);
    2306     paranoid_free(tmp);
    23072059    paranoid_free(command);
    2308     paranoid_free(sz);
    2309 }
    2310 
    2311 
    2312 
    2313 
     2060}
    23142061
    23152062
     
    23292076    char *command;
    23302077
    2331     malloc_string(command);
    23322078    if (!dev || dev[0] == '\0') {
    23332079        output[0] = '\0';
    23342080        return (FALSE);
    23352081    }
    2336 //  assert_string_is_neither_NULL_nor_zerolength(dev);
    23372082    log_msg(10, "Injecting %s", dev);
    23382083    inject_device(dev);
     
    23412086        return (FALSE);
    23422087    }
    2343     sprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
     2088    asprintf(&command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
    23442089            512L, dev);
    23452090    if (!run_program_and_log_output(command, FALSE)
     
    23472092        strcpy(output, dev);
    23482093        log_msg(4, "Found it - %s", dev);
     2094        paranoid_free(command);
    23492095        return (TRUE);
    23502096    } else {
    23512097        output[0] = '\0';
    23522098        log_msg(4, "It's not %s", dev);
     2099        paranoid_free(command);
    23532100        return (FALSE);
    23542101    }
    23552102}
    2356 
    2357 
    2358 
    23592103
    23602104
     
    23722116    char *tmp;
    23732117
    2374     malloc_string(mountdev);
    2375     malloc_string(tmp);
    23762118    assert(bkpinfo != NULL);
    2377 //  log_it("Asking what_number_cd_is_this");
    23782119    if (g_ISO_restore_mode) {
    2379         sprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
    2380 //      log_it("tmp = %s", tmp);
    2381 
    2382         strcpy(mountdev, call_program_and_get_last_line_of_output(tmp));
    2383         strcat(mountdev, "/archives/THIS-CD-NUMBER");
    2384 //      log_it("mountdev = %s", mountdev);
     2120        asprintf(&tmp, "mount | grep iso9660 | awk '{print $3;}'");
     2121
     2122        asprintf(&mountdev, "%s/archives/THIS-CD-NUMBER", call_program_and_get_last_line_of_output(tmp));
     2123        paranoid_free(tmp);
     2124
    23852125        cd_number = atoi(last_line_of_file(mountdev));
    2386 //      log_it("cd_number = %d", cd_number);
    23872126        paranoid_free(mountdev);
    2388         paranoid_free(tmp);
     2127
    23892128        return (cd_number);
    23902129    }
    23912130
    2392     strcpy(mountdev, bkpinfo->media_device);
     2131    asprintf(&mountdev, bkpinfo->media_device);
    23932132    if (!mountdev[0]) {
    23942133        log_it
     
    24012140    cd_number =
    24022141        atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
    2403 //  log_it("cd_number..later.. = %d", cd_number);
    24042142    paranoid_free(mountdev);
    2405     paranoid_free(tmp);
    24062143    return (cd_number);
    24072144}
    2408 
    2409 
    2410 
    2411 
    2412 
    24132145
    24142146
     
    24232155{
    24242156    /*@ buffers **************** */
    2425     static char tmp[MAX_STR_LEN];
     2157    char *tmp;
    24262158
    24272159
    24282160#ifdef __FreeBSD__
    2429     strcpy(tmp, call_program_and_get_last_line_of_output
     2161    asprintf(&tmp, call_program_and_get_last_line_of_output
    24302162           ("mount | grep \" on / \" | cut -d' ' -f1"));
    24312163#else
    2432     strcpy(tmp, call_program_and_get_last_line_of_output
     2164    asprintf(&tmp, call_program_and_get_last_line_of_output
    24332165           ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
    24342166    if (strstr(tmp, "/dev/cciss/")) {
    2435         strcpy(tmp, call_program_and_get_last_line_of_output
     2167        paranoid_free(tmp);
     2168        asprintf(&tmp, call_program_and_get_last_line_of_output
    24362169               ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
    24372170    }
    24382171    if (strstr(tmp, "/dev/md")) {
    2439         strcpy(tmp,
     2172        paranoid_free(tmp);
     2173        asprintf(&tmp,
    24402174               call_program_and_get_last_line_of_output
    24412175               ("mount | grep \" on / \" | cut -d' ' -f1"));
     
    25022236    /*@ buffer ***************************************************** */
    25032237    char *list_drives_cmd;
    2504     char *current_drive;
     2238    char *current_drive = NULL;
    25052239
    25062240    /*@ pointers *************************************************** */
     
    25102244    int count_lilos = 0;
    25112245    int count_grubs = 0;
     2246    int n = 0;
    25122247
    25132248    /*@ end vars *************************************************** */
    2514 
    2515     malloc_string(list_drives_cmd);
    2516     malloc_string(current_drive);
    25172249
    25182250#ifdef __IA64__
     
    25212253#endif
    25222254    assert(which_device != NULL);
    2523     //  sprintf (list_drives_cmd,
    2524     //       "fdisk -l | grep /dev | grep cyl | tr ':' ' ' | cut -d' ' -f2");
    2525 
    2526     sprintf(list_drives_cmd,
     2255    asprintf(&list_drives_cmd,
    25272256            // "parted2fdisk
    25282257            "fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s",
     
    25332262        log_OS_error("Unable to open list of drives");
    25342263        paranoid_free(list_drives_cmd);
    2535         paranoid_free(current_drive);
    25362264        return ('\0');
    25372265    }
    2538     for (fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
    2539          fgets(current_drive, MAX_STR_LEN, pdrives)) {
     2266    paranoid_free(list_drives_cmd);
     2267
     2268    for (getline(&current_drive, &n, pdrives); !feof(pdrives);
     2269         getline(&current_drive, &n, pdrives)) {
    25402270        strip_spaces(current_drive);
    25412271        log_it("looking at drive %s's MBR", current_drive);
     
    25512281        }
    25522282    }
     2283    paranoid_free(current_drive);
     2284
    25532285    if (pclose(pdrives)) {
    25542286        log_OS_error("Cannot pclose pdrives");
    25552287    }
    25562288    log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
    2557     paranoid_free(list_drives_cmd);
    2558     paranoid_free(current_drive);
    25592289    if (count_grubs && !count_lilos) {
    25602290        return ('G');
     
    25722302
    25732303
    2574 
    2575 
    25762304/**
    25772305 * Write zeroes over the first 16K of @p device.
     
    25992327}
    26002328
     2329
    26012330/**
    26022331 * Return the device pointed to by @p incoming.
    26032332 * @param incoming The device to resolve symlinks for.
    26042333 * @return The path to the real device file.
    2605  * @note The returned string points to static storage that will be overwritten with each call.
     2334 * @note The returned string points to a storage that need to be freed by the caller
    26062335 * @bug Won't work with file v4.0; needs to be written in C.
    26072336 */
    26082337char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
    26092338{
    2610     static char output[MAX_STR_LEN];
     2339    char *output;
    26112340    char *command;
    26122341    char *curr_fname;
     
    26162345
    26172346    struct stat statbuf;
    2618     command = malloc(1000);
    2619     malloc_string(tmp);
    2620     malloc_string(scratch);
    2621     malloc_string(curr_fname);
    26222347    if (!does_file_exist(incoming)) {
    26232348        log_it
    26242349            ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
    2625         strcpy(output, incoming);
    2626     } else {
    2627         strcpy(curr_fname, incoming);
     2350        asprintf(&output, incoming);
     2351    } else {
     2352        asprintf(&curr_fname, incoming);
    26282353        lstat(curr_fname, &statbuf);
    26292354        while (S_ISLNK(statbuf.st_mode)) {
    26302355            log_msg(1, "curr_fname = %s", curr_fname);
    2631             sprintf(command, "file %s", curr_fname);
    2632             strcpy(tmp, call_program_and_get_last_line_of_output(command));
     2356            asprintf(&command, "file %s", curr_fname);
     2357            asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     2358            paranoid_free(command);
     2359
    26332360            for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
    26342361                 p--);
    26352362            p++;
    2636             strcpy(scratch, p);
     2363            asprintf(&scratch, p);
    26372364            for (p = scratch; *p != '\0' && *p != '\''; p++);
    26382365            *p = '\0';
    26392366            log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp,
    26402367                    scratch);
     2368            paranoid_free(tmp);
     2369
    26412370            if (scratch[0] == '/') {
    2642                 strcpy(curr_fname, scratch);    // copy whole thing because it's an absolute softlink
     2371                paranoid_free(curr_fname);
     2372                asprintf(&curr_fname, scratch); // copy whole thing because it's an absolute softlink
    26432373            } else {            // copy over the basename cos it's a relative softlink
    26442374                p = curr_fname + strlen(curr_fname);
     
    26492379                    p++;
    26502380                }
    2651                 strcpy(p, scratch);
    2652             }
     2381                *p = '\0';
     2382                asprintf(&tmp, "%s%s", curr_fname, scratch);
     2383                paranoid_free(curr_fname);
     2384                curr_fname = tmp;
     2385            }
     2386            paranoid_free(scratch);
    26532387            lstat(curr_fname, &statbuf);
    26542388        }
    2655         strcpy(output, curr_fname);
     2389        asprintf(&output, curr_fname);
    26562390        log_it("resolved %s to %s", incoming, output);
    2657     }
    2658     paranoid_free(command);
    2659     paranoid_free(curr_fname);
    2660     paranoid_free(tmp);
     2391        paranoid_free(curr_fname);
     2392    }
    26612393    return (output);
    26622394}
     
    26672399/**
    26682400 * Return the type of partition format (GPT or MBR)
     2401 * Caller needs to free the return value
    26692402 */
    26702403char *which_partition_format(const char *drive)
    26712404{
    2672     static char output[4];
     2405    char *output;
    26732406    char *tmp;
    26742407    char *command;
    26752408    char *fdisk;
    26762409
    2677     malloc_string(tmp);
    2678     malloc_string(command);
    2679     malloc_string(fdisk);
    26802410    log_msg(0, "Looking for partition table format type");
    26812411// BERLIOS: Do that temporarily: we need to put back parted2fdisk everywhere
     
    26832413    struct stat buf;
    26842414
    2685     sprintf(fdisk, "/usr/local/bin/fdisk");
     2415    asprintf(&fdisk, "/usr/local/bin/fdisk");
    26862416    if (stat(fdisk, &buf) != 0) {
    2687 #endif
    2688         sprintf(fdisk, "/sbin/fdisk");
     2417        paranoid_free(fdisk);
     2418#endif
     2419        asprintf(&fdisk, "/sbin/fdisk");
    26892420#ifdef __IA64__
    26902421    }
    26912422#endif
    26922423    log_msg(1, "Using %s", fdisk);
    2693     sprintf(command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
    2694     strcpy(tmp, call_program_and_get_last_line_of_output(command));
     2424    asprintf(&command, "%s -l %s | grep 'EFI GPT'", fdisk, drive);
     2425    paranoid_free(fdisk);
     2426
     2427    asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     2428    paranoid_free(command);
     2429
    26952430    if (strstr(tmp, "GPT") == NULL) {
    2696         strcpy(output, "MBR");
    2697     } else {
    2698         strcpy(output, "GPT");
    2699     }
     2431        asprintf(&output, "MBR");
     2432    } else {
     2433        asprintf(&output, "GPT");
     2434    }
     2435    paranoid_free(tmp);
    27002436    log_msg(0, "Found %s partition table format type", output);
    2701     paranoid_free(command);
    2702     paranoid_free(tmp);
    2703     paranoid_free(fdisk);
    27042437    return (output);
    27052438}
Note: See TracChangeset for help on using the changeset viewer.