Changeset 2262 in MondoRescue


Ignore:
Timestamp:
Jul 12, 2009, 2:04:30 AM (15 years ago)
Author:
Bruno Cornec
Message:

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.10/mondo/src/common/libmondo-tools.c

    r2241 r2262  
    414414    char *extra_cdrom_params = NULL;
    415415    char *mondo_mkisofs_sz = NULL;
    416     char *command;
     416    char *command = NULL;
    417417    char *mtpt;
    418418    char *hostname;
     
    421421    char *colon;
    422422    char *cdr_exe;
    423     char *tmp;
     423    char *tmp = NULL;
    424424    char call_before_iso_user[MAX_STR_LEN] = "\0";
    425425    /*
     
    427427    int rdsiz_MB;
    428428    */
    429     char *iso_dev;
    430     char *iso_mnt;
    431     char *iso_tmp;
    432     char *iso_path;
     429    char *iso_dev = NULL;
     430    char *iso_mnt = NULL;
     431    char *iso_tmp = NULL;
     432    char *iso_path = NULL;
    433433
    434434    assert(bkpinfo != NULL);
    435     malloc_string(command);
    436435    malloc_string(mtpt);
    437436    malloc_string(hostname);
    438437    malloc_string(cdr_exe);
    439     malloc_string(tmp);
    440     malloc_string(iso_dev);
    441     malloc_string(iso_mnt);
    442     malloc_string(iso_tmp);
    443     malloc_string(iso_path);
    444438    bkpinfo->optimal_set_size =
    445439        (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type) ? 16 : 16) *
     
    449443    if (bkpinfo->backup_media_type == tape) {
    450444        log_msg(1, "Bar");
    451         sprintf(tmp, "mt -f %s status", bkpinfo->media_device);
     445        mr_asprintf(&tmp, "mt -f %s status", bkpinfo->media_device);
    452446        log_msg(1, "tmp = '%s'", tmp);
    453447        if (run_program_and_log_output(tmp, 3)) {
     448            mr_free(tmp);
    454449            fatal_error
    455450                ("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.");
    456451        }
     452        mr_free(tmp);
    457453    }
    458454    make_hole_for_dir(bkpinfo->scratchdir);
     
    526522        }
    527523        if (getenv ("SUDO_COMMAND")) {
    528             sprintf(command, "strings `which growisofs` | grep -c SUDO_COMMAND");
     524            mr_asprintf(&command, "strings `which growisofs` | grep -c SUDO_COMMAND");
    529525            if (!strcmp(call_program_and_get_last_line_of_output(command), "1")) {
     526                mr_free(command);
    530527                popup_and_OK("Fatal Error: Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
    531528                fatal_error("Can't write DVDs as sudo because growisofs doesn't support this - see the growisofs manpage for details.");
    532529            }       
     530            mr_free(command);
    533531        }
    534532        log_msg(2, "call_make_iso (DVD res) is ... %s",
     
    602600    }                           // end of CD code
    603601
    604     /*
    605        if (bkpinfo->backup_data && bkpinfo->backup_media_type == tape)
    606        {
    607        sprintf (tmp,
    608        "dd if=/dev/zero of=%s bs=%ld count=32 2> /dev/null",
    609        bkpinfo->media_device, bkpinfo->internal_tape_block_size);
    610        if (system(tmp))
    611        {
    612        retval++;
    613        fprintf (stderr,
    614        "Cannot write to tape device. Is the tape set read-only?\n");
    615        }
    616        } // end of tape code
    617      */
    618 
    619 
    620602    if (bkpinfo->backup_media_type == iso) {
    621603
     
    628610
    629611        log_it("isodir = %s", bkpinfo->isodir);
    630         sprintf(command, "df -P %s | tail -n1 | cut -d' ' -f1",
     612        mr_asprintf(&command, "df -P %s | tail -n1 | cut -d' ' -f1",
    631613                bkpinfo->isodir);
    632614        log_it("command = %s", command);
    633         log_it("res of it = %s",
    634                call_program_and_get_last_line_of_output(command));
    635         sprintf(iso_dev, "%s",
    636                 call_program_and_get_last_line_of_output(command));
    637         sprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
    638         write_one_liner_data_file(tmp,
    639                                   call_program_and_get_last_line_of_output
    640                                   (command));
    641 
    642         sprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3",
    643                 iso_dev);
     615        log_it("res of it = %s", call_program_and_get_last_line_of_output(command));
     616        mr_asprintf(&iso_dev, "%s", call_program_and_get_last_line_of_output(command));
     617        mr_asprintf(&tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
     618        write_one_liner_data_file(tmp, call_program_and_get_last_line_of_output(command));
     619        mr_free(tmp);
     620        mr_free(command);
     621
     622        mr_asprintf(&command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3", iso_dev);
     623        mr_free(iso_dev);
     624
    644625        log_it("command = %s", command);
    645         log_it("res of it = %s",
    646                call_program_and_get_last_line_of_output(command));
    647         sprintf(iso_mnt, "%s",
    648                 call_program_and_get_last_line_of_output(command));
    649         sprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
    650         write_one_liner_data_file(tmp,
    651                                   call_program_and_get_last_line_of_output
    652                                   (command));
     626        log_it("res of it = %s", call_program_and_get_last_line_of_output(command));
     627        mr_asprintf(&iso_mnt, "%s", call_program_and_get_last_line_of_output(command));
     628        mr_asprintf(&tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
     629        write_one_liner_data_file(tmp, call_program_and_get_last_line_of_output(command));
     630        mr_free(tmp);
     631        mr_free(command);
     632
    653633        log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
    654         sprintf(iso_tmp, "%s", bkpinfo->isodir);
     634        mr_asprintf(&iso_tmp, "%s", bkpinfo->isodir);
    655635        if (strlen(iso_tmp) < strlen(iso_mnt)) {
    656             iso_path[0] = '\0';
     636            mr_asprintf(&iso_path, "%s", "");
    657637        } else {
    658             sprintf(iso_path, "%s", iso_tmp + strlen(iso_mnt));
    659         }
    660         sprintf(tmp, "%s/ISODIR", bkpinfo->tmpdir);
     638            mr_asprintf(&iso_path, "%s", iso_tmp + strlen(iso_mnt));
     639        }
     640        mr_free(iso_tmp);
     641
     642        mr_asprintf(&tmp, "%s/ISODIR", bkpinfo->tmpdir);
    661643        write_one_liner_data_file(tmp, iso_path);
     644        mr_free(tmp);
     645
    662646        log_it("isodir: %s", iso_path);
    663         sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
     647        mr_free(iso_path);
     648
     649        mr_asprintf(&tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
    664650        write_one_liner_data_file(tmp, bkpinfo->prefix);
     651        mr_free(tmp);
     652
    665653        log_it("iso-prefix: %s",  bkpinfo->prefix);
    666654
     
    710698    g_backup_media_type = bkpinfo->backup_media_type;
    711699    paranoid_free(mtpt);
    712     paranoid_free(command);
    713700    paranoid_free(hostname);
    714701    paranoid_free(cdr_exe);
    715     paranoid_free(tmp);
    716     paranoid_free(iso_dev);
    717     paranoid_free(iso_mnt);
    718     paranoid_free(iso_tmp);
    719     paranoid_free(iso_path);
    720702    return (retval);
    721703}
     
    872854long free_space_on_given_partition(char *partition)
    873855{
    874     char command[MAX_STR_LEN], out_sz[MAX_STR_LEN];
     856    char *out_sz = NULL;
     857    char *command = NULL;
    875858    long res;
    876859
    877860    assert_string_is_neither_NULL_nor_zerolength(partition);
    878861
    879     sprintf(command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
     862    mr_asprintf(&command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
    880863    if (system(command)) {
     864        mr_free(command);
    881865        return (-1);
    882866    }                           // partition does not exist
    883     sprintf(command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
     867    mr_free(command);
     868
     869    mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
    884870            partition);
    885     strcpy(out_sz, call_program_and_get_last_line_of_output(command));
     871    mr_asprintf(&out_sz, "%s", call_program_and_get_last_line_of_output(command));
     872    mr_free(command);
     873
    886874    if (strlen(out_sz) == 0) {
     875        mr_free(out_sz);
    887876        return (-1);
    888877    }                           // error within df, probably
    889878    res = atol(out_sz);
     879    mr_free(out_sz);
    890880    return (res);
    891881}
     
    908898
    909899    /*@ buffers ************ */
    910     char tmp[MAX_STR_LEN];
    911     //  char command[MAX_STR_LEN];
     900    char *tmp = NULL;
    912901
    913902    /*@ int's *************** */
     
    924913            ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
    925914    }
    926     strcpy(tmp,
     915    mr_asprintf(&tmp, "%s",
    927916           call_program_and_get_last_line_of_output
    928917           ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
     
    935924            ("WARNING! You have very little RAM. Please upgrade to 64MB or more.");
    936925    }
     926    mr_free(tmp);
    937927#endif
    938928
     
    996986    run_program_and_log_output
    997987        ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
    998     strcpy(tmp,
     988    mr_asprintf(&tmp, "%s",
    999989           call_program_and_get_last_line_of_output
    1000990           ("mount | grep -E \"cdr(om|w)\""));
     
    10111001            if (run_program_and_log_output("uname -a | grep Knoppix", 5)) {
    10121002            retval++;
    1013             fatal_error
    1014                 ("Your CD-ROM drive is mounted. Please unmount it.");
    1015         }
    1016     }
     1003            mr_free(tmp);
     1004            fatal_error("Your CD-ROM drive is mounted. Please unmount it.");
     1005        }
     1006    }
     1007    mr_free(tmp);
    10171008
    10181009    run_program_and_log_output("cat /etc/fstab", 5);
     
    10301021        fatal_error("Please reinstall Mondo and Mindi.");
    10311022    }
    1032     sprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
     1023    mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
    10331024    if (run_program_and_log_output(tmp, 5)) {
    1034         sprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test failed for some reason.", bkpinfo->tmpdir);
     1025        mr_free(tmp);
     1026        mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test failed for some reason.", bkpinfo->tmpdir);
    10351027        log_to_screen(tmp);
    10361028        log_to_screen
     
    10421034        retval++;
    10431035    }
     1036    mr_free(tmp);
    10441037
    10451038    if (!run_program_and_log_output("parted2fdisk -l | grep -i raid", 1)
     
    10681061{
    10691062    /*@ buffer ****************************************************** */
    1070     char command[MAX_STR_LEN * 2];
    1071     char tmp[MAX_STR_LEN];
     1063    char *command = NULL;
     1064    char *tmp = NULL;
    10721065
    10731066    /*@ end vars *************************************************** */
     
    10761069    assert_string_is_neither_NULL_nor_zerolength(label);
    10771070    if (!does_file_exist(config_file)) {
    1078         sprintf(tmp, "(read_cfg_var) Cannot find %s config file",
     1071        mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
    10791072                config_file);
    10801073        log_to_screen(tmp);
     1074        mr_free(tmp);
     1075
    10811076        value[0] = '\0';
    10821077        return (1);
     
    10851080        return (0);
    10861081    } else {
    1087         sprintf(command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
     1082        mr_asprintf(&command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
    10881083                label, config_file);
    10891084        strcpy(value, call_program_and_get_last_line_of_output(command));
     1085        mr_free(command);
     1086
    10901087        if (strlen(value) == 0) {
    10911088            return (1);
     
    11531150void stop_autofs_if_necessary()
    11541151{
    1155     char tmp[MAX_STR_LEN];
     1152    char *tmp = NULL;
    11561153
    11571154    g_autofs_exe[0] = '\0';
     
    11661163    } else {
    11671164        log_msg(3, "%s --- autofs detected", g_autofs_exe);
    1168 // FIXME -- only disable it if it's running ---  sprintf(tmp, "%s status", autofs_exe);
    1169         sprintf(tmp, "%s stop", g_autofs_exe);
     1165        // FIXME -- only disable it if it's running ---  sprintf(tmp, "%s status", autofs_exe);
     1166        mr_asprintf(&tmp, "%s stop", g_autofs_exe);
    11701167        if (run_program_and_log_output(tmp, 2)) {
    11711168            log_it("Failed to stop autofs - I assume it wasn't running");
     
    11741171            log_it("Stopped autofs OK");
    11751172        }
     1173        mr_free(tmp);
    11761174    }
    11771175}
     
    11821180void restart_autofs_if_necessary()
    11831181{
    1184     char tmp[MAX_STR_LEN];
     1182    char *tmp = NULL;
    11851183
    11861184    if (!g_autofs_stopped || !g_autofs_exe[0]) {
     
    11881186        return;
    11891187    }
    1190     sprintf(tmp, "%s start", g_autofs_exe);
     1188    mr_asprintf(&tmp, "%s start", g_autofs_exe);
    11911189    if (run_program_and_log_output(tmp, 2)) {
    11921190        log_it("Failed to start autofs");
     
    11951193        log_it("Started autofs OK");
    11961194    }
     1195    mr_free(tmp);
    11971196}
    11981197
     
    12031202void mount_boot_if_necessary()
    12041203{
    1205     char tmp[MAX_STR_LEN];
    1206     char command[MAX_STR_LEN];
     1204    char *tmp = NULL;
     1205    char *command = NULL;
    12071206
    12081207    log_msg(1, "Started sub");
     
    12101209    g_boot_mountpt[0] = '\0';
    12111210    log_msg(4, "Done. Great. Seeting command to something");
    1212     strcpy(command,
     1211    mr_asprintf(&command, "%s",
    12131212           "grep -v \":\" /etc/fstab | grep -vE '^#.*$' | grep -E \"[   ]/boot[     ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
    12141213    log_msg(4, "Cool. Command = '%s'", command);
    1215     strcpy(tmp, call_program_and_get_last_line_of_output(command));
     1214    mr_asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
     1215    mr_free(command);
     1216
    12161217    log_msg(4, "tmp = '%s'", tmp);
    12171218    if (tmp[0]) {
    12181219        log_it("/boot is at %s according to /etc/fstab", tmp);
    1219         strcpy(command, "mount | grep -Ew '/boot'");
    1220         strcpy(tmp, call_program_and_get_last_line_of_output(command));
     1220        mr_asprintf(&command, "mount | grep -Ew '/boot'");
     1221        mr_free(tmp);
     1222        mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     1223        mr_free(command);
     1224
    12211225        if (!strcmp(tmp,"")) {
    12221226            if ((strstr(tmp, "LABEL=") || strstr(tmp,"UUID="))) {
     
    12281232                }
    12291233            } else {
    1230                 sprintf(command, "mount | grep -E '^%s'", tmp);
     1234                mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
    12311235                log_msg(3, "command = %s", command);
    12321236                if (run_program_and_log_output(command, 5)) {
    12331237                    strcpy(g_boot_mountpt, tmp);
    1234                     sprintf(tmp,
     1238                    mr_free(tmp);
     1239                    mr_asprintf(&tmp,
    12351240                            "%s (your /boot partition) is not mounted. I'll mount it before backing up",
    12361241                            g_boot_mountpt);
    12371242                    log_it(tmp);
    1238                     sprintf(tmp, "mount %s", g_boot_mountpt);
     1243                    mr_free(tmp);
     1244
     1245                    mr_asprintf(&tmp, "mount %s", g_boot_mountpt);
    12391246                    if (run_program_and_log_output(tmp, 5)) {
    12401247                        g_boot_mountpt[0] = '\0';
     
    12481255                        }
    12491256                    }
     1257                    mr_free(tmp);
    12501258                }
     1259                mr_free(command);
    12511260            }
    12521261        }
     
    12611270void unmount_boot_if_necessary()
    12621271{
    1263     char tmp[MAX_STR_LEN];
     1272    char *tmp = NULL;
    12641273
    12651274    log_msg(3, "starting");
    12661275    if (g_boot_mountpt[0]) {
    1267         sprintf(tmp, "umount %s", g_boot_mountpt);
     1276        mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
    12681277        if (run_program_and_log_output(tmp, 5)) {
    12691278            log_it("WARNING - unable to unmount /boot");
    12701279        }
     1280        mr_free(tmp);
    12711281    }
    12721282    log_msg(3, "leaving");
     
    12861296{
    12871297    /*@ buffers ***************************************************** */
    1288     char command[MAX_STR_LEN * 2];
    1289     char tempfile[MAX_STR_LEN];
    1290     char tmp[MAX_STR_LEN];
     1298    char *command = NULL;
     1299    char *tempfile = NULL;
     1300    char *tmp = NULL;
    12911301
    12921302
     
    12961306    assert(value != NULL);
    12971307    if (!does_file_exist(config_file)) {
    1298         sprintf(tmp, "(write_cfg_file) Cannot find %s config file",
     1308        mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
    12991309                config_file);
    13001310        log_to_screen(tmp);
     1311        mr_free(tmp);
    13011312        return (1);
    13021313    }
    1303     sprintf(tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
     1314    mr_asprintf(&tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
    13041315    if (does_file_exist(config_file)) {
    1305         sprintf(command, "grep -vE '^%s .*$' %s > %s",
     1316        mr_sprintf(&command, "grep -vE '^%s .*$' %s > %s",
    13061317                label, config_file, tempfile);
    13071318        paranoid_system(command);
    1308     }
    1309     sprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
     1319        mr_free(command);
     1320    }
     1321    mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
    13101322    paranoid_system(command);
    1311     sprintf(command, "mv -f %s %s", tempfile, config_file);
     1323    mr_free(command);
     1324
     1325    mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
    13121326    paranoid_system(command);
     1327    mr_free(command);
    13131328    unlink(tempfile);
     1329    mr_free(tempfile);
    13141330    return (0);
    13151331}
     
    14751491void restart_magicdev_if_necessary()
    14761492{
    1477     char *tmp;
    1478 
    1479     malloc_string(tmp);
     1493    char *tmp = NULL;
     1494
    14801495    if (g_magicdev_command && g_magicdev_command[0]) {
    1481         sprintf(tmp, "%s &", g_magicdev_command);
     1496        mr_asprintf(&tmp, "%s &", g_magicdev_command);
    14821497        paranoid_system(tmp);
    1483     }
    1484     paranoid_free(tmp);
     1498        mr_free(tmp);
     1499    }
    14851500}
    14861501
Note: See TracChangeset for help on using the changeset viewer.