source: trunk/mondo/mondo/common/libmondo-string.c @ 57

Last change on this file since 57 was 57, checked in by bcornec, 14 years ago

asprintf for libmondo-string.c (first step)

  • Property svn:keywords set to Id
File size: 29.4 KB
Line 
1/*
2   $Id: libmondo-string.c 57 2005-10-08 23:48:42Z bcornec $
3*/
4
5
6/**
7 * @file
8 * Functions for handling strings.
9 */
10
11#include "my-stuff.h"
12#include "mondostructures.h"
13#include "libmondo-string.h"
14#include "lib-common-externs.h"
15#include "libmondo-files-EXT.h"
16#include "libmondo-gui-EXT.h"
17#include "libmondo-tools-EXT.h"
18
19/*@unused@*/
20//static char cvsid[] = "$Id: libmondo-string.c 57 2005-10-08 23:48:42Z bcornec $";
21
22extern int g_current_media_number;
23extern long long g_tape_posK;
24
25/**
26 * @addtogroup stringGroup
27 * @{
28 */
29/**
30 * Build a partition name from a drive and a partition number.
31 * @param drive The drive basename of the partition name (e.g. /dev/hda)
32 * @param partno The partition number (e.g. 1)
33 * @param partition Where to put the partition name (e.g. /dev/hda1)
34 * @return @p partition.
35 * @note If @p drive ends in a digit, then 'p' (on Linux) or 's' (on *BSD) is added before @p partno.
36 */
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++ =
56#ifdef BSD
57                's';
58#else
59                'p';
60#endif
61        }
62    }
63    sprintf(p, "%d", partno);
64    return (partition);
65}
66
67
68/**
69 * Pad a string on both sides so it appears centered.
70 * @param in_out The string to be center-padded (modified).
71 * @param width The width of the final result.
72 */
73void center_string(char *in_out, int width)
74{
75    char *scratch;
76    char *p;
77    int i;                      /* purpose */
78    int len;                    /* purpose */
79    int mid;                    /* purpose */
80    int x;                      /* purpose */
81
82    assert(in_out != NULL);
83    assert(width > 2);
84
85    if (strlen(in_out) == 0) {
86        return;
87    }
88    for (p = in_out; *p == ' '; p++);
89    asprintf(&scratch, "%s", p);
90    len = (int) strlen(scratch);
91    mid = width / 2;
92    x = mid - len / 2;
93    for (i = 0; i < x; i++) {
94        in_out[i] = ' ';
95    }
96    in_out[i] = '\0';
97    strcat(in_out, scratch);
98    paranoid_free(scratch);
99}
100
101
102inline void turn_wildcard_chars_into_literal_chars(char *sout, char *sin)
103{
104    char *p, *q;
105
106    for (p = sin, q = sout; *p != '\0'; *(q++) = *(p++)) {
107        if (strchr("[]*?", *p)) {
108            *(q++) = '\\';
109        }
110    }
111    *q = *p;                    // for the final '\0'
112}
113
114
115/*
116 * Add commas every third place in @p input.
117 * @param input The string to commarize.
118 * @return The string with commas.
119 * @note The returned string points to static storage that will be overwritten with each call.
120 */
121char *commarize(char *input)
122{
123    char *pos_w_commas;
124    char *output;
125    char *tmp;
126    int j;
127
128    assert(input != NULL);
129
130    asprintf(&tmp, "%s", input);
131    if (strlen(tmp) > 6) {
132        asprintf(&pos_w_commas, "%s", tmp);
133        j = (int) strlen(pos_w_commas);
134        tmp[j - 6] = ',';
135        strcpy(tmp + j - 5, pos_w_commas + j - 6);
136        paranoid_free(pos_w_commas);
137        asprintf(&pos_w_commas, "%s", tmp);
138    }
139    if (strlen(tmp) > 3) {
140        j = (int) strlen(tmp);
141        asprintf(&pos_w_commas, "%s", tmp);
142        pos_w_commas[j - 3] = ',';
143        strcpy(pos_w_commas + j - 2, tmp + j - 3);
144    } else {
145        asprintf(&pos_w_commas, "%s", tmp);
146    }
147    asprintf(&output, "%s", pos_w_commas);
148    paranoid_free(pos_w_commas);
149    paranoid_free(tmp);
150    return (output);
151}
152
153
154/**
155 * Turn an entry from the RAID editor's disklist into a GUI-friendly string.
156 * The format is: the device left-aligned and padded to 24 places, followed by a space and the
157 * index, right-aligned and padded to eight places. The total string length
158 * is exactly 33.
159 * @param disklist The disklist to operate on.
160 * @param lino The line number from @p disklist to convert to a string.
161 * @return The string form of the disklist entry.
162 * @note The returned string points to static storage and will be overwritten with each call.
163 */
164char *disklist_entry_to_string(struct list_of_disks *disklist, int lino)
165{
166
167    /*@ buffers ********************************************************** */
168    char *output;
169
170    assert(disklist != NULL);
171
172    asprintf(&output, "%-24s %8d", disklist->el[lino].device,
173             disklist->el[lino].index);
174    return (output);
175}
176
177
178/**
179 * Turn a "friendly" sizestring into a number of megabytes.
180 * Supports the suffixes 'k'/'K', 'm'/'M', and 'g'/'G'. Calls
181 * fatal_error() if an unknown suffix is encountered.
182 * @param incoming The sizestring to convert (e.g. "40m", "2g").
183 * @return The size in megabytes.
184 */
185long friendly_sizestr_to_sizelong(char *incoming)
186{
187    long outval;
188    int i;
189    char *tmp;
190    char ch;
191
192    assert_string_is_neither_NULL_nor_zerolength(incoming);
193
194    if (!incoming[0]) {
195        return (0);
196    }
197    if (strchr(incoming, '.')) {
198        fatal_error("Please use integers only. No decimal points.");
199    }
200    asprintf(&tmp, "%s", incoming);
201    i = (int) strlen(tmp);
202    if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') {
203        tmp[i - 1] = '\0';
204    }
205    for (i = 0; i < (int) strlen(tmp) && isdigit(tmp[i]); i++);
206    ch = tmp[i];
207    tmp[i] = '\0';
208    outval = atol(tmp);
209    paranoid_free(tmp);
210
211    if (ch == 'g' || ch == 'G') {
212        outval = outval * 1024;
213    } else if (ch == 'k' || ch == 'K') {
214        outval = outval / 1024;
215    } else if (ch == 't' || ch == 'T')  // terabyte
216    {
217        outval *= 1048576;
218    } else if (ch == 'Y' || ch == 'y')  // yottabyte - the biggest measure in the info file
219    {
220        log_it
221            ("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?!?!");
222        popup_and_OK
223            ("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 :-)");
224        fatal_error("Integer overflow.");
225    } else if (ch != 'm' && ch != 'M') {
226        asprintf(&tmp, "Re: parameter '%s' - bad multiplier ('%c')",
227                 incoming, ch);
228        fatal_error(tmp);
229    }
230    return (outval);
231}
232
233
234/**
235 * Add spaces to the right of @p incoming to make it @p width characters wide.
236 * @param incoming The string to left-pad.
237 * @param width The width to pad it to.
238 * @return The left-padded string.
239 * @note The returned string points to static storage that will be overwritten with each call.
240 * @bug Why does center_string() modify its argument but leftpad_string() returns a modified copy?
241 */
242/* BERLIOS; useless ?
243char *leftpad_string(char *incoming, int width)
244{
245    char *output;
246
247    int i;
248
249    assert(incoming != NULL);
250    assert(width > 2);
251
252    asprintf(&output, "%s", incoming);
253    for (i = (int) strlen(output); i < width; i++) {
254        output[i] = ' ';
255    }
256    output[i] = '\0';
257    return (output);
258}
259*/
260
261
262
263/**
264 * Turn a marker byte (e.g. BLK_START_OF_BACKUP) into a string (e.g. "BLK_START_OF_BACKUP").
265 * Unknown markers are identified as "BLK_UNKNOWN (%d)" where %d is the decimal value.
266 * @param marker The marker byte to stringify.
267 * @return @p marker as a string. this should be freed by the caller
268 * @note The returned string points to static storage that will be overwritten with each call.
269 */
270char *marker_to_string(int marker)
271{
272    /*@ buffer ****************************************************** */
273    char *outstr;
274
275
276    /*@ end vars *************************************************** */
277
278    switch (marker) {
279    case BLK_START_OF_BACKUP:
280        asprintf(&outstr, "%s", "BLK_START_OF_BACKUP");
281        break;
282    case BLK_START_OF_TAPE:
283        asprintf(&outstr, "%s", "BLK_START_OF_TAPE");
284        break;
285    case BLK_START_AN_AFIO_OR_SLICE:
286        asprintf(&outstr, "%s", "BLK_START_AN_AFIO_OR_SLICE");
287        break;
288    case BLK_STOP_AN_AFIO_OR_SLICE:
289        asprintf(&outstr, "%s", "BLK_STOP_AN_AFIO_OR_SLICE");
290        break;
291    case BLK_START_AFIOBALLS:
292        asprintf(&outstr, "%s", "BLK_START_AFIOBALLS");
293        break;
294    case BLK_STOP_AFIOBALLS:
295        asprintf(&outstr, "%s", "BLK_STOP_AFIOBALLS");
296        break;
297    case BLK_STOP_BIGGIEFILES:
298        asprintf(&outstr, "%s", "BLK_STOP_BIGGIEFILES");
299        break;
300    case BLK_START_A_NORMBIGGIE:
301        asprintf(&outstr, "%s", "BLK_START_A_NORMBIGGIE");
302        break;
303    case BLK_START_A_PIHBIGGIE:
304        asprintf(&outstr, "%s", "BLK_START_A_PIHBIGGIE");
305        break;
306    case BLK_START_EXTENDED_ATTRIBUTES:
307        asprintf(&outstr, "%s", "BLK_START_EXTENDED_ATTRIBUTES");
308        break;
309    case BLK_STOP_EXTENDED_ATTRIBUTES:
310        asprintf(&outstr, "%s", "BLK_STOP_EXTENDED_ATTRIBUTES");
311        break;
312    case BLK_START_EXAT_FILE:
313        asprintf(&outstr, "%s", "BLK_START_EXAT_FILE");
314        break;
315    case BLK_STOP_EXAT_FILE:
316        asprintf(&outstr, "%s", "BLK_STOP_EXAT_FILE");
317        break;
318    case BLK_START_BIGGIEFILES:
319        asprintf(&outstr, "%s", "BLK_START_BIGGIEFILES");
320        break;
321    case BLK_STOP_A_BIGGIE:
322        asprintf(&outstr, "%s", "BLK_STOP_A_BIGGIE");
323        break;
324    case BLK_END_OF_TAPE:
325        asprintf(&outstr, "%s", "BLK_END_OF_TAPE");
326        break;
327    case BLK_END_OF_BACKUP:
328        asprintf(&outstr, "%s", "BLK_END_OF_BACKUP");
329        break;
330    case BLK_ABORTED_BACKUP:
331        asprintf(&outstr, "%s", "BLK_ABORTED_BACKUP");
332        break;
333    case BLK_START_FILE:
334        asprintf(&outstr, "%s", "BLK_START_FILE");
335        break;
336    case BLK_STOP_FILE:
337        asprintf(&outstr, "%s", "BLK_STOP_FILE");
338        break;
339    default:
340        asprintf(&outstr, "%s", "BLK_UNKNOWN (%d)", marker);
341        break;
342    }
343    return (outstr);
344}
345
346
347/**
348 * Turn a line from the mountlist into a GUI-friendly string.
349 * The format is as follows: the left-aligned @p device field padded to 24 places,
350 * a space, the left-aligned @p mountpoint field again padded to 24 places, a space,
351 * the left-aligned @p format field padded to 10 places, a space, and the right-aligned
352 * @p size field (in MB) padded to 8 places. The total string length is exactly 69.
353 * @param mountlist The mountlist to operate on.
354 * @param lino The line number in @p mountlist to stringify.
355 * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>]. To be freed by the caller
356 * @note The returned string points to static storage and will be overwritten with each call.
357 */
358char *mountlist_entry_to_string(struct mountlist_itself *mountlist,
359                                int lino)
360{
361
362    /*@ buffer *********************************************************** */
363    char *output;
364
365    assert(mountlist != NULL);
366
367    asprintf(&output, "%-24s %-24s %-10s %8lld",
368             mountlist->el[lino].device, mountlist->el[lino].mountpoint,
369             mountlist->el[lino].format, mountlist->el[lino].size / 1024);
370    return (output);
371}
372
373
374/**
375 * Generate a friendly string containing "X blah blah disk(s)"
376 * @param noof_disks The number of disks (the X).
377 * @param label The "blah blah" part in the middle. If you leave this blank
378 * there will be a weird double space in the middle, so pass *something*.
379 * @return The string containing "X blah blah disk(s)". To be  freed by the caller
380 * @note The returned string points to static storage and will be overwritten with each call.
381 */
382char *number_of_disks_as_string(int noof_disks, char *label)
383{
384
385    /*@ buffers ********************************************************* */
386    char *output;
387
388    /*@ char     ******************************************************** */
389    char p;
390
391    assert(label != NULL);
392
393    if (noof_disks > 1) {
394        p = 's';
395    } else {
396        p = ' ';
397    }
398    asprintf(&output, "%d %s disk%c    ", noof_disks, label, p);
399    /* BERLIOS: replaced with space^^^^ here !
400       while (strlen(output) < 14) {
401       strcat(output, " ");
402       }
403     */
404    return (output);
405}
406
407/**
408 * Change @p i into a friendly string. If @p i is \<= 10 then write out the
409 * number (e.g. "one", "two", ..., "nine", "ten", "11", ...).
410 * @param i The number to stringify.
411 * @return The string form of @p i. To be freed by caller.
412 * @note The returned value points to static strorage that will be overwritten with each call.
413 */
414char *number_to_text(int i)
415{
416
417    /*@ buffers ***************************************************** */
418    char *output;
419
420
421    /*@ end vars *************************************************** */
422
423    switch (i) {
424    case 0:
425        asprintf(&output, "%s", "zero");
426        break;
427    case 1:
428        asprintf(&output, "%s", "one");
429        break;
430    case 2:
431        asprintf(&output, "%s", "two");
432        break;
433    case 3:
434        asprintf(&output, "%s", "three");
435        break;
436    case 4:
437        asprintf(&output, "%s", "four");
438        break;
439    case 5:
440        asprintf(&output, "%s", "five");
441        break;
442    case 6:
443        asprintf(&output, "%s", "six");
444        break;
445    case 7:
446        asprintf(&output, "%s", "seven");
447        break;
448    case 8:
449        asprintf(&output, "%s", "eight");
450        break;
451    case 9:
452        asprintf(&output, "%s", "nine");
453    case 10:
454        asprintf(&output, "%s", "ten");
455    default:
456        asprintf(&output, "%d", i);
457    }
458    return (output);
459}
460
461
462/**
463 * Replace all occurences of @p token with @p value while copying @p ip to @p output.
464 * @param ip The input string containing zero or more <tt>token</tt>s.
465 * @param output The output string written with the <tt>token</tt>s replaced by @p value.
466 * @param token The token to be relaced with @p value.
467 * @param value The value to replace @p token.
468 */
469void resolve_naff_tokens(char *output, char *ip, char *value, char *token)
470{
471    /*@ buffers *** */
472    char *input;
473
474    /*@ pointers * */
475    char *p;
476
477    assert_string_is_neither_NULL_nor_zerolength(ip);
478    assert_string_is_neither_NULL_nor_zerolength(token);
479    assert(value != NULL);
480
481    strcpy(output, ip);         /* just in case the token doesn't appear in string at all */
482    asprintf(&input, "%s", ip);
483    while (strstr(input, token)) {
484        strcpy(output, input);
485        p = strstr(output, token);
486        *p = '\0';
487        strcat(output, value);
488        p = strstr(input, token) + strlen(token);
489        strcat(output, p);
490        paranoid_free(input);
491        asprintf(&input, "%s", output);
492    }
493    paranoid_free(input);
494}
495
496
497/**
498 * Generate the filename of slice @p sliceno of biggiefile @p bigfileno
499 * in @p path with suffix @p s. The format is as follows: @p path, followed
500 * by "/slice-" and @p bigfileno zero-padded to 7 places, followed by
501 * a dot and @p sliceno zero-padded to 5 places, followed by ".dat" and the
502 * suffix. The string is a minimum of 24 characters long.
503 * @param bigfileno The biggiefile number. Starts from 0.
504 * @param sliceno The slice number of biggiefile @p bigfileno. 0 is a "header"
505 * slice (no suffix) containing the biggiestruct, then are the compressed
506 * slices, then an empty uncompressed "trailer" slice.
507 * @param path The path to append (with a / in the middle) to the slice filename.
508 * @param s If not "" then add a "." and this to the end.
509 * @return The slice filename. To be freed by caller
510 * @note The returned value points to static storage and will be overwritten with each call.
511 */
512char *slice_fname(long bigfileno, long sliceno, char *path, char *s)
513{
514
515    /*@ buffers **************************************************** */
516    char *output;
517    char *suffix;
518
519    /*@ end vars *************************************************** */
520
521    assert_string_is_neither_NULL_nor_zerolength(path);
522    if (s[0] != '\0') {
523        asprintf(&suffix, ".%s", s);
524    } else {
525        asprintf(&suffix, "%s", "");
526    }
527    asprintf(&output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno,
528             sliceno, suffix);
529    paranoid_free(suffix);
530    return (output);
531}
532
533
534/**
535 * Generate a spinning symbol based on integer @p i.
536 * The symbol rotates through the characters / - \ | to form an ASCII "spinner"
537 * if successively written to the same location on screen.
538 * @param i The amount of progress or whatever else to use to determine the character
539 * for this iteration of the spinner.
540 * @return The character for this iteration.
541 */
542int special_dot_char(int i)
543{
544    switch (i % 4) {
545    case 0:
546        return ('/');
547    case 1:
548        return ('-');
549    case 2:
550        return ('\\');
551    case 3:
552        return ('|');
553    default:
554        return ('.');
555    }
556    return ('.');
557}
558
559
560/**
561 * Wrap @p flaws_str across three lines. The first two are no more than 74 characters wide.
562 * @param flaws_str The original string to split.
563 * @param flaws_str_A Where to put the first 74-or-less characters.
564 * @param flaws_str_B Where to put the second 74-or-less characters.
565 * @param flaws_str_C Where to put the rest.
566 * @param res The result of the original evaluate_mountlist() operation.
567 * @return TRUE if res == 0, FALSE otherwise.
568 */
569bool
570spread_flaws_across_three_lines(char *flaws_str, char *flaws_str_A,
571                                char *flaws_str_B, char *flaws_str_C,
572                                int res)
573{
574
575    /*@ int ************************************************************* */
576    int i = 0;
577
578    /*@ initialize ****************************************************** */
579    assert(flaws_str_A != NULL);
580    assert(flaws_str_B != NULL);
581    assert(flaws_str_C != NULL);
582    assert(flaws_str != NULL);
583
584    flaws_str_A[0] = flaws_str_B[0] = flaws_str_C[0] = '\0';
585
586
587    if (!res && !strlen(flaws_str)) {
588        return (TRUE);
589    }
590    if (strlen(flaws_str) > 0) {
591        sprintf(flaws_str_A, "%s", flaws_str + 1);
592    }
593    if (strlen(flaws_str_A) >= 74) {
594        for (i = 74; flaws_str_A[i] != ' '; i--);
595        strcpy(flaws_str_B, flaws_str_A + i + 1);
596        flaws_str_A[i] = '\0';
597    }
598    if (strlen(flaws_str_B) >= 74) {
599        for (i = 74; flaws_str_B[i] != ' '; i--);
600        strcpy(flaws_str_C, flaws_str_B + i + 1);
601        flaws_str_B[i] = '\0';
602    }
603    if (res) {
604        return (FALSE);
605    } else {
606        return (TRUE);
607    }
608}
609
610
611/**
612 * Compare @p stringA and @p stringB. This uses an ASCII sort for everything
613 * up to the digits on the end but a numerical sort for the digits on the end.
614 * @param stringA The first string to compare.
615 * @param stringB The second string to compare.
616 * @return The same as strcmp() - <0 if A<B, 0 if A=B, >0 if A>B.
617 * @note This function only does a numerical sort on the @e last set of numbers. If
618 * there are any in the middle those will be sorted ASCIIbetically.
619 */
620int strcmp_inc_numbers(char *stringA, char *stringB)
621{
622    /*@ int ********************************************************* */
623    int i;
624    int start_of_numbers_in_A;
625    int start_of_numbers_in_B;
626    int res;
627
628    /*@ long ******************************************************* */
629    long numA;
630    long numB;
631
632    /*@ end vars *************************************************** */
633    assert(stringA != NULL);
634    assert(stringB != NULL);
635
636    if (strlen(stringA) == strlen(stringB)) {
637        return (strcmp(stringA, stringB));
638    }
639    for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--);
640    if (i == (int) strlen(stringA)) {
641        return (strcmp(stringA, stringB));
642    }
643    start_of_numbers_in_A = i;
644    for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--);
645    if (i == (int) strlen(stringB)) {
646        return (strcmp(stringA, stringB));
647    }
648    start_of_numbers_in_B = i;
649    if (start_of_numbers_in_A != start_of_numbers_in_B) {
650        return (strcmp(stringA, stringB));
651    }
652    res = strncmp(stringA, stringB, (size_t) i);
653    if (res) {
654        return (res);
655    }
656    numA = atol(stringA + start_of_numbers_in_A);
657    numB = atol(stringB + start_of_numbers_in_B);
658    /*
659       sprintf(tmp,"Comparing %s and %s --> %ld,%ld\n",stringA,stringB,numA,numB);
660       log_to_screen(tmp);
661     */
662    return ((int) (numA - numB));
663}
664
665
666
667/**
668 * Strip excess baggage from @p input, which should be a line from afio.
669 * For now this copies the whole line unless it finds a set of quotes, in which case
670 * it copies their contents only.
671 * @param input The input line (presumably from afio).
672 * @return The stripped line.
673 * @note The returned string points to static storage that will be overwritten with each call.
674 */
675char *strip_afio_output_line(char *input)
676{
677    /*@ buffer ****************************************************** */
678    char *output;
679
680    /*@ pointers **************************************************** */
681    char *p;
682    char *q;
683    /*@ end vars *************************************************** */
684
685    assert(input != NULL);
686    asprintf(&output, "%s", input);
687    p = strchr(input, '\"');
688    if (p) {
689        q = strchr(++p, '\"');
690        if (q) {
691            paranoid_free(output);
692            asprintf(&output, "%s", p);
693            *(strchr(output, '\"')) = '\0';
694        }
695    }
696    return (output);
697}
698
699
700/**
701 * Remove all characters whose ASCII value is less than or equal to 32
702 * (spaces and control characters) from both sides of @p in_out.
703 * @param in_out The string to strip spaces/control characters from (modified).
704 */
705void strip_spaces(char *in_out)
706{
707    /*@ buffers ***************************************************** */
708    char *tmp;
709
710    /*@ pointers **************************************************** */
711    char *p;
712
713    /*@ int ******************************************************** */
714    int i;
715    int original_incoming_length;
716
717    /*@ end vars *************************************************** */
718
719    assert(in_out != NULL);
720    original_incoming_length = (int) strlen(in_out);
721    for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out); i++);
722    asprintf(&tmp, "%s", in_out + i);
723    for (i = (int) strlen(tmp); i > 0 && tmp[i - 1] <= 32; i--);
724    tmp[i] = '\0';
725    for (i = 0; i < original_incoming_length; i++) {
726        in_out[i] = ' ';
727    }
728    in_out[i] = '\0';
729    i = 0;
730    p = tmp;
731    while (*p != '\0') {
732        in_out[i] = *(p++);
733        in_out[i + 1] = '\0';
734        if (in_out[i] < 32 && i > 0) {
735            if (in_out[i] == 8) {
736                i--;
737            } else if (in_out[i] == 9) {
738                in_out[i++] = ' ';
739            } else if (in_out[i] == '\r') {
740                paranoid_free(tmp);
741                asprintf(&tmp, "%s", in_out + i);
742                strcpy(in_out, tmp);
743                i = -1;
744                continue;
745            } else if (in_out[i] == '\t') {
746                for (i++; i % 5; i++);
747            } else if (in_out[i] >= 10 && in_out[i] <= 13) {
748                break;
749            } else {
750                i--;
751            }
752        } else {
753            i++;
754        }
755    }
756    in_out[i] = '\0';
757    paranoid_free(tmp);
758}
759
760
761/**
762 * If there are double quotes "" around @p incoming then remove them.
763 * This does not affect other quotes that may be embedded within the string.
764 * @param incoming The string to trim quotes from (modified).
765 * @return @p outcoming. To be freed by caller
766 */
767char *trim_empty_quotes(char *incoming)
768{
769    /*@ buffer ****************************************************** */
770    char *outgoing;
771
772    /*@ end vars *************************************************** */
773    assert(incoming != NULL);
774
775    if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') {
776        asprintf(&outgoing, "%s", incoming + 1);
777        outgoing[strlen(outgoing) - 1] = '\0';
778    } else {
779        asprintf(&outgoing, incoming);
780    }
781    return (outgoing);
782}
783
784
785
786
787/**
788 * Remove any partition info from @p partition, leaving just the drive name.
789 * @param partition The partition name soon-to-become drive name. (modified)
790 * @return @p partition.
791 */
792char *truncate_to_drive_name(char *partition)
793{
794    int i = strlen(partition) - 1;
795    char *c;
796
797#ifdef __FreeBSD__
798
799    if (islower(partition[i]))  // BSD subpartition
800        i--;
801    if (partition[i - 1] == 's') {
802        while (isdigit(partition[i]))
803            i--;
804        i--;
805    }
806    partition[i + 1] = '\0';
807
808#else
809
810    assert_string_is_neither_NULL_nor_zerolength(partition);
811    /* first see if it's a devfs style device */
812    c = strrchr(partition, '/');
813    if (c && strncmp(c, "/part", 5) == 0) {
814        /* yup it's devfs, return the "disc" path */
815        strcpy(c + 1, "disc");
816        return partition;
817    }
818
819    for (i = strlen(partition); isdigit(partition[i - 1]); i--)
820        continue;
821    if (partition[i - 1] == 'p' && isdigit(partition[i - 2])) {
822        i--;
823    }
824    partition[i] = '\0';
825
826#endif
827
828    return partition;
829}
830
831
832/**
833 * Turn a RAID level number (-1 to 5) into a friendly string. The string
834 * is either "Linear RAID" for -1, or " RAID %-2d " (%d = @p raid_level)
835 * for anything else.
836 * @param raid_level The RAID level to stringify.
837 * @return The string form of @p raid_level. To be freed by caller
838 * @note The returned value points to static storage that will be overwritten with each call.
839 */
840char *turn_raid_level_number_to_string(int raid_level)
841{
842
843    /*@ buffer ********************************************************** */
844    char *output;
845
846    if (raid_level >= 0) {
847        asprintf(&output, " RAID %-2d ", raid_level);
848    } else {
849        asprintf(&output, "Linear RAID");
850    }
851    return (output);
852}
853
854
855/**
856 * Determine the severity (1-3, 1 being low) of the fact that
857 * @p fn changed in the live filesystem (verify/compare).
858 * @param fn The filename that changed.
859 * @param out_reason If non-NULL, a descriptive reason for the difference will be copied here.
860 * @return The severity (1-3).
861 */
862int severity_of_difference(char *fn, char *out_reason)
863{
864    int sev;
865    char *reason;
866    char *filename;
867
868    malloc_string(reason);
869// out_reason might be null on purpose, so don't bomb if it is :) OK?
870    assert_string_is_neither_NULL_nor_zerolength(fn);
871    if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
872        asprintf(&filename, "%s", fn + strlen(MNT_RESTORING));
873    } else if (fn[0] != '/') {
874        asprintf(&filename, "/%s", fn);
875    } else {
876        asprintf(&filename, "%s", fn);
877    }
878
879    sev = 3;
880    sprintf(reason,
881            "Changed since backup. Consider running a differential backup in a day or two.");
882    if (!strncmp(filename, "/var/", 5)) {
883        sev = 2;
884        sprintf(reason,
885                "/var's contents will change regularly, inevitably.");
886    }
887    if (!strncmp(filename, "/home", 5)) {
888        sev = 2;
889        sprintf(reason,
890                "It's in your /home partiton. Therefore, it is important.");
891    }
892    if (!strncmp(filename, "/usr/", 5)) {
893        sev = 3;
894        sprintf(reason,
895                "You may have installed/removed software during the backup.");
896    }
897    if (!strncmp(filename, "/etc/", 5)) {
898        sev = 3;
899        sprintf(reason,
900                "Do not edit config files while backing up your PC.");
901    }
902    if (!strcmp(filename, "/etc/adjtime")
903        || !strcmp(filename, "/etc/mtab")) {
904        sev = 1;
905        sprintf(reason, "This file changes all the time. It's OK.");
906    }
907    if (!strncmp(filename, "/root/", 6)) {
908        sev = 3;
909        sprintf(reason, "Were you compiling/editing something in /root?");
910    }
911    if (!strncmp(filename, "/root/.", 7)) {
912        sev = 2;
913        sprintf(reason, "Temp or 'dot' files changed in /root.");
914    }
915    if (!strncmp(filename, "/var/lib/", 9)) {
916        sev = 2;
917        sprintf(reason, "Did you add/remove software during backing?");
918    }
919    if (!strncmp(filename, "/var/lib/rpm", 12)) {
920        sev = 3;
921        sprintf(reason, "Did you add/remove software during backing?");
922    }
923    if (!strncmp(filename, "/var/lib/slocate", 16)) {
924        sev = 1;
925        sprintf(reason,
926                "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
927    }
928    if (!strncmp(filename, "/var/log/", 9)
929        || strstr(filename, "/.xsession")
930        || !strcmp(filename + strlen(filename) - 4, ".log")) {
931        sev = 1;
932        sprintf(reason,
933                "Log files change frequently as the computer runs. Fret not.");
934    }
935    if (!strncmp(filename, "/var/spool", 10)) {
936        sev = 1;
937        sprintf(reason,
938                "Background processes or printers were active. This does not affect the integrity of your backup.");
939    }
940    if (!strncmp(filename, "/var/spool/mail", 10)) {
941        sev = 2;
942        sprintf(reason, "Mail was sent/received during backup.");
943    }
944    if (filename[strlen(filename) - 1] == '~') {
945        sev = 1;
946        sprintf(reason,
947                "Backup copy of another file which was modified recently.");
948    }
949    if (strstr(filename, "cache")) {
950        sev = 1;
951        sprintf(reason,
952                "Part of a cache of data. Caches change from time to time. Don't worry.");
953    }
954    if (!strncmp(filename, "/var/run/", 9)
955        || !strncmp(filename, "/var/lock", 8)
956        || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
957        || strstr(filename, "/.Xauthority")) {
958        sev = 1;
959        sprintf(reason,
960                "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
961    }
962    paranoid_free(filename);
963
964    if (out_reason) {
965        strcpy(out_reason, reason);
966    }
967    paranoid_free(reason);
968    return (sev);
969}
970
971
972
973/**
974 * Compare the filenames in two filelist entries (s_filelist_entry*) casted
975 * to void*.
976 * @param va The first filelist entry, cast as a @c void pointer.
977 * @param vb The second filelist entry, cast as a @c void pointer.
978 * @return The return value of strcmp().
979 */
980int compare_two_filelist_entries(void *va, void *vb)
981{
982    static int res;
983    struct s_filelist_entry *fa, *fb;
984
985    assert(va != NULL);
986    assert(vb != NULL);
987    fa = (struct s_filelist_entry *) va;
988    fb = (struct s_filelist_entry *) vb;
989    res = strcmp(fa->filename, fb->filename);
990    return (res);
991}
992
993
994/**
995 * Generate a line intended to be passed to update_evalcall_form(), indicating
996 * the current media fill percentage (or number of kilobytes if size is not known).
997 * @param bkpinfo The backup media structure. Fields used:
998 * - @c bkpinfo->backup_media_type
999 * - @c bkpinfo->media_size
1000 * - @c bkpinfo->scratchdir
1001 * @return The string indicating media fill.
1002 * @note The returned string points to static storage that will be overwritten with each call.
1003 */
1004char *percent_media_full_comment(struct s_bkpinfo *bkpinfo)
1005{
1006    /*@ int *********************************************** */
1007    int percentage;
1008    int j;
1009
1010    /*@ buffers ******************************************* */
1011    static char outstr[MAX_STR_LEN];
1012    char *pos_w_commas, *tmp;
1013
1014    assert(bkpinfo != NULL);
1015    malloc_string(pos_w_commas);
1016    malloc_string(tmp);
1017    sprintf(tmp, "%lld", g_tape_posK);
1018    strcpy(pos_w_commas, commarize(tmp));
1019
1020
1021
1022    if (bkpinfo->media_size[g_current_media_number] <= 0)
1023//    { fatal_error( "percentage_media_full_comment() - unknown media size"); }
1024    {
1025        sprintf(outstr, "Volume %d: %s kilobytes archived so far",
1026                g_current_media_number, pos_w_commas);
1027        return (outstr);
1028    }
1029
1030/* update screen */
1031    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1032        percentage =
1033            (int) (g_tape_posK / 10 /
1034                   bkpinfo->media_size[g_current_media_number]);
1035        if (percentage > 100) {
1036            percentage = 100;
1037        }
1038        sprintf(outstr, "Volume %d: [", g_current_media_number);
1039    } else {
1040        percentage =
1041            (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 /
1042                   bkpinfo->media_size[g_current_media_number]);
1043        sprintf(outstr, "%s %d: [",
1044                media_descriptor_string(bkpinfo->backup_media_type),
1045                g_current_media_number);
1046    }
1047    for (j = 0; j < percentage; j += 5) {
1048        strcat(outstr, "*");
1049    }
1050    for (; j < 100; j += 5) {
1051        strcat(outstr, ".");
1052    }
1053    j = (int) strlen(outstr);
1054    sprintf(outstr + j, "] %d%% used", percentage);
1055    paranoid_free(pos_w_commas);
1056    paranoid_free(tmp);
1057    return (outstr);
1058}
1059
1060/**
1061 * Get a string form of @p type_of_bkp.
1062 * @param type_of_bkp The backup type to stringify.
1063 * @return The stringification of @p type_of_bkp.
1064 * @note The returned string points to static storage that will be overwritten with each call.
1065 */
1066char *media_descriptor_string(t_bkptype type_of_bkp)
1067{
1068    static char *type_of_backup = NULL;
1069
1070    if (!type_of_backup) {
1071        malloc_string(type_of_backup);
1072    }
1073
1074    switch (type_of_bkp) {
1075    case dvd:
1076        strcpy(type_of_backup, "DVD");
1077        break;
1078    case cdr:
1079        strcpy(type_of_backup, "CDR");
1080        break;
1081    case cdrw:
1082        strcpy(type_of_backup, "CDRW");
1083        break;
1084    case tape:
1085        strcpy(type_of_backup, "tape");
1086        break;
1087    case cdstream:
1088        strcpy(type_of_backup, "CDR");
1089        break;
1090    case udev:
1091        strcpy(type_of_backup, "udev");
1092        break;
1093    case iso:
1094        strcpy(type_of_backup, "ISO");
1095        break;
1096    case nfs:
1097        strcpy(type_of_backup, "nfs");
1098        break;
1099    default:
1100        strcpy(type_of_backup, "ISO");
1101    }
1102    return (type_of_backup);
1103}
1104
1105/* @} - end of stringGroup */
Note: See TracBrowser for help on using the repository browser.