Changeset 2518 in MondoRescue for branches


Ignore:
Timestamp:
Jan 4, 2010, 7:42:06 PM (14 years ago)
Author:
Bruno Cornec
Message:

r3228@localhost (orig r2262): bruno | 2009-07-12 02:04:30 +0200

r3203@localhost: bruno | 2009-07-06 19:21:29 +0200

  • Replace sprintf by mr_asprintf in libmondo-tools.c


File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mondo/src/common/libmondo-tools.c

    r2506 r2518  
    368368    char *extra_cdrom_params = NULL;
    369369    char *mondo_mkisofs_sz = NULL;
    370     char *command;
     370    char *command = NULL;
    371371    char *mtpt;
    372372    char *hostname;
     
    375375    char *colon;
    376376    char *cdr_exe;
    377     char *tmp;
     377    char *tmp = NULL;
    378378    char call_before_iso_user[MAX_STR_LEN] = "\0";
    379379    /*
     
    381381    int rdsiz_MB;
    382382    */
    383     char *iso_dev;
    384     char *iso_mnt;
    385     char *iso_tmp;
    386     char *iso_path;
     383    char *iso_dev = NULL;
     384    char *iso_mnt = NULL;
     385    char *iso_tmp = NULL;
     386    char *iso_path = NULL;
    387387
    388388    assert(bkpinfo != NULL);
    389     malloc_string(command);
    390389    malloc_string(mtpt);
    391390    malloc_string(hostname);
    392391    malloc_string(cdr_exe);
    393     malloc_string(tmp);
    394     malloc_string(iso_dev);
    395     malloc_string(iso_mnt);
    396     malloc_string(iso_tmp);
    397     malloc_string(iso_path);
    398392    bkpinfo->optimal_set_size =
    399393        (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 16 : 16) *
     
    403397    if (bkpinfo->backup_media_type == tape) {
    404398        log_msg(1, "Bar");
    405         sprintf(tmp, "mt -f %s status", bkpinfo->media_device);
     399        mr_asprintf(&tmp, "mt -f %s status", bkpinfo->media_device);
    406400        log_msg(1, "tmp = '%s'", tmp);
    407401        if (run_program_and_log_output(tmp, 3)) {
     402            mr_free(tmp);
    408403            fatal_error
    409404                ("Unable to open tape device. If you haven't specified it with -d, do so. If you already have, check your parameter. I think it's wrong.");
    410405        }
     406        mr_free(tmp);
    411407    }
    412408    make_hole_for_dir(bkpinfo->scratchdir);
     
    480476        }
    481477        if (getenv ("SUDO_COMMAND")) {
    482             sprintf(command, "strings `which growisofs` | grep -c SUDO_COMMAND");
     478            mr_asprintf(&command, "strings `which growisofs` | grep -c SUDO_COMMAND");
    483479            if (!strcmp(call_program_and_get_last_line_of_output(command), "1")) {
     480                mr_free(command);
    484481                popup_and_OK("Fatal Error: Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
    485482                fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
    486483            }       
     484            mr_free(command);
    487485        }
    488486        log_msg(2, "call_make_iso (DVD res) is ... %s",
     
    556554    }                           // end of CD code
    557555
    558     /*
    559        if (bkpinfo->backup_data && bkpinfo->backup_media_type == tape)
    560        {
    561        sprintf (tmp,
    562        "dd if=/dev/zero of=%s bs=%ld count=32 2> /dev/null",
    563        bkpinfo->media_device, bkpinfo->internal_tape_block_size);
    564        if (system(tmp))
    565        {
    566        retval++;
    567        fprintf (stderr,
    568        "Cannot write to tape device. Is the tape set read-only?\n");
    569        }
    570        } // end of tape code
    571      */
    572 
    573 
    574556    if (bkpinfo->backup_media_type == iso) {
    575557
     
    582564
    583565        log_it("isodir = %s", bkpinfo->isodir);
    584         sprintf(command, "df -P %s | tail -n1 | cut -d' ' -f1",
     566        mr_asprintf(&command, "df -P %s | tail -n1 | cut -d' ' -f1",
    585567                bkpinfo->isodir);
    586568        log_it("command = %s", command);
    587         log_it("res of it = %s",
    588                call_program_and_get_last_line_of_output(command));
    589         sprintf(iso_dev, "%s",
    590                 call_program_and_get_last_line_of_output(command));
    591         sprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
    592         write_one_liner_data_file(tmp,
    593                                   call_program_and_get_last_line_of_output
    594                                   (command));
    595 
    596         sprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
    597                 iso_dev);
     569        log_it("res of it = %s", call_program_and_get_last_line_of_output(command));
     570        mr_asprintf(&iso_dev, "%s", call_program_and_get_last_line_of_output(command));
     571        mr_asprintf(&tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
     572        write_one_liner_data_file(tmp, call_program_and_get_last_line_of_output(command));
     573        mr_free(tmp);
     574        mr_free(command);
     575
     576        mr_asprintf(&command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3", iso_dev);
     577        mr_free(iso_dev);
     578
    598579        log_it("command = %s", command);
    599         log_it("res of it = %s",
    600                call_program_and_get_last_line_of_output(command));
    601         sprintf(iso_mnt, "%s",
    602                 call_program_and_get_last_line_of_output(command));
    603         sprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
    604         write_one_liner_data_file(tmp,
    605                                   call_program_and_get_last_line_of_output
    606                                   (command));
     580        log_it("res of it = %s", call_program_and_get_last_line_of_output(command));
     581        mr_asprintf(&iso_mnt, "%s", call_program_and_get_last_line_of_output(command));
     582        mr_asprintf(&tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
     583        write_one_liner_data_file(tmp, call_program_and_get_last_line_of_output(command));
     584        mr_free(tmp);
     585        mr_free(command);
     586
    607587        log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
    608         sprintf(iso_tmp, "%s", bkpinfo->isodir);
     588        mr_asprintf(&iso_tmp, "%s", bkpinfo->isodir);
    609589        if (strlen(iso_tmp) < strlen(iso_mnt)) {
    610             iso_path[0] = '\0';
     590            mr_asprintf(&iso_path, "%s", "");
    611591        } else {
    612             // If iso_mnt is only / then iso_path is the full dir
    613             // (the formula bellow doesn't work in this case)
    614             if (strcmp(iso_mnt, "/") == 0) {
    615                 sprintf(iso_path, "%s", iso_tmp);
    616             // else it's a part of iso_tmp
    617             } else {
    618                 sprintf(iso_path, "%s", iso_tmp + strlen(iso_mnt));
    619             }
    620         }
    621         sprintf(tmp, "%s/ISODIR", bkpinfo->tmpdir);
     592            mr_asprintf(&iso_path, "%s", iso_tmp + strlen(iso_mnt));
     593        }
     594        mr_free(iso_tmp);
     595
     596        mr_asprintf(&tmp, "%s/ISODIR", bkpinfo->tmpdir);
    622597        write_one_liner_data_file(tmp, iso_path);
     598        mr_free(tmp);
     599
    623600        log_it("isodir: %s", iso_path);
    624         sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
     601        mr_free(iso_path);
     602
     603        mr_asprintf(&tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
    625604        write_one_liner_data_file(tmp, bkpinfo->prefix);
     605        mr_free(tmp);
     606
    626607        log_it("iso-prefix: %s",  bkpinfo->prefix);
    627608
     
    669650    g_backup_media_type = bkpinfo->backup_media_type;
    670651    paranoid_free(mtpt);
    671     paranoid_free(command);
    672652    paranoid_free(hostname);
    673653    paranoid_free(cdr_exe);
    674     paranoid_free(tmp);
    675     paranoid_free(iso_dev);
    676     paranoid_free(iso_mnt);
    677     paranoid_free(iso_tmp);
    678     paranoid_free(iso_path);
    679654    return (retval);
    680655}
     
    834809long free_space_on_given_partition(char *partition)
    835810{
    836     char command[MAX_STR_LEN], out_sz[MAX_STR_LEN];
     811    char *out_sz = NULL;
     812    char *command = NULL;
    837813    long res;
    838814
    839815    assert_string_is_neither_NULL_nor_zerolength(partition);
    840816
    841     sprintf(command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
     817    mr_asprintf(&command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
    842818    if (system(command)) {
     819        mr_free(command);
    843820        return (-1);
    844821    }                           // partition does not exist
    845     sprintf(command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
     822    mr_free(command);
     823
     824    mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
    846825            partition);
    847     strcpy(out_sz, call_program_and_get_last_line_of_output(command));
     826    mr_asprintf(&out_sz, "%s", call_program_and_get_last_line_of_output(command));
     827    mr_free(command);
     828
    848829    if (strlen(out_sz) == 0) {
     830        mr_free(out_sz);
    849831        return (-1);
    850832    }                           // error within df, probably
    851833    res = atol(out_sz);
     834    mr_free(out_sz);
    852835    return (res);
    853836}
     
    870853
    871854    /*@ buffers ************ */
    872     char tmp[MAX_STR_LEN];
    873     //  char command[MAX_STR_LEN];
     855    char *tmp = NULL;
    874856
    875857    /*@ int's *************** */
     
    886868            ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
    887869    }
    888     strcpy(tmp,
     870    mr_asprintf(&tmp, "%s",
    889871           call_program_and_get_last_line_of_output
    890872           ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
     
    897879            ("WARNING! You have very little RAM. Please upgrade to 64MB or more.");
    898880    }
     881    mr_free(tmp);
    899882#endif
    900883
     
    958941    run_program_and_log_output
    959942        ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
    960     strcpy(tmp,
     943    mr_asprintf(&tmp, "%s",
    961944           call_program_and_get_last_line_of_output
    962945           ("mount | grep -E \"cdr(om|w)\""));
     
    973956            if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
    974957            retval++;
    975             fatal_error
    976                 ("Your CD-ROM drive is mounted. Please unmount it.");
    977         }
    978     }
     958            mr_free(tmp);
     959            fatal_error("Your CD-ROM drive is mounted. Please unmount it.");
     960        }
     961    }
     962    mr_free(tmp);
    979963
    980964    run_program_and_log_output("cat /etc/fstab", 5);
     
    992976        fatal_error("Please reinstall Mondo and Mindi.");
    993977    }
    994     sprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
     978    mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
    995979    if (run_program_and_log_output(tmp, 5)) {
    996         sprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test failed for some reason.", bkpinfo->tmpdir);
     980        mr_free(tmp);
     981        mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test failed for some reason.", bkpinfo->tmpdir);
    997982        log_to_screen(tmp);
    998983        log_to_screen
     
    1004989        retval++;
    1005990    }
     991    mr_free(tmp);
    1006992
    1007993    if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
     
    10301016{
    10311017    /*@ buffer ****************************************************** */
    1032     char command[MAX_STR_LEN * 2];
    1033     char tmp[MAX_STR_LEN];
     1018    char *command = NULL;
     1019    char *tmp = NULL;
    10341020
    10351021    /*@ end vars *************************************************** */
     
    10381024    assert_string_is_neither_NULL_nor_zerolength(label);
    10391025    if (!does_file_exist(config_file)) {
    1040         sprintf(tmp, "(read_cfg_var) Cannot find %s config file",
     1026        mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
    10411027                config_file);
    10421028        log_to_screen(tmp);
     1029        mr_free(tmp);
     1030
    10431031        value[0] = '\0';
    10441032        return (1);
     
    10471035        return (0);
    10481036    } else {
    1049         sprintf(command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
     1037        mr_asprintf(&command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
    10501038                label, config_file);
    10511039        strcpy(value, call_program_and_get_last_line_of_output(command));
     1040        mr_free(command);
     1041
    10521042        if (strlen(value) == 0) {
    10531043            return (1);
     
    11151105void stop_autofs_if_necessary()
    11161106{
    1117     char tmp[MAX_STR_LEN];
     1107    char *tmp = NULL;
    11181108
    11191109    g_autofs_exe[0] = '\0';
     
    11281118    } else {
    11291119        log_msg(3, "%s --- autofs detected", g_autofs_exe);
    1130 // FIXME -- only disable it if it's running ---  sprintf(tmp, "%s status", autofs_exe);
    1131         sprintf(tmp, "%s stop", g_autofs_exe);
     1120        // FIXME -- only disable it if it's running ---  sprintf(tmp, "%s status", autofs_exe);
     1121        mr_asprintf(&tmp, "%s stop", g_autofs_exe);
    11321122        if (run_program_and_log_output(tmp, 2)) {
    11331123            log_it("Failed to stop autofs - I assume it wasn't running");
     
    11361126            log_it("Stopped autofs OK");
    11371127        }
     1128        mr_free(tmp);
    11381129    }
    11391130}
     
    11441135void restart_autofs_if_necessary()
    11451136{
    1146     char tmp[MAX_STR_LEN];
     1137    char *tmp = NULL;
    11471138
    11481139    if (!g_autofs_stopped || !g_autofs_exe[0]) {
     
    11501141        return;
    11511142    }
    1152     sprintf(tmp, "%s start", g_autofs_exe);
     1143    mr_asprintf(&tmp, "%s start", g_autofs_exe);
    11531144    if (run_program_and_log_output(tmp, 2)) {
    11541145        log_it("Failed to start autofs");
     
    11571148        log_it("Started autofs OK");
    11581149    }
     1150    mr_free(tmp);
    11591151}
    11601152
     
    11651157void mount_boot_if_necessary()
    11661158{
    1167     char tmp[MAX_STR_LEN];
    1168     char command[MAX_STR_LEN];
     1159    char *tmp = NULL;
     1160    char *command = NULL;
    11691161
    11701162    log_msg(1, "Started sub");
     
    11721164    g_boot_mountpt[0] = '\0';
    11731165    log_msg(4, "Done. Great. Seeting command to something");
    1174     strcpy(command,
     1166    mr_asprintf(&command, "%s",
    11751167           "grep -v \":\" /etc/fstab | grep -vE '^#.*$' | grep -E \"[   ]/boot[     ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
    11761168    log_msg(4, "Cool. Command = '%s'", command);
    1177     strcpy(tmp, call_program_and_get_last_line_of_output(command));
     1169    mr_asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
     1170    mr_free(command);
     1171
    11781172    log_msg(4, "tmp = '%s'", tmp);
    11791173    if (tmp[0]) {
    11801174        log_it("/boot is at %s according to /etc/fstab", tmp);
    1181         strcpy(command, "mount | grep -Ew '/boot'");
    1182         strcpy(tmp, call_program_and_get_last_line_of_output(command));
     1175        mr_asprintf(&command, "mount | grep -Ew '/boot'");
     1176        mr_free(tmp);
     1177        mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     1178        mr_free(command);
     1179
    11831180        if (!strcmp(tmp,"")) {
    11841181            if ((strstr(tmp, "LABEL=") || strstr(tmp,"UUID="))) {
     
    11901187                }
    11911188            } else {
    1192                 sprintf(command, "mount | grep -E '^%s'", tmp);
     1189                mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
    11931190                log_msg(3, "command = %s", command);
    11941191                if (run_program_and_log_output(command, 5)) {
    11951192                    strcpy(g_boot_mountpt, tmp);
    1196                     sprintf(tmp,
     1193                    mr_free(tmp);
     1194                    mr_asprintf(&tmp,
    11971195                            "%s (your /boot partition) is not mounted. I'll mount it before backing up",
    11981196                            g_boot_mountpt);
    11991197                    log_it(tmp);
    1200                     sprintf(tmp, "mount %s", g_boot_mountpt);
     1198                    mr_free(tmp);
     1199
     1200                    mr_asprintf(&tmp, "mount %s", g_boot_mountpt);
    12011201                    if (run_program_and_log_output(tmp, 5)) {
    12021202                        g_boot_mountpt[0] = '\0';
     
    12101210                        }
    12111211                    }
     1212                    mr_free(tmp);
    12121213                }
     1214                mr_free(command);
    12131215            }
    12141216        }
     
    12231225void unmount_boot_if_necessary()
    12241226{
    1225     char tmp[MAX_STR_LEN];
     1227    char *tmp = NULL;
    12261228
    12271229    log_msg(3, "starting");
    12281230    if (g_boot_mountpt[0]) {
    1229         sprintf(tmp, "umount %s", g_boot_mountpt);
     1231        mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
    12301232        if (run_program_and_log_output(tmp, 5)) {
    12311233            log_it("WARNING - unable to unmount /boot");
    12321234        }
     1235        mr_free(tmp);
    12331236    }
    12341237    log_msg(3, "leaving");
     
    12481251{
    12491252    /*@ buffers ***************************************************** */
    1250     char command[MAX_STR_LEN * 2];
    1251     char tempfile[MAX_STR_LEN];
    1252     char tmp[MAX_STR_LEN];
     1253    char *command = NULL;
     1254    char *tempfile = NULL;
     1255    char *tmp = NULL;
    12531256
    12541257
     
    12581261    assert(value != NULL);
    12591262    if (!does_file_exist(config_file)) {
    1260         sprintf(tmp, "(write_cfg_file) Cannot find %s config file",
     1263        mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
    12611264                config_file);
    12621265        log_to_screen(tmp);
     1266        mr_free(tmp);
    12631267        return (1);
    12641268    }
    1265     sprintf(tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
     1269    mr_asprintf(&tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
    12661270    if (does_file_exist(config_file)) {
    1267         sprintf(command, "grep -vE '^%s .*$' %s > %s",
     1271        mr_sprintf(&command, "grep -vE '^%s .*$' %s > %s",
    12681272                label, config_file, tempfile);
    12691273        paranoid_system(command);
    1270     }
    1271     sprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
     1274        mr_free(command);
     1275    }
     1276    mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
    12721277    paranoid_system(command);
    1273     sprintf(command, "mv -f %s %s", tempfile, config_file);
     1278    mr_free(command);
     1279
     1280    mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
    12741281    paranoid_system(command);
     1282    mr_free(command);
    12751283    unlink(tempfile);
     1284    mr_free(tempfile);
    12761285    return (0);
    12771286}
     
    14371446void restart_magicdev_if_necessary()
    14381447{
    1439     char *tmp;
    1440 
    1441     malloc_string(tmp);
     1448    char *tmp = NULL;
     1449
    14421450    if (g_magicdev_command && g_magicdev_command[0]) {
    1443         sprintf(tmp, "%s &", g_magicdev_command);
     1451        mr_asprintf(&tmp, "%s &", g_magicdev_command);
    14441452        paranoid_system(tmp);
    1445     }
    1446     paranoid_free(tmp);
     1453        mr_free(tmp);
     1454    }
    14471455}
    14481456
Note: See TracChangeset for help on using the changeset viewer.