Changeset 44 for trunk/mondo/mondo


Ignore:
Timestamp:
Oct 4, 2005, 6:26:13 PM (14 years ago)
Author:
bcornec
Message:

asprintf and memory management for libmondo-string.c

File:
1 edited

Legend:

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

    r30 r44  
    1 /* libmondo-string.c
    2    $Id: libmondo-string.c,v 1.3 2004/06/21 20:20:36 hugo Exp $
    3 
    4 - string manipulation
    5 
    6 
    7 08/02
    8 - added function turn_wildcard_chars_into_literal_chars()
    9 
    10 03/10/2004?
    11 - make percent_media_full_comment() use media_descriptor_string():
    12   "DVD %d" or "ISO %d" (if that's what it is) rather than "CD %d"
    13 - fix build_partition_name() to use 'adXsY' rather than 'adXpY' on FreeBSD
    14 
    15 10/08/2003
    16 - changed 'CD %d' to '<media> %d' (msg)
    17 
    18 10/01
    19 - fixing strip_spaces() to handle /r properly
    20 
    21 09/26
    22 - added char *media_descriptor_string(t_bkptype);
    23 
    24 05/06
    25 - cleaned up severity_of_difference() a bit
    26 
    27 05/05
    28 - added Joshua Oreman's FreeBSD patches
    29 
    30 04/24
    31 - added lots of assert()'s and log_OS_error()'s
    32 - severity_of_difference() skips "/mnt/RESTORING" at start
    33   of filename if it's there
    34 
    35 04/04/2003
    36 - misc clean-up (Tom Mortell)
    37 
    38 11/17/2002
    39 - strip_spaces() now accommodates _smaller_ strings auto'y
    40 
    41 11/08
    42 - if decimal point in string sent to friendly_sizestr_to_sizelong()
    43   then fatal error: we expect integers only
    44 
    45 10/01 - 10/31
    46 - commented subroutines
    47 - strip_spaces() allows up to MAX_STR_LEN-len input string
    48 
    49 08/01 - 08/31
    50 - fixed bug in friendly_sizestr_to_sizelong() which stopped
    51   it from working with capital G's and K's
    52 - fixed bug in build_partition_name()
    53 
    54 07/24
    55 - created
     1/*
     2   $Id$
    563*/
    574
     
    7118
    7219/*@unused@*/
    73 //static char cvsid[] = "$Id: libmondo-string.c,v 1.3 2004/06/21 20:20:36 hugo Exp $";
     20//static char cvsid[] = "$Id$";
    7421
    7522extern int g_current_media_number;
    7623extern long long g_tape_posK;
    77 
    7824
    7925/**
     
    8935 * @note If @p drive ends in a digit, then 'p' (on Linux) or 's' (on *BSD) is added before @p partno.
    9036 */
    91 char *
    92 build_partition_name (char *partition, const char *drive, int partno)
    93 {
    94     char *p, *c;
    95 
    96   assert(partition!=NULL);
    97   assert_string_is_neither_NULL_nor_zerolength(drive);
    98   assert(partno>=0);
    99 
    100   p = strcpy(partition, drive);
    101   /* is this a devfs device path? */
    102   c = strrchr (partition, '/');
    103   if (c && strncmp (c, "/disc", 5) == 0) {
    104       /* yup it's devfs, return the "part" path */
    105       strcpy (c + 1, "part");
    106       p = c + 5;
    107   } else {
    108       p += strlen (p);
    109       if (isdigit (p[-1])) {
    110       *p++ =
     37char *build_partition_name(char *partition, const char *drive, int partno)
     38{
     39    char *p, *c;
     40
     41    assert(partition != NULL);
     42    assert_string_is_neither_NULL_nor_zerolength(drive);
     43    assert(partno >= 0);
     44
     45    p = strcpy(partition, drive);
     46    /* is this a devfs device path? */
     47    c = strrchr(partition, '/');
     48    if (c && strncmp(c, "/disc", 5) == 0) {
     49        /* yup it's devfs, return the "part" path */
     50        strcpy(c + 1, "part");
     51        p = c + 5;
     52    } else {
     53        p += strlen(p);
     54        if (isdigit(p[-1])) {
     55            *p++ =
    11156#ifdef BSD
    112          's';
     57                's';
    11358#else
    114              'p';
     59                'p';
    11560#endif
    116       }
    117     }
    118   sprintf (p, "%d", partno);
    119   return (partition);
    120 }
    121 
    122 
    123 
    124 
    125 
    126 
    127 
    128 
    129 
    130 
     61        }
     62    }
     63    sprintf(p, "%d", partno);
     64    return (partition);
     65}
    13166
    13267
     
    13772 * @param width The width of the final result.
    13873 */
    139 void center_string (char *in_out, int width)
    140 {
    141   char scratch[MAX_STR_LEN];
    142   char *p;
    143   int i;            /* purpose */
    144   int len;          /* purpose */
    145   int mid;          /* purpose */
    146   int x;            /* purpose */
    147 
    148   assert(in_out!=NULL);
    149   assert(width>2);
    150 
    151   if (strlen (in_out) == 0)
    152     {
    153       return;
    154     }
    155   for (p = in_out; *p == ' '; p++);
    156   strcpy (scratch, p);
    157   len = (int) strlen (scratch);
    158   mid = width / 2;
    159   x = mid - len / 2;
    160   for (i = 0; i < x; i++)
    161     {
    162       in_out[i] = ' ';
    163     }
    164   in_out[i] = '\0';
    165   strcat (in_out, scratch);
    166 }
    167 
    168 
    169 
    170 
    171 inline void turn_wildcard_chars_into_literal_chars(char*sout, char*sin)
     74void center_string(char *in_out, int width)
     75{
     76    char *scratch;
     77    char *p;
     78    int i;                      /* purpose */
     79    int len;                    /* purpose */
     80    int mid;                    /* purpose */
     81    int x;                      /* purpose */
     82
     83    assert(in_out != NULL);
     84    assert(width > 2);
     85
     86    if (strlen(in_out) == 0) {
     87        return;
     88    }
     89    for (p = in_out; *p == ' '; p++);
     90    asprintf(&scratch, "%s", p);
     91    len = (int) strlen(scratch);
     92    mid = width / 2;
     93    x = mid - len / 2;
     94    for (i = 0; i < x; i++) {
     95        in_out[i] = ' ';
     96    }
     97    in_out[i] = '\0';
     98    strcat(in_out, scratch);
     99    paranoid_free(scratch);
     100}
     101
     102
     103
     104
     105inline void turn_wildcard_chars_into_literal_chars(char *sout, char *sin)
    172106{
    173107    char *p, *q;
    174    
    175     for(p=sin, q=sout; *p!='\0'; *(q++) = *(p++) )
    176       {
    177         if (strchr("[]*?", *p)) { *(q++) = '\\'; }
    178       }
    179     *q = *p; // for the final '\0'
    180 }
    181 
    182 
    183 
    184 /**
     108
     109    for (p = sin, q = sout; *p != '\0'; *(q++) = *(p++)) {
     110        if (strchr("[]*?", *p)) {
     111            *(q++) = '\\';
     112        }
     113    }
     114    *q = *p;                    // for the final '\0'
     115}
     116
     117
     118
     119/*
    185120 * Add commas every third place in @p input.
    186121 * @param input The string to commarize.
     
    188123 * @note The returned string points to static storage that will be overwritten with each call.
    189124 */
    190 char* commarize(char*input)
    191 {
    192   char pos_w_commas[MAX_STR_LEN];
    193   static char output[MAX_STR_LEN];
    194   char tmp[MAX_STR_LEN];
    195   int j;
    196 
    197   assert(input!=NULL);
    198 
    199   strcpy(tmp, input);
    200   if (strlen(tmp) > 6)
    201     {
    202       strcpy(pos_w_commas, tmp);
    203       j = (int) strlen(pos_w_commas);
    204       tmp[j-6] = ',';
    205       strcpy(tmp+j-5, pos_w_commas+j-6);
    206 //      tmp[j-2] = ',';
    207 //      strcpy(tmp+j-1, pos_w_commas+j-3);
    208       strcpy(pos_w_commas, tmp);
    209     }
    210   if (strlen(tmp) > 3)
    211     {
    212       j = (int) strlen(tmp);
    213       strcpy(pos_w_commas, tmp);
    214       pos_w_commas[j-3] = ',';
    215       strcpy(pos_w_commas+j-2, tmp+j-3);
    216     }
    217   else
    218     {
    219       strcpy(pos_w_commas, tmp);
    220     }
    221   strcpy(output, pos_w_commas);
    222   return(output);
    223 }
    224 
    225 
    226 
    227 
    228 
    229 
    230 
     125char *commarize(char *input)
     126{
     127    char *pos_w_commas;
     128    static char output[MAX_STR_LEN];
     129    char tmp[MAX_STR_LEN];
     130    int j;
     131
     132    assert(input != NULL);
     133
     134    strcpy(tmp, input);
     135    if (strlen(tmp) > 6) {
     136        asprintf(&pos_w_commas, "%s", tmp);
     137        j = (int) strlen(pos_w_commas);
     138        tmp[j - 6] = ',';
     139        strcpy(tmp + j - 5, pos_w_commas + j - 6);
     140        paranoid_free(pos_w_commas);
     141        asprintf(&pos_w_commas, "%s", tmp);
     142    }
     143    if (strlen(tmp) > 3) {
     144        j = (int) strlen(tmp);
     145        asprintf(&pos_w_commas, "%s", tmp);
     146        pos_w_commas[j - 3] = ',';
     147        strcpy(pos_w_commas + j - 2, tmp + j - 3);
     148    } else {
     149        asprintf(&pos_w_commas, "%s", tmp);
     150    }
     151    strcpy(output, pos_w_commas);
     152    paranoid_free(pos_w_commas);
     153    return (output);
     154}
    231155
    232156
     
    241165 * @note The returned string points to static storage and will be overwritten with each call.
    242166 */
    243 char *
    244 disklist_entry_to_string (struct list_of_disks *disklist, int lino)
    245 {
    246 
    247     /*@ buffers ***********************************************************/
    248   static char output[MAX_STR_LEN];
    249 
    250   assert(disklist!=NULL);
    251 
    252   sprintf (output, "%-24s %8d", disklist->el[lino].device,
    253        disklist->el[lino].index);
    254   return (output);
     167char *disklist_entry_to_string(struct list_of_disks *disklist, int lino)
     168{
     169
     170    /*@ buffers ********************************************************** */
     171    static char output[MAX_STR_LEN];
     172
     173    assert(disklist != NULL);
     174
     175    sprintf(output, "%-24s %8d", disklist->el[lino].device,
     176            disklist->el[lino].index);
     177    return (output);
    255178}
    256179
     
    266189 * @return The size in megabytes.
    267190 */
    268 long
    269 friendly_sizestr_to_sizelong (char *incoming)
    270 {
    271   long outval;
    272   int i;
    273   char *tmp;
    274   char ch;
    275 
    276   assert_string_is_neither_NULL_nor_zerolength(incoming);
    277 
    278   malloc_string(tmp);
    279   if (!incoming[0]) { free(tmp); return(0); }
    280   if (strchr(incoming, '.'))
    281     { fatal_error("Please use integers only. No decimal points."); }
    282   strcpy (tmp, incoming);
    283   i = (int) strlen(tmp);
    284   if (tmp[i-1]=='B' || tmp[i-1]=='b') { tmp[i-1]='\0'; }
    285   for (i = 0; i < (int) strlen (tmp) && isdigit (tmp[i]); i++);
    286   ch = tmp[i];
    287   tmp[i] = '\0';
    288   outval = atol (tmp);
    289   if (ch == 'g' || ch == 'G')
    290     {
    291       outval = outval * 1024;
    292     }
    293   else if (ch == 'k' || ch == 'K')
    294     {
    295       outval = outval / 1024;
    296     }
    297   else if (ch == 't' || ch == 'T') // terabyte
    298     {
    299     outval *= 1048576;
    300     }
    301   else if (ch == 'Y' || ch == 'y') // yottabyte - the biggest measure in the info file
    302     {
    303     log_it ("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?!?!");
    304     popup_and_OK ("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 :-)");
    305     fatal_error ("Integer overflow.");
    306     }
    307   else if (ch != 'm' && ch != 'M')
    308     {
    309       sprintf(tmp, "Re: parameter '%s' - bad multiplier ('%c')", incoming, ch);
    310       fatal_error(tmp);
    311     }
    312   paranoid_free(tmp);
    313   return (outval);
    314 }
    315 
     191long friendly_sizestr_to_sizelong(char *incoming)
     192{
     193    long outval;
     194    int i;
     195    char *tmp;
     196    char ch;
     197
     198    assert_string_is_neither_NULL_nor_zerolength(incoming);
     199
     200    if (!incoming[0]) {
     201        return (0);
     202    }
     203    if (strchr(incoming, '.')) {
     204        fatal_error("Please use integers only. No decimal points.");
     205    }
     206    asprintf(&tmp, "%s", incoming);
     207    i = (int) strlen(tmp);
     208    if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') {
     209        tmp[i - 1] = '\0';
     210    }
     211    for (i = 0; i < (int) strlen(tmp) && isdigit(tmp[i]); i++);
     212    ch = tmp[i];
     213    tmp[i] = '\0';
     214    outval = atol(tmp);
     215    paranoid_free(tmp);
     216    if (ch == 'g' || ch == 'G') {
     217        outval = outval * 1024;
     218    } else if (ch == 'k' || ch == 'K') {
     219        outval = outval / 1024;
     220    } else if (ch == 't' || ch == 'T')  // terabyte
     221    {
     222        outval *= 1048576;
     223    } else if (ch == 'Y' || ch == 'y')  // yottabyte - the biggest measure in the info file
     224    {
     225        log_it
     226            ("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?!?!");
     227        popup_and_OK
     228            ("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 :-)");
     229        fatal_error("Integer overflow.");
     230    } else if (ch != 'm' && ch != 'M') {
     231        asprintf(&tmp, "Re: parameter '%s' - bad multiplier ('%c')",
     232                 incoming, ch);
     233        fatal_error(tmp);
     234    }
     235    return (outval);
     236}
    316237
    317238
     
    324245 * @bug Why does center_string() modify its argument but leftpad_string() returns a modified copy?
    325246 */
    326 char *
    327 leftpad_string (char *incoming, int width)
    328 {
    329     /*@ buffers ******************************************************/
    330   static char output[MAX_STR_LEN];
    331 
    332     /*@ ints *********************************************************/
    333   int i;
    334 
    335     /*@ end vars *****************************************************/
    336   assert(incoming!=NULL);
    337   assert(width>2);
    338 
    339   strcpy (output, incoming);
    340   for (i = (int) strlen (output); i < width; i++)
    341     {
    342       output[i] = ' ';
    343     }
    344   output[i] = '\0';
    345   return (output);
    346 }
     247/* BERLIOS; useless ?
     248char *leftpad_string(char *incoming, int width)
     249{
     250    char *output;
     251
     252    int i;
     253
     254    assert(incoming != NULL);
     255    assert(width > 2);
     256
     257    asprintf(output, "%s", incoming);
     258    for (i = (int) strlen(output); i < width; i++) {
     259        output[i] = ' ';
     260    }
     261    output[i] = '\0';
     262    return (output);
     263}
     264*/
    347265
    348266
     
    352270 * Unknown markers are identified as "BLK_UNKNOWN (%d)" where %d is the decimal value.
    353271 * @param marker The marker byte to stringify.
    354  * @return @p marker as a string.
     272 * @return @p marker as a string. this should be freed by the caller
    355273 * @note The returned string points to static storage that will be overwritten with each call.
    356274 */
    357 char *
    358 marker_to_string (int marker)
    359 {
    360     /*@ buffer *******************************************************/
    361   static char outstr[MAX_STR_LEN];
    362 
    363 
    364     /*@ end vars ****************************************************/
    365 
    366   switch (marker)
    367     {
    368     case BLK_START_OF_BACKUP:
    369       strcpy (outstr, "BLK_START_OF_BACKUP");
    370       break;
    371     case BLK_START_OF_TAPE:
    372       strcpy (outstr, "BLK_START_OF_TAPE");
    373       break;
    374     case BLK_START_AN_AFIO_OR_SLICE:
    375       strcpy (outstr, "BLK_START_AN_AFIO_OR_SLICE");
    376       break;
    377     case BLK_STOP_AN_AFIO_OR_SLICE:
    378       strcpy (outstr, "BLK_STOP_AN_AFIO_OR_SLICE");
    379       break;
    380     case BLK_START_AFIOBALLS:
    381       strcpy (outstr, "BLK_START_AFIOBALLS");
    382       break;
    383     case BLK_STOP_AFIOBALLS:
    384       strcpy (outstr, "BLK_STOP_AFIOBALLS");
    385       break;
    386     case BLK_STOP_BIGGIEFILES:
    387       strcpy (outstr, "BLK_STOP_BIGGIEFILES");
    388       break;
    389     case BLK_START_A_NORMBIGGIE:
    390       strcpy (outstr, "BLK_START_A_NORMBIGGIE");
    391       break;
    392     case BLK_START_A_PIHBIGGIE:
    393       strcpy (outstr, "BLK_START_A_PIHBIGGIE");
    394       break;
    395     case BLK_START_EXTENDED_ATTRIBUTES:
    396       strcpy (outstr, "BLK_START_EXTENDED_ATTRIBUTES");
    397       break;
    398     case BLK_STOP_EXTENDED_ATTRIBUTES:
    399       strcpy (outstr, "BLK_STOP_EXTENDED_ATTRIBUTES");
    400       break;
    401     case BLK_START_EXAT_FILE:
    402       strcpy (outstr, "BLK_START_EXAT_FILE");
    403       break;
    404     case BLK_STOP_EXAT_FILE:
    405       strcpy (outstr, "BLK_STOP_EXAT_FILE");
    406       break;
    407     case BLK_START_BIGGIEFILES:
    408       strcpy (outstr, "BLK_START_BIGGIEFILES");
    409       break;
    410     case BLK_STOP_A_BIGGIE:
    411       strcpy (outstr, "BLK_STOP_A_BIGGIE");
    412       break;
    413     case BLK_END_OF_TAPE:
    414       strcpy (outstr, "BLK_END_OF_TAPE");
    415       break;
    416     case BLK_END_OF_BACKUP:
    417       strcpy (outstr, "BLK_END_OF_BACKUP");
    418       break;
    419     case BLK_ABORTED_BACKUP:
    420       strcpy (outstr, "BLK_ABORTED_BACKUP");
    421       break;
    422     case BLK_START_FILE:
    423       strcpy (outstr, "BLK_START_FILE");
    424       break;
    425     case BLK_STOP_FILE:
    426       strcpy (outstr, "BLK_STOP_FILE");
    427       break;
    428     default:
    429       sprintf (outstr, "BLK_UNKNOWN (%d)", marker);
    430       break;
    431     }
    432   return (outstr);
    433 }
    434 
    435 
     275char *marker_to_string(int marker)
     276{
     277    /*@ buffer ****************************************************** */
     278    char *outstr;
     279
     280
     281    /*@ end vars *************************************************** */
     282
     283    switch (marker) {
     284    case BLK_START_OF_BACKUP:
     285        asprintf(&outstr, "%s", "BLK_START_OF_BACKUP");
     286        break;
     287    case BLK_START_OF_TAPE:
     288        asprintf(&outstr, "%s", "BLK_START_OF_TAPE");
     289        break;
     290    case BLK_START_AN_AFIO_OR_SLICE:
     291        asprintf(&outstr, "%s", "BLK_START_AN_AFIO_OR_SLICE");
     292        break;
     293    case BLK_STOP_AN_AFIO_OR_SLICE:
     294        asprintf(&outstr, "%s", "BLK_STOP_AN_AFIO_OR_SLICE");
     295        break;
     296    case BLK_START_AFIOBALLS:
     297        asprintf(&outstr, "%s", "BLK_START_AFIOBALLS");
     298        break;
     299    case BLK_STOP_AFIOBALLS:
     300        asprintf(&outstr, "%s", "BLK_STOP_AFIOBALLS");
     301        break;
     302    case BLK_STOP_BIGGIEFILES:
     303        asprintf(&outstr, "%s", "BLK_STOP_BIGGIEFILES");
     304        break;
     305    case BLK_START_A_NORMBIGGIE:
     306        asprintf(&outstr, "%s", "BLK_START_A_NORMBIGGIE");
     307        break;
     308    case BLK_START_A_PIHBIGGIE:
     309        asprintf(&outstr, "%s", "BLK_START_A_PIHBIGGIE");
     310        break;
     311    case BLK_START_EXTENDED_ATTRIBUTES:
     312        asprintf(&outstr, "%s", "BLK_START_EXTENDED_ATTRIBUTES");
     313        break;
     314    case BLK_STOP_EXTENDED_ATTRIBUTES:
     315        asprintf(&outstr, "%s", "BLK_STOP_EXTENDED_ATTRIBUTES");
     316        break;
     317    case BLK_START_EXAT_FILE:
     318        asprintf(&outstr, "%s", "BLK_START_EXAT_FILE");
     319        break;
     320    case BLK_STOP_EXAT_FILE:
     321        asprintf(&outstr, "%s", "BLK_STOP_EXAT_FILE");
     322        break;
     323    case BLK_START_BIGGIEFILES:
     324        asprintf(&outstr, "%s", "BLK_START_BIGGIEFILES");
     325        break;
     326    case BLK_STOP_A_BIGGIE:
     327        asprintf(&outstr, "%s", "BLK_STOP_A_BIGGIE");
     328        break;
     329    case BLK_END_OF_TAPE:
     330        asprintf(&outstr, "%s", "BLK_END_OF_TAPE");
     331        break;
     332    case BLK_END_OF_BACKUP:
     333        asprintf(&outstr, "%s", "BLK_END_OF_BACKUP");
     334        break;
     335    case BLK_ABORTED_BACKUP:
     336        asprintf(&outstr, "%s", "BLK_ABORTED_BACKUP");
     337        break;
     338    case BLK_START_FILE:
     339        asprintf(&outstr, "%s", "BLK_START_FILE");
     340        break;
     341    case BLK_STOP_FILE:
     342        asprintf(&outstr, "%s", "BLK_STOP_FILE");
     343        break;
     344    default:
     345        asprintf(&outstr, "%s", "BLK_UNKNOWN (%d)", marker);
     346        break;
     347    }
     348    return (outstr);
     349}
    436350
    437351
     
    444358 * @param mountlist The mountlist to operate on.
    445359 * @param lino The line number in @p mountlist to stringify.
    446  * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>].
     360 * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>]. To be freed by the caller
    447361 * @note The returned string points to static storage and will be overwritten with each call.
    448362 */
    449 char *
    450 mountlist_entry_to_string (struct mountlist_itself *mountlist, int lino)
    451 {
    452 
    453     /*@ buffer ************************************************************/
    454   static char output[MAX_STR_LEN];
    455 
    456   assert(mountlist!=NULL);
    457 
    458   sprintf (output, "%-24s %-24s %-10s %8lld", mountlist->el[lino].device,
    459        mountlist->el[lino].mountpoint, mountlist->el[lino].format,
    460        mountlist->el[lino].size / 1024);
    461   return (output);
    462 }
    463 
    464 
    465 
    466 
     363char *mountlist_entry_to_string(struct mountlist_itself *mountlist,
     364                                int lino)
     365{
     366
     367    /*@ buffer *********************************************************** */
     368    char *output;
     369
     370    assert(mountlist != NULL);
     371
     372    asprintf(&output, "%-24s %-24s %-10s %8lld",
     373             mountlist->el[lino].device, mountlist->el[lino].mountpoint,
     374             mountlist->el[lino].format, mountlist->el[lino].size / 1024);
     375    return (output);
     376}
    467377
    468378
     
    472382 * @param label The "blah blah" part in the middle. If you leave this blank
    473383 * there will be a weird double space in the middle, so pass *something*.
    474  * @return The string containing "X blah blah disk(s)".
     384 * @return The string containing "X blah blah disk(s)". To be  freed by the caller
    475385 * @note The returned string points to static storage and will be overwritten with each call.
    476386 */
    477 char *
    478 number_of_disks_as_string (int noof_disks, char *label)
    479 {
    480 
    481     /*@ buffers **********************************************************/
    482   static char output[MAX_STR_LEN];
    483 
    484     /*@ char     *********************************************************/
    485   char p;
    486 
    487   assert(label!=NULL);
    488 
    489   if (noof_disks > 1)
    490     {
    491       p = 's';
    492     }
    493   else
    494     {
    495       p = ' ';
    496     }
    497   sprintf (output, "%d %s disk%c", noof_disks, label, p);
    498   while (strlen (output) < 14)
    499     {
    500       strcat (output, " ");
    501     }
    502   return (output);
    503 }
    504 
    505 
     387char *number_of_disks_as_string(int noof_disks, char *label)
     388{
     389
     390    /*@ buffers ********************************************************* */
     391    char *output;
     392
     393    /*@ char     ******************************************************** */
     394    char p;
     395
     396    assert(label != NULL);
     397
     398    if (noof_disks > 1) {
     399        p = 's';
     400    } else {
     401        p = ' ';
     402    }
     403    asprintf(&output, "%d %s disk%c    ", noof_disks, label, p);
     404    /* BERLIOS: replaced with space^^^^ here !
     405       while (strlen(output) < 14) {
     406       strcat(output, " ");
     407       }
     408     */
     409    return (output);
     410}
    506411
    507412/**
     
    509414 * number (e.g. "one", "two", ..., "nine", "ten", "11", ...).
    510415 * @param i The number to stringify.
    511  * @return The string form of @p i.
     416 * @return The string form of @p i. To be freed by caller.
    512417 * @note The returned value points to static strorage that will be overwritten with each call.
    513418 */
    514 char *
    515 number_to_text (int i)
    516 {
    517 
    518     /*@ buffers ******************************************************/
    519   static char output[MAX_STR_LEN];
    520 
    521 
    522     /*@ end vars ****************************************************/
    523 
    524   switch (i)
    525     {
    526     case 0:
    527       strcpy (output, "zero");
    528       break;
    529     case 1:
    530       strcpy (output, "one");
    531       break;
    532     case 2:
    533       strcpy (output, "two");
    534       break;
    535     case 3:
    536       strcpy (output, "three");
    537       break;
    538     case 4:
    539       strcpy (output, "four");
    540       break;
    541     case 5:
    542       strcpy (output, "five");
    543       break;
    544     case 6:
    545       strcpy (output, "six");
    546       break;
    547     case 7:
    548       strcpy (output, "seven");
    549       break;
    550     case 8:
    551       strcpy (output, "eight");
    552       break;
    553     case 9:
    554       strcpy (output, "nine");
    555     case 10:
    556       strcpy (output, "ten");
    557     default:
    558       sprintf (output, "%d", i);
    559     }
    560   return (output);
    561 }
    562 
    563 
     419char *number_to_text(int i)
     420{
     421
     422    /*@ buffers ***************************************************** */
     423    char *output;
     424
     425
     426    /*@ end vars *************************************************** */
     427
     428    switch (i) {
     429    case 0:
     430        asprintf(&output, "%s", "zero");
     431        break;
     432    case 1:
     433        asprintf(&output, "%s", "one");
     434        break;
     435    case 2:
     436        asprintf(&output, "%s", "two");
     437        break;
     438    case 3:
     439        asprintf(&output, "%s", "three");
     440        break;
     441    case 4:
     442        asprintf(&output, "%s", "four");
     443        break;
     444    case 5:
     445        asprintf(&output, "%s", "five");
     446        break;
     447    case 6:
     448        asprintf(&output, "%s", "six");
     449        break;
     450    case 7:
     451        asprintf(&output, "%s", "seven");
     452        break;
     453    case 8:
     454        asprintf(&output, "%s", "eight");
     455        break;
     456    case 9:
     457        asprintf(&output, "%s", "nine");
     458    case 10:
     459        asprintf(&output, "%s", "ten");
     460    default:
     461        asprintf(&output, "%d", i);
     462    }
     463    return (output);
     464}
    564465
    565466
     
    571472 * @param value The value to replace @p token.
    572473 */
    573 void
    574 resolve_naff_tokens (char *output, char *ip, char *value, char *token)
    575 {
    576     /*@ buffers ****/
    577   char *input;
    578 
    579     /*@ pointers **/
    580   char *p;
    581 
    582   input = malloc(2000);
    583   assert_string_is_neither_NULL_nor_zerolength(ip);
    584   assert_string_is_neither_NULL_nor_zerolength(token);
    585   assert(value!=NULL);
    586 
    587   strcpy (output, ip);      /* just in case the token doesn't appear in string at all */
    588   for (strcpy (input, ip); strstr (input, token); strcpy (input, output))
    589     {
    590       strcpy (output, input);
    591       p = strstr (output, token);
    592       *p = '\0';
    593       strcat (output, value);
    594       p = strstr (input, token) + strlen (token);
    595       strcat (output, p);
    596     }
    597   paranoid_free(input);
    598 }
    599 
    600 
    601 
     474void resolve_naff_tokens(char *output, char *ip, char *value, char *token)
     475{
     476    /*@ buffers *** */
     477    char *input;
     478
     479    /*@ pointers * */
     480    char *p;
     481
     482    assert_string_is_neither_NULL_nor_zerolength(ip);
     483    assert_string_is_neither_NULL_nor_zerolength(token);
     484    assert(value != NULL);
     485
     486    strcpy(output, ip);         /* just in case the token doesn't appear in string at all */
     487    asprintf(&input, "%s", ip);
     488    while (strstr(input, token)) {
     489        strcpy(output, input);
     490        p = strstr(output, token);
     491        *p = '\0';
     492        strcat(output, value);
     493        p = strstr(input, token) + strlen(token);
     494        strcat(output, p);
     495        paranoid_free(input);
     496        asprintf(&input, "%s", output);
     497    }
     498    paranoid_free(input);
     499}
    602500
    603501
     
    614512 * @param path The path to append (with a / in the middle) to the slice filename.
    615513 * @param s If not "" then add a "." and this to the end.
    616  * @return The slice filename.
     514 * @return The slice filename. To be freed by caller
    617515 * @note The returned value points to static storage and will be overwritten with each call.
    618516 */
    619 char *
    620 slice_fname (long bigfileno, long sliceno, char *path, char *s)
    621 {
    622 
    623     /*@ buffers *****************************************************/
    624   static char output[MAX_STR_LEN];
    625   static char suffix[MAX_STR_LEN];
    626 
    627     /*@ end vars ****************************************************/
    628 
    629   assert_string_is_neither_NULL_nor_zerolength(path);
    630   if (s[0] != '\0')
    631     {
    632       sprintf (suffix, ".%s", s);
    633     }
    634   else
    635     {
    636       suffix[0] = '\0';
    637     }
    638   sprintf (output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno,
    639        suffix);
    640   return (output);
     517char *slice_fname(long bigfileno, long sliceno, char *path, char *s)
     518{
     519
     520    /*@ buffers **************************************************** */
     521    char *output;
     522    char *suffix;
     523
     524    /*@ end vars *************************************************** */
     525
     526    assert_string_is_neither_NULL_nor_zerolength(path);
     527    if (s[0] != '\0') {
     528        asprintf(&suffix, ".%s", s);
     529    } else {
     530        asprintf(&suffix, "%s", "");
     531    }
     532    asprintf(&output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno,
     533             sliceno, suffix);
     534    paranoid_free(suffix);
     535    return (output);
    641536}
    642537
     
    650545 * @return The character for this iteration.
    651546 */
    652 int
    653 special_dot_char (int i)
    654 {
    655   switch (i % 4)
    656     {
    657     case 0:
    658       return ('/');
    659     case 1:
    660       return ('-');
    661     case 2:
    662       return ('\\');
    663     case 3:
    664       return ('|');
    665     default:
    666       return ('.');
    667     }
    668   return ('.');
     547int special_dot_char(int i)
     548{
     549    switch (i % 4) {
     550    case 0:
     551        return ('/');
     552    case 1:
     553        return ('-');
     554    case 2:
     555        return ('\\');
     556    case 3:
     557        return ('|');
     558    default:
     559        return ('.');
     560    }
     561    return ('.');
    669562}
    670563
     
    682575 */
    683576bool
    684 spread_flaws_across_three_lines (char *flaws_str, char *flaws_str_A,
    685                  char *flaws_str_B, char *flaws_str_C,
    686                  int res)
    687 {
    688 
    689     /*@ int **************************************************************/
    690   int i = 0;
    691 
    692     /*@ initialize *******************************************************/
    693   assert(flaws_str_A!=NULL);
    694   assert(flaws_str_B!=NULL);
    695   assert(flaws_str_C!=NULL);
    696   assert(flaws_str!=NULL);
    697 
    698   flaws_str_A[0] = flaws_str_B[0] = flaws_str_C[0] = '\0';
    699 
    700 
    701   if (!res && !strlen (flaws_str))
    702     {
    703       return (TRUE);
    704     }
    705   if (strlen (flaws_str) > 0)
    706     {
    707       sprintf (flaws_str_A, "%s", flaws_str + 1);
    708     }
    709   if (strlen (flaws_str_A) >= 74)
    710     {
    711       for (i = 74; flaws_str_A[i] != ' '; i--);
    712       strcpy (flaws_str_B, flaws_str_A + i + 1);
    713       flaws_str_A[i] = '\0';
    714     }
    715   if (strlen (flaws_str_B) >= 74)
    716     {
    717       for (i = 74; flaws_str_B[i] != ' '; i--);
    718       strcpy (flaws_str_C, flaws_str_B + i + 1);
    719       flaws_str_B[i] = '\0';
    720     }
    721   if (res)
    722     {
    723       return (FALSE);
    724     }
    725   else
    726     {
    727       return (TRUE);
    728     }
     577spread_flaws_across_three_lines(char *flaws_str, char *flaws_str_A,
     578                                char *flaws_str_B, char *flaws_str_C,
     579                                int res)
     580{
     581
     582    /*@ int ************************************************************* */
     583    int i = 0;
     584
     585    /*@ initialize ****************************************************** */
     586    assert(flaws_str_A != NULL);
     587    assert(flaws_str_B != NULL);
     588    assert(flaws_str_C != NULL);
     589    assert(flaws_str != NULL);
     590
     591    flaws_str_A[0] = flaws_str_B[0] = flaws_str_C[0] = '\0';
     592
     593
     594    if (!res && !strlen(flaws_str)) {
     595        return (TRUE);
     596    }
     597    if (strlen(flaws_str) > 0) {
     598        sprintf(flaws_str_A, "%s", flaws_str + 1);
     599    }
     600    if (strlen(flaws_str_A) >= 74) {
     601        for (i = 74; flaws_str_A[i] != ' '; i--);
     602        strcpy(flaws_str_B, flaws_str_A + i + 1);
     603        flaws_str_A[i] = '\0';
     604    }
     605    if (strlen(flaws_str_B) >= 74) {
     606        for (i = 74; flaws_str_B[i] != ' '; i--);
     607        strcpy(flaws_str_C, flaws_str_B + i + 1);
     608        flaws_str_B[i] = '\0';
     609    }
     610    if (res) {
     611        return (FALSE);
     612    } else {
     613        return (TRUE);
     614    }
    729615}
    730616
     
    740626 * there are any in the middle those will be sorted ASCIIbetically.
    741627 */
    742 int
    743 strcmp_inc_numbers (char *stringA, char *stringB)
    744 {
    745     /*@ int **********************************************************/
    746   int i;
    747   int start_of_numbers_in_A;
    748   int start_of_numbers_in_B;
    749   int res;
    750 
    751     /*@ long ********************************************************/
    752   long numA;
    753   long numB;
    754 
    755     /*@ end vars ****************************************************/
    756   assert(stringA!=NULL);
    757   assert(stringB!=NULL);
    758 
    759   if (strlen (stringA) == strlen (stringB))
    760     {
    761       return (strcmp (stringA, stringB));
    762     }
    763   for (i = (int) strlen (stringA); i > 0 && isdigit (stringA[i - 1]); i--);
    764   if (i == (int) strlen (stringA))
    765     {
    766       return (strcmp (stringA, stringB));
    767     }
    768   start_of_numbers_in_A = i;
    769   for (i = (int) strlen (stringB); i > 0 && isdigit (stringB[i - 1]); i--);
    770   if (i == (int) strlen (stringB))
    771     {
    772       return (strcmp (stringA, stringB));
    773     }
    774   start_of_numbers_in_B = i;
    775   if (start_of_numbers_in_A != start_of_numbers_in_B)
    776     {
    777       return (strcmp (stringA, stringB));
    778     }
    779   res = strncmp (stringA, stringB, (size_t) i);
    780   if (res)
    781     {
    782       return (res);
    783     }
    784   numA = atol (stringA + start_of_numbers_in_A);
    785   numB = atol (stringB + start_of_numbers_in_B);
    786   /*
    787      sprintf(tmp,"Comparing %s and %s --> %ld,%ld\n",stringA,stringB,numA,numB);
    788      log_to_screen(tmp);
    789    */
    790   return ((int) (numA - numB));
     628int strcmp_inc_numbers(char *stringA, char *stringB)
     629{
     630    /*@ int ********************************************************* */
     631    int i;
     632    int start_of_numbers_in_A;
     633    int start_of_numbers_in_B;
     634    int res;
     635
     636    /*@ long ******************************************************* */
     637    long numA;
     638    long numB;
     639
     640    /*@ end vars *************************************************** */
     641    assert(stringA != NULL);
     642    assert(stringB != NULL);
     643
     644    if (strlen(stringA) == strlen(stringB)) {
     645        return (strcmp(stringA, stringB));
     646    }
     647    for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--);
     648    if (i == (int) strlen(stringA)) {
     649        return (strcmp(stringA, stringB));
     650    }
     651    start_of_numbers_in_A = i;
     652    for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--);
     653    if (i == (int) strlen(stringB)) {
     654        return (strcmp(stringA, stringB));
     655    }
     656    start_of_numbers_in_B = i;
     657    if (start_of_numbers_in_A != start_of_numbers_in_B) {
     658        return (strcmp(stringA, stringB));
     659    }
     660    res = strncmp(stringA, stringB, (size_t) i);
     661    if (res) {
     662        return (res);
     663    }
     664    numA = atol(stringA + start_of_numbers_in_A);
     665    numB = atol(stringB + start_of_numbers_in_B);
     666    /*
     667       sprintf(tmp,"Comparing %s and %s --> %ld,%ld\n",stringA,stringB,numA,numB);
     668       log_to_screen(tmp);
     669     */
     670    return ((int) (numA - numB));
    791671}
    792672
     
    801681 * @note The returned string points to static storage that will be overwritten with each call.
    802682 */
    803 char *
    804 strip_afio_output_line (char *input)
    805 {
    806     /*@ buffer *******************************************************/
    807   static char output[MAX_STR_LEN];
    808 
    809     /*@ pointers *****************************************************/
    810   char *p;
    811   char *q;
    812     /*@ end vars ****************************************************/
    813 
    814   assert(input!=NULL);
    815   strcpy (output, input);
    816   p = strchr (input, '\"');
    817   if (p)
    818     {
    819       q = strchr (++p, '\"');
    820       if (q)
    821     {
    822       strcpy (output, p);
    823       *(strchr (output, '\"')) = '\0';
    824     }
    825     }
    826   return (output);
    827 }
    828 
     683char *strip_afio_output_line(char *input)
     684{
     685    /*@ buffer ****************************************************** */
     686    char *output;
     687
     688    /*@ pointers **************************************************** */
     689    char *p;
     690    char *q;
     691    /*@ end vars *************************************************** */
     692
     693    assert(input != NULL);
     694    asprintf(&output, "%s", input);
     695    p = strchr(input, '\"');
     696    if (p) {
     697        q = strchr(++p, '\"');
     698        if (q) {
     699            paranoid_free(output);
     700            asprintf(&output, "%s", p);
     701            *(strchr(output, '\"')) = '\0';
     702        }
     703    }
     704    return (output);
     705}
    829706
    830707
     
    834711 * @param in_out The string to strip spaces/control characters from (modified).
    835712 */
    836 void
    837 strip_spaces (char *in_out)
    838 {
    839     /*@ buffers ******************************************************/
    840   char *tmp;
    841 
    842     /*@ pointers *****************************************************/
    843   char *p;
    844 
    845     /*@ int *********************************************************/
    846   int i;
    847   int original_incoming_length;
    848 
    849     /*@ end vars ****************************************************/
    850 
    851   assert(in_out!=NULL);
    852   malloc_string(tmp);
    853   original_incoming_length = (int) strlen(in_out);
    854   for (i = 0; in_out[i] <= ' ' && i < (int) strlen (in_out); i++);
    855   strcpy (tmp, in_out + i);
    856   for (i = (int) strlen (tmp); i>0 && tmp[i - 1] <= 32; i--);
    857   tmp[i] = '\0';
    858   for (i = 0; i < original_incoming_length && MAX_STR_LEN; i++)
    859     {
    860       in_out[i] = ' ';
    861     }
    862   in_out[i] = '\0';
    863   i = 0;
    864   p = tmp;
    865   while (*p != '\0')
    866     {
    867       in_out[i] = *(p++);
    868       in_out[i + 1] = '\0';
    869       if (in_out[i] < 32 && i > 0)
    870     {
    871       if (in_out[i] == 8)
    872         {
    873           i--;
    874         }
    875       else if (in_out[i] == 9)
    876         {
    877           in_out[i++] = ' ';
    878         }
    879       else if (in_out[i] == '\r') // added 1st October 2003 -- FIXME
    880         {
    881               strcpy(tmp, in_out+i);
    882           strcpy(in_out,tmp);
    883           i=-1;
    884           continue;
    885         }
    886       else if (in_out[i] == '\t')
    887         {
    888           for (i++; i % 5; i++);
    889         }
    890       else if (in_out[i] >= 10 && in_out[i] <= 13)
    891         {
    892           break;
    893         }
    894       else
    895         {
    896           i--;
    897         }
    898     }
    899       else
    900     {
    901       i++;
    902     }
    903     }
    904   in_out[i] = '\0';
    905   paranoid_free(tmp);
    906 /*  for(i=strlen(in_out); i>0 && in_out[i-1]<=32; i--) {in_out[i-1]='\0';} */
     713void strip_spaces(char *in_out)
     714{
     715    /*@ buffers ***************************************************** */
     716    char *tmp;
     717
     718    /*@ pointers **************************************************** */
     719    char *p;
     720
     721    /*@ int ******************************************************** */
     722    int i;
     723    int original_incoming_length;
     724
     725    /*@ end vars *************************************************** */
     726
     727    assert(in_out != NULL);
     728    original_incoming_length = (int) strlen(in_out);
     729    for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out); i++);
     730    asprintf(&tmp, "%s", in_out + i);
     731    for (i = (int) strlen(tmp); i > 0 && tmp[i - 1] <= 32; i--);
     732    tmp[i] = '\0';
     733    for (i = 0; i < original_incoming_length; i++) {
     734        in_out[i] = ' ';
     735    }
     736    in_out[i] = '\0';
     737    i = 0;
     738    p = tmp;
     739    while (*p != '\0') {
     740        in_out[i] = *(p++);
     741        in_out[i + 1] = '\0';
     742        if (in_out[i] < 32 && i > 0) {
     743            if (in_out[i] == 8) {
     744                i--;
     745            } else if (in_out[i] == 9) {
     746                in_out[i++] = ' ';
     747            } else if (in_out[i] == '\r') {
     748                paranoid_free(tmp);
     749                asprintf(&tmp, "%s", in_out + i);
     750                strcpy(in_out, tmp);
     751                i = -1;
     752                continue;
     753            } else if (in_out[i] == '\t') {
     754                for (i++; i % 5; i++);
     755            } else if (in_out[i] >= 10 && in_out[i] <= 13) {
     756                break;
     757            } else {
     758                i--;
     759            }
     760        } else {
     761            i++;
     762        }
     763    }
     764    in_out[i] = '\0';
     765    paranoid_free(tmp);
    907766}
    908767
     
    912771 * This does not affect other quotes that may be embedded within the string.
    913772 * @param incoming The string to trim quotes from (modified).
    914  * @return @p incoming.
    915  */
    916 char *
    917 trim_empty_quotes (char *incoming)
    918 {
    919     /*@ buffer *******************************************************/
    920   static char outgoing[MAX_STR_LEN];
    921 
    922     /*@ end vars ****************************************************/
    923   assert(incoming!=NULL);
    924 
    925   if (incoming[0] == '\"' && incoming[strlen (incoming) - 1] == '\"')
    926     {
    927       strcpy (outgoing, incoming + 1);
    928       outgoing[strlen (outgoing) - 1] = '\0';
    929     }
    930   else
    931     {
    932       strcpy (outgoing, incoming);
    933     }
    934   return (outgoing);
     773 * @return @p outcoming. To be freed by caller
     774 */
     775char *trim_empty_quotes(char *incoming)
     776{
     777    /*@ buffer ****************************************************** */
     778    char *outgoing;
     779
     780    /*@ end vars *************************************************** */
     781    assert(incoming != NULL);
     782
     783    if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') {
     784        asprintf(&outgoing, "%s", incoming + 1);
     785        outgoing[strlen(outgoing) - 1] = '\0';
     786    } else {
     787        asprintf(&outgoing, incoming);
     788    }
     789    return (outgoing);
    935790}
    936791
     
    943798 * @return @p partition.
    944799 */
    945 char *
    946 truncate_to_drive_name (char *partition)
    947 {
    948   int i = strlen (partition) - 1;
    949   char *c;
     800char *truncate_to_drive_name(char *partition)
     801{
     802    int i = strlen(partition) - 1;
     803    char *c;
    950804
    951805#ifdef __FreeBSD__
    952806
    953   if (islower (partition[i])) // BSD subpartition
    954       i--;
    955   if (partition[i - 1] == 's') {
    956       while (isdigit (partition[i]))
    957       i--;
    958       i--;
    959   }
    960   partition[i + 1] = '\0';
     807    if (islower(partition[i]))  // BSD subpartition
     808        i--;
     809    if (partition[i - 1] == 's') {
     810        while (isdigit(partition[i]))
     811            i--;
     812        i--;
     813    }
     814    partition[i + 1] = '\0';
    961815
    962816#else
    963817
    964   assert_string_is_neither_NULL_nor_zerolength(partition);
    965   /* first see if it's a devfs style device */
    966   c = strrchr(partition, '/');
    967   if (c && strncmp(c, "/part", 5) == 0) {
    968     /* yup it's devfs, return the "disc" path */
    969     strcpy(c + 1, "disc");
    970     return partition;
    971   }
    972 
    973   for (i = strlen (partition); isdigit (partition[i - 1]); i--)
    974     continue;
    975   if (partition[i - 1] == 'p' && isdigit (partition[i - 2]))
    976     {
    977       i--;
    978     }
    979   partition[i] = '\0';
     818    assert_string_is_neither_NULL_nor_zerolength(partition);
     819    /* first see if it's a devfs style device */
     820    c = strrchr(partition, '/');
     821    if (c && strncmp(c, "/part", 5) == 0) {
     822        /* yup it's devfs, return the "disc" path */
     823        strcpy(c + 1, "disc");
     824        return partition;
     825    }
     826
     827    for (i = strlen(partition); isdigit(partition[i - 1]); i--)
     828        continue;
     829    if (partition[i - 1] == 'p' && isdigit(partition[i - 2])) {
     830        i--;
     831    }
     832    partition[i] = '\0';
    980833
    981834#endif
    982835
    983   return partition;
    984 }
    985 
    986 
    987 
     836    return partition;
     837}
    988838
    989839
     
    993843 * for anything else.
    994844 * @param raid_level The RAID level to stringify.
    995  * @return The string form of @p raid_level.
     845 * @return The string form of @p raid_level. To be freed by caller
    996846 * @note The returned value points to static storage that will be overwritten with each call.
    997847 */
    998 char *
    999 turn_raid_level_number_to_string (int raid_level)
    1000 {
    1001 
    1002     /*@ buffer ***********************************************************/
    1003   static char output[MAX_STR_LEN];
    1004 
    1005 
    1006 
    1007   if (raid_level >= 0)
    1008     {
    1009       sprintf (output, " RAID %-2d ", raid_level);
    1010     }
    1011   else
    1012     {
    1013       sprintf (output, "Linear RAID");
    1014     }
    1015   return (output);
    1016 }
    1017 
    1018 
    1019 
    1020 
    1021 
    1022 
    1023 
     848char *turn_raid_level_number_to_string(int raid_level)
     849{
     850
     851    /*@ buffer ********************************************************** */
     852    char *output;
     853
     854
     855
     856    if (raid_level >= 0) {
     857        asprintf(&output, " RAID %-2d ", raid_level);
     858    } else {
     859        asprintf(&output, "Linear RAID");
     860    }
     861    return (output);
     862}
    1024863
    1025864
     
    1033872int severity_of_difference(char *fn, char *out_reason)
    1034873{
    1035   int sev;
    1036   char *reason;
    1037   char *filename;
    1038 
    1039   malloc_string(reason);
    1040   malloc_string(filename);
     874    int sev;
     875    char *reason;
     876    char *filename;
     877
     878    malloc_string(reason);
    1041879// out_reason might be null on purpose, so don't bomb if it is :) OK?
    1042   assert_string_is_neither_NULL_nor_zerolength(fn);
    1043   if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING)))
    1044     { strcpy(filename, fn+strlen(MNT_RESTORING)); }
    1045   else if (fn[0]!='/')
    1046     { sprintf(filename, "/%s", fn); }
    1047   else
    1048     { strcpy(filename, fn); }
    1049 
    1050   sev = 3;
    1051   sprintf(reason, "Changed since backup. Consider running a differential backup in a day or two.");
    1052   if (!strncmp(filename, "/var/", 5)) { sev = 2; sprintf(reason, "/var's contents will change regularly, inevitably."); }
    1053   if (!strncmp(filename, "/home", 5)) { sev = 2; sprintf(reason, "It's in your /home partiton. Therefore, it is important."); }
    1054   if (!strncmp(filename, "/usr/", 5)) { sev = 3; sprintf(reason, "You may have installed/removed software during the backup."); }
    1055   if (!strncmp(filename, "/etc/", 5)) { sev = 3; sprintf(reason, "Do not edit config files while backing up your PC."); }
    1056   if (!strcmp(filename, "/etc/adjtime") || !strcmp(filename, "/etc/mtab")) { sev = 1; sprintf(reason, "This file changes all the time. It's OK."); }
    1057   if (!strncmp(filename, "/root/", 6)) { sev = 3; sprintf(reason, "Were you compiling/editing something in /root?"); }
    1058   if (!strncmp(filename, "/root/.", 7)) { sev = 2; sprintf(reason, "Temp or 'dot' files changed in /root."); }
    1059   if (!strncmp(filename, "/var/lib/", 9)) { sev = 2; sprintf(reason, "Did you add/remove software during backing?"); }
    1060   if (!strncmp(filename, "/var/lib/rpm", 12)) { sev = 3; sprintf(reason, "Did you add/remove software during backing?"); }
    1061   if (!strncmp(filename, "/var/lib/slocate", 16)) { sev = 1; sprintf(reason, "The 'update' daemon ran during backup. This does not affect the integrity of your backup."); }
    1062   if (!strncmp(filename, "/var/log/", 9) || strstr(filename,"/.xsession") || !strcmp(filename+strlen(filename)-4, ".log")) { sev = 1; sprintf(reason, "Log files change frequently as the computer runs. Fret not."); }
    1063   if (!strncmp(filename, "/var/spool", 10)) { sev = 1; sprintf(reason, "Background processes or printers were active. This does not affect the integrity of your backup."); }
    1064   if (!strncmp(filename, "/var/spool/mail", 10)) { sev = 2; sprintf(reason, "Mail was sent/received during backup."); }
    1065   if (filename[strlen(filename)-1]== '~')
    1066     { sev = 1; sprintf(reason, "Backup copy of another file which was modified recently."); }
    1067   if (strstr(filename, "cache"))
    1068     { sev = 1; sprintf(reason, "Part of a cache of data. Caches change from time to time. Don't worry."); }
    1069   if (!strncmp(filename, "/var/run/", 9) || !strncmp(filename, "/var/lock", 8) || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP") || strstr(filename, "/.Xauthority"))
    1070     { sev = 1; sprintf(reason, "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence."); }
    1071   if (out_reason) { strcpy(out_reason,reason); }
    1072   paranoid_free(filename);
    1073   paranoid_free(reason);
    1074   return(sev);
     880    assert_string_is_neither_NULL_nor_zerolength(fn);
     881    if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
     882        asprintf(&filename, "%s", fn + strlen(MNT_RESTORING));
     883    } else if (fn[0] != '/') {
     884        asprintf(&filename, "/%s", fn);
     885    } else {
     886        asprintf(&filename, "%s", fn);
     887    }
     888
     889    sev = 3;
     890    sprintf(reason,
     891            "Changed since backup. Consider running a differential backup in a day or two.");
     892    if (!strncmp(filename, "/var/", 5)) {
     893        sev = 2;
     894        sprintf(reason,
     895                "/var's contents will change regularly, inevitably.");
     896    }
     897    if (!strncmp(filename, "/home", 5)) {
     898        sev = 2;
     899        sprintf(reason,
     900                "It's in your /home partiton. Therefore, it is important.");
     901    }
     902    if (!strncmp(filename, "/usr/", 5)) {
     903        sev = 3;
     904        sprintf(reason,
     905                "You may have installed/removed software during the backup.");
     906    }
     907    if (!strncmp(filename, "/etc/", 5)) {
     908        sev = 3;
     909        sprintf(reason,
     910                "Do not edit config files while backing up your PC.");
     911    }
     912    if (!strcmp(filename, "/etc/adjtime")
     913        || !strcmp(filename, "/etc/mtab")) {
     914        sev = 1;
     915        sprintf(reason, "This file changes all the time. It's OK.");
     916    }
     917    if (!strncmp(filename, "/root/", 6)) {
     918        sev = 3;
     919        sprintf(reason, "Were you compiling/editing something in /root?");
     920    }
     921    if (!strncmp(filename, "/root/.", 7)) {
     922        sev = 2;
     923        sprintf(reason, "Temp or 'dot' files changed in /root.");
     924    }
     925    if (!strncmp(filename, "/var/lib/", 9)) {
     926        sev = 2;
     927        sprintf(reason, "Did you add/remove software during backing?");
     928    }
     929    if (!strncmp(filename, "/var/lib/rpm", 12)) {
     930        sev = 3;
     931        sprintf(reason, "Did you add/remove software during backing?");
     932    }
     933    if (!strncmp(filename, "/var/lib/slocate", 16)) {
     934        sev = 1;
     935        sprintf(reason,
     936                "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
     937    }
     938    if (!strncmp(filename, "/var/log/", 9)
     939        || strstr(filename, "/.xsession")
     940        || !strcmp(filename + strlen(filename) - 4, ".log")) {
     941        sev = 1;
     942        sprintf(reason,
     943                "Log files change frequently as the computer runs. Fret not.");
     944    }
     945    if (!strncmp(filename, "/var/spool", 10)) {
     946        sev = 1;
     947        sprintf(reason,
     948                "Background processes or printers were active. This does not affect the integrity of your backup.");
     949    }
     950    if (!strncmp(filename, "/var/spool/mail", 10)) {
     951        sev = 2;
     952        sprintf(reason, "Mail was sent/received during backup.");
     953    }
     954    if (filename[strlen(filename) - 1] == '~') {
     955        sev = 1;
     956        sprintf(reason,
     957                "Backup copy of another file which was modified recently.");
     958    }
     959    if (strstr(filename, "cache")) {
     960        sev = 1;
     961        sprintf(reason,
     962                "Part of a cache of data. Caches change from time to time. Don't worry.");
     963    }
     964    if (!strncmp(filename, "/var/run/", 9)
     965        || !strncmp(filename, "/var/lock", 8)
     966        || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
     967        || strstr(filename, "/.Xauthority")) {
     968        sev = 1;
     969        sprintf(reason,
     970                "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
     971    }
     972    paranoid_free(filename);
     973
     974    if (out_reason) {
     975        strcpy(out_reason, reason);
     976    }
     977    paranoid_free(reason);
     978    return (sev);
    1075979}
    1076980
     
    1084988 * @return The return value of strcmp().
    1085989 */
    1086 int compare_two_filelist_entries(void*va,void*vb)
    1087 {
    1088   static int res;
    1089   struct s_filelist_entry *fa, *fb;
    1090 
    1091   assert(va!=NULL);
    1092   assert(vb!=NULL);
    1093   fa = (struct s_filelist_entry*)va;
    1094   fb = (struct s_filelist_entry*)vb;
    1095   res = strcmp(fa->filename, fb->filename);
    1096   return(res);
    1097 }
    1098 
    1099 
    1100 
    1101 
    1102 
     990int compare_two_filelist_entries(void *va, void *vb)
     991{
     992    static int res;
     993    struct s_filelist_entry *fa, *fb;
     994
     995    assert(va != NULL);
     996    assert(vb != NULL);
     997    fa = (struct s_filelist_entry *) va;
     998    fb = (struct s_filelist_entry *) vb;
     999    res = strcmp(fa->filename, fb->filename);
     1000    return (res);
     1001}
    11031002
    11041003
     
    11131012 * @note The returned string points to static storage that will be overwritten with each call.
    11141013 */
    1115 char *
    1116 percent_media_full_comment (struct s_bkpinfo *bkpinfo)
    1117 {
    1118     /*@ int ************************************************/
    1119   int percentage;
    1120   int j;
    1121 
    1122     /*@ buffers ********************************************/
    1123   static char outstr[MAX_STR_LEN];
    1124   char *pos_w_commas, *tmp;
    1125 
    1126   assert(bkpinfo!=NULL);
    1127   malloc_string(pos_w_commas);
    1128   malloc_string(tmp);
    1129   sprintf(tmp, "%lld", g_tape_posK);
    1130   strcpy(pos_w_commas, commarize(tmp));
    1131 
    1132 
    1133 
    1134   if (bkpinfo->media_size[g_current_media_number]<=0)
     1014char *percent_media_full_comment(struct s_bkpinfo *bkpinfo)
     1015{
     1016    /*@ int *********************************************** */
     1017    int percentage;
     1018    int j;
     1019
     1020    /*@ buffers ******************************************* */
     1021    static char outstr[MAX_STR_LEN];
     1022    char *pos_w_commas, *tmp;
     1023
     1024    assert(bkpinfo != NULL);
     1025    malloc_string(pos_w_commas);
     1026    malloc_string(tmp);
     1027    sprintf(tmp, "%lld", g_tape_posK);
     1028    strcpy(pos_w_commas, commarize(tmp));
     1029
     1030
     1031
     1032    if (bkpinfo->media_size[g_current_media_number] <= 0)
    11351033//    { fatal_error( "percentage_media_full_comment() - unknown media size"); }
    1136     {
    1137       sprintf( outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
    1138       return( outstr );
    1139     }
     1034    {
     1035        sprintf(outstr, "Volume %d: %s kilobytes archived so far",
     1036                g_current_media_number, pos_w_commas);
     1037        return (outstr);
     1038    }
    11401039
    11411040/* update screen */
    1142   if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type))
    1143     {
    1144       percentage = (int) (g_tape_posK / 10 / bkpinfo->media_size[g_current_media_number]);
    1145       if (percentage > 100)
    1146     {
    1147       percentage = 100;
    1148     }
    1149       sprintf (outstr, "Volume %d: [", g_current_media_number);
    1150     }
    1151   else
    1152     {
    1153       percentage =
    1154     (int) (space_occupied_by_cd (bkpinfo->scratchdir) * 100 / 1024 /
    1155            bkpinfo->media_size[g_current_media_number]);
    1156       sprintf (outstr, "%s %d: [", media_descriptor_string(bkpinfo->backup_media_type), g_current_media_number);
    1157     }
    1158   for (j = 0; j < percentage; j += 5)
    1159     {
    1160       strcat (outstr, "*");
    1161     }
    1162   for (; j < 100; j += 5)
    1163     {
    1164       strcat (outstr, ".");
    1165     }
    1166   j = (int) strlen (outstr);
    1167   sprintf (outstr + j, "] %d%% used", percentage);
    1168   paranoid_free(pos_w_commas);
    1169   paranoid_free(tmp);
    1170   return (outstr);
     1041    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
     1042        percentage =
     1043            (int) (g_tape_posK / 10 /
     1044                   bkpinfo->media_size[g_current_media_number]);
     1045        if (percentage > 100) {
     1046            percentage = 100;
     1047        }
     1048        sprintf(outstr, "Volume %d: [", g_current_media_number);
     1049    } else {
     1050        percentage =
     1051            (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 /
     1052                   bkpinfo->media_size[g_current_media_number]);
     1053        sprintf(outstr, "%s %d: [",
     1054                media_descriptor_string(bkpinfo->backup_media_type),
     1055                g_current_media_number);
     1056    }
     1057    for (j = 0; j < percentage; j += 5) {
     1058        strcat(outstr, "*");
     1059    }
     1060    for (; j < 100; j += 5) {
     1061        strcat(outstr, ".");
     1062    }
     1063    j = (int) strlen(outstr);
     1064    sprintf(outstr + j, "] %d%% used", percentage);
     1065    paranoid_free(pos_w_commas);
     1066    paranoid_free(tmp);
     1067    return (outstr);
    11711068}
    11721069
     
    11791076char *media_descriptor_string(t_bkptype type_of_bkp)
    11801077{
    1181   static char *type_of_backup=NULL;
    1182  
    1183   if (!type_of_backup)
    1184     { malloc_string(type_of_backup); }
    1185    
    1186   switch(type_of_bkp)
    1187     {
     1078    static char *type_of_backup = NULL;
     1079
     1080    if (!type_of_backup) {
     1081        malloc_string(type_of_backup);
     1082    }
     1083
     1084    switch (type_of_bkp) {
    11881085    case dvd:
    11891086        strcpy(type_of_backup, "DVD");
     
    12131110        strcpy(type_of_backup, "ISO");
    12141111    }
    1215   return(type_of_backup);
     1112    return (type_of_backup);
    12161113}
    12171114
Note: See TracChangeset for help on using the changeset viewer.