Ignore:
Timestamp:
Feb 17, 2007, 2:27:26 AM (13 years ago)
Author:
Bruno Cornec
Message:

Continue to merge trunk memory management enhancements for libmondo-tools.c & libmondo-string.c
Introduction + test of a new function mr_strcat

File:
1 edited

Legend:

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

    r1168 r1178  
    1 /* $Id$ */
    2 
     1/*
     2   $Id$
     3*/
    34
    45/**
     
    1415#include "libmondo-string.h"
    1516#include "libmondo-files-EXT.h"
    16 #include "libmondo-gui-EXT.h"
     17#include "newt-specific-EXT.h"
    1718#include "libmondo-tools-EXT.h"
     19#include <math.h>
    1820
    1921/*@unused@*/
     
    2224extern int g_current_media_number;
    2325extern long long g_tape_posK;
    24 
    2526
    2627/**
     
    3839char *build_partition_name(char *partition, const char *drive, int partno)
    3940{
    40     char *p, *c;
     41    char *p = NULL;
     42    char *c = NULL;
    4143
    4244    assert(partition != NULL);
     
    4951    if (c && strncmp(c, "/disc", 5) == 0) {
    5052        /* yup it's devfs, return the "part" path */
     53        /* format /dev/.../disc */
    5154        strcpy(c + 1, "part");
    5255        p = c + 5;
     
    6770
    6871
    69 
    70 
    71 
    72 
    73 
    74 
    75 
    76 
    77 
    78 
    79 
    8072/**
    8173 * Pad a string on both sides so it appears centered.
     
    8577void center_string(char *in_out, int width)
    8678{
    87     char scratch[MAX_STR_LEN];
    88     char *p;
    89     int i;                      /* purpose */
    90     int len;                    /* purpose */
    91     int mid;                    /* purpose */
    92     int x;                      /* purpose */
     79    char *scratch = NULL;
     80    char *out = NULL;
     81    char *p = NULL;
     82    int i = 0;                      /* purpose */
     83    int len = 0;                    /* purpose */
     84    int mid = 0;                    /* purpose */
     85    int x = 0;                      /* purpose */
    9386
    9487    assert(in_out != NULL);
     
    9891        return;
    9992    }
    100     for (p = in_out; *p == ' '; p++);
    101     strcpy(scratch, p);
    102     mr_strip_spaces (scratch);
     93    mr_asprintf(&scratch, in_out);
     94    mr_strip_spaces(scratch);
    10395    len = (int) strlen(scratch);
    10496    mid = width / 2;
     
    129121    *q = *p;                    // for the final '\0'
    130122}
    131 
    132 
    133 
    134 /**
    135  * Add commas every third place in @p input.
    136  * @param input The string to commarize.
    137  * @return The string with commas.
    138  * @note The returned string points to static storage that will be overwritten with each call.
    139  */
    140 char *commarize(char *input)
    141 {
    142     char pos_w_commas[MAX_STR_LEN];
    143     static char output[MAX_STR_LEN];
    144     char tmp[MAX_STR_LEN];
    145     int j;
    146 
    147     assert(input != NULL);
    148 
    149     strcpy(tmp, input);
    150     if (strlen(tmp) > 6) {
    151         strcpy(pos_w_commas, tmp);
    152         j = (int) strlen(pos_w_commas);
    153         tmp[j - 6] = ',';
    154         strcpy(tmp + j - 5, pos_w_commas + j - 6);
    155 //      tmp[j-2] = ',';
    156 //      strcpy(tmp+j-1, pos_w_commas+j-3);
    157         strcpy(pos_w_commas, tmp);
    158     }
    159     if (strlen(tmp) > 3) {
    160         j = (int) strlen(tmp);
    161         strcpy(pos_w_commas, tmp);
    162         pos_w_commas[j - 3] = ',';
    163         strcpy(pos_w_commas + j - 2, tmp + j - 3);
    164     } else {
    165         strcpy(pos_w_commas, tmp);
    166     }
    167     strcpy(output, pos_w_commas);
    168     return (output);
    169 }
    170 
    171 
    172 
    173 
    174 
    175 
    176 
    177123
    178124
     
    201147
    202148
    203 
    204 
    205 
    206149/**
    207150 * Turn a "friendly" sizestring into a number of megabytes.
     
    213156long friendly_sizestr_to_sizelong(char *incoming)
    214157{
    215     long outval;
    216     int i;
    217     char *tmp;
    218     char ch;
     158    long outval = 0L;
     159    int i = 0;
     160    char *tmp = NULL;
     161    char ch = ' ';
    219162
    220163    assert_string_is_neither_NULL_nor_zerolength(incoming);
    221164
    222     malloc_string(tmp);
    223165    if (!incoming[0]) {
    224         mr_free(tmp);
    225         return (0);
     166        return(0L);
    226167    }
    227168    if (strchr(incoming, '.')) {
    228169        fatal_error("Please use integers only. No decimal points.");
    229170    }
    230     strcpy(tmp, incoming);
     171    mr_asprintf(&tmp, "%s", incoming);
    231172    i = (int) strlen(tmp);
    232173    if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') {
     
    237178    tmp[i] = '\0';
    238179    outval = atol(tmp);
     180    mr_free(tmp);
     181
    239182    if (ch == 'g' || ch == 'G') {
    240183        outval = outval * 1024;
     
    249192            ("Oh my gosh. You actually think a YOTTABYTE will get you anywhere? What're you going to do with 1,208,925,819,614,629,174,706,176 bytes of data?!?!");
    250193        popup_and_OK
    251             ("That sizespec is more than 1,208,925,819,614,629,174,706,176 bytes. You have a shocking amount of data. Please send a screenshot to the list :-)");
     194            (_("That sizespec is more than 1,208,925,819,614,629,174,706,176 bytes. You have a shocking amount of data. Please send a screenshot to the list :-)"));
    252195        fatal_error("Integer overflow.");
    253196    } else if (ch != 'm' && ch != 'M') {
    254         sprintf(tmp, "Re: parameter '%s' - bad multiplier ('%c')",
    255                 incoming, ch);
     197        mr_asprintf(&tmp, "Re: parameter '%s' - bad multiplier ('%c')",
     198                 incoming, ch);
    256199        fatal_error(tmp);
    257200    }
    258     mr_free(tmp);
    259201    return (outval);
    260202}
    261 
    262 
    263 
    264 /**
    265  * Add spaces to the right of @p incoming to make it @p width characters wide.
    266  * @param incoming The string to left-pad.
    267  * @param width The width to pad it to.
    268  * @return The left-padded string.
    269  * @note The returned string points to static storage that will be overwritten with each call.
    270  * @bug Why does center_string() modify its argument but leftpad_string() returns a modified copy?
    271  */
    272 char *leftpad_string(char *incoming, int width)
    273 {
    274     /*@ buffers ***************************************************** */
    275     static char output[MAX_STR_LEN];
    276 
    277     /*@ ints ******************************************************** */
    278     int i;
    279 
    280     /*@ end vars **************************************************** */
    281     assert(incoming != NULL);
    282     assert(width > 2);
    283 
    284     strcpy(output, incoming);
    285     for (i = (int) strlen(output); i < width; i++) {
    286         output[i] = ' ';
    287     }
    288     output[i] = '\0';
    289     return (output);
    290 }
    291 
    292203
    293204
     
    374285    return (outstr);
    375286}
    376 
    377 
    378287
    379288
     
    405314
    406315
    407 
    408 
    409 
    410 
    411316/**
    412317 * Generate a friendly string containing "X blah blah disk(s)"
     
    424329
    425330    /*@ char     ******************************************************** */
    426     char p;
     331    char p = NULL;
    427332
    428333    assert(label != NULL);
     
    495400    return (output);
    496401}
    497 
    498 
    499402
    500403
     
    530433    mr_free(input);
    531434}
    532 
    533 
    534 
    535435
    536436
     
    555455    /*@ buffers **************************************************** */
    556456    static char output[MAX_STR_LEN];
    557     static char suffix[MAX_STR_LEN];
     457    char *suffix = NULL;
    558458
    559459    /*@ end vars *************************************************** */
     
    561461    assert_string_is_neither_NULL_nor_zerolength(path);
    562462    if (s[0] != '\0') {
    563         sprintf(suffix, ".%s", s);
     463        mr_asprintf(&suffix, ".%s", s);
    564464    } else {
    565         suffix[0] = '\0';
     465        mr_asprintf(&suffix, "");
    566466    }
    567467    sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno,
    568468            suffix);
     469    mr_free(suffix);
    569470    return (output);
    570471}
     
    597498
    598499
    599 
    600 
    601500/**
    602501 * Wrap @p flaws_str across three lines. The first two are no more than 74 characters wide.
     
    650549
    651550
    652 
    653551/**
    654552 * Compare @p stringA and @p stringB. This uses an ASCII sort for everything
     
    698596    numA = atol(stringA + start_of_numbers_in_A);
    699597    numB = atol(stringB + start_of_numbers_in_B);
    700     /*
    701        sprintf(tmp,"Comparing %s and %s --> %ld,%ld\n",stringA,stringB,numA,numB);
    702        log_to_screen(tmp);
    703      */
    704598    return ((int) (numA - numB));
    705599}
     
    773667{
    774668    int i = strlen(partition) - 1;
    775     char *c;
     669    char *c = NULL;
    776670
    777671#ifdef __FreeBSD__
     
    793687    if (c && strncmp(c, "/part", 5) == 0) {
    794688        /* yup it's devfs, return the "disc" path */
    795         strcpy(c + 1, "disc");
     689        strncpy(c + 1, "disc", (size_t)5);
    796690        return partition;
    797691    }
     
    808702    return partition;
    809703}
    810 
    811 
    812 
    813704
    814705
     
    838729
    839730
    840 
    841 
    842 
    843 
    844 
    845 
    846 
    847731/**
    848732 * Determine the severity (1-3, 1 being low) of the fact that
     
    854738int severity_of_difference(char *fn, char *out_reason)
    855739{
    856     int sev;
    857     char *reason;
    858     char *filename;
    859 
    860     malloc_string(reason);
    861     malloc_string(filename);
    862 // out_reason might be null on purpose, so don't bomb if it is :) OK?
     740    int sev = 0;
     741    char *reason = NULL;
     742    char *filename = NULL;
     743
     744    // out_reason might be null on purpose, so don't bomb if it is :) OK?
    863745    assert_string_is_neither_NULL_nor_zerolength(fn);
    864746    if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
    865         strcpy(filename, fn + strlen(MNT_RESTORING));
     747        mr_asprintf(&filename, fn + strlen(MNT_RESTORING));
    866748    } else if (fn[0] != '/') {
    867         sprintf(filename, "/%s", fn);
     749        mr_asprintf(&filename, "/%s", fn);
    868750    } else {
    869         strcpy(filename, fn);
    870     }
    871 
    872     sev = 3;
    873     sprintf(reason,
    874             "Changed since backup. Consider running a differential backup in a day or two.");
     751        mr_asprintf(&filename, fn);
     752    }
     753
    875754    if (!strncmp(filename, "/var/", 5)) {
    876755        sev = 2;
    877         sprintf(reason,
    878                 "/var's contents will change regularly, inevitably.");
     756        mr_asprintf(&reason,
     757                 _("/var's contents will change regularly, inevitably."));
    879758    }
    880759    if (!strncmp(filename, "/home", 5)) {
    881760        sev = 2;
    882         sprintf(reason,
    883                 "It's in your /home partiton. Therefore, it is important.");
     761        mr_asprintf(&reason,
     762                 _("It's in your /home partiton. Therefore, it is important."));
    884763    }
    885764    if (!strncmp(filename, "/usr/", 5)) {
    886765        sev = 3;
    887         sprintf(reason,
    888                 "You may have installed/removed software during the backup.");
     766        mr_asprintf(&reason,
     767                 _("You may have installed/removed software during the backup."));
    889768    }
    890769    if (!strncmp(filename, "/etc/", 5)) {
    891770        sev = 3;
    892         sprintf(reason,
    893                 "Do not edit config files while backing up your PC.");
     771        mr_asprintf(&reason,
     772                 _("Do not edit config files while backing up your PC."));
    894773    }
    895774    if (!strcmp(filename, "/etc/adjtime")
    896775        || !strcmp(filename, "/etc/mtab")) {
    897776        sev = 1;
    898         sprintf(reason, "This file changes all the time. It's OK.");
     777        mr_asprintf(&reason, _("This file changes all the time. It's OK."));
    899778    }
    900779    if (!strncmp(filename, "/root/", 6)) {
    901780        sev = 3;
    902         sprintf(reason, "Were you compiling/editing something in /root?");
     781        mr_asprintf(&reason,
     782                 _("Were you compiling/editing something in /root?"));
    903783    }
    904784    if (!strncmp(filename, "/root/.", 7)) {
    905785        sev = 2;
    906         sprintf(reason, "Temp or 'dot' files changed in /root.");
     786        mr_asprintf(&reason, _("Temp or 'dot' files changed in /root."));
    907787    }
    908788    if (!strncmp(filename, "/var/lib/", 9)) {
    909789        sev = 2;
    910         sprintf(reason, "Did you add/remove software during backing?");
     790        mr_asprintf(&reason, _("Did you add/remove software during backing?"));
    911791    }
    912792    if (!strncmp(filename, "/var/lib/rpm", 12)) {
    913793        sev = 3;
    914         sprintf(reason, "Did you add/remove software during backing?");
     794        mr_asprintf(&reason, _("Did you add/remove software during backing?"));
    915795    }
    916796    if (!strncmp(filename, "/var/lib/slocate", 16)) {
    917797        sev = 1;
    918         sprintf(reason,
    919                 "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
     798        mr_asprintf(&reason,
     799                 _("The 'update' daemon ran during backup. This does not affect the integrity of your backup."));
    920800    }
    921801    if (!strncmp(filename, "/var/log/", 9)
     
    923803        || !strcmp(filename + strlen(filename) - 4, ".log")) {
    924804        sev = 1;
    925         sprintf(reason,
    926                 "Log files change frequently as the computer runs. Fret not.");
     805        mr_asprintf(&reason,
     806                 _("Log files change frequently as the computer runs. Fret not."));
    927807    }
    928808    if (!strncmp(filename, "/var/spool", 10)) {
    929809        sev = 1;
    930         sprintf(reason,
    931                 "Background processes or printers were active. This does not affect the integrity of your backup.");
     810        mr_asprintf(&reason,
     811                 _("Background processes or printers were active. This does not affect the integrity of your backup."));
    932812    }
    933813    if (!strncmp(filename, "/var/spool/mail", 10)) {
    934814        sev = 2;
    935         sprintf(reason, "Mail was sent/received during backup.");
     815        mr_asprintf(&reason, _("Mail was sent/received during backup."));
    936816    }
    937817    if (filename[strlen(filename) - 1] == '~') {
    938818        sev = 1;
    939         sprintf(reason,
    940                 "Backup copy of another file which was modified recently.");
     819        mr_asprintf(&reason,
     820                 _("Backup copy of another file which was modified recently."));
    941821    }
    942822    if (strstr(filename, "cache")) {
    943823        sev = 1;
    944         sprintf(reason,
    945                 "Part of a cache of data. Caches change from time to time. Don't worry.");
     824        mr_asprintf(&reason,
     825                 _("Part of a cache of data. Caches change from time to time. Don't worry."));
    946826    }
    947827    if (!strncmp(filename, "/var/run/", 9)
     
    950830        || strstr(filename, "/.Xauthority")) {
    951831        sev = 1;
    952         sprintf(reason,
    953                 "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
     832        mr_asprintf(&reason,
     833                _("Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence."));
     834    }
     835    mr_free(filename);
     836
     837    if (sev == 0) {
     838        sev = 3;
     839        mr_asprintf(&reason,
     840                 _("Changed since backup. Consider running a differential backup in a day or two."));
    954841    }
    955842    if (out_reason) {
    956843        strcpy(out_reason, reason);
    957844    }
    958     mr_free(filename);
     845
    959846    mr_free(reason);
    960847    return (sev);
    961848}
    962 
    963849
    964850
     
    982868    return (res);
    983869}
    984 
    985 
    986 
    987 
    988 
    989870
    990871
     
    1002883{
    1003884    /*@ int *********************************************** */
    1004     int percentage;
    1005     int j;
     885    int percentage = 0;
     886    int i = 0;
     887    int j = 0;
    1006888
    1007889    /*@ buffers ******************************************* */
    1008890    static char outstr[MAX_STR_LEN];
    1009     char *pos_w_commas, *tmp;
    1010891
    1011892    assert(bkpinfo != NULL);
    1012     malloc_string(pos_w_commas);
    1013     malloc_string(tmp);
    1014     sprintf(tmp, "%lld", g_tape_posK);
    1015     strcpy(pos_w_commas, commarize(tmp));
    1016 
    1017 
    1018 
    1019     if (bkpinfo->media_size[g_current_media_number] <= 0)
    1020 //    { fatal_error( "percentage_media_full_comment() - unknown media size"); }
    1021     {
    1022         sprintf(outstr, "Volume %d: %s kilobytes archived so far",
    1023                 g_current_media_number, pos_w_commas);
     893
     894    if (bkpinfo->media_size[g_current_media_number] <= 0) {
     895        sprintf(outstr, _("Volume %d: %'lld kilobytes archived so far"),
     896                g_current_media_number, g_tape_posK);
    1024897        return (outstr);
    1025898    }
    1026899
    1027 /* update screen */
     900    /* update screen */
    1028901    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
    1029902        percentage =
     
    1033906            percentage = 100;
    1034907        }
    1035         sprintf(outstr, "Volume %d: [", g_current_media_number);
     908        sprintf(outstr, _("Volume %d: ["), g_current_media_number);
    1036909    } else {
    1037910        percentage =
     
    1039912                   bkpinfo->media_size[g_current_media_number]);
    1040913        sprintf(outstr, "%s %d: [",
    1041                 media_descriptor_string(bkpinfo->backup_media_type),
     914                bkpinfo->backup_media_string,
    1042915                g_current_media_number);
    1043916    }
     
    1050923    j = (int) strlen(outstr);
    1051924    sprintf(outstr + j, "] %d%% used", percentage);
    1052     mr_free(pos_w_commas);
    1053     mr_free(tmp);
    1054925    return (outstr);
    1055926}
     927
     928
    1056929
    1057930/**
     
    1063936char *media_descriptor_string(t_bkptype type_of_bkp)
    1064937{
    1065     static char *type_of_backup = NULL;
    1066 
    1067     if (!type_of_backup) {
    1068         malloc_string(type_of_backup);
    1069     }
    1070 
    1071     switch (type_of_bkp) {
    1072     case dvd:
    1073         strcpy(type_of_backup, "DVD");
    1074         break;
    1075     case cdr:
    1076         strcpy(type_of_backup, "CDR");
    1077         break;
    1078     case cdrw:
    1079         strcpy(type_of_backup, "CDRW");
    1080         break;
    1081     case tape:
    1082         strcpy(type_of_backup, "tape");
    1083         break;
    1084     case cdstream:
    1085         strcpy(type_of_backup, "CDR");
    1086         break;
    1087     case udev:
    1088         strcpy(type_of_backup, "udev");
    1089         break;
    1090     case iso:
    1091         strcpy(type_of_backup, "ISO");
    1092         break;
    1093     case nfs:
    1094         strcpy(type_of_backup, "nfs");
    1095         break;
    1096     case usb:
    1097         strcpy(type_of_backup, "USB");
    1098         break;
    1099     default:
    1100         strcpy(type_of_backup, "ISO");
    1101     }
    1102     return (type_of_backup);
    1103 }
     938       static char *type_of_backup = NULL;
     939
     940       if (!type_of_backup) {
     941               malloc_string(type_of_backup);
     942       }
     943
     944       switch (type_of_bkp) {
     945       case dvd:
     946               strcpy(type_of_backup, "DVD");
     947               break;
     948       case cdr:
     949               strcpy(type_of_backup, "CDR");
     950               break;
     951       case cdrw:
     952               strcpy(type_of_backup, "CDRW");
     953               break;
     954       case tape:
     955               strcpy(type_of_backup, "tape");
     956               break;
     957       case cdstream:
     958               strcpy(type_of_backup, "CDR");
     959               break;
     960       case udev:
     961               strcpy(type_of_backup, "udev");
     962               break;
     963       case iso:
     964               strcpy(type_of_backup, "ISO");
     965               break;
     966       case nfs:
     967               strcpy(type_of_backup, "nfs");
     968               break;
     969       case usb:
     970               strcpy(type_of_backup, "USB");
     971               break;
     972       default:
     973               strcpy(type_of_backup, "ISO");
     974       }
     975       return (type_of_backup);
     976}
     977
Note: See TracChangeset for help on using the changeset viewer.