Ignore:
Timestamp:
Sep 20, 2013, 2:29:57 PM (11 years ago)
Author:
Bruno Cornec
Message:

Simplify the interface of mr_getline and mr_asprintf. With 3.1 compatibility now will allow backports from this branch into 3.0

File:
1 edited

Legend:

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

    r3154 r3185  
    224224
    225225    uname(&utsn);
    226     mr_asprintf(&tmp, "%s", utsn.machine);
     226    mr_asprintf(tmp, "%s", utsn.machine);
    227227    return (tmp);
    228228}
     
    400400        }
    401401        log_msg(1, "Tape");
    402         mr_asprintf(&tmp, "mt -f %s status", bkpinfo->media_device);
     402        mr_asprintf(tmp, "mt -f %s status", bkpinfo->media_device);
    403403        log_msg(1, "tmp = '%s'", tmp);
    404404        if (run_program_and_log_output(tmp, 3)) {
     
    446446        }
    447447        if (bkpinfo->nonbootable_backup) {
    448             mr_asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_NONBOOT);
     448            mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_NONBOOT);
    449449        } else if
    450450#ifdef __FreeBSD__
     
    455455#ifdef __IA64__
    456456    {
    457         mr_asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_ELILO);
     457        mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_ELILO);
    458458    }
    459459#else
    460460    {
    461         mr_asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
     461        mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_LILO);
    462462    }
    463463#endif
    464464        else
    465465        {
    466             mr_asprintf(&mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
     466            mr_asprintf(mondo_mkisofs_sz, MONDO_GROWISOFS_REGULAR_SYSLINUX);
    467467        }
    468468        if (bkpinfo->manual_cd_tray) {
     
    478478        }
    479479        if (getenv ("SUDO_COMMAND")) {
    480             mr_asprintf(&command, "strings `which growisofs` | grep -c SUDO_COMMAND");
     480            mr_asprintf(command, "strings `which growisofs` | grep -c SUDO_COMMAND");
    481481            if (!strcmp(call_program_and_get_last_line_of_output(command), "1")) {
    482482                mr_free(command);
     
    494494        || bkpinfo->backup_media_type == cdr) {
    495495        if (!bkpinfo->manual_cd_tray) {
    496             mr_asprintf(&extra_cdrom_params, "-waiti ");
     496            mr_asprintf(extra_cdrom_params, "-waiti ");
    497497        }
    498498        if (bkpinfo->backup_media_type == cdrw) {
    499             mr_asprintf(&extra_cdrom_params, "blank=fast ");
     499            mr_asprintf(extra_cdrom_params, "blank=fast ");
    500500        }
    501501        if (extra_cdrom_params == NULL) {
    502502            // If not initialized earlier, do it now
    503             mr_asprintf(&extra_cdrom_params, " ");
     503            mr_asprintf(extra_cdrom_params, " ");
    504504        }
    505505        if (find_home_of_exe("cdrecord")) {
     
    511511        }
    512512        if (find_home_of_exe("genisoimage")) {
    513             mr_asprintf(&isofs_cmd, "%s", MONDO_GENISOIMAGE_CMD);
     513            mr_asprintf(isofs_cmd, "%s", MONDO_GENISOIMAGE_CMD);
    514514        } else {
    515             mr_asprintf(&isofs_cmd, "%s", MONDO_MKISOFS_CMD);
     515            mr_asprintf(isofs_cmd, "%s", MONDO_MKISOFS_CMD);
    516516        }
    517517        if (bkpinfo->nonbootable_backup) {
    518             mr_asprintf(&mondo_mkisofs_sz, "%s%s", isofs_cmd, MONDO_MKISOFS);
     518            mr_asprintf(mondo_mkisofs_sz, "%s%s", isofs_cmd, MONDO_MKISOFS);
    519519        } else if
    520520#ifdef __FreeBSD__
     
    525525#ifdef __IA64__
    526526    {
    527         mr_asprintf(&mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_ELILO);
     527        mr_asprintf(mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_ELILO);
    528528    }
    529529#else
    530530    {
    531         mr_asprintf(&mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_LILO);
     531        mr_asprintf(mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_LILO);
    532532    }
    533533#endif
    534534        else
    535535        {
    536             mr_asprintf(&mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_SYSLINUX);
     536            mr_asprintf(mondo_mkisofs_sz, "%s%s -V _CD#_", isofs_cmd, MONDO_MKISOFS_REGULAR_SYSLINUX);
    537537        }
    538538        mr_free(isofs_cmd);
     
    577577
    578578        log_it("isodir = %s", bkpinfo->isodir);
    579         mr_asprintf(&command, "df -P %s | tail -n1 | cut -d' ' -f1",
     579        mr_asprintf(command, "df -P %s | tail -n1 | cut -d' ' -f1",
    580580                bkpinfo->isodir);
    581581        log_it("command = %s", command);
    582582        log_it("res of it = %s", call_program_and_get_last_line_of_output(command));
    583         mr_asprintf(&iso_dev, "%s", call_program_and_get_last_line_of_output(command));
    584         mr_asprintf(&tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
     583        mr_asprintf(iso_dev, "%s", call_program_and_get_last_line_of_output(command));
     584        mr_asprintf(tmp, "%s/ISO-DEV", bkpinfo->tmpdir);
    585585        write_one_liner_data_file(tmp, call_program_and_get_last_line_of_output(command));
    586586        mr_free(tmp);
    587587        mr_free(command);
    588588
    589         mr_asprintf(&command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3", iso_dev);
     589        mr_asprintf(command, "mount | grep -w %s | tail -n1 | cut -d' ' -f3", iso_dev);
    590590        mr_free(iso_dev);
    591591
    592592        log_it("command = %s", command);
    593593        log_it("res of it = %s", call_program_and_get_last_line_of_output(command));
    594         mr_asprintf(&iso_mnt, "%s", call_program_and_get_last_line_of_output(command));
    595         mr_asprintf(&tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
     594        mr_asprintf(iso_mnt, "%s", call_program_and_get_last_line_of_output(command));
     595        mr_asprintf(tmp, "%s/ISO-MNT", bkpinfo->tmpdir);
    596596        write_one_liner_data_file(tmp, call_program_and_get_last_line_of_output(command));
    597597        mr_free(tmp);
     
    599599
    600600        log_it("isomnt: %s, %d", iso_mnt, strlen(iso_mnt));
    601         mr_asprintf(&iso_tmp, "%s", bkpinfo->isodir);
     601        mr_asprintf(iso_tmp, "%s", bkpinfo->isodir);
    602602        if (strlen(iso_tmp) < strlen(iso_mnt)) {
    603             mr_asprintf(&iso_path, "%s", "");
     603            mr_asprintf(iso_path, "%s", "");
    604604        } else {
    605605            // If iso_mnt is only / then iso_path is the full dir
    606606            // (the formula bellow doesn't work in this case)
    607607            if (strcmp(iso_mnt, "/") == 0) {
    608                 mr_asprintf(&iso_path, "%s", iso_tmp);
     608                mr_asprintf(iso_path, "%s", iso_tmp);
    609609            // else it's a part of iso_tmp
    610610            } else {
    611                 mr_asprintf(&iso_path, "%s", iso_tmp + strlen(iso_mnt));
     611                mr_asprintf(iso_path, "%s", iso_tmp + strlen(iso_mnt));
    612612            }
    613613        }
     
    615615        mr_free(iso_mnt);
    616616
    617         mr_asprintf(&tmp, "%s/ISODIR", bkpinfo->tmpdir);
     617        mr_asprintf(tmp, "%s/ISODIR", bkpinfo->tmpdir);
    618618        write_one_liner_data_file(tmp, iso_path);
    619619        mr_free(tmp);
     
    622622        mr_free(iso_path);
    623623
    624         mr_asprintf(&tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
     624        mr_asprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
    625625        write_one_liner_data_file(tmp, bkpinfo->prefix);
    626626        mr_free(tmp);
     
    646646                colon++;
    647647                hent = gethostbyname(colon);
    648                 mr_asprintf(&(bkpinfo->netfs_user),"%s", hostname);
     648                mr_asprintf(bkpinfo->netfs_user,"%s", hostname);
    649649            } else {
    650650                hent = gethostbyname(hostname);
     
    655655                retval++;
    656656            } else {
    657                 mr_asprintf(&ip_address, "%s", inet_ntoa((struct in_addr) *((struct in_addr *) hent->h_addr)));
     657                mr_asprintf(ip_address, "%s", inet_ntoa((struct in_addr) *((struct in_addr *) hent->h_addr)));
    658658                mr_strcat(ip_address, strchr(bkpinfo->netfs_mount, ':'));
    659659                strcpy(bkpinfo->netfs_mount, ip_address);
     
    709709    }
    710710
    711     mr_asprintf(&tmp,"rm -Rf %s/changed.files*",MONDO_CACHE);
     711    mr_asprintf(tmp,"rm -Rf %s/changed.files*",MONDO_CACHE);
    712712    run_program_and_log_output(tmp, FALSE);
    713713    paranoid_free(tmp);
     
    750750        if (strstr(bkpinfo->tmpdir,"mondo.tmp.") != NULL) {
    751751            log_it("Purging old tmpdir %s", bkpinfo->tmpdir);
    752             mr_asprintf(&tmp,"rm -Rf %s",bkpinfo->tmpdir);
     752            mr_asprintf(tmp,"rm -Rf %s",bkpinfo->tmpdir);
    753753        } else {
    754754            log_it("Purging old tmpdir %s/mondo.tmp.*", bkpinfo->tmpdir);
    755             mr_asprintf(&tmp,"rm -Rf %s/mondo.tmp.*",bkpinfo->tmpdir);
     755            mr_asprintf(tmp,"rm -Rf %s/mondo.tmp.*",bkpinfo->tmpdir);
    756756        }
    757757        paranoid_system(tmp);
     
    761761    /* Always take in account arg first, then env, then default */
    762762    if (path != NULL) {
    763         mr_asprintf(&tmp, "%s/mondo.tmp.XXXXXX", path);
     763        mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", path);
    764764    } else if (getenv("TMPDIR")) {
    765         mr_asprintf(&tmp, "%s/mondo.tmp.XXXXXX", getenv("TMPDIR"));
     765        mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", getenv("TMPDIR"));
    766766    } else if (getenv("MRTMP")) {
    767         mr_asprintf(&tmp, "%s/mondo.tmp.XXXXXX", getenv("MRTMP"));
     767        mr_asprintf(tmp, "%s/mondo.tmp.XXXXXX", getenv("MRTMP"));
    768768    } else {
    769         mr_asprintf(&tmp, "/tmp/mondo.tmp.XXXXXX");
     769        mr_asprintf(tmp, "/tmp/mondo.tmp.XXXXXX");
    770770    }
    771771    p = mkdtemp(tmp);
     
    777777    paranoid_free(tmp);
    778778
    779     mr_asprintf(&tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
     779    mr_asprintf(tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
    780780    paranoid_system(tmp);
    781781    mr_free(tmp);
     
    794794        if (strstr(bkpinfo->scratchdir,"mondo.scratch.") != NULL) {
    795795            log_it("Purging old scratchdir %s", bkpinfo->scratchdir);
    796             mr_asprintf(&tmp,"rm -Rf %s",bkpinfo->scratchdir);
     796            mr_asprintf(tmp,"rm -Rf %s",bkpinfo->scratchdir);
    797797        } else {
    798798            log_it("Purging old scratchdir %s/mondo.scratch.*", bkpinfo->scratchdir);
    799             mr_asprintf(&tmp,"rm -Rf %s/mondo.scratch.*",bkpinfo->scratchdir);
     799            mr_asprintf(tmp,"rm -Rf %s/mondo.scratch.*",bkpinfo->scratchdir);
    800800        }
    801801        paranoid_system(tmp);
     
    805805    /* Always take in account arg first, then env, then default */
    806806    if (path != NULL) {
    807         mr_asprintf(&tmp, "%s/mondo.scratch.XXXXXX", path);
     807        mr_asprintf(tmp, "%s/mondo.scratch.XXXXXX", path);
    808808    } else if (getenv("MRSCRATCH")) {
    809         mr_asprintf(&tmp, "%s/mondo.scratch.XXXXXX", getenv("MRSCRATCH"));
     809        mr_asprintf(tmp, "%s/mondo.scratch.XXXXXX", getenv("MRSCRATCH"));
    810810    } else {
    811         mr_asprintf(&tmp, "/tmp/mondo.scratch.XXXXXX");
     811        mr_asprintf(tmp, "/tmp/mondo.scratch.XXXXXX");
    812812    }
    813813    p = mkdtemp(tmp);
     
    819819    paranoid_free(tmp);
    820820
    821     mr_asprintf(&tmp, "mkdir -p %s", bkpinfo->scratchdir);
     821    mr_asprintf(tmp, "mkdir -p %s", bkpinfo->scratchdir);
    822822    paranoid_system(tmp);
    823823    mr_free(tmp);
     
    909909    assert_string_is_neither_NULL_nor_zerolength(partition);
    910910
    911     mr_asprintf(&command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
     911    mr_asprintf(command, "df -m -P %s 1> /dev/null 2> /dev/null", partition);
    912912    if (system(command)) {
    913913        mr_free(command);
     
    916916    mr_free(command);
    917917
    918     mr_asprintf(&command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
     918    mr_asprintf(command, "df -m -P %s | tail -n1 | tr -s ' ' '\t' | cut -f4",
    919919            partition);
    920     mr_asprintf(&out_sz, "%s", call_program_and_get_last_line_of_output(command));
     920    mr_asprintf(out_sz, "%s", call_program_and_get_last_line_of_output(command));
    921921    mr_free(command);
    922922
     
    962962            ("ln -sf `which mkfs.msdos` /sbin/mkfs.vfat", FALSE);
    963963    }
    964     mr_asprintf(&tmp, "%s",
     964    mr_asprintf(tmp, "%s",
    965965           call_program_and_get_last_line_of_output
    966966           ("free | grep Mem | head -n1 | tr -s ' ' '\t' | cut -f2"));
     
    10281028    run_program_and_log_output
    10291029        ("umount `mount | grep cdr | cut -d' ' -f3 | tr '\n' ' '`", 5);
    1030     mr_asprintf(&tmp, "%s",
     1030    mr_asprintf(tmp, "%s",
    10311031           call_program_and_get_last_line_of_output
    10321032           ("mount | grep -E \"cdr(om|w)\""));
     
    10631063        fatal_error("Please reinstall Mondo and Mindi.");
    10641064    }
    1065     mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
     1065    mr_asprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test", bkpinfo->tmpdir);
    10661066    if (run_program_and_log_output(tmp, 5)) {
    10671067        mr_free(tmp);
    1068         mr_asprintf(&tmp, "mindi --makemountlist %s/mountlist.txt.test failed for some reason.", bkpinfo->tmpdir);
     1068        mr_asprintf(tmp, "mindi --makemountlist %s/mountlist.txt.test failed for some reason.", bkpinfo->tmpdir);
    10691069        log_to_screen(tmp);
    10701070        log_to_screen
     
    11111111    assert_string_is_neither_NULL_nor_zerolength(label);
    11121112    if (!does_file_exist(config_file)) {
    1113         mr_asprintf(&tmp, "(read_cfg_var) Cannot find %s config file",
     1113        mr_asprintf(tmp, "(read_cfg_var) Cannot find %s config file",
    11141114                config_file);
    11151115        log_to_screen(tmp);
     
    11221122        return (0);
    11231123    } else {
    1124         mr_asprintf(&command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
     1124        mr_asprintf(command, "grep '%s .*' %s| cut -d' ' -f2,3,4,5",
    11251125                label, config_file);
    11261126        strcpy(value, call_program_and_get_last_line_of_output(command));
     
    12061206        log_msg(3, "%s --- autofs detected", g_autofs_exe);
    12071207        // FIXME -- only disable it if it's running ---  sprintf(tmp, "%s status", autofs_exe);
    1208         mr_asprintf(&tmp, "%s stop", g_autofs_exe);
     1208        mr_asprintf(tmp, "%s stop", g_autofs_exe);
    12091209        if (run_program_and_log_output(tmp, 2)) {
    12101210            log_it("Failed to stop autofs - I assume it wasn't running");
     
    12281228        return;
    12291229    }
    1230     mr_asprintf(&tmp, "%s start", g_autofs_exe);
     1230    mr_asprintf(tmp, "%s start", g_autofs_exe);
    12311231    if (run_program_and_log_output(tmp, 2)) {
    12321232        log_it("Failed to start autofs");
     
    12511251    g_boot_mountpt[0] = '\0';
    12521252    log_msg(4, "Done. Great. Seeting command to something");
    1253     mr_asprintf(&command, "%s",
     1253    mr_asprintf(command, "%s",
    12541254           "grep -v \":\" /etc/fstab | grep -vE '^#.*$' | grep -E \"[   ]/boot[     ]\" | tr -s ' ' '\t' | cut -f1 | head -n1");
    12551255    log_msg(4, "Cool. Command = '%s'", command);
    1256     mr_asprintf(&tmp, "%s", call_program_and_get_last_line_of_output(command));
     1256    mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
    12571257    mr_free(command);
    12581258
     
    12601260    if (tmp[0]) {
    12611261        log_it("/boot is at %s according to /etc/fstab", tmp);
    1262         mr_asprintf(&command, "mount | grep -Ew '/boot'");
     1262        mr_asprintf(command, "mount | grep -Ew '/boot'");
    12631263        mr_free(tmp);
    1264         mr_asprintf(&tmp, call_program_and_get_last_line_of_output(command));
     1264        mr_asprintf(tmp, call_program_and_get_last_line_of_output(command));
    12651265        mr_free(command);
    12661266
     
    12741274                }
    12751275            } else {
    1276                 mr_asprintf(&command, "mount | grep -E '^%s'", tmp);
     1276                mr_asprintf(command, "mount | grep -E '^%s'", tmp);
    12771277                log_msg(3, "command = %s", command);
    12781278                if (run_program_and_log_output(command, 5)) {
    12791279                    strcpy(g_boot_mountpt, tmp);
    12801280                    mr_free(tmp);
    1281                     mr_asprintf(&tmp,
     1281                    mr_asprintf(tmp,
    12821282                            "%s (your /boot partition) is not mounted. I'll mount it before backing up",
    12831283                            g_boot_mountpt);
     
    12851285                    mr_free(tmp);
    12861286
    1287                     mr_asprintf(&tmp, "mount %s", g_boot_mountpt);
     1287                    mr_asprintf(tmp, "mount %s", g_boot_mountpt);
    12881288                    if (run_program_and_log_output(tmp, 5)) {
    12891289                        g_boot_mountpt[0] = '\0';
     
    13161316    log_msg(3, "starting");
    13171317    if (g_boot_mountpt[0]) {
    1318         mr_asprintf(&tmp, "umount %s", g_boot_mountpt);
     1318        mr_asprintf(tmp, "umount %s", g_boot_mountpt);
    13191319        if (run_program_and_log_output(tmp, 5)) {
    13201320            log_it("WARNING - unable to unmount /boot");
     
    13481348    assert(value != NULL);
    13491349    if (!does_file_exist(config_file)) {
    1350         mr_asprintf(&tmp, "(write_cfg_file) Cannot find %s config file",
     1350        mr_asprintf(tmp, "(write_cfg_file) Cannot find %s config file",
    13511351                config_file);
    13521352        log_to_screen(tmp);
     
    13541354        return (1);
    13551355    }
    1356     mr_asprintf(&tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
     1356    mr_asprintf(tempfile, "%s/mojo-jojo.blah", bkpinfo->tmpdir);
    13571357    if (does_file_exist(config_file)) {
    1358         mr_asprintf(&command, "grep -vE '^%s .*$' %s > %s",
     1358        mr_asprintf(command, "grep -vE '^%s .*$' %s > %s",
    13591359                label, config_file, tempfile);
    13601360        paranoid_system(command);
    13611361        mr_free(command);
    13621362    }
    1363     mr_asprintf(&command, "echo \"%s %s\" >> %s", label, value, tempfile);
     1363    mr_asprintf(command, "echo \"%s %s\" >> %s", label, value, tempfile);
    13641364    paranoid_system(command);
    13651365    mr_free(command);
    13661366
    1367     mr_asprintf(&command, "mv -f %s %s", tempfile, config_file);
     1367    mr_asprintf(command, "mv -f %s %s", tempfile, config_file);
    13681368    paranoid_system(command);
    13691369    mr_free(command);
     
    15361536
    15371537    if (g_magicdev_command && g_magicdev_command[0]) {
    1538         mr_asprintf(&tmp, "%s &", g_magicdev_command);
     1538        mr_asprintf(tmp, "%s &", g_magicdev_command);
    15391539        paranoid_system(tmp);
    15401540        mr_free(tmp);
Note: See TracChangeset for help on using the changeset viewer.