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


Ignore:
Timestamp:
Aug 31, 2006, 5:09:20 PM (18 years ago)
Author:
Bruno Cornec
Message:
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

File:
1 edited

Legend:

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

    r768 r783  
    1414#include "newt-specific-EXT.h"
    1515#include "libmondo-tools-EXT.h"
     16#include <math.h>
    1617
    1718/*@unused@*/
     
    136137
    137138
    138 /*
    139  * Add commas every third place in @p input.
    140  * @param input The string to commarize.
    141  * @return The string with commas.
    142  * @note The returned string points to static storage that will be overwritten with each call.
    143  */
    144 char *commarize(char *input)
    145 {
    146     char *pos_w_commas;
    147     static char output[MAX_STR_LEN];
    148     char *tmp;
    149     int j;
    150 
    151     assert(input != NULL);
    152 
    153     asprintf(&tmp, "%s", input);
    154     if (strlen(tmp) > 6) {
    155         asprintf(&pos_w_commas, "%s", tmp);
    156         j = (int) strlen(pos_w_commas);
    157         tmp[j - 6] = ',';
    158         strcpy(tmp + j - 5, pos_w_commas + j - 6);
    159         paranoid_free(pos_w_commas);
    160         asprintf(&pos_w_commas, "%s", tmp);
    161     }
    162     if (strlen(tmp) > 3) {
    163         j = (int) strlen(tmp);
    164         asprintf(&pos_w_commas, "%s", tmp);
    165         pos_w_commas[j - 3] = ',';
    166         strcpy(pos_w_commas + j - 2, tmp + j - 3);
    167     } else {
    168         asprintf(&pos_w_commas, "%s", tmp);
    169     }
    170     strcpy(output, pos_w_commas);
    171     paranoid_free(pos_w_commas);
    172     paranoid_free(tmp);
    173     return (output);
    174 }
    175 
    176 
    177139/**
    178140 * Turn an entry from the RAID editor's disklist into a GUI-friendly string.
     
    601563
    602564    /*@ initialize ****************************************************** */
    603     assert(flaws_str_A != NULL);
    604     assert(flaws_str_B != NULL);
    605     assert(flaws_str_C != NULL);
    606565    assert(flaws_str != NULL);
    607566
    608     flaws_str_A[0] = flaws_str_B[0] = flaws_str_C[0] = '\0';
    609 
     567    flaws_str_A = NULL;
     568    flaws_str_B = NULL;
     569    flaws_str_C = NULL;
    610570
    611571    if (!res && !strlen(flaws_str)) {
     
    613573    }
    614574    if (strlen(flaws_str) > 0) {
    615         sprintf(flaws_str_A, "%s", flaws_str + 1);
     575        asprintf(&flaws_str_A, "%s", flaws_str + 1);
    616576    }
    617577    if (strlen(flaws_str_A) >= 74) {
    618578        for (i = 74; flaws_str_A[i] != ' '; i--);
    619         strcpy(flaws_str_B, flaws_str_A + i + 1);
     579        asprintf(&flaws_str_B, "%s", flaws_str_A + i + 1);
    620580        flaws_str_A[i] = '\0';
    621581    }
    622582    if (strlen(flaws_str_B) >= 74) {
    623583        for (i = 74; flaws_str_B[i] != ' '; i--);
    624         strcpy(flaws_str_C, flaws_str_B + i + 1);
     584        asprintf(&flaws_str_C, "%s", flaws_str_B + i + 1);
    625585        flaws_str_B[i] = '\0';
    626586    }
     
    834794    if (c && strncmp(c, "/part", 5) == 0) {
    835795        /* yup it's devfs, return the "disc" path */
    836         strcpy(c + 1, "disc");
     796        strncpy(c + 1, "disc", (size_t)5);
    837797        return partition;
    838798    }
     
    1027987    /*@ int *********************************************** */
    1028988    int percentage = 0;
    1029     int i;
    1030     int j;
     989    int i = 0;
     990    int j = 0;
    1031991
    1032992    /*@ buffers ******************************************* */
    1033     char *outstr;
    1034     char *tmp;
    1035     char *tmp1;
    1036     char *tmp2;
    1037     char *prepstr;
    1038     char *p;
     993    char *outstr = NULL;
     994    char *tmp1 = NULL;
     995    char *tmp2 = NULL;
     996    char *prepstr = NULL;
     997    char *p = NULL;
    1039998
    1040999    assert(bkpinfo != NULL);
    10411000
    10421001    if (bkpinfo->media_size[g_current_media_number] <= 0) {
    1043         asprintf(&tmp, "%lld", g_tape_posK);
    1044         asprintf(&outstr, _("Volume %d: %s kilobytes archived so far"),
    1045                  g_current_media_number, commarize(tmp));
    1046         paranoid_free(tmp);
     1002        asprintf(&outstr, _("Volume %d: %'lld kilobytes archived so far"),
     1003                 g_current_media_number, g_tape_posK);
    10471004
    10481005        return (outstr);
     
    10601017                   bkpinfo->media_size[g_current_media_number]);
    10611018        asprintf(&prepstr, "%s %d: [",
    1062                  media_descriptor_string(bkpinfo->backup_media_type),
     1019                 bkpinfo->backup_media_string,
    10631020                 g_current_media_number);
    10641021    }
     
    10931050    return (outstr);
    10941051}
    1095 
    1096 /**
    1097  * Get a string form of @p type_of_bkp.
    1098  * @param type_of_bkp The backup type to stringify.
    1099  * @return The stringification of @p type_of_bkp.
    1100  * @note The returned string points to static storage that will be overwritten with each call.
    1101  */
    1102 char *media_descriptor_string(t_bkptype type_of_bkp)
    1103 {
    1104     static char *type_of_backup = NULL;
    1105 
    1106     if (!type_of_backup) {
    1107         malloc_string(type_of_backup);
    1108     }
    1109 
    1110     switch (type_of_bkp) {
    1111     case dvd:
    1112         strcpy(type_of_backup, "DVD");
    1113         break;
    1114     case cdr:
    1115         strcpy(type_of_backup, "CDR");
    1116         break;
    1117     case cdrw:
    1118         strcpy(type_of_backup, "CDRW");
    1119         break;
    1120     case tape:
    1121         strcpy(type_of_backup, "tape");
    1122         break;
    1123     case cdstream:
    1124         strcpy(type_of_backup, "CDR");
    1125         break;
    1126     case udev:
    1127         strcpy(type_of_backup, "udev");
    1128         break;
    1129     case iso:
    1130         strcpy(type_of_backup, "ISO");
    1131         break;
    1132     case nfs:
    1133         strcpy(type_of_backup, "nfs");
    1134         break;
    1135     default:
    1136         strcpy(type_of_backup, "ISO");
    1137     }
    1138     return (type_of_backup);
    1139 }
Note: See TracChangeset for help on using the changeset viewer.