Changeset 87 in MondoRescue for trunk/mondo/mondo/common/libmondo-string.c


Ignore:
Timestamp:
Oct 27, 2005, 10:03:00 PM (18 years ago)
Author:
bcornec
Message:
  • Now use -Wall to compile
  • asprintf for newt-specific.c
  • Bug in libmondo-string.c line 1120: if we use %% in this format, then the percentage is printeed wrongly (after the real percentage we have a huge number)
File:
1 edited

Legend:

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

    r58 r87  
    6868/**
    6969 * Pad a string on both sides so it appears centered.
    70  * @param in_out The string to be center-padded (modified).
     70 * @param in_out The string to be center-padded (modified). The caller needs to free this string
    7171 * @param width The width of the final result.
    7272 */
     
    7474{
    7575    char *scratch;
     76    char *out;
    7677    char *p;
    7778    int i;                      /* purpose */
     
    8788    }
    8889    for (p = in_out; *p == ' '; p++);
    89     asprintf(&scratch, "%s", p);
     90    asprintf(&scratch, p);
    9091    len = (int) strlen(scratch);
    9192    mid = width / 2;
     
    9596    }
    9697    in_out[i] = '\0';
    97     strcat(in_out, scratch);
     98    asprintf(&out, "%s%s", in_out, scratch);
    9899    paranoid_free(scratch);
     100    in_out = out;
    99101}
    100102
     
    863865int severity_of_difference(char *fn, char *out_reason)
    864866{
    865     int sev;
     867    int sev = 0;
    866868    char *reason;
    867869    char *filename;
    868870
    869     malloc_string(reason);
    870 // out_reason might be null on purpose, so don't bomb if it is :) OK?
     871    // out_reason might be null on purpose, so don't bomb if it is :) OK?
    871872    assert_string_is_neither_NULL_nor_zerolength(fn);
    872873    if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
     
    878879    }
    879880
    880     sev = 3;
    881     sprintf(reason,
    882             "Changed since backup. Consider running a differential backup in a day or two.");
    883881    if (!strncmp(filename, "/var/", 5)) {
    884882        sev = 2;
    885         sprintf(reason,
     883        asprintf(&reason,
    886884                "/var's contents will change regularly, inevitably.");
    887885    }
    888886    if (!strncmp(filename, "/home", 5)) {
    889887        sev = 2;
    890         sprintf(reason,
     888        asprintf(&reason,
    891889                "It's in your /home partiton. Therefore, it is important.");
    892890    }
    893891    if (!strncmp(filename, "/usr/", 5)) {
    894892        sev = 3;
    895         sprintf(reason,
     893        asprintf(&reason,
    896894                "You may have installed/removed software during the backup.");
    897895    }
    898896    if (!strncmp(filename, "/etc/", 5)) {
    899897        sev = 3;
    900         sprintf(reason,
     898        asprintf(&reason,
    901899                "Do not edit config files while backing up your PC.");
    902900    }
     
    904902        || !strcmp(filename, "/etc/mtab")) {
    905903        sev = 1;
    906         sprintf(reason, "This file changes all the time. It's OK.");
     904        asprintf(&reason, "This file changes all the time. It's OK.");
    907905    }
    908906    if (!strncmp(filename, "/root/", 6)) {
    909907        sev = 3;
    910         sprintf(reason, "Were you compiling/editing something in /root?");
     908        asprintf(&reason, "Were you compiling/editing something in /root?");
    911909    }
    912910    if (!strncmp(filename, "/root/.", 7)) {
    913911        sev = 2;
    914         sprintf(reason, "Temp or 'dot' files changed in /root.");
     912        asprintf(&reason, "Temp or 'dot' files changed in /root.");
    915913    }
    916914    if (!strncmp(filename, "/var/lib/", 9)) {
    917915        sev = 2;
    918         sprintf(reason, "Did you add/remove software during backing?");
     916        asprintf(&reason, "Did you add/remove software during backing?");
    919917    }
    920918    if (!strncmp(filename, "/var/lib/rpm", 12)) {
    921919        sev = 3;
    922         sprintf(reason, "Did you add/remove software during backing?");
     920        asprintf(&reason, "Did you add/remove software during backing?");
    923921    }
    924922    if (!strncmp(filename, "/var/lib/slocate", 16)) {
    925923        sev = 1;
    926         sprintf(reason,
     924        asprintf(&reason,
    927925                "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
    928926    }
     
    931929        || !strcmp(filename + strlen(filename) - 4, ".log")) {
    932930        sev = 1;
    933         sprintf(reason,
     931        asprintf(&reason,
    934932                "Log files change frequently as the computer runs. Fret not.");
    935933    }
    936934    if (!strncmp(filename, "/var/spool", 10)) {
    937935        sev = 1;
    938         sprintf(reason,
     936        asprintf(&reason,
    939937                "Background processes or printers were active. This does not affect the integrity of your backup.");
    940938    }
    941939    if (!strncmp(filename, "/var/spool/mail", 10)) {
    942940        sev = 2;
    943         sprintf(reason, "Mail was sent/received during backup.");
     941        asprintf(&reason, "Mail was sent/received during backup.");
    944942    }
    945943    if (filename[strlen(filename) - 1] == '~') {
    946944        sev = 1;
    947         sprintf(reason,
     945        asprintf(&reason,
    948946                "Backup copy of another file which was modified recently.");
    949947    }
    950948    if (strstr(filename, "cache")) {
    951949        sev = 1;
    952         sprintf(reason,
     950        asprintf(&reason,
    953951                "Part of a cache of data. Caches change from time to time. Don't worry.");
    954952    }
     
    958956        || strstr(filename, "/.Xauthority")) {
    959957        sev = 1;
    960         sprintf(reason,
     958        asprintf(&reason,
    961959                "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
    962960    }
    963961    paranoid_free(filename);
    964962
    965     if (out_reason) {
    966         strcpy(out_reason, reason);
    967     }
    968     paranoid_free(reason);
     963    if (sev == 0) {
     964        sev = 3;
     965        asprintf(&reason,
     966            "Changed since backup. Consider running a differential backup in a day or two.");
     967    }
     968
     969    out_reason = reason;
    969970    return (sev);
    970971}
     
    10001001 * - @c bkpinfo->media_size
    10011002 * - @c bkpinfo->scratchdir
    1002  * @return The string indicating media fill.
     1003 * @return The string indicating media fill. Needs to be freed by caller
    10031004 * @note The returned string points to static storage that will be overwritten with each call.
    10041005 */
     
    10061007{
    10071008    /*@ int *********************************************** */
    1008     int percentage;
     1009    int percentage = 0;
     1010    int i;
    10091011    int j;
    10101012
    10111013    /*@ buffers ******************************************* */
    1012     static char outstr[MAX_STR_LEN];
    1013     char *pos_w_commas, *tmp;
     1014    char *outstr;
     1015    char *tmp;
     1016    char *tmp1;
     1017    char *tmp2;
     1018    char *prepstr;
     1019    char *p;
    10141020
    10151021    assert(bkpinfo != NULL);
    1016     malloc_string(pos_w_commas);
    1017     malloc_string(tmp);
    1018     sprintf(tmp, "%lld", g_tape_posK);
    1019     strcpy(pos_w_commas, commarize(tmp));
    1020 
    1021 
    1022 
    1023     if (bkpinfo->media_size[g_current_media_number] <= 0)
    1024 //    { fatal_error( "percentage_media_full_comment() - unknown media size"); }
    1025     {
    1026         sprintf(outstr, "Volume %d: %s kilobytes archived so far",
    1027                 g_current_media_number, pos_w_commas);
     1022
     1023    if (bkpinfo->media_size[g_current_media_number] <= 0) {
     1024        asprintf(&tmp, "%lld", g_tape_posK);
     1025        asprintf(&outstr, "Volume %d: %s kilobytes archived so far",
     1026                g_current_media_number, commarize(tmp));
     1027        paranoid_free(tmp);
    10281028        return (outstr);
    10291029    }
     
    10341034            (int) (g_tape_posK / 10 /
    10351035                   bkpinfo->media_size[g_current_media_number]);
    1036         if (percentage > 100) {
    1037             percentage = 100;
    1038         }
    1039         sprintf(outstr, "Volume %d: [", g_current_media_number);
     1036        asprintf(&prepstr, "Volume %d: [", g_current_media_number);
    10401037    } else {
    10411038        percentage =
    10421039            (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 /
    10431040                   bkpinfo->media_size[g_current_media_number]);
    1044         sprintf(outstr, "%s %d: [",
     1041        asprintf(&prepstr, "%s %d: [",
    10451042                media_descriptor_string(bkpinfo->backup_media_type),
    10461043                g_current_media_number);
    10471044    }
    1048     for (j = 0; j < percentage; j += 5) {
    1049         strcat(outstr, "*");
    1050     }
    1051     for (; j < 100; j += 5) {
    1052         strcat(outstr, ".");
    1053     }
    1054     j = (int) strlen(outstr);
    1055     sprintf(outstr + j, "] %d%% used", percentage);
    1056     paranoid_free(pos_w_commas);
    1057     paranoid_free(tmp);
     1045    if (percentage > 100) {
     1046        percentage = 100;
     1047    }
     1048    j = trunc(percentage/5);
     1049    tmp1 = (char *)malloc((j + 1) * sizeof(char));
     1050    for (i = 0, p = tmp1 ; i < j ; i++, p++) {
     1051            *p = '*';
     1052    }
     1053    *p = '\0';
     1054
     1055    tmp2 = (char *)malloc((20 - j + 1) * sizeof(char));
     1056    for (i = 0, p = tmp2 ; i < 20 - j ; i++, p++) {
     1057            *p = '.';
     1058    }
     1059    *p = '\0';
     1060
     1061    /* BERLIOS There is a bug here I can't solve for the moment. If you
     1062     * replace %% in the asprintf below by 'percent' it just works, but
     1063     * like this it creates a huge number. Memory pb somewhere */
     1064    /*
     1065    log_it("percentage: %d", percentage);
     1066    asprintf(&outstr, "%s%s%s] %3d%% used", prepstr, tmp1, tmp2, percentage);
     1067    */
     1068    asprintf(&outstr, "%s%s%s] %3d percent used", prepstr, tmp1, tmp2, percentage);
     1069    paranoid_free(prepstr);
     1070    paranoid_free(tmp1);
     1071    paranoid_free(tmp2);
    10581072    return (outstr);
    10591073}
Note: See TracChangeset for help on using the changeset viewer.