Ignore:
Timestamp:
Sep 25, 2013, 8:55:39 AM (11 years ago)
Author:
Bruno Cornec
Message:
  • Backport tons of dynamic memory management rewrite from 3.1 branch. Test is needed
File:
1 edited

Legend:

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

    r3185 r3188  
    5151    if (c && strncmp(c, "/disc", 5) == 0) {
    5252        /* yup it's devfs, return the "part" path */
     53        /* This strcpy is safe */
    5354        strcpy(c + 1, "part");
    5455        p = c + 5;
     
    6566    }
    6667    sprintf(p, "%d", partno);
    67     return (partition);
    68 }
    69 
    70 
    71 
    72 
    73 
    74 
    75 
    76 
    77 
    78 
    79 
     68    return(partition);
     69}
    8070
    8171
     
    118108
    119109
    120 
    121 
    122 inline void turn_wildcard_chars_into_literal_chars(char *sout, char *sin)
    123 {
    124     char *p, *q;
    125 
    126     for (p = sin, q = sout; *p != '\0'; *(q++) = *(p++)) {
    127         if (strchr("[]*?", *p)) {
    128             *(q++) = '\\';
    129         }
    130     }
    131     *q = *p;                    // for the final '\0'
    132 }
    133 
    134 
    135 
    136110/**
    137111 * Add commas every third place in @p input.
     
    155129        tmp[j - 6] = ',';
    156130        strcpy(tmp + j - 5, pos_w_commas + j - 6);
    157 //      tmp[j-2] = ',';
    158 //      strcpy(tmp+j-1, pos_w_commas+j-3);
    159131        strcpy(pos_w_commas, tmp);
    160132    }
     
    170142    return (output);
    171143}
    172 
    173 
    174 
    175 
    176 
    177 
    178 
    179144
    180145
     
    217182    long outval;
    218183    int i;
    219     char *tmp;
     184    char *tmp = NULL;
    220185    char ch;
    221186
     
    228193        fatal_error("Please use integers only. No decimal points.");
    229194    }
    230     malloc_string(tmp);
    231     strcpy(tmp, incoming);
     195
     196    mr_asprintf(tmp, "%s", incoming);
    232197    i = (int) strlen(tmp);
    233198    if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') {
     
    238203    tmp[i] = '\0';
    239204    outval = atol(tmp);
     205    mr_free(tmp);
     206
    240207    if (ch == 'g' || ch == 'G') {
    241208        outval = outval * 1024;
     
    253220        fatal_error("Integer overflow.");
    254221    } else if (ch != 'm' && ch != 'M') {
    255         sprintf(tmp, "Re: parameter '%s' - bad multiplier ('%c')",
    256                 incoming, ch);
     222        mr_asprintf(tmp, "Re: parameter '%s' - bad multiplier ('%c')", incoming, ch);
    257223        fatal_error(tmp);
    258224    }
    259     paranoid_free(tmp);
    260225    return (outval);
    261226}
     
    516481
    517482    input = malloc(2000);
    518     // BERLIOS: seems to cause invalid errors
    519     //assert_string_is_neither_NULL_nor_zerolength(ip);
    520     assert_string_is_neither_NULL_nor_zerolength(token);
    521483    assert(value != NULL);
    522484
     
    557519    /*@ buffers **************************************************** */
    558520    static char output[MAX_STR_LEN];
    559     static char suffix[MAX_STR_LEN];
     521    char *suffix = NULL;
    560522
    561523    /*@ end vars *************************************************** */
     
    563525    assert_string_is_neither_NULL_nor_zerolength(path);
    564526    if (s[0] != '\0') {
    565         sprintf(suffix, ".%s", s);
     527        mr_asprintf(suffix, ".%s", s);
    566528    } else {
    567         suffix[0] = '\0';
    568     }
    569     sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno,
    570             suffix);
     529        mr_asprintf(suffix, "");
     530    }
     531    sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno, suffix);
     532    mr_free(suffix);
    571533    return (output);
    572534}
     
    646608    numA = atol(stringA + start_of_numbers_in_A);
    647609    numB = atol(stringB + start_of_numbers_in_B);
    648     /*
    649        sprintf(tmp,"Comparing %s and %s --> %ld,%ld\n",stringA,stringB,numA,numB);
    650        log_to_screen(tmp);
    651      */
    652610    return ((int) (numA - numB));
    653611}
     
    775733    in_out[i] = '\0';
    776734    paranoid_free(tmp);
    777 /*  for(i=strlen(in_out); i>0 && in_out[i-1]<=32; i--) {in_out[i-1]='\0';} */
    778735}
    779736
     
    909866 * @return The severity (1-3).
    910867 */
    911 int severity_of_difference(char *fn, char *out_reason)
    912 {
    913     int sev;
    914     char *reason;
    915     char *filename;
    916 
    917     malloc_string(reason);
    918     malloc_string(filename);
    919 // out_reason might be null on purpose, so don't bomb if it is :) OK?
     868int severity_of_difference(char *fn, char *out_reason) {
     869
     870    int sev = 3;
     871    char *reason = NULL;
     872    char *filename = NULL;
     873
     874    // out_reason might be null on purpose, so don't bomb if it is :) OK?
    920875    assert_string_is_neither_NULL_nor_zerolength(fn);
    921876    if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
    922         strcpy(filename, fn + strlen(MNT_RESTORING));
     877        mr_asprintf(filename, "%s", fn + strlen(MNT_RESTORING));
    923878    } else if (fn[0] != '/') {
    924         sprintf(filename, "/%s", fn);
     879        mr_asprintf(filename, "/%s", fn);
    925880    } else {
    926         strcpy(filename, fn);
    927     }
    928 
    929     sev = 3;
    930     sprintf(reason,
    931             "Changed since backup. Consider running a differential backup in a day or two.");
     881        mr_asprintf(filename, "%s", fn);
     882    }
     883
     884    mr_asprintf(reason, "Changed since backup. Consider running a differential backup in a day or two.");
     885
    932886    if (!strncmp(filename, "/var/", 5)) {
    933887        sev = 2;
    934         sprintf(reason,
    935                 "/var's contents will change regularly, inevitably.");
     888        mr_free(reason);
     889        mr_asprintf(reason, "/var's contents will change regularly, inevitably.");
    936890    }
    937891    if (!strncmp(filename, "/home", 5)) {
    938892        sev = 2;
    939         sprintf(reason,
    940                 "It's in your /home directory. Therefore, it is important.");
     893        mr_free(reason);
     894        mr_asprintf(reason, "It's in your /home directory. Therefore, it is important.");
    941895    }
    942896    if (!strncmp(filename, "/usr/", 5)) {
    943897        sev = 3;
    944         sprintf(reason,
    945                 "You may have installed/removed software during the backup.");
     898        mr_free(reason);
     899        mr_asprintf(reason, "You may have installed/removed software during the backup.");
    946900    }
    947901    if (!strncmp(filename, "/etc/", 5)) {
    948902        sev = 3;
    949         sprintf(reason,
    950                 "Do not edit config files while backing up your PC.");
     903        mr_free(reason);
     904        mr_asprintf(reason, "Do not edit config files while backing up your PC.");
    951905    }
    952906    if (!strcmp(filename, "/etc/adjtime")
    953907        || !strcmp(filename, "/etc/mtab")) {
    954908        sev = 1;
    955         sprintf(reason, "This file changes all the time. It's OK.");
     909        mr_free(reason);
     910        mr_asprintf(reason, "This file changes all the time. It's OK.");
    956911    }
    957912    if (!strncmp(filename, "/root/", 6)) {
    958913        sev = 3;
    959         sprintf(reason, "Were you compiling/editing something in /root?");
     914        mr_free(reason);
     915        mr_asprintf(reason, "Were you compiling/editing something in /root?");
    960916    }
    961917    if (!strncmp(filename, "/root/.", 7)) {
    962918        sev = 2;
    963         sprintf(reason, "Temp or 'dot' files changed in /root.");
     919        mr_free(reason);
     920        mr_asprintf(reason, "Temp or 'dot' files changed in /root.");
    964921    }
    965922    if (!strncmp(filename, "/var/lib/", 9)) {
    966923        sev = 2;
    967         sprintf(reason, "Did you add/remove software during backing?");
     924        mr_free(reason);
     925        mr_asprintf(reason, "Did you add/remove software during backing?");
    968926    }
    969927    if (!strncmp(filename, "/var/lib/rpm", 12)) {
    970928        sev = 3;
    971         sprintf(reason, "Did you add/remove software during backing?");
     929        mr_free(reason);
     930        mr_asprintf(reason, "Did you add/remove software during backing?");
    972931    }
    973932    if (!strncmp(filename, "/var/lib/slocate", 16)) {
    974933        sev = 1;
    975         sprintf(reason,
    976                 "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
     934        mr_free(reason);
     935        mr_asprintf(reason, "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
    977936    }
    978937    if (!strncmp(filename, "/var/log/", 9)
     
    980939        || !strcmp(filename + strlen(filename) - 4, ".log")) {
    981940        sev = 1;
    982         sprintf(reason,
    983                 "Log files change frequently as the computer runs. Fret not.");
     941        mr_free(reason);
     942        mr_asprintf(reason, "Log files change frequently as the computer runs. Fret not.");
    984943    }
    985944    if (!strncmp(filename, "/var/spool", 10)) {
    986945        sev = 1;
    987         sprintf(reason,
    988                 "Background processes or printers were active. This does not affect the integrity of your backup.");
     946        mr_free(reason);
     947        mr_asprintf(reason, "Background processes or printers were active. This does not affect the integrity of your backup.");
    989948    }
    990949    if (!strncmp(filename, "/var/spool/mail", 10)) {
    991950        sev = 2;
    992         sprintf(reason, "Mail was sent/received during backup.");
     951        mr_free(reason);
     952        mr_asprintf(reason, "Mail was sent/received during backup.");
    993953    }
    994954    if (filename[strlen(filename) - 1] == '~') {
    995955        sev = 1;
    996         sprintf(reason,
    997                 "Backup copy of another file which was modified recently.");
     956        mr_free(reason);
     957        mr_asprintf(reason, "Backup copy of another file which was modified recently.");
    998958    }
    999959    if (strstr(filename, "cache")) {
    1000960        sev = 1;
    1001         sprintf(reason,
    1002                 "Part of a cache of data. Caches change from time to time. Don't worry.");
     961        mr_free(reason);
     962        mr_asprintf(reason, "Part of a cache of data. Caches change from time to time. Don't worry.");
    1003963    }
    1004964    if (!strncmp(filename, "/var/run/", 9)
     
    1007967        || strstr(filename, "/.Xauthority")) {
    1008968        sev = 1;
    1009         sprintf(reason,
    1010                 "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
     969        mr_free(reason);
     970        mr_asprintf(reason, "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
    1011971    }
    1012972    if (out_reason) {
    1013973        strcpy(out_reason, reason);
    1014974    }
    1015     paranoid_free(filename);
    1016     paranoid_free(reason);
     975    mr_free(filename);
     976    mr_free(reason);
    1017977    return (sev);
    1018978}
     
    10641024    /*@ buffers ******************************************* */
    10651025    static char outstr[MAX_STR_LEN];
    1066     char *pos_w_commas, *tmp;
     1026    char *pos_w_commas = NULL;
    10671027    char *mds = NULL;
     1028    char *tmp = NULL;
    10681029
    10691030    assert(bkpinfo != NULL);
    1070     malloc_string(pos_w_commas);
    1071     malloc_string(tmp);
    1072     sprintf(tmp, "%lld", g_tape_posK);
    1073     strcpy(pos_w_commas, commarize(tmp));
    1074 
    1075 
    1076 
    1077     if (bkpinfo->media_size <= 0)
    1078 //    { fatal_error( "percentage_media_full_comment() - unknown media size"); }
    1079     {
    1080         sprintf(outstr, "Volume %d: %s kilobytes archived so far",
    1081                 g_current_media_number, pos_w_commas);
     1031
     1032    if (bkpinfo->media_size <= 0) {
     1033        mr_asprintf(tmp, "%lld", g_tape_posK);
     1034        mr_asprintf(pos_w_commas, "%s", commarize(tmp));
     1035        mr_free(tmp);
     1036        sprintf(outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
     1037        mr_free(pos_w_commas);
    10821038        return (outstr);
    10831039    }
    10841040
    1085 /* update screen */
     1041    /* update screen */
    10861042    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
    10871043        percentage = (int) (g_tape_posK / 10 / bkpinfo->media_size);
     
    10911047        sprintf(outstr, "Volume %d: [", g_current_media_number);
    10921048    } else {
    1093         percentage =
    1094             (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 / bkpinfo->media_size);
     1049        percentage = (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 / bkpinfo->media_size);
    10951050        mds = media_descriptor_string(bkpinfo->backup_media_type);
    10961051        sprintf(outstr, "%s %d: [", mds, g_current_media_number);
     
    11051060    j = (int) strlen(outstr);
    11061061    sprintf(outstr + j, "] %d%% used", percentage);
    1107     paranoid_free(pos_w_commas);
    1108     paranoid_free(tmp);
    11091062    return (outstr);
    11101063}
Note: See TracChangeset for help on using the changeset viewer.