Ignore:
Timestamp:
Nov 10, 2016, 9:09:54 PM (7 years ago)
Author:
Bruno Cornec
Message:

Remove more static allocation

File:
1 edited

Legend:

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

    r3413 r3611  
    147147
    148148/**
    149  * Add commas every third place in @p input.
    150  * @param input The string to commarize.
    151  * @return The string with commas.
    152  * @note The returned string points to static storage that will be overwritten with each call.
    153  */
    154 char *commarize(char *input)
    155 {
    156     char pos_w_commas[MAX_STR_LEN];
    157     static char output[MAX_STR_LEN];
    158     char tmp[MAX_STR_LEN];
    159     int j;
    160 
    161     assert(input != NULL);
    162 
    163     strcpy(tmp, input);
    164     if (strlen(tmp) > 6) {
    165         strcpy(pos_w_commas, tmp);
    166         j = (int) strlen(pos_w_commas);
    167         tmp[j - 6] = ',';
    168         strcpy(tmp + j - 5, pos_w_commas + j - 6);
    169         strcpy(pos_w_commas, tmp);
    170     }
    171     if (strlen(tmp) > 3) {
    172         j = (int) strlen(tmp);
    173         strcpy(pos_w_commas, tmp);
    174         pos_w_commas[j - 3] = ',';
    175         strcpy(pos_w_commas + j - 2, tmp + j - 3);
    176     } else {
    177         strcpy(pos_w_commas, tmp);
    178     }
    179     strcpy(output, pos_w_commas);
    180     return (output);
    181 }
    182 
    183 
    184 /**
    185149 * Turn an entry from the RAID editor's disklist into a GUI-friendly string.
    186150 * The format is: the device left-aligned and padded to 24 places, followed by a space and the
     
    190154 * @param lino The line number from @p disklist to convert to a string.
    191155 * @return The string form of the disklist entry.
    192  * @note The returned string points to static storage and will be overwritten with each call.
     156 * @note The returned string points to storage that needs to be freed by
     157 * caller
    193158 */
    194159char *disklist_entry_to_string(struct list_of_disks *disklist, int lino)
     
    196161
    197162    /*@ buffers ********************************************************** */
    198     static char output[MAX_STR_LEN];
     163    char *output = NULL;
    199164
    200165    assert(disklist != NULL);
    201166
    202     sprintf(output, "%-24s %8d", disklist->el[lino].device,
    203             disklist->el[lino].index);
    204     return (output);
     167    mr_asprintf(output, "%-24s %8d", disklist->el[lino].device, disklist->el[lino].index);
     168    return(output);
    205169}
    206170
     
    301265 * @param marker The marker byte to stringify.
    302266 * @return @p marker as a string.
    303  * @note The returned string points to static storage that will be overwritten with each call.
     267 * @note The returned string points to storage that needs to be freed by
     268 * caller
    304269 */
    305270char *marker_to_string(int marker)
    306271{
    307272    /*@ buffer ****************************************************** */
    308     static char outstr[MAX_STR_LEN];
     273    char *outstr = NULL;
    309274
    310275
     
    313278    switch (marker) {
    314279    case BLK_START_OF_BACKUP:
    315         strcpy(outstr, "BLK_START_OF_BACKUP");
     280        mr_asprintf(outstr, "BLK_START_OF_BACKUP");
    316281        break;
    317282    case BLK_START_OF_TAPE:
    318         strcpy(outstr, "BLK_START_OF_TAPE");
     283        mr_asprintf(outstr, "BLK_START_OF_TAPE");
    319284        break;
    320285    case BLK_START_AN_AFIO_OR_SLICE:
    321         strcpy(outstr, "BLK_START_AN_AFIO_OR_SLICE");
     286        mr_asprintf(outstr, "BLK_START_AN_AFIO_OR_SLICE");
    322287        break;
    323288    case BLK_STOP_AN_AFIO_OR_SLICE:
    324         strcpy(outstr, "BLK_STOP_AN_AFIO_OR_SLICE");
     289        mr_asprintf(outstr, "BLK_STOP_AN_AFIO_OR_SLICE");
    325290        break;
    326291    case BLK_START_AFIOBALLS:
    327         strcpy(outstr, "BLK_START_AFIOBALLS");
     292        mr_asprintf(outstr, "BLK_START_AFIOBALLS");
    328293        break;
    329294    case BLK_STOP_AFIOBALLS:
    330         strcpy(outstr, "BLK_STOP_AFIOBALLS");
     295        mr_asprintf(outstr, "BLK_STOP_AFIOBALLS");
    331296        break;
    332297    case BLK_STOP_BIGGIEFILES:
    333         strcpy(outstr, "BLK_STOP_BIGGIEFILES");
     298        mr_asprintf(outstr, "BLK_STOP_BIGGIEFILES");
    334299        break;
    335300    case BLK_START_A_NORMBIGGIE:
    336         strcpy(outstr, "BLK_START_A_NORMBIGGIE");
     301        mr_asprintf(outstr, "BLK_START_A_NORMBIGGIE");
    337302        break;
    338303    case BLK_START_A_PIHBIGGIE:
    339         strcpy(outstr, "BLK_START_A_PIHBIGGIE");
     304        mr_asprintf(outstr, "BLK_START_A_PIHBIGGIE");
    340305        break;
    341306    case BLK_START_EXTENDED_ATTRIBUTES:
    342         strcpy(outstr, "BLK_START_EXTENDED_ATTRIBUTES");
     307        mr_asprintf(outstr, "BLK_START_EXTENDED_ATTRIBUTES");
    343308        break;
    344309    case BLK_STOP_EXTENDED_ATTRIBUTES:
    345         strcpy(outstr, "BLK_STOP_EXTENDED_ATTRIBUTES");
     310        mr_asprintf(outstr, "BLK_STOP_EXTENDED_ATTRIBUTES");
    346311        break;
    347312    case BLK_START_EXAT_FILE:
    348         strcpy(outstr, "BLK_START_EXAT_FILE");
     313        mr_asprintf(outstr, "BLK_START_EXAT_FILE");
    349314        break;
    350315    case BLK_STOP_EXAT_FILE:
    351         strcpy(outstr, "BLK_STOP_EXAT_FILE");
     316        mr_asprintf(outstr, "BLK_STOP_EXAT_FILE");
    352317        break;
    353318    case BLK_START_BIGGIEFILES:
    354         strcpy(outstr, "BLK_START_BIGGIEFILES");
     319        mr_asprintf(outstr, "BLK_START_BIGGIEFILES");
    355320        break;
    356321    case BLK_STOP_A_BIGGIE:
    357         strcpy(outstr, "BLK_STOP_A_BIGGIE");
     322        mr_asprintf(outstr, "BLK_STOP_A_BIGGIE");
    358323        break;
    359324    case BLK_END_OF_TAPE:
    360         strcpy(outstr, "BLK_END_OF_TAPE");
     325        mr_asprintf(outstr, "BLK_END_OF_TAPE");
    361326        break;
    362327    case BLK_END_OF_BACKUP:
    363         strcpy(outstr, "BLK_END_OF_BACKUP");
     328        mr_asprintf(outstr, "BLK_END_OF_BACKUP");
    364329        break;
    365330    case BLK_ABORTED_BACKUP:
    366         strcpy(outstr, "BLK_ABORTED_BACKUP");
     331        mr_asprintf(outstr, "BLK_ABORTED_BACKUP");
    367332        break;
    368333    case BLK_START_FILE:
    369         strcpy(outstr, "BLK_START_FILE");
     334        mr_asprintf(outstr, "BLK_START_FILE");
    370335        break;
    371336    case BLK_STOP_FILE:
    372         strcpy(outstr, "BLK_STOP_FILE");
     337        mr_asprintf(outstr, "BLK_STOP_FILE");
    373338        break;
    374339    default:
    375         sprintf(outstr, "BLK_UNKNOWN (%d)", marker);
     340        mr_asprintf(outstr, "BLK_UNKNOWN (%d)", marker);
    376341        break;
    377342    }
    378343    return (outstr);
    379344}
    380 
    381 
    382345
    383346
     
    391354 * @param lino The line number in @p mountlist to stringify.
    392355 * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>].
    393  * @note The returned string points to static storage and will be overwritten with each call.
    394  */
    395 char *mountlist_entry_to_string(struct mountlist_itself *mountlist,
    396                                 int lino)
    397 {
     356 * @note The returned string points to storage that needs to be freed by
     357 * caller
     358 */
     359char *mountlist_entry_to_string(struct mountlist_itself *mountlist, int lino) {
    398360
    399361    /*@ buffer *********************************************************** */
    400     static char output[MAX_STR_LEN];
     362    char *output = NULL;
    401363
    402364    assert(mountlist != NULL);
    403365
    404     sprintf(output, "%-24s %-24s %-10s %8lld", mountlist->el[lino].device,
    405             mountlist->el[lino].mountpoint, mountlist->el[lino].format,
    406             mountlist->el[lino].size / 1024L);
    407     return (output);
     366    mr_asprintf(output, "%-24s %-24s %-10s %8lld", mountlist->el[lino].device, mountlist->el[lino].mountpoint, mountlist->el[lino].format, mountlist->el[lino].size / 1024L);
     367    return(output);
    408368}
    409369
     
    453413 * @param i The number to stringify.
    454414 * @return The string form of @p i.
    455  * @note The returned value points to static strorage that will be overwritten with each call.
     415 * @note The returned value points to strorage that needs to be freed by
     416 * caller
    456417 */
    457418char *number_to_text(int i)
     
    459420
    460421    /*@ buffers ***************************************************** */
    461     static char output[MAX_STR_LEN];
     422    char *output = NULL;
    462423
    463424
     
    466427    switch (i) {
    467428    case 0:
    468         strcpy(output, "zero");
     429        mr_asprintf(output, "zero");
    469430        break;
    470431    case 1:
    471         strcpy(output, "one");
     432        mr_asprintf(output, "one");
    472433        break;
    473434    case 2:
    474         strcpy(output, "two");
     435        mr_asprintf(output, "two");
    475436        break;
    476437    case 3:
    477         strcpy(output, "three");
     438        mr_asprintf(output, "three");
    478439        break;
    479440    case 4:
    480         strcpy(output, "four");
     441        mr_asprintf(output, "four");
    481442        break;
    482443    case 5:
    483         strcpy(output, "five");
     444        mr_asprintf(output, "five");
    484445        break;
    485446    case 6:
    486         strcpy(output, "six");
     447        mr_asprintf(output, "six");
    487448        break;
    488449    case 7:
    489         strcpy(output, "seven");
     450        mr_asprintf(output, "seven");
    490451        break;
    491452    case 8:
    492         strcpy(output, "eight");
     453        mr_asprintf(output, "eight");
    493454        break;
    494455    case 9:
    495         strcpy(output, "nine");
     456        mr_asprintf(output, "nine");
    496457    case 10:
    497         strcpy(output, "ten");
     458        mr_asprintf(output, "ten");
    498459    default:
    499         sprintf(output, "%d", i);
     460        mr_asprintf(output, "%d", i);
    500461    }
    501462    return (output);
     
    875836 * @param raid_level The RAID level to stringify.
    876837 * @return The string form of @p raid_level.
    877  * @note The returned value points to static storage that will be overwritten with each call.
     838 * @note The returned value points to storage that needs to be freed by caller
    878839 */
    879840char *turn_raid_level_number_to_string(int raid_level)
     
    881842
    882843    /*@ buffer ********************************************************** */
    883     static char output[MAX_STR_LEN];
     844    char *output = NULL;
    884845
    885846
    886847
    887848    if (raid_level >= 0) {
    888         sprintf(output, " RAID %-2d ", raid_level);
     849        mr_asprintf(output, " RAID %-2d ", raid_level);
    889850    } else {
    890         sprintf(output, "Linear RAID");
    891     }
    892     return (output);
     851        mr_asprintf(output, "Linear RAID");
     852    }
     853    return(output);
    893854}
    894855
     
    10681029    char *pos_w_commas = NULL;
    10691030    char *mds = NULL;
    1070     char *tmp = NULL;
    10711031
    10721032    assert(bkpinfo != NULL);
    10731033
    10741034    if (bkpinfo->media_size <= 0) {
    1075         mr_asprintf(tmp, "%lld", g_tape_posK);
    1076         mr_asprintf(pos_w_commas, "%s", commarize(tmp));
    1077         mr_free(tmp);
     1035        mr_asprintf(pos_w_commas, "%lld", g_tape_posK);
    10781036        mr_asprintf(outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
    10791037        mr_free(pos_w_commas);
Note: See TracChangeset for help on using the changeset viewer.